23#include <finders_interface.h>
30 QgsTextureRect(
const rectpack2D::rect_xywh &rect,
int id,
const QImage &image = QImage() )
42 const auto &get_rect()
const
49 return QRect( rect.x, rect.y, rect.w, rect.h );
52 rectpack2D::rect_xywh rect;
67 return static_cast< int >( mRects.size() );
72 return mRects[id].asQRect();
77 if ( mAtlasSize.isEmpty() )
80 QImage res( mAtlasSize, QImage::Format_ARGB32_Premultiplied );
81 res.fill( Qt::transparent );
83 QPainter painter( &res );
84 for (
const QgsTextureRect &
rect : mRects )
86 if ( !
rect.image.isNull() )
88 painter.drawImage(
rect.asQRect(),
rect.image );
98 if ( mAtlasSize.isEmpty() )
101 QImage res( mAtlasSize, QImage::Format_ARGB32_Premultiplied );
102 res.fill( Qt::transparent );
104 QPainter painter( &res );
105 painter.setPen( Qt::NoPen );
109 for (
const QgsTextureRect &
rect : mRects )
111 const QColor color = ramp.
color( index / (
static_cast< int >( mRects.size() ) - 1 ) );
113 painter.setBrush( QBrush( color ) );
114 painter.drawRect(
rect.asQRect() );
128 std::vector< QgsTextureRect > rects;
129 rects.reserve( rectangles.size() );
131 for (
const QRect &rect : rectangles )
133 rects.emplace_back( QgsTextureRect( rectpack2D::rect_xywh( 0, 0, rect.width(), rect.height() ), index++ ) );
135 return generateAtlas( std::move( rects ), maxSide );
140 std::vector< QgsTextureRect > rects;
141 rects.reserve( images.size() );
143 for (
const QImage &image : images )
145 rects.emplace_back( QgsTextureRect( rectpack2D::rect_xywh( 0, 0, image.width(), image.height() ), index++, image ) );
147 return generateAtlas( std::move( rects ), maxSide );
150QgsTextureAtlas QgsTextureAtlasGenerator::generateAtlas( std::vector< QgsTextureRect > rects,
int maxSide )
152 using spacesType = rectpack2D::empty_spaces<false, rectpack2D::default_empty_spaces>;
155 auto reportSuccessful = []( rectpack2D::rect_xywh & ) {
156 return rectpack2D::callback_result::CONTINUE_PACKING;
159 auto reportUnsuccessful = [&result]( rectpack2D::rect_xywh & ) {
161 return rectpack2D::callback_result::ABORT_PACKING;
164 const auto discardStep = -4;
166 auto byWidth = [](
const rectpack2D::rect_xywh *a,
const rectpack2D::rect_xywh *b ) {
170 const rectpack2D::rect_wh resultSize = rectpack2D::find_best_packing<spacesType>(
172 rectpack2D::make_finder_input(
177 rectpack2D::flipping_option::DISABLED
183 return QgsTextureAtlas();
187 std::sort( rects.begin(), rects.end(), [](
const QgsTextureRect &a,
const QgsTextureRect &b ) {
192 res.mRects = std::move( rects );
193 res.mAtlasSize = QSize( resultSize.w, resultSize.h );
A color ramp consisting of random colors, constrained within component ranges.
virtual void setTotalColorCount(int colorCount)
Sets the desired total number of unique colors for the resultant ramp.
QColor color(double value) const override
Returns the color corresponding to a specified value.
static QgsTextureAtlas createFromRects(const QVector< QRect > &rectangles, int maxSide=1000)
Creates a texture atlas for a set of rectangles.
static QgsTextureAtlas createFromImages(const QVector< QImage > &images, int maxSide=1000)
Creates a texture atlas for a set of images.
Encapsulates a texture atlas.
int count() const
Returns the number of textures in the atlas.
QgsTextureAtlas & operator=(const QgsTextureAtlas &other)
QRect rect(int index) const
Returns the packed rectangle for the texture with the specified index.
QImage renderDebugTexture() const
Renders a debug texture.
QImage renderAtlasTexture() const
Renders the combined texture atlas, containing all source images.