| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #include "SkImageDecoder.h" | 10 #include "SkImageDecoder.h" |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 bool hasAlpha = false; | 292 bool hasAlpha = false; |
| 293 bool doDither = this->getDitherImage(); | 293 bool doDither = this->getDitherImage(); |
| 294 SkPMColor theTranspColor = 0; // 0 tells us not to try to match | 294 SkPMColor theTranspColor = 0; // 0 tells us not to try to match |
| 295 | 295 |
| 296 if (!getBitmapConfig(png_ptr, info_ptr, &config, &hasAlpha, &doDither, &theT
ranspColor)) { | 296 if (!getBitmapConfig(png_ptr, info_ptr, &config, &hasAlpha, &doDither, &theT
ranspColor)) { |
| 297 return false; | 297 return false; |
| 298 } | 298 } |
| 299 | 299 |
| 300 const int sampleSize = this->getSampleSize(); | 300 const int sampleSize = this->getSampleSize(); |
| 301 SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize); | 301 SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize); |
| 302 decodedBitmap->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight
()); |
| 302 | 303 |
| 303 decodedBitmap->lockPixels(); | |
| 304 void* rowptr = (void*) decodedBitmap->getPixels(); | |
| 305 bool reuseBitmap = (rowptr != NULL); | |
| 306 decodedBitmap->unlockPixels(); | |
| 307 if (reuseBitmap && (sampler.scaledWidth() != decodedBitmap->width() || | |
| 308 sampler.scaledHeight() != decodedBitmap->height())) { | |
| 309 // Dimensions must match | |
| 310 return false; | |
| 311 } | |
| 312 | |
| 313 if (!reuseBitmap) { | |
| 314 decodedBitmap->setConfig(config, sampler.scaledWidth(), | |
| 315 sampler.scaledHeight()); | |
| 316 } | |
| 317 if (SkImageDecoder::kDecodeBounds_Mode == mode) { | 304 if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
| 318 return true; | 305 return true; |
| 319 } | 306 } |
| 320 | 307 |
| 321 // from here down we are concerned with colortables and pixels | 308 // from here down we are concerned with colortables and pixels |
| 322 | 309 |
| 323 // we track if we actually see a non-opaque pixels, since sometimes a PNG se
ts its colortype | 310 // we track if we actually see a non-opaque pixels, since sometimes a PNG se
ts its colortype |
| 324 // to |= PNG_COLOR_MASK_ALPHA, but all of its pixels are in fact opaque. We
care, since we | 311 // to |= PNG_COLOR_MASK_ALPHA, but all of its pixels are in fact opaque. We
care, since we |
| 325 // draw lots faster if we can flag the bitmap has being opaque | 312 // draw lots faster if we can flag the bitmap has being opaque |
| 326 bool reallyHasAlpha = false; | 313 bool reallyHasAlpha = false; |
| 327 SkColorTable* colorTable = NULL; | 314 SkColorTable* colorTable = NULL; |
| 328 | 315 |
| 329 if (colorType == PNG_COLOR_TYPE_PALETTE) { | 316 if (colorType == PNG_COLOR_TYPE_PALETTE) { |
| 330 decodePalette(png_ptr, info_ptr, &hasAlpha, &reallyHasAlpha, &colorTable
); | 317 decodePalette(png_ptr, info_ptr, &hasAlpha, &reallyHasAlpha, &colorTable
); |
| 331 } | 318 } |
| 332 | 319 |
| 333 SkAutoUnref aur(colorTable); | 320 SkAutoUnref aur(colorTable); |
| 334 | 321 |
| 335 if (!reuseBitmap) { | 322 if (!this->allocPixelRef(decodedBitmap, |
| 336 if (!this->allocPixelRef(decodedBitmap, | 323 SkBitmap::kIndex8_Config == config ? colorTable : N
ULL)) { |
| 337 SkBitmap::kIndex8_Config == config ? colorTable
: NULL)) { | 324 return false; |
| 338 return false; | |
| 339 } | |
| 340 } | 325 } |
| 341 | 326 |
| 342 SkAutoLockPixels alp(*decodedBitmap); | 327 SkAutoLockPixels alp(*decodedBitmap); |
| 343 | 328 |
| 344 /* Add filler (or alpha) byte (before/after each RGB triplet) */ | 329 /* Add filler (or alpha) byte (before/after each RGB triplet) */ |
| 345 if (colorType == PNG_COLOR_TYPE_RGB || colorType == PNG_COLOR_TYPE_GRAY) { | 330 if (colorType == PNG_COLOR_TYPE_RGB || colorType == PNG_COLOR_TYPE_GRAY) { |
| 346 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); | 331 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); |
| 347 } | 332 } |
| 348 | 333 |
| 349 /* Turn on interlace handling. REQUIRED if you are not using | 334 /* Turn on interlace handling. REQUIRED if you are not using |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 reallyHasAlpha |= substituteTranspColor(decodedBitmap, theTranspColor); | 423 reallyHasAlpha |= substituteTranspColor(decodedBitmap, theTranspColor); |
| 439 } | 424 } |
| 440 if (reallyHasAlpha && this->getRequireUnpremultipliedColors() && | 425 if (reallyHasAlpha && this->getRequireUnpremultipliedColors() && |
| 441 SkBitmap::kARGB_8888_Config != decodedBitmap->config()) { | 426 SkBitmap::kARGB_8888_Config != decodedBitmap->config()) { |
| 442 // If the caller wants an unpremultiplied bitmap, and we let them get | 427 // If the caller wants an unpremultiplied bitmap, and we let them get |
| 443 // away with a config other than 8888, and it has alpha after all, | 428 // away with a config other than 8888, and it has alpha after all, |
| 444 // return false, since the result will have premultiplied colors. | 429 // return false, since the result will have premultiplied colors. |
| 445 return false; | 430 return false; |
| 446 } | 431 } |
| 447 decodedBitmap->setIsOpaque(!reallyHasAlpha); | 432 decodedBitmap->setIsOpaque(!reallyHasAlpha); |
| 448 if (reuseBitmap) { | |
| 449 decodedBitmap->notifyPixelsChanged(); | |
| 450 } | |
| 451 return true; | 433 return true; |
| 452 } | 434 } |
| 453 | 435 |
| 454 | 436 |
| 455 | 437 |
| 456 bool SkPNGImageDecoder::getBitmapConfig(png_structp png_ptr, png_infop info_ptr, | 438 bool SkPNGImageDecoder::getBitmapConfig(png_structp png_ptr, png_infop info_ptr, |
| 457 SkBitmap::Config *configp, bool * SK_RES
TRICT hasAlphap, | 439 SkBitmap::Config *configp, bool * SK_RES
TRICT hasAlphap, |
| 458 bool *doDitherp, SkPMColor *theTranspCol
orp) { | 440 bool *doDitherp, SkPMColor *theTranspCol
orp) { |
| 459 png_uint_32 origWidth, origHeight; | 441 png_uint_32 origWidth, origHeight; |
| 460 int bitDepth, colorType; | 442 int bitDepth, colorType; |
| (...skipping 705 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1166 return SkImageDecoder::kUnknown_Format; | 1148 return SkImageDecoder::kUnknown_Format; |
| 1167 } | 1149 } |
| 1168 | 1150 |
| 1169 SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) { | 1151 SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) { |
| 1170 return (SkImageEncoder::kPNG_Type == t) ? SkNEW(SkPNGImageEncoder) : NULL; | 1152 return (SkImageEncoder::kPNG_Type == t) ? SkNEW(SkPNGImageEncoder) : NULL; |
| 1171 } | 1153 } |
| 1172 | 1154 |
| 1173 static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libpng_efacto
ry); | 1155 static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libpng_efacto
ry); |
| 1174 static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_png)
; | 1156 static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_png)
; |
| 1175 static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libpng_dfactory); | 1157 static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libpng_dfactory); |
| OLD | NEW |