Convert pixel formats for animated PNG frames using the source blend mode (flutter/engine#42581)
The destination buffer format may not exactly match the source format. In particular, the alpha type of the source may need to be converted to the alpha type of the destination. Fixes https://github.com/flutter/flutter/issues/128102
This commit is contained in:
BIN
engine/src/flutter/lib/ui/fixtures/alpha_animated.apng
Normal file
BIN
engine/src/flutter/lib/ui/fixtures/alpha_animated.apng
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 367 B |
@@ -156,51 +156,58 @@ bool APNGImageGenerator::GetPixels(const SkImageInfo& info,
|
||||
|
||||
FML_DCHECK(frame_info.bytesPerPixel() == sizeof(Pixel));
|
||||
|
||||
for (int y = 0; y < frame_info.height(); y++) {
|
||||
auto src_row = frame.pixels.data() + y * frame_row_bytes;
|
||||
auto dst_row = static_cast<uint8_t*>(pixels) +
|
||||
(y + frame.y_offset) * row_bytes +
|
||||
frame.x_offset * frame_info.bytesPerPixel();
|
||||
bool result = true;
|
||||
|
||||
switch (frame.frame_info->blend_mode) {
|
||||
case SkCodecAnimation::Blend::kSrcOver: {
|
||||
for (int x = 0; x < frame_info.width(); x++) {
|
||||
auto x_offset_bytes = x * frame_info.bytesPerPixel();
|
||||
if (frame.frame_info->blend_mode == SkCodecAnimation::Blend::kSrc) {
|
||||
SkPixmap src_pixmap(frame_info, frame.pixels.data(), frame_row_bytes);
|
||||
uint8_t* dst_pixels = static_cast<uint8_t*>(pixels) +
|
||||
frame.y_offset * row_bytes +
|
||||
frame.x_offset * frame_info.bytesPerPixel();
|
||||
result = src_pixmap.readPixels(info, dst_pixels, row_bytes);
|
||||
if (!result) {
|
||||
FML_DLOG(ERROR) << "Failed to copy pixels at index " << image_index
|
||||
<< " (frame index: " << frame_index << ") of APNG.";
|
||||
}
|
||||
} else if (frame.frame_info->blend_mode ==
|
||||
SkCodecAnimation::Blend::kSrcOver) {
|
||||
for (int y = 0; y < frame_info.height(); y++) {
|
||||
auto src_row = frame.pixels.data() + y * frame_row_bytes;
|
||||
auto dst_row = static_cast<uint8_t*>(pixels) +
|
||||
(y + frame.y_offset) * row_bytes +
|
||||
frame.x_offset * frame_info.bytesPerPixel();
|
||||
|
||||
Pixel src = *reinterpret_cast<Pixel*>(src_row + x_offset_bytes);
|
||||
Pixel* dst_p = reinterpret_cast<Pixel*>(dst_row + x_offset_bytes);
|
||||
Pixel dst = *dst_p;
|
||||
for (int x = 0; x < frame_info.width(); x++) {
|
||||
auto x_offset_bytes = x * frame_info.bytesPerPixel();
|
||||
|
||||
// Ensure both colors are premultiplied for the blending operation.
|
||||
if (info.alphaType() == kUnpremul_SkAlphaType) {
|
||||
dst.Premultiply();
|
||||
}
|
||||
if (frame_info.alphaType() == kUnpremul_SkAlphaType) {
|
||||
src.Premultiply();
|
||||
}
|
||||
Pixel src = *reinterpret_cast<Pixel*>(src_row + x_offset_bytes);
|
||||
Pixel* dst_p = reinterpret_cast<Pixel*>(dst_row + x_offset_bytes);
|
||||
Pixel dst = *dst_p;
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
dst.channel[i] = src.channel[i] +
|
||||
dst.channel[i] * (0xFF - src.GetAlpha()) / 0xFF;
|
||||
}
|
||||
|
||||
// The final color is premultiplied. Unpremultiply to match the
|
||||
// backdrop surface if necessary.
|
||||
if (info.alphaType() == kUnpremul_SkAlphaType) {
|
||||
dst.Unpremultiply();
|
||||
}
|
||||
|
||||
*dst_p = dst;
|
||||
// Ensure both colors are premultiplied for the blending operation.
|
||||
if (info.alphaType() == kUnpremul_SkAlphaType) {
|
||||
dst.Premultiply();
|
||||
}
|
||||
break;
|
||||
if (frame_info.alphaType() == kUnpremul_SkAlphaType) {
|
||||
src.Premultiply();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
dst.channel[i] =
|
||||
src.channel[i] + dst.channel[i] * (0xFF - src.GetAlpha()) / 0xFF;
|
||||
}
|
||||
|
||||
// The final color is premultiplied. Unpremultiply to match the
|
||||
// backdrop surface if necessary.
|
||||
if (info.alphaType() == kUnpremul_SkAlphaType) {
|
||||
dst.Unpremultiply();
|
||||
}
|
||||
|
||||
*dst_p = dst;
|
||||
}
|
||||
case SkCodecAnimation::Blend::kSrc:
|
||||
memcpy(dst_row, src_row, frame_row_bytes);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return result;
|
||||
}
|
||||
|
||||
std::unique_ptr<ImageGenerator> APNGImageGenerator::MakeFromData(
|
||||
|
||||
@@ -202,6 +202,24 @@ void main() {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('Animated apng alpha type handling', () async {
|
||||
// https://github.com/flutter/engine/pull/42153
|
||||
|
||||
final Uint8List data = File(
|
||||
path.join('flutter', 'lib', 'ui', 'fixtures', 'alpha_animated.apng'),
|
||||
).readAsBytesSync();
|
||||
final ui.Codec codec = await ui.instantiateImageCodec(data);
|
||||
|
||||
// The test image contains two frames of solid red. The first has
|
||||
// alpha=0.2, and the second has alpha=0.6.
|
||||
ui.Image image = (await codec.getNextFrame()).image;
|
||||
ByteData imageData = (await image.toByteData())!;
|
||||
expect(imageData.getUint32(0), 0x33000033);
|
||||
image = (await codec.getNextFrame()).image;
|
||||
imageData = (await image.toByteData())!;
|
||||
expect(imageData.getUint32(0), 0x99000099);
|
||||
});
|
||||
}
|
||||
|
||||
/// Returns a File handle to a file in the skia/resources directory.
|
||||
|
||||
Reference in New Issue
Block a user