gaaclarke
2024-06-11 15:34:19 -07:00
committed by GitHub
parent 577c5a5761
commit 41f2a43998
2 changed files with 87 additions and 3 deletions

View File

@@ -294,6 +294,7 @@ if (enable_unittests) {
"//flutter/testing",
"//flutter/testing:dart",
"//flutter/testing:fixture_test",
"//flutter/third_party/libpng",
"//flutter/third_party/tonic",
]

View File

@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "png.h"
#include "flutter/lib/ui/painting/image_encoding.h"
#include "flutter/lib/ui/painting/image_encoding_impl.h"
@@ -469,6 +471,81 @@ TEST(ImageEncodingImpellerTest, PngEncoding10XR) {
EXPECT_TRUE(png.ok());
}
namespace {
struct PngMemoryReader {
const uint8_t* data;
size_t offset;
size_t size;
};
void PngMemoryRead(png_structp png_ptr,
png_bytep out_bytes,
png_size_t byte_count_to_read) {
PngMemoryReader* memory_reader =
reinterpret_cast<PngMemoryReader*>(png_get_io_ptr(png_ptr));
if (memory_reader->offset + byte_count_to_read > memory_reader->size) {
png_error(png_ptr, "Read error in PngMemoryRead");
}
memcpy(out_bytes, memory_reader->data + memory_reader->offset,
byte_count_to_read);
memory_reader->offset += byte_count_to_read;
}
fml::StatusOr<std::vector<uint32_t>> ReadPngFromMemory(const uint8_t* png_data,
size_t png_size) {
png_structp png =
png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!png) {
return fml::Status(fml::StatusCode::kAborted, "unknown");
}
png_infop info = png_create_info_struct(png);
if (!info) {
png_destroy_read_struct(&png, nullptr, nullptr);
return fml::Status(fml::StatusCode::kAborted, "unknown");
}
fml::ScopedCleanupClosure png_cleanup(
[&png, &info]() { png_destroy_read_struct(&png, &info, nullptr); });
if (setjmp(png_jmpbuf(png))) {
return fml::Status(fml::StatusCode::kAborted, "unknown");
}
PngMemoryReader memory_reader = {
.data = png_data, .offset = 0, .size = png_size};
png_set_read_fn(png, &memory_reader, PngMemoryRead);
png_read_info(png, info);
int width = png_get_image_width(png, info);
int height = png_get_image_height(png, info);
png_byte color_type = png_get_color_type(png, info);
png_byte bit_depth = png_get_bit_depth(png, info);
if (bit_depth == 16) {
png_set_strip_16(png);
}
if (color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png);
}
png_read_update_info(png, info);
std::vector<uint32_t> result(width * height);
std::vector<png_bytep> row_pointers;
row_pointers.reserve(height);
for (int i = 0; i < height; ++i) {
row_pointers.push_back(
reinterpret_cast<png_bytep>(result.data() + i * width));
}
png_read_image(png, row_pointers.data());
return result;
}
} // namespace
TEST(ImageEncodingImpellerTest, PngEncodingBGRA10XR) {
int width = 100;
int height = 100;
@@ -482,13 +559,19 @@ TEST(ImageEncodingImpellerTest, PngEncodingBGRA10XR) {
paint.setColor(SK_ColorBLUE);
paint.setAntiAlias(true);
canvas->clear(SK_ColorWHITE);
canvas->drawCircle(width / 2, height / 2, 100, paint);
canvas->clear(SK_ColorRED);
canvas->drawCircle(width / 2, height / 2, 25, paint);
sk_sp<SkImage> image = surface->makeImageSnapshot();
fml::StatusOr<sk_sp<SkData>> png = EncodeImage(image, ImageByteFormat::kPNG);
EXPECT_TRUE(png.ok());
ASSERT_TRUE(png.ok());
fml::StatusOr<std::vector<uint32_t>> pixels =
ReadPngFromMemory(png.value()->bytes(), png.value()->size());
ASSERT_TRUE(pixels.ok());
EXPECT_EQ(pixels.value()[0], 0xff0000ff);
int middle = 100 * 50 + 50;
EXPECT_EQ(pixels.value()[middle], 0xffff0000);
}
#endif // IMPELLER_SUPPORTS_RENDERING