Skip to content

Commit

Permalink
Fix bug when encoding premultiplied RGBA data to Webp, and support en…
Browse files Browse the repository at this point in the history
…coding Alpha8 data to Webp.
  • Loading branch information
h3r3x3 authored Jan 27, 2022
1 parent af14e29 commit ccd243c
Show file tree
Hide file tree
Showing 29 changed files with 47 additions and 19 deletions.
39 changes: 26 additions & 13 deletions test/PAGReadPixelsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
namespace pag {
using nlohmann::json;

#define CHECK_PIXELS(info, pixels, key) \
{ \
PixelMap pm(info, pixels); \
Baseline::Compare(pm, "PAGReadPixelsTest/" + std::string(key) + ".webp"); \
#define CHECK_PIXELS(info, pixels, key) \
{ \
PixelMap pm(info, pixels); \
EXPECT_TRUE(Baseline::Compare(pm, "PAGReadPixelsTest/" + std::string(key))); \
}

/**
Expand Down Expand Up @@ -261,8 +261,10 @@ PAG_TEST(PAGReadPixelsTest, PngCodec) {
auto rowBytes = image->width() * 4;
auto pixels = new (std::nothrow) uint8_t[rowBytes * image->height()];
ASSERT_TRUE(pixels);
auto info = ImageInfo::Make(1280, 720, ColorType::RGBA_8888, AlphaType::Premultiplied);
auto info = ImageInfo::Make(image->width(), image->height(), ColorType::RGBA_8888,
AlphaType::Premultiplied);
ASSERT_TRUE(image->readPixels(info, pixels));
CHECK_PIXELS(info, pixels, "PngCodec_Decode");
PixelMap pixelMap(info, pixels);
auto bytes = Image::Encode(pixelMap.info(), pixelMap.pixels(), EncodedFormat::PNG, 100);
image = Image::MakeFrom(bytes);
Expand All @@ -282,20 +284,29 @@ PAG_TEST(PAGReadPixelsTest, WebpCodec) {
ASSERT_EQ(image->width(), 110);
ASSERT_EQ(image->height(), 110);
ASSERT_EQ(static_cast<int>(image->orientation()), static_cast<int>(Orientation::TopLeft));
auto rowBytes = image->width() * 4;
auto pixels = new (std::nothrow) uint8_t[rowBytes * image->height()];
auto info = ImageInfo::Make(image->width(), image->height(), ColorType::RGBA_8888,
AlphaType::Premultiplied);
auto pixels = new (std::nothrow) uint8_t[info.byteSize()];
ASSERT_TRUE(pixels);
auto info = ImageInfo::Make(110, 110, ColorType::RGBA_8888, AlphaType::Premultiplied);
bool res = image->readPixels(info, pixels);
ASSERT_TRUE(res);
PixelMap pixelMap(info, pixels);
auto bytes = Image::Encode(pixelMap.info(), pixelMap.pixels(), EncodedFormat::WEBP, 100);
ASSERT_TRUE(image->readPixels(info, pixels));
CHECK_PIXELS(info, pixels, "WebpCodec_Decode");
auto bytes = Image::Encode(info, pixels, EncodedFormat::WEBP, 100);
image = Image::MakeFrom(bytes);
ASSERT_TRUE(image != nullptr);
ASSERT_EQ(image->width(), 110);
ASSERT_EQ(image->height(), 110);
ASSERT_EQ(static_cast<int>(image->orientation()), static_cast<int>(Orientation::TopLeft));

auto a8Info = ImageInfo::Make(image->width(), image->height(), ColorType::ALPHA_8,
AlphaType::Premultiplied);
auto a8Pixels = new (std::nothrow) uint8_t[a8Info.byteSize()];
ASSERT_TRUE(image->readPixels(a8Info, a8Pixels));
auto rgbaFromA8Data = image->Encode(a8Info, a8Pixels, EncodedFormat::WEBP, 100);
auto rgbaFromA8Image = Image::MakeFrom(rgbaFromA8Data);
rgbaFromA8Image->readPixels(info, pixels);
CHECK_PIXELS(info, pixels, "WebpCodec_EncodeA8");
delete[] pixels;
delete[] a8Pixels;
}

/**
Expand All @@ -311,8 +322,10 @@ PAG_TEST(PAGReadPixelsTest, JpegCodec) {
auto pixels = new (std::nothrow)
uint8_t[image->height() * image->width() * ImageInfo::GetBytesPerPixel(outputColorType)];
ASSERT_TRUE(pixels);
auto info = ImageInfo::Make(4032, 3024, outputColorType, AlphaType::Premultiplied);
auto info =
ImageInfo::Make(image->width(), image->height(), outputColorType, AlphaType::Premultiplied);
bool res = image->readPixels(info, pixels);
CHECK_PIXELS(info, pixels, "JpegCodec_Decode");
PixelMap pixelMap(info, pixels);

auto bytes = Image::Encode(pixelMap.info(), pixelMap.pixels(), EncodedFormat::JPEG, 20);
Expand Down
3 changes: 3 additions & 0 deletions test/baseline/PAGReadPixelsTest/JpegCodec_Decode.lzma2
Git LFS file not shown
3 changes: 3 additions & 0 deletions test/baseline/PAGReadPixelsTest/PngCodec_Decode.lzma2
Git LFS file not shown
3 changes: 3 additions & 0 deletions test/baseline/PAGReadPixelsTest/WebpCodec_Decode.lzma2
Git LFS file not shown
3 changes: 3 additions & 0 deletions test/baseline/PAGReadPixelsTest/WebpCodec_EncodeA8.lzma2
Git LFS file not shown
15 changes: 9 additions & 6 deletions tgfx/src/image/webp/WebpImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,16 @@ static int webp_reader_write_data(const uint8_t* data, size_t data_size,

std::shared_ptr<Data> WebpImage::Encode(const ImageInfo& imageInfo, const void* pixels,
EncodedFormat, int quality) {
if (imageInfo.colorType() == ColorType::ALPHA_8) return nullptr;
const uint8_t* srcPixels = static_cast<uint8_t*>(const_cast<void*>(pixels));
const uint8_t* convertPixels = nullptr;
if (imageInfo.colorType() == ColorType::ALPHA_8) {
if (imageInfo.alphaType() == AlphaType::Premultiplied ||
imageInfo.colorType() == ColorType::ALPHA_8) {
PixelMap pixelMap(imageInfo, srcPixels);
auto dstPixels = new uint8_t[imageInfo.byteSize()];
auto dstInfo = ImageInfo::Make(imageInfo.width(), imageInfo.height(), ColorType::RGBA_8888,
AlphaType::Opaque);
auto dstPixels = new uint8_t[imageInfo.width() * imageInfo.height() * 4];
auto colorType =
imageInfo.colorType() == ColorType::ALPHA_8 ? ColorType::RGBA_8888 : imageInfo.colorType();
auto dstInfo = ImageInfo::Make(imageInfo.width(), imageInfo.height(), colorType,
AlphaType::Unpremultiplied);
if (!pixelMap.readPixels(dstInfo, dstPixels)) {
delete[] dstPixels;
return nullptr;
Expand Down Expand Up @@ -177,7 +179,8 @@ std::shared_ptr<Data> WebpImage::Encode(const ImageInfo& imageInfo, const void*
pic.custom_ptr = &webpWriter;
const int rgbStride = pic.width * 4;
auto importProc = WebPPictureImportRGBX;
if (ColorType::RGBA_8888 == imageInfo.colorType()) {
if (ColorType::RGBA_8888 == imageInfo.colorType() ||
ColorType::ALPHA_8 == imageInfo.colorType()) {
if (AlphaType::Opaque == imageInfo.alphaType()) {
importProc = WebPPictureImportRGBX;
} else {
Expand Down

0 comments on commit ccd243c

Please sign in to comment.