23#include <finders_interface.h>
30 QgsTextureRect(
const rectpack2D::rect_xywh &rect,
const QImage &image = QImage() )
41 const auto &get_rect()
const
48 return QRect( rect.x, rect.y, rect.w, rect.h );
51 rectpack2D::rect_xywh rect;
65 return static_cast< int >( mRects.size() );
70 return mRects[id].asQRect();
75 if ( mAtlasSize.isEmpty() )
78 QImage res( mAtlasSize, QImage::Format_ARGB32_Premultiplied );
79 res.fill( Qt::transparent );
81 QPainter painter( &res );
82 for (
const QgsTextureRect &
rect : mRects )
84 if ( !
rect.image.isNull() )
86 painter.drawImage(
rect.asQRect(),
rect.image );
96 if ( mAtlasSize.isEmpty() )
99 QImage res( mAtlasSize, QImage::Format_ARGB32_Premultiplied );
100 res.fill( Qt::transparent );
102 QPainter painter( &res );
103 painter.setPen( Qt::NoPen );
107 for (
const QgsTextureRect &
rect : mRects )
109 const QColor color = ramp.
color( index / (
static_cast< int >( mRects.size() ) - 1 ) );
111 painter.setBrush( QBrush( color ) );
112 painter.drawRect(
rect.asQRect() );
126 std::vector< QgsTextureRect > rects;
127 rects.reserve( rectangles.size() );
128 for (
const QRect &rect : rectangles )
130 rects.emplace_back( QgsTextureRect( rectpack2D::rect_xywh( 0, 0, rect.width(), rect.height() ) ) );
132 return generateAtlas( std::move( rects ), maxSide );
137 std::vector< QgsTextureRect > rects;
138 rects.reserve( images.size() );
139 for (
const QImage &image : images )
141 rects.emplace_back( QgsTextureRect( rectpack2D::rect_xywh( 0, 0, image.width(), image.height() ), image ) );
143 return generateAtlas( std::move( rects ), maxSide );
146QgsTextureAtlas QgsTextureAtlasGenerator::generateAtlas( std::vector< QgsTextureRect > rects,
int maxSide )
148 using spacesType = rectpack2D::empty_spaces<false, rectpack2D::default_empty_spaces>;
151 auto reportSuccessful = []( rectpack2D::rect_xywh & ) {
152 return rectpack2D::callback_result::CONTINUE_PACKING;
155 auto reportUnsuccessful = [&result]( rectpack2D::rect_xywh & ) {
157 return rectpack2D::callback_result::ABORT_PACKING;
160 const auto discardStep = -4;
162 auto byWidth = [](
const rectpack2D::rect_xywh *a,
const rectpack2D::rect_xywh *b ) {
166 const rectpack2D::rect_wh resultSize = rectpack2D::find_best_packing<spacesType>(
168 rectpack2D::make_finder_input(
173 rectpack2D::flipping_option::DISABLED
179 return QgsTextureAtlas();
182 res.mRects = std::move( rects );
183 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.