23#include <finders_interface.h>
30 QgsTextureRect(
const rectpack2D::rect_xywh &rect,
const QImage &image = QImage() )
36 auto &get_rect() {
return rect; }
37 const auto &get_rect()
const {
return rect; }
39 QRect asQRect()
const {
return QRect( rect.x, rect.y, rect.w, rect.h ); }
41 rectpack2D::rect_xywh rect;
55 return static_cast< int >( mRects.size() );
60 return mRects[id].asQRect();
65 if ( mAtlasSize.isEmpty() )
68 QImage res( mAtlasSize, QImage::Format_ARGB32_Premultiplied );
69 res.fill( Qt::transparent );
71 QPainter painter( &res );
72 for (
const QgsTextureRect &
rect : mRects )
74 if ( !
rect.image.isNull() )
76 painter.drawImage(
rect.asQRect(),
rect.image );
86 if ( mAtlasSize.isEmpty() )
89 QImage res( mAtlasSize, QImage::Format_ARGB32_Premultiplied );
90 res.fill( Qt::transparent );
92 QPainter painter( &res );
93 painter.setPen( Qt::NoPen );
97 for (
const QgsTextureRect &
rect : mRects )
99 const QColor color = ramp.
color( index / (
static_cast< int >( mRects.size() ) - 1 ) );
101 painter.setBrush( QBrush( color ) );
102 painter.drawRect(
rect.asQRect() );
116 std::vector< QgsTextureRect > rects;
117 rects.reserve( rectangles.size() );
118 for (
const QRect &rect : rectangles )
120 rects.emplace_back( QgsTextureRect( rectpack2D::rect_xywh( 0, 0, rect.width(), rect.height() ) ) );
122 return generateAtlas( std::move( rects ), maxSide );
127 std::vector< QgsTextureRect > rects;
128 rects.reserve( images.size() );
129 for (
const QImage &image : images )
131 rects.emplace_back( QgsTextureRect( rectpack2D::rect_xywh( 0, 0, image.width(), image.height() ), image ) );
133 return generateAtlas( std::move( rects ), maxSide );
136QgsTextureAtlas QgsTextureAtlasGenerator::generateAtlas( std::vector< QgsTextureRect > rects,
int maxSide )
138 using spacesType = rectpack2D::empty_spaces<false, rectpack2D::default_empty_spaces>;
141 auto reportSuccessful = []( rectpack2D::rect_xywh & ) {
return rectpack2D::callback_result::CONTINUE_PACKING; };
143 auto reportUnsuccessful = [&result]( rectpack2D::rect_xywh & ) {
145 return rectpack2D::callback_result::ABORT_PACKING;
148 const auto discardStep = -4;
150 auto byWidth = [](
const rectpack2D::rect_xywh *a,
const rectpack2D::rect_xywh *b ) {
return a->w > b->w; };
152 const rectpack2D::rect_wh resultSize
153 = rectpack2D::find_best_packing<spacesType>( rects, rectpack2D::make_finder_input( maxSide, discardStep, reportSuccessful, reportUnsuccessful, rectpack2D::flipping_option::DISABLED ), byWidth );
156 return QgsTextureAtlas();
159 res.mRects = std::move( rects );
160 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.