| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 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 "SkCodecPriv.h" | 8 #include "SkCodecPriv.h" |
| 9 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
| 10 #include "SkColorTable.h" | 10 #include "SkColorTable.h" |
| 11 #include "SkBitmap.h" | 11 #include "SkBitmap.h" |
| 12 #include "SkMath.h" | 12 #include "SkMath.h" |
| 13 #include "SkPngCodec.h" | 13 #include "SkPngCodec.h" |
| 14 #include "SkPngFilters.h" |
| 14 #include "SkSize.h" | 15 #include "SkSize.h" |
| 15 #include "SkStream.h" | 16 #include "SkStream.h" |
| 16 #include "SkSwizzler.h" | 17 #include "SkSwizzler.h" |
| 17 #include "SkTemplates.h" | 18 #include "SkTemplates.h" |
| 18 | 19 |
| 20 #if defined(__SSE2__) |
| 21 #include "pngstruct.h" |
| 22 |
| 23 extern "C" void sk_png_init_filter_functions_sse2(png_structp png, unsigned
int bpp) { |
| 24 if (bpp == 3) { |
| 25 png->read_filter[PNG_FILTER_VALUE_SUB -1] = sk_sub3_sse2; |
| 26 png->read_filter[PNG_FILTER_VALUE_AVG -1] = sk_avg3_sse2; |
| 27 png->read_filter[PNG_FILTER_VALUE_PAETH-1] = sk_paeth3_sse2; |
| 28 } |
| 29 if (bpp == 4) { |
| 30 png->read_filter[PNG_FILTER_VALUE_SUB -1] = sk_sub4_sse2; |
| 31 png->read_filter[PNG_FILTER_VALUE_AVG -1] = sk_avg4_sse2; |
| 32 png->read_filter[PNG_FILTER_VALUE_PAETH-1] = sk_paeth4_sse2; |
| 33 } |
| 34 } |
| 35 #endif |
| 36 |
| 19 /////////////////////////////////////////////////////////////////////////////// | 37 /////////////////////////////////////////////////////////////////////////////// |
| 20 // Helper macros | 38 // Helper macros |
| 21 /////////////////////////////////////////////////////////////////////////////// | 39 /////////////////////////////////////////////////////////////////////////////// |
| 22 | 40 |
| 23 #ifndef png_jmpbuf | 41 #ifndef png_jmpbuf |
| 24 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf) | 42 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf) |
| 25 #endif | 43 #endif |
| 26 | 44 |
| 27 /* These were dropped in libpng >= 1.4 */ | 45 /* These were dropped in libpng >= 1.4 */ |
| 28 #ifndef png_infopp_NULL | 46 #ifndef png_infopp_NULL |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 png_set_tRNS_to_alpha(png_ptr); | 342 png_set_tRNS_to_alpha(png_ptr); |
| 325 png_set_gray_to_rgb(png_ptr); | 343 png_set_gray_to_rgb(png_ptr); |
| 326 skColorType = kN32_SkColorType; | 344 skColorType = kN32_SkColorType; |
| 327 skAlphaType = kUnpremul_SkAlphaType; | 345 skAlphaType = kUnpremul_SkAlphaType; |
| 328 } else { | 346 } else { |
| 329 skColorType = kGray_8_SkColorType; | 347 skColorType = kGray_8_SkColorType; |
| 330 skAlphaType = kOpaque_SkAlphaType; | 348 skAlphaType = kOpaque_SkAlphaType; |
| 331 } | 349 } |
| 332 break; | 350 break; |
| 333 case PNG_COLOR_TYPE_GRAY_ALPHA: | 351 case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 334 //FIXME: support gray with alpha as a color type | 352 //FIXME: support gray with alpha as a color type |
| 335 //convert to RGBA | 353 //convert to RGBA |
| 336 png_set_gray_to_rgb(png_ptr); | 354 png_set_gray_to_rgb(png_ptr); |
| 337 skColorType = kN32_SkColorType; | 355 skColorType = kN32_SkColorType; |
| 338 skAlphaType = kUnpremul_SkAlphaType; | 356 skAlphaType = kUnpremul_SkAlphaType; |
| 339 break; | 357 break; |
| 340 case PNG_COLOR_TYPE_RGBA: | 358 case PNG_COLOR_TYPE_RGBA: |
| 341 skColorType = kN32_SkColorType; | 359 skColorType = kN32_SkColorType; |
| 342 skAlphaType = kUnpremul_SkAlphaType; | 360 skAlphaType = kUnpremul_SkAlphaType; |
| 343 break; | 361 break; |
| 344 default: | 362 default: |
| 345 //all the color types have been covered above | 363 //all the color types have been covered above |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 SkCodec::Result SkPngCodec::initializeSwizzler(const SkImageInfo& requestedInfo, | 417 SkCodec::Result SkPngCodec::initializeSwizzler(const SkImageInfo& requestedInfo, |
| 400 const Options& options, | 418 const Options& options, |
| 401 SkPMColor ctable[], | 419 SkPMColor ctable[], |
| 402 int* ctableCount) { | 420 int* ctableCount) { |
| 403 // FIXME: Could we use the return value of setjmp to specify the type of | 421 // FIXME: Could we use the return value of setjmp to specify the type of |
| 404 // error? | 422 // error? |
| 405 if (setjmp(png_jmpbuf(fPng_ptr))) { | 423 if (setjmp(png_jmpbuf(fPng_ptr))) { |
| 406 SkCodecPrintf("setjmp long jump!\n"); | 424 SkCodecPrintf("setjmp long jump!\n"); |
| 407 return kInvalidInput; | 425 return kInvalidInput; |
| 408 } | 426 } |
| 409 png_read_update_info(fPng_ptr, fInfo_ptr); | 427 png_read_update_info(fPng_ptr, fInfo_ptr); |
| 410 | 428 |
| 411 //srcColorType was determined in read_header() which determined png color ty
pe | 429 //srcColorType was determined in read_header() which determined png color ty
pe |
| 412 const SkColorType srcColorType = this->getInfo().colorType(); | 430 const SkColorType srcColorType = this->getInfo().colorType(); |
| 413 | 431 |
| 414 switch (srcColorType) { | 432 switch (srcColorType) { |
| 415 case kIndex_8_SkColorType: | 433 case kIndex_8_SkColorType: |
| 416 //decode palette to Skia format | 434 //decode palette to Skia format |
| 417 fSrcConfig = SkSwizzler::kIndex; | 435 fSrcConfig = SkSwizzler::kIndex; |
| 418 if (!this->decodePalette(kPremul_SkAlphaType == requestedInfo.alphaT
ype(), | 436 if (!this->decodePalette(kPremul_SkAlphaType == requestedInfo.alphaT
ype(), |
| 419 ctableCount)) { | 437 ctableCount)) { |
| 420 return kInvalidInput; | 438 return kInvalidInput; |
| 421 } | 439 } |
| 422 break; | 440 break; |
| 423 case kGray_8_SkColorType: | 441 case kGray_8_SkColorType: |
| 424 fSrcConfig = SkSwizzler::kGray; | 442 fSrcConfig = SkSwizzler::kGray; |
| 425 break; | 443 break; |
| 426 case kN32_SkColorType: | 444 case kN32_SkColorType: |
| 427 if (this->getInfo().alphaType() == kOpaque_SkAlphaType) { | 445 if (this->getInfo().alphaType() == kOpaque_SkAlphaType) { |
| 428 fSrcConfig = SkSwizzler::kRGB; | 446 fSrcConfig = SkSwizzler::kRGB; |
| 429 } else { | 447 } else { |
| 430 fSrcConfig = SkSwizzler::kRGBA; | 448 fSrcConfig = SkSwizzler::kRGBA; |
| 431 } | 449 } |
| 432 break; | 450 break; |
| 433 default: | 451 default: |
| 434 //would have exited before now if the colorType was supported by png | 452 //would have exited before now if the colorType was supported by png |
| 435 SkASSERT(false); | 453 SkASSERT(false); |
| 436 } | 454 } |
| 437 | 455 |
| 438 // Copy the color table to the client if they request kIndex8 mode | 456 // Copy the color table to the client if they request kIndex8 mode |
| 439 copy_color_table(requestedInfo, fColorTable, ctable, ctableCount); | 457 copy_color_table(requestedInfo, fColorTable, ctable, ctableCount); |
| 440 | 458 |
| 441 // Create the swizzler. SkPngCodec retains ownership of the color table. | 459 // Create the swizzler. SkPngCodec retains ownership of the color table. |
| 442 const SkPMColor* colors = get_color_ptr(fColorTable.get()); | 460 const SkPMColor* colors = get_color_ptr(fColorTable.get()); |
| 443 fSwizzler.reset(SkSwizzler::CreateSwizzler(fSrcConfig, colors, requestedInfo
, options)); | 461 fSwizzler.reset(SkSwizzler::CreateSwizzler(fSrcConfig, colors, requestedInfo
, options)); |
| 444 if (!fSwizzler) { | 462 if (!fSwizzler) { |
| 445 // FIXME: CreateSwizzler could fail for another reason. | 463 // FIXME: CreateSwizzler could fail for another reason. |
| 446 return kUnimplemented; | 464 return kUnimplemented; |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 617 | 635 |
| 618 return row; | 636 return row; |
| 619 } | 637 } |
| 620 | 638 |
| 621 bool onSkipScanlines(int count) override { | 639 bool onSkipScanlines(int count) override { |
| 622 // Assume that an error in libpng indicates an incomplete input. | 640 // Assume that an error in libpng indicates an incomplete input. |
| 623 if (setjmp(png_jmpbuf(this->png_ptr()))) { | 641 if (setjmp(png_jmpbuf(this->png_ptr()))) { |
| 624 SkCodecPrintf("setjmp long jump!\n"); | 642 SkCodecPrintf("setjmp long jump!\n"); |
| 625 return false; | 643 return false; |
| 626 } | 644 } |
| 627 //there is a potential tradeoff of memory vs speed created by putting th
is in a loop. | 645 //there is a potential tradeoff of memory vs speed created by putting th
is in a loop. |
| 628 //calling png_read_rows in a loop is insignificantly slower than calling
it once with count | 646 //calling png_read_rows in a loop is insignificantly slower than calling
it once with count |
| 629 //as png_read_rows has it's own loop which calls png_read_row count time
s. | 647 //as png_read_rows has it's own loop which calls png_read_row count time
s. |
| 630 for (int row = 0; row < count; row++) { | 648 for (int row = 0; row < count; row++) { |
| 631 png_read_rows(this->png_ptr(), &fSrcRow, png_bytepp_NULL, 1); | 649 png_read_rows(this->png_ptr(), &fSrcRow, png_bytepp_NULL, 1); |
| 632 } | 650 } |
| 633 return true; | 651 return true; |
| 634 } | 652 } |
| 635 | 653 |
| 636 private: | 654 private: |
| 637 SkAutoTMalloc<uint8_t> fStorage; | 655 SkAutoTMalloc<uint8_t> fStorage; |
| 638 uint8_t* fSrcRow; | 656 uint8_t* fSrcRow; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 649 : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, n
umberPasses) | 667 : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, n
umberPasses) |
| 650 , fHeight(-1) | 668 , fHeight(-1) |
| 651 , fCanSkipRewind(false) | 669 , fCanSkipRewind(false) |
| 652 { | 670 { |
| 653 SkASSERT(numberPasses != 1); | 671 SkASSERT(numberPasses != 1); |
| 654 } | 672 } |
| 655 | 673 |
| 656 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& opti
ons, | 674 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& opti
ons, |
| 657 SkPMColor ctable[], int* ctableCount) override { | 675 SkPMColor ctable[], int* ctableCount) override { |
| 658 if (!conversion_possible(dstInfo, this->getInfo())) { | 676 if (!conversion_possible(dstInfo, this->getInfo())) { |
| 659 return kInvalidConversion; | 677 return kInvalidConversion; |
| 660 } | 678 } |
| 661 | 679 |
| 662 const Result result = this->initializeSwizzler(dstInfo, options, ctable, | 680 const Result result = this->initializeSwizzler(dstInfo, options, ctable, |
| 663 ctableCount); | 681 ctableCount); |
| 664 if (result != kSuccess) { | 682 if (result != kSuccess) { |
| 665 return result; | 683 return result; |
| 666 } | 684 } |
| 667 | 685 |
| 668 fHeight = dstInfo.height(); | 686 fHeight = dstInfo.height(); |
| 669 // FIXME: This need not be called on a second call to onStartScanlineDec
ode. | 687 // FIXME: This need not be called on a second call to onStartScanlineDec
ode. |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 } | 796 } |
| 779 | 797 |
| 780 if (1 == numberPasses) { | 798 if (1 == numberPasses) { |
| 781 return new SkPngScanlineDecoder(imageInfo, streamDeleter.detach(), chunk
Reader, | 799 return new SkPngScanlineDecoder(imageInfo, streamDeleter.detach(), chunk
Reader, |
| 782 png_ptr, info_ptr, bitDepth); | 800 png_ptr, info_ptr, bitDepth); |
| 783 } | 801 } |
| 784 | 802 |
| 785 return new SkPngInterlacedScanlineDecoder(imageInfo, streamDeleter.detach(),
chunkReader, | 803 return new SkPngInterlacedScanlineDecoder(imageInfo, streamDeleter.detach(),
chunkReader, |
| 786 png_ptr, info_ptr, bitDepth, numbe
rPasses); | 804 png_ptr, info_ptr, bitDepth, numbe
rPasses); |
| 787 } | 805 } |
| OLD | NEW |