Index: src/images/SkPNGImageEncoder.cpp |
diff --git a/src/images/SkPNGImageEncoder.cpp b/src/images/SkPNGImageEncoder.cpp |
index a9c8b3d7857e2dd604fe9a531e3d676b04158c0e..445f8c73e8a41289c84e48b57828360925e5131e 100644 |
--- a/src/images/SkPNGImageEncoder.cpp |
+++ b/src/images/SkPNGImageEncoder.cpp |
@@ -77,6 +77,7 @@ static transform_scanline_proc choose_proc(SkColorType ct, bool hasAlpha) { |
{ kARGB_4444_SkColorType, true, transform_scanline_4444 }, |
{ kIndex_8_SkColorType, false, transform_scanline_memcpy }, |
{ kGray_8_SkColorType, false, transform_scanline_memcpy }, |
+ { kAlpha_8_SkColorType, true, transform_scanline_memcpy }, |
}; |
for (int i = SK_ARRAY_COUNT(gMap) - 1; i >= 0; --i) { |
@@ -180,13 +181,13 @@ bool SkPNGImageEncoder::onEncode(SkWStream* stream, |
switch (originalBitmap.colorType()) { |
case kIndex_8_SkColorType: |
case kGray_8_SkColorType: |
+ case kAlpha_8_SkColorType: |
case kN32_SkColorType: |
case kARGB_4444_SkColorType: |
case kRGB_565_SkColorType: |
break; |
default: |
// TODO(scroggo): support 8888-but-not-N32 natively. |
- // TODO(scroggo): support Alpha_8 as Grayscale(black)+Alpha |
if (originalBitmap.copyTo(©, kN32_SkColorType)) { |
bitmap = © |
} |
@@ -212,6 +213,15 @@ bool SkPNGImageEncoder::onEncode(SkWStream* stream, |
colorType = PNG_COLOR_TYPE_GRAY; |
SkASSERT(!hasAlpha); |
break; |
+ case kAlpha_8_SkColorType: |
+ // This is a bit of a trick. PNG does not have an alpha-only format, so |
+ // we tell libpng that we are encoding a gray image. We'll also encode |
+ // an extra custom chunk, so our decoder knows that the image is actually |
+ // Alpha8. |
+ sig_bit.gray = 8; |
+ colorType = PNG_COLOR_TYPE_GRAY; |
+ SkASSERT(hasAlpha); |
+ break; |
case kN32_SkColorType: |
sig_bit.red = 8; |
sig_bit.green = 8; |
@@ -313,6 +323,22 @@ bool SkPNGImageEncoder::doEncode(SkWStream* stream, const SkBitmap& bitmap, |
} |
png_set_sBIT(png_ptr, info_ptr, &sig_bit); |
+ |
+ if (kAlpha_8_SkColorType == ct) { |
+ static constexpr char key[] = "SkColorType"; |
+ static constexpr char value[] = "Alpha8"; |
+ |
+ png_text text; |
+ text.compression = 0; // Use deflate for text compression. |
+ text.key = const_cast<char*>(key); |
+ text.text = const_cast<char*>(value); |
+ text.text_length = sizeof(value) - 1; |
+ text.itxt_length = 0; |
+ text.lang = nullptr; |
+ text.lang_key = nullptr; |
+ png_set_text(png_ptr, info_ptr, &text, 1); |
+ } |
+ |
png_write_info(png_ptr, info_ptr); |
const char* srcImage = (const char*)bitmap.getPixels(); |