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 "SkImageDecoder.h" | 8 #include "SkImageDecoder.h" |
9 #include "SkImageEncoder.h" | 9 #include "SkImageEncoder.h" |
10 #include "SkColor.h" | 10 #include "SkColor.h" |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& region) SK_OVERRIDE; | 92 bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& region) SK_OVERRIDE; |
93 #endif | 93 #endif |
94 Result onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; | 94 Result onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; |
95 | 95 |
96 private: | 96 private: |
97 SkPNGImageIndex* fImageIndex; | 97 SkPNGImageIndex* fImageIndex; |
98 | 98 |
99 bool onDecodeInit(SkStream* stream, png_structp *png_ptrp, png_infop *info_p trp); | 99 bool onDecodeInit(SkStream* stream, png_structp *png_ptrp, png_infop *info_p trp); |
100 bool decodePalette(png_structp png_ptr, png_infop info_ptr, | 100 bool decodePalette(png_structp png_ptr, png_infop info_ptr, |
101 bool * SK_RESTRICT hasAlphap, bool *reallyHasAlphap, | 101 bool * SK_RESTRICT hasAlphap, bool *reallyHasAlphap, |
102 SkColorTable **colorTablep); | 102 SkColorTable **colorTablep, int bitDepth); |
scroggo
2015/03/05 23:23:30
nit: I would prefer to have this new input paramet
David Lattimore
2015/03/06 00:15:26
Done.
| |
103 bool getBitmapColorType(png_structp, png_infop, SkColorType*, bool* hasAlpha , | 103 bool getBitmapColorType(png_structp, png_infop, SkColorType*, bool* hasAlpha , |
104 SkPMColor* theTranspColor); | 104 SkPMColor* theTranspColor); |
105 | 105 |
106 typedef SkImageDecoder INHERITED; | 106 typedef SkImageDecoder INHERITED; |
107 }; | 107 }; |
108 | 108 |
109 #ifndef png_jmpbuf | 109 #ifndef png_jmpbuf |
110 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf) | 110 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf) |
111 #endif | 111 #endif |
112 | 112 |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
343 | 343 |
344 // from here down we are concerned with colortables and pixels | 344 // from here down we are concerned with colortables and pixels |
345 | 345 |
346 // we track if we actually see a non-opaque pixels, since sometimes a PNG se ts its colortype | 346 // we track if we actually see a non-opaque pixels, since sometimes a PNG se ts its colortype |
347 // to |= PNG_COLOR_MASK_ALPHA, but all of its pixels are in fact opaque. We care, since we | 347 // to |= PNG_COLOR_MASK_ALPHA, but all of its pixels are in fact opaque. We care, since we |
348 // draw lots faster if we can flag the bitmap has being opaque | 348 // draw lots faster if we can flag the bitmap has being opaque |
349 bool reallyHasAlpha = false; | 349 bool reallyHasAlpha = false; |
350 SkColorTable* colorTable = NULL; | 350 SkColorTable* colorTable = NULL; |
351 | 351 |
352 if (pngColorType == PNG_COLOR_TYPE_PALETTE) { | 352 if (pngColorType == PNG_COLOR_TYPE_PALETTE) { |
353 decodePalette(png_ptr, info_ptr, &hasAlpha, &reallyHasAlpha, &colorTable ); | 353 decodePalette(png_ptr, info_ptr, &hasAlpha, &reallyHasAlpha, &colorTable , bitDepth); |
354 } | 354 } |
355 | 355 |
356 SkAutoUnref aur(colorTable); | 356 SkAutoUnref aur(colorTable); |
357 | 357 |
358 if (!this->allocPixelRef(decodedBitmap, | 358 if (!this->allocPixelRef(decodedBitmap, |
359 kIndex_8_SkColorType == colorType ? colorTable : NU LL)) { | 359 kIndex_8_SkColorType == colorType ? colorTable : NU LL)) { |
360 return kFailure; | 360 return kFailure; |
361 } | 361 } |
362 | 362 |
363 SkAutoLockPixels alp(*decodedBitmap); | 363 SkAutoLockPixels alp(*decodedBitmap); |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
645 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); | 645 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); |
646 } | 646 } |
647 | 647 |
648 return true; | 648 return true; |
649 } | 649 } |
650 | 650 |
651 typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b); | 651 typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b); |
652 | 652 |
653 bool SkPNGImageDecoder::decodePalette(png_structp png_ptr, png_infop info_ptr, | 653 bool SkPNGImageDecoder::decodePalette(png_structp png_ptr, png_infop info_ptr, |
654 bool *hasAlphap, bool *reallyHasAlphap, | 654 bool *hasAlphap, bool *reallyHasAlphap, |
655 SkColorTable **colorTablep) { | 655 SkColorTable **colorTablep, int bitDepth) { |
656 int numPalette; | 656 int numPalette; |
657 png_colorp palette; | 657 png_colorp palette; |
658 png_bytep trans; | 658 png_bytep trans; |
659 int numTrans; | 659 int numTrans; |
660 | 660 |
661 png_get_PLTE(png_ptr, info_ptr, &palette, &numPalette); | 661 png_get_PLTE(png_ptr, info_ptr, &palette, &numPalette); |
662 | 662 |
663 /* BUGGY IMAGE WORKAROUND | |
664 | |
665 We hit some images (e.g. fruit_.png) who contain bytes that are == color table_count | |
666 which is a problem since we use the byte as an index. To work around thi s we grow | |
667 the colortable by 1 (if its < 256) and duplicate the last color into tha t slot. | |
668 */ | |
669 int colorCount = numPalette + (numPalette < 256); | |
670 SkPMColor colorStorage[256]; // worst-case storage | 663 SkPMColor colorStorage[256]; // worst-case storage |
671 SkPMColor* colorPtr = colorStorage; | 664 SkPMColor* colorPtr = colorStorage; |
672 | 665 |
673 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { | 666 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
674 png_get_tRNS(png_ptr, info_ptr, &trans, &numTrans, NULL); | 667 png_get_tRNS(png_ptr, info_ptr, &trans, &numTrans, NULL); |
675 *hasAlphap = (numTrans > 0); | 668 *hasAlphap = (numTrans > 0); |
676 } else { | 669 } else { |
677 numTrans = 0; | 670 numTrans = 0; |
678 } | 671 } |
679 | 672 |
(...skipping 18 matching lines...) Expand all Loading... | |
698 *colorPtr++ = proc(*trans++, palette->red, palette->green, palette->blue ); | 691 *colorPtr++ = proc(*trans++, palette->red, palette->green, palette->blue ); |
699 palette++; | 692 palette++; |
700 } | 693 } |
701 bool reallyHasAlpha = (transLessThanFF < 0); | 694 bool reallyHasAlpha = (transLessThanFF < 0); |
702 | 695 |
703 for (; index < numPalette; index++) { | 696 for (; index < numPalette; index++) { |
704 *colorPtr++ = SkPackARGB32(0xFF, palette->red, palette->green, palette-> blue); | 697 *colorPtr++ = SkPackARGB32(0xFF, palette->red, palette->green, palette-> blue); |
705 palette++; | 698 palette++; |
706 } | 699 } |
707 | 700 |
708 // see BUGGY IMAGE WORKAROUND comment above | 701 /* BUGGY IMAGE WORKAROUND |
709 if (numPalette < 256) { | 702 |
710 *colorPtr = colorPtr[-1]; | 703 Invalid images could contain pixel values that are greater than the numb er of palette |
704 entries. Since we use pixel values as indices into the palette this coul d result in reading | |
705 beyond the end of the palette which could leak the contents of uninitial ized memory. To | |
706 ensure this doesn't happen, we grow the colortable to the maximum size t hat can be | |
707 addressed by the bitdepth of the image and fill it with the last palette color or black if | |
708 the palette is empty (really broken image). | |
709 */ | |
710 int colorCount = SkTMax(numPalette, 1 << SkTMin(bitDepth, 8)); | |
711 SkPMColor lastColor = index > 0 ? colorPtr[-1] : SkPackARGB32(0xFF, 0, 0, 0) ; | |
712 for (; index < colorCount; index++) { | |
713 *colorPtr++ = lastColor; | |
711 } | 714 } |
712 | 715 |
713 *colorTablep = SkNEW_ARGS(SkColorTable, (colorStorage, colorCount)); | 716 *colorTablep = SkNEW_ARGS(SkColorTable, (colorStorage, colorCount)); |
714 *reallyHasAlphap = reallyHasAlpha; | 717 *reallyHasAlphap = reallyHasAlpha; |
715 return true; | 718 return true; |
716 } | 719 } |
717 | 720 |
718 #ifdef SK_BUILD_FOR_ANDROID | 721 #ifdef SK_BUILD_FOR_ANDROID |
719 | 722 |
720 bool SkPNGImageDecoder::onBuildTileIndex(SkStreamRewindable* sk_stream, int *wid th, int *height) { | 723 bool SkPNGImageDecoder::onBuildTileIndex(SkStreamRewindable* sk_stream, int *wid th, int *height) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
790 | 793 |
791 // from here down we are concerned with colortables and pixels | 794 // from here down we are concerned with colortables and pixels |
792 | 795 |
793 // we track if we actually see a non-opaque pixels, since sometimes a PNG se ts its colortype | 796 // we track if we actually see a non-opaque pixels, since sometimes a PNG se ts its colortype |
794 // to |= PNG_COLOR_MASK_ALPHA, but all of its pixels are in fact opaque. We care, since we | 797 // to |= PNG_COLOR_MASK_ALPHA, but all of its pixels are in fact opaque. We care, since we |
795 // draw lots faster if we can flag the bitmap has being opaque | 798 // draw lots faster if we can flag the bitmap has being opaque |
796 bool reallyHasAlpha = false; | 799 bool reallyHasAlpha = false; |
797 SkColorTable* colorTable = NULL; | 800 SkColorTable* colorTable = NULL; |
798 | 801 |
799 if (pngColorType == PNG_COLOR_TYPE_PALETTE) { | 802 if (pngColorType == PNG_COLOR_TYPE_PALETTE) { |
800 decodePalette(png_ptr, info_ptr, &hasAlpha, &reallyHasAlpha, &colorTable ); | 803 decodePalette(png_ptr, info_ptr, &hasAlpha, &reallyHasAlpha, &colorTable , bitDepth); |
801 } | 804 } |
802 | 805 |
803 SkAutoUnref aur(colorTable); | 806 SkAutoUnref aur(colorTable); |
804 | 807 |
805 // Check ahead of time if the swap(dest, src) is possible. | 808 // Check ahead of time if the swap(dest, src) is possible. |
806 // If yes, then we will stick to AllocPixelRef since it's cheaper with the s wap happening. | 809 // If yes, then we will stick to AllocPixelRef since it's cheaper with the s wap happening. |
807 // If no, then we will use alloc to allocate pixels to prevent garbage colle ction. | 810 // If no, then we will use alloc to allocate pixels to prevent garbage colle ction. |
808 int w = rect.width() / sampleSize; | 811 int w = rect.width() / sampleSize; |
809 int h = rect.height() / sampleSize; | 812 int h = rect.height() / sampleSize; |
810 const bool swapOnly = (rect == region) && (w == decodedBitmap.width()) && | 813 const bool swapOnly = (rect == region) && (w == decodedBitmap.width()) && |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1270 return SkImageDecoder::kUnknown_Format; | 1273 return SkImageDecoder::kUnknown_Format; |
1271 } | 1274 } |
1272 | 1275 |
1273 SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) { | 1276 SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) { |
1274 return (SkImageEncoder::kPNG_Type == t) ? SkNEW(SkPNGImageEncoder) : NULL; | 1277 return (SkImageEncoder::kPNG_Type == t) ? SkNEW(SkPNGImageEncoder) : NULL; |
1275 } | 1278 } |
1276 | 1279 |
1277 static SkImageDecoder_DecodeReg gDReg(sk_libpng_dfactory); | 1280 static SkImageDecoder_DecodeReg gDReg(sk_libpng_dfactory); |
1278 static SkImageDecoder_FormatReg gFormatReg(get_format_png); | 1281 static SkImageDecoder_FormatReg gFormatReg(get_format_png); |
1279 static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory); | 1282 static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory); |
OLD | NEW |