From 6b49d09a55c8ce4a54b493e881e07747811883e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Sun, 15 Jul 2018 23:05:57 +0300 Subject: [PATCH] Fixed|libgui|Image: Saving image contents to a file stb_image_write was given the wrong number of components. --- doomsday/libs/core/src/data/block.cpp | 2 +- doomsday/libs/gui/src/graphics/atlas.cpp | 16 +++++++++++--- doomsday/libs/gui/src/graphics/image.cpp | 27 ++++++++++++++++++------ 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/doomsday/libs/core/src/data/block.cpp b/doomsday/libs/core/src/data/block.cpp index e73917acb4..91ed19c9ed 100644 --- a/doomsday/libs/core/src/data/block.cpp +++ b/doomsday/libs/core/src/data/block.cpp @@ -82,7 +82,7 @@ Block::Block(const IIStream &stream) stream >> *this; } -Block::Block(const IByteArray &other, Offset at, Size count) : IByteArray() +Block::Block(const IByteArray &other, Offset at, Size count) { init_Block(&_block, 0); copyFrom(other, at, count); diff --git a/doomsday/libs/gui/src/graphics/atlas.cpp b/doomsday/libs/gui/src/graphics/atlas.cpp index 5b1a48f56a..25089a8880 100644 --- a/doomsday/libs/gui/src/graphics/atlas.cpp +++ b/doomsday/libs/gui/src/graphics/atlas.cpp @@ -137,8 +137,17 @@ DE_PIMPL(Atlas) * @param image Image. * @param rect Rectangle for the image determined by an IAllocator. */ - void submitImage(Image const &image, Rectanglei const &rect) + void submitImage(const Image &submittedImage, Rectanglei const &rect) { + Image image = submittedImage.convertToFormat(hasBacking() ? backing.format() + : submittedImage.format()); + /*{ + static int count = 0; + NativePath p = NativePath::workPath() / Stringf("submitted-%04i.png", count++); + debug("writing %s", p.c_str()); + image.save(p); + }*/ + Rectanglei const noBorders = rect.shrunk(border); Rectanglei const withMargin = rect.expanded(margin); @@ -169,7 +178,7 @@ DE_PIMPL(Atlas) } backing.draw(image, noBorders.topLeft); - //backing.toQImage().save(QString("backing-%1.png").arg(uint64_t(this))); + //backing.save(Stringf("backing-%p.png", this)); markAsChanged(rect); } @@ -381,7 +390,8 @@ Id Atlas::alloc(Image const &image, Id const &chosenId) if (image.isNull()) { LOG_AS("Atlas"); - LOGDEV_GL_WARNING("Cannot allocate a zero-size image"); + LOGDEV_GL_WARNING("Cannot allocate an empty image (%ix%i)") << image.width() + << image.height(); return Id::None; } diff --git a/doomsday/libs/gui/src/graphics/image.cpp b/doomsday/libs/gui/src/graphics/image.cpp index f7913849e8..52ca18d74a 100644 --- a/doomsday/libs/gui/src/graphics/image.cpp +++ b/doomsday/libs/gui/src/graphics/image.cpp @@ -574,7 +574,7 @@ duint32 *Image::rowEnd32(duint y) bool Image::isNull() const { - return size() == Size(0, 0); + return size().area() == 0; } bool Image::isGLCompatible() const @@ -626,10 +626,23 @@ Image Image::convertToFormat(Format toFormat) const // No conversion necessary. return *this; } + const int inStep = bytesPerPixel(); + Image conv(size(), toFormat); + if (d->format == Luminance_8 && toFormat == RGBA_8888) + { + for (duint y = 0; y < height(); ++y) + { + const duint8 *in = row(y); + for (duint32 *out = conv.row32(y), *outEnd = conv.rowEnd32(y); + out != outEnd; ++out, ++in) + { + *out = packColor(Color(*in, *in, *in, 255)); + } + } + return conv; + } if (d->format == RGB_888 && toFormat == RGBA_8888) { - const int inStep = bytesPerPixel(); - Image conv(size(), toFormat); for (duint y = 0; y < height(); ++y) { const duint8 *in = row(y); @@ -641,7 +654,8 @@ Image Image::convertToFormat(Format toFormat) const } return conv; } - DE_ASSERT_FAIL("Image::convertToFormat not implemented"); + DE_ASSERT_FAIL("Image::convertToFormat not implemented for the given input/output formats"); + return conv; } //bool Image::canConvertToQImage() const @@ -1052,7 +1066,7 @@ static void dataWriter(void *context, void *data, int size) Block Image::serialize(SerializationFormat format) const { Block data; - const int comp = bytesPerPixel() / 4; + const int comp = bytesPerPixel(); switch (format) { case Png: @@ -1318,10 +1332,11 @@ Image Image::fromData(Block const &data, String const &formatHint) Image Image::fromRgbaData(const Size &size, const IByteArray &rgba) { + const int rowLen = size.x * 4; Image img(size, Image::RGBA_8888); for (duint y = 0; y < size.y; ++y) { - rgba.get(size.x * y * 4, img.row(y), size.x * 4); + rgba.get(rowLen * y, img.row(y), rowLen); } return img; }