Skip to content

Commit

Permalink
Fixed|libgui|Image: Saving image contents to a file
Browse files Browse the repository at this point in the history
stb_image_write was given the wrong number of components.
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 28fd8e3 commit 6b49d09
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion doomsday/libs/core/src/data/block.cpp
Expand Up @@ -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);
Expand Down
16 changes: 13 additions & 3 deletions doomsday/libs/gui/src/graphics/atlas.cpp
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand Down
27 changes: 21 additions & 6 deletions doomsday/libs/gui/src/graphics/image.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 6b49d09

Please sign in to comment.