SkPNGImageEncoder encodes all SkColorTypes

Review URL: https://codereview.chromium.org/1506663002
This commit is contained in:
halcanary 2015-12-07 14:07:31 -08:00 committed by Commit bot
parent 987deab2b5
commit dfd6c6e3bb

View File

@ -827,10 +827,28 @@ private:
typedef SkImageEncoder INHERITED; typedef SkImageEncoder INHERITED;
}; };
bool SkPNGImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap, int /*quality*/) { bool SkPNGImageEncoder::onEncode(SkWStream* stream,
SkColorType ct = bitmap.colorType(); const SkBitmap& originalBitmap,
int /*quality*/) {
SkBitmap copy;
const SkBitmap* bitmap = &originalBitmap;
switch (originalBitmap.colorType()) {
case kIndex_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 kGray_8 directly.
// TODO(scroggo): support Alpha_8 as Grayscale(black)+Alpha
if (originalBitmap.copyTo(&copy, kN32_SkColorType)) {
bitmap = ©
}
}
SkColorType ct = bitmap->colorType();
const bool hasAlpha = !bitmap.isOpaque(); const bool hasAlpha = !bitmap->isOpaque();
int colorType = PNG_COLOR_MASK_COLOR; int colorType = PNG_COLOR_MASK_COLOR;
int bitDepth = 8; // default for color int bitDepth = 8; // default for color
png_color_8 sig_bit; png_color_8 sig_bit;
@ -870,14 +888,14 @@ bool SkPNGImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap, int
sig_bit.alpha = 0; sig_bit.alpha = 0;
} }
SkAutoLockPixels alp(bitmap); SkAutoLockPixels alp(*bitmap);
// readyToDraw checks for pixels (and colortable if that is required) // readyToDraw checks for pixels (and colortable if that is required)
if (!bitmap.readyToDraw()) { if (!bitmap->readyToDraw()) {
return false; return false;
} }
// we must do this after we have locked the pixels // we must do this after we have locked the pixels
SkColorTable* ctable = bitmap.getColorTable(); SkColorTable* ctable = bitmap->getColorTable();
if (ctable) { if (ctable) {
if (ctable->count() == 0) { if (ctable->count() == 0) {
return false; return false;
@ -886,7 +904,7 @@ bool SkPNGImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap, int
bitDepth = computeBitDepth(ctable->count()); bitDepth = computeBitDepth(ctable->count());
} }
return doEncode(stream, bitmap, hasAlpha, colorType, bitDepth, ct, sig_bit); return doEncode(stream, *bitmap, hasAlpha, colorType, bitDepth, ct, sig_bit);
} }
bool SkPNGImageEncoder::doEncode(SkWStream* stream, const SkBitmap& bitmap, bool SkPNGImageEncoder::doEncode(SkWStream* stream, const SkBitmap& bitmap,