| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkImageEncoder.h" | 8 #include "SkImageEncoder.h" |
| 9 #include "SkColor.h" | 9 #include "SkColor.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 private: | 175 private: |
| 176 bool doEncode(SkWStream* stream, const SkBitmap& bm, | 176 bool doEncode(SkWStream* stream, const SkBitmap& bm, |
| 177 SkAlphaType alphaType, int colorType, | 177 SkAlphaType alphaType, int colorType, |
| 178 int bitDepth, SkColorType ct, | 178 int bitDepth, SkColorType ct, |
| 179 png_color_8& sig_bit); | 179 png_color_8& sig_bit); |
| 180 | 180 |
| 181 typedef SkImageEncoder INHERITED; | 181 typedef SkImageEncoder INHERITED; |
| 182 }; | 182 }; |
| 183 | 183 |
| 184 bool SkPNGImageEncoder::onEncode(SkWStream* stream, | 184 bool SkPNGImageEncoder::onEncode(SkWStream* stream, |
| 185 const SkBitmap& originalBitmap, | 185 const SkBitmap& bitmap, |
| 186 int /*quality*/) { | 186 int /*quality*/) { |
| 187 SkBitmap copy; | 187 const SkColorType ct = bitmap.colorType(); |
| 188 const SkBitmap* bitmap = &originalBitmap; | 188 switch (ct) { |
| 189 switch (originalBitmap.colorType()) { | |
| 190 case kIndex_8_SkColorType: | 189 case kIndex_8_SkColorType: |
| 191 case kGray_8_SkColorType: | 190 case kGray_8_SkColorType: |
| 192 case kRGBA_8888_SkColorType: | 191 case kRGBA_8888_SkColorType: |
| 193 case kBGRA_8888_SkColorType: | 192 case kBGRA_8888_SkColorType: |
| 194 case kARGB_4444_SkColorType: | 193 case kARGB_4444_SkColorType: |
| 195 case kRGB_565_SkColorType: | 194 case kRGB_565_SkColorType: |
| 196 break; | 195 break; |
| 197 default: | 196 default: |
| 198 // TODO(scroggo): support Alpha_8 as Grayscale(black)+Alpha | 197 return false; |
| 199 if (originalBitmap.copyTo(©, kN32_SkColorType)) { | |
| 200 bitmap = © | |
| 201 } | |
| 202 } | 198 } |
| 203 SkColorType ct = bitmap->colorType(); | |
| 204 | 199 |
| 205 const SkAlphaType alphaType = bitmap->alphaType(); | 200 const SkAlphaType alphaType = bitmap.alphaType(); |
| 206 switch (alphaType) { | 201 switch (alphaType) { |
| 207 case kUnpremul_SkAlphaType: | 202 case kUnpremul_SkAlphaType: |
| 208 if (kARGB_4444_SkColorType == ct) { | 203 if (kARGB_4444_SkColorType == ct) { |
| 209 return false; | 204 return false; |
| 210 } | 205 } |
| 211 | 206 |
| 212 break; | 207 break; |
| 213 case kOpaque_SkAlphaType: | 208 case kOpaque_SkAlphaType: |
| 214 case kPremul_SkAlphaType: | 209 case kPremul_SkAlphaType: |
| 215 break; | 210 break; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 sig_bit.red = 5; | 250 sig_bit.red = 5; |
| 256 sig_bit.green = 6; | 251 sig_bit.green = 6; |
| 257 sig_bit.blue = 5; | 252 sig_bit.blue = 5; |
| 258 colorType = PNG_COLOR_TYPE_RGB; | 253 colorType = PNG_COLOR_TYPE_RGB; |
| 259 SkASSERT(isOpaque); | 254 SkASSERT(isOpaque); |
| 260 break; | 255 break; |
| 261 default: | 256 default: |
| 262 return false; | 257 return false; |
| 263 } | 258 } |
| 264 | 259 |
| 265 SkAutoLockPixels alp(*bitmap); | 260 SkAutoLockPixels alp(bitmap); |
| 266 // readyToDraw checks for pixels (and colortable if that is required) | 261 // readyToDraw checks for pixels (and colortable if that is required) |
| 267 if (!bitmap->readyToDraw()) { | 262 if (!bitmap.readyToDraw()) { |
| 268 return false; | 263 return false; |
| 269 } | 264 } |
| 270 | 265 |
| 271 // we must do this after we have locked the pixels | 266 // we must do this after we have locked the pixels |
| 272 SkColorTable* ctable = bitmap->getColorTable(); | 267 SkColorTable* ctable = bitmap.getColorTable(); |
| 273 if (ctable) { | 268 if (ctable) { |
| 274 if (ctable->count() == 0) { | 269 if (ctable->count() == 0) { |
| 275 return false; | 270 return false; |
| 276 } | 271 } |
| 277 // check if we can store in fewer than 8 bits | 272 // check if we can store in fewer than 8 bits |
| 278 bitDepth = computeBitDepth(ctable->count()); | 273 bitDepth = computeBitDepth(ctable->count()); |
| 279 } | 274 } |
| 280 | 275 |
| 281 return doEncode(stream, *bitmap, alphaType, colorType, bitDepth, ct, sig_bit
); | 276 return doEncode(stream, bitmap, alphaType, colorType, bitDepth, ct, sig_bit)
; |
| 282 } | 277 } |
| 283 | 278 |
| 284 bool SkPNGImageEncoder::doEncode(SkWStream* stream, const SkBitmap& bitmap, | 279 bool SkPNGImageEncoder::doEncode(SkWStream* stream, const SkBitmap& bitmap, |
| 285 SkAlphaType alphaType, int colorType, | 280 SkAlphaType alphaType, int colorType, |
| 286 int bitDepth, SkColorType ct, | 281 int bitDepth, SkColorType ct, |
| 287 png_color_8& sig_bit) { | 282 png_color_8& sig_bit) { |
| 288 | 283 |
| 289 png_structp png_ptr; | 284 png_structp png_ptr; |
| 290 png_infop info_ptr; | 285 png_infop info_ptr; |
| 291 | 286 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 | 356 |
| 362 /////////////////////////////////////////////////////////////////////////////// | 357 /////////////////////////////////////////////////////////////////////////////// |
| 363 DEFINE_ENCODER_CREATOR(PNGImageEncoder); | 358 DEFINE_ENCODER_CREATOR(PNGImageEncoder); |
| 364 /////////////////////////////////////////////////////////////////////////////// | 359 /////////////////////////////////////////////////////////////////////////////// |
| 365 | 360 |
| 366 SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) { | 361 SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) { |
| 367 return (SkImageEncoder::kPNG_Type == t) ? new SkPNGImageEncoder : nullptr; | 362 return (SkImageEncoder::kPNG_Type == t) ? new SkPNGImageEncoder : nullptr; |
| 368 } | 363 } |
| 369 | 364 |
| 370 static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory); | 365 static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory); |
| OLD | NEW |