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 "SkCodec.h" | 8 #include "SkCodec.h" |
9 #include "SkMSAN.h" | 9 #include "SkMSAN.h" |
10 #include "SkJpegCodec.h" | 10 #include "SkJpegCodec.h" |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 fColorXform.reset(nullptr); | 350 fColorXform.reset(nullptr); |
351 | 351 |
352 return true; | 352 return true; |
353 } | 353 } |
354 | 354 |
355 /* | 355 /* |
356 * Checks if the conversion between the input image and the requested output | 356 * Checks if the conversion between the input image and the requested output |
357 * image has been implemented | 357 * image has been implemented |
358 * Sets the output color space | 358 * Sets the output color space |
359 */ | 359 */ |
360 bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dstInfo) { | 360 bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dstInfo, bool needsColo
rXform) { |
361 if (kUnknown_SkAlphaType == dstInfo.alphaType()) { | 361 if (kUnknown_SkAlphaType == dstInfo.alphaType()) { |
362 return false; | 362 return false; |
363 } | 363 } |
364 | 364 |
365 if (kOpaque_SkAlphaType != dstInfo.alphaType()) { | 365 if (kOpaque_SkAlphaType != dstInfo.alphaType()) { |
366 SkCodecPrintf("Warning: an opaque image should be decoded as opaque " | 366 SkCodecPrintf("Warning: an opaque image should be decoded as opaque " |
367 "- it is being decoded as non-opaque, which will draw slow
er\n"); | 367 "- it is being decoded as non-opaque, which will draw slow
er\n"); |
368 } | 368 } |
369 | 369 |
370 // Check if we will decode to CMYK. libjpeg-turbo does not convert CMYK to
RGBA, so | 370 // Check if we will decode to CMYK. libjpeg-turbo does not convert CMYK to
RGBA, so |
371 // we must do it ourselves. | 371 // we must do it ourselves. |
372 J_COLOR_SPACE encodedColorType = fDecoderMgr->dinfo()->jpeg_color_space; | 372 J_COLOR_SPACE encodedColorType = fDecoderMgr->dinfo()->jpeg_color_space; |
373 bool isCMYK = (JCS_CMYK == encodedColorType || JCS_YCCK == encodedColorType)
; | 373 bool isCMYK = (JCS_CMYK == encodedColorType || JCS_YCCK == encodedColorType)
; |
374 | 374 |
375 // Check for valid color types and set the output color space | 375 // Check for valid color types and set the output color space |
376 switch (dstInfo.colorType()) { | 376 switch (dstInfo.colorType()) { |
377 case kRGBA_8888_SkColorType: | 377 case kRGBA_8888_SkColorType: |
378 if (isCMYK) { | 378 if (isCMYK) { |
379 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; | 379 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
380 } else { | 380 } else { |
381 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; | 381 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; |
382 } | 382 } |
383 return true; | 383 return true; |
384 case kBGRA_8888_SkColorType: | 384 case kBGRA_8888_SkColorType: |
385 if (isCMYK) { | 385 if (isCMYK) { |
386 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; | 386 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
387 } else if (fColorXform) { | 387 } else if (needsColorXform) { |
388 // Our color transformation code requires RGBA order inputs, but
it'll swizzle | 388 // Our color transformation code requires RGBA order inputs, but
it'll swizzle |
389 // to BGRA for us. | 389 // to BGRA for us. |
390 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; | 390 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; |
391 } else { | 391 } else { |
392 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA; | 392 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA; |
393 } | 393 } |
394 return true; | 394 return true; |
395 case kRGB_565_SkColorType: | 395 case kRGB_565_SkColorType: |
396 if (fColorXform) { | 396 if (needsColorXform) { |
397 return false; | 397 return false; |
398 } | 398 } |
399 | 399 |
400 if (isCMYK) { | 400 if (isCMYK) { |
401 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; | 401 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
402 } else { | 402 } else { |
403 fDecoderMgr->dinfo()->dither_mode = JDITHER_NONE; | 403 fDecoderMgr->dinfo()->dither_mode = JDITHER_NONE; |
404 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565; | 404 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565; |
405 } | 405 } |
406 return true; | 406 return true; |
407 case kGray_8_SkColorType: | 407 case kGray_8_SkColorType: |
408 if (fColorXform || JCS_GRAYSCALE != encodedColorType) { | 408 if (needsColorXform || JCS_GRAYSCALE != encodedColorType) { |
409 return false; | 409 return false; |
410 } | 410 } |
411 | 411 |
412 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE; | 412 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE; |
413 return true; | 413 return true; |
414 case kRGBA_F16_SkColorType: | 414 case kRGBA_F16_SkColorType: |
415 SkASSERT(fColorXform); | 415 SkASSERT(needsColorXform); |
416 if (!dstInfo.colorSpace()->gammaIsLinear()) { | |
417 return false; | |
418 } | |
419 | 416 |
420 if (isCMYK) { | 417 if (isCMYK) { |
421 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; | 418 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
422 } else { | 419 } else { |
423 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; | 420 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; |
424 } | 421 } |
425 return true; | 422 return true; |
426 default: | 423 default: |
427 return false; | 424 return false; |
428 } | 425 } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 } | 538 } |
542 | 539 |
543 // Get a pointer to the decompress info since we will use it quite frequentl
y | 540 // Get a pointer to the decompress info since we will use it quite frequentl
y |
544 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); | 541 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); |
545 | 542 |
546 // Set the jump location for libjpeg errors | 543 // Set the jump location for libjpeg errors |
547 if (setjmp(fDecoderMgr->getJmpBuf())) { | 544 if (setjmp(fDecoderMgr->getJmpBuf())) { |
548 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 545 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
549 } | 546 } |
550 | 547 |
551 this->initializeColorXform(dstInfo); | |
552 | |
553 // Check if we can decode to the requested destination and set the output co
lor space | 548 // Check if we can decode to the requested destination and set the output co
lor space |
554 if (!this->setOutputColorSpace(dstInfo)) { | 549 bool needsColorXform = needs_color_xform(dstInfo, this->getInfo()); |
| 550 if (!this->setOutputColorSpace(dstInfo, needsColorXform)) { |
555 return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConvers
ion); | 551 return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConvers
ion); |
556 } | 552 } |
557 | 553 |
| 554 if (!this->initializeColorXform(dstInfo, needsColorXform)) { |
| 555 return fDecoderMgr->returnFailure("initializeColorXform", kInvalidParame
ters); |
| 556 } |
| 557 |
558 if (!jpeg_start_decompress(dinfo)) { | 558 if (!jpeg_start_decompress(dinfo)) { |
559 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); | 559 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); |
560 } | 560 } |
561 | 561 |
562 // The recommended output buffer height should always be 1 in high quality m
odes. | 562 // The recommended output buffer height should always be 1 in high quality m
odes. |
563 // If it's not, we want to know because it means our strategy is not optimal
. | 563 // If it's not, we want to know because it means our strategy is not optimal
. |
564 SkASSERT(1 == dinfo->rec_outbuf_height); | 564 SkASSERT(1 == dinfo->rec_outbuf_height); |
565 | 565 |
566 J_COLOR_SPACE colorSpace = dinfo->out_color_space; | 566 J_COLOR_SPACE colorSpace = dinfo->out_color_space; |
567 if (JCS_CMYK == colorSpace) { | 567 if (JCS_CMYK == colorSpace) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
623 // Also, verify that fSwizzlerSubset is initialized and valid. | 623 // Also, verify that fSwizzlerSubset is initialized and valid. |
624 SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fS
ubset->x() && | 624 SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fS
ubset->x() && |
625 fSwizzlerSubset.width() == options.fSubset->width()); | 625 fSwizzlerSubset.width() == options.fSubset->width()); |
626 swizzlerOptions.fSubset = &fSwizzlerSubset; | 626 swizzlerOptions.fSubset = &fSwizzlerSubset; |
627 } | 627 } |
628 fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, nullptr, dstInfo, s
wizzlerOptions, | 628 fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, nullptr, dstInfo, s
wizzlerOptions, |
629 nullptr, preSwizzled)); | 629 nullptr, preSwizzled)); |
630 SkASSERT(fSwizzler); | 630 SkASSERT(fSwizzler); |
631 } | 631 } |
632 | 632 |
633 void SkJpegCodec::initializeColorXform(const SkImageInfo& dstInfo) { | 633 bool SkJpegCodec::initializeColorXform(const SkImageInfo& dstInfo, bool needsCol
orXform) { |
634 if (needs_color_xform(dstInfo, this->getInfo())) { | 634 if (needsColorXform) { |
635 fColorXform = SkColorSpaceXform::New(sk_ref_sp(this->getInfo().colorSpac
e()), | 635 fColorXform = SkColorSpaceXform::New(sk_ref_sp(this->getInfo().colorSpac
e()), |
636 sk_ref_sp(dstInfo.colorSpace())); | 636 sk_ref_sp(dstInfo.colorSpace())); |
637 SkASSERT(fColorXform); | 637 if (!fColorXform && kRGBA_F16_SkColorType == dstInfo.colorType()) { |
| 638 return false; |
| 639 } |
638 } | 640 } |
| 641 |
| 642 return true; |
639 } | 643 } |
640 | 644 |
641 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { | 645 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { |
642 if (!createIfNecessary || fSwizzler) { | 646 if (!createIfNecessary || fSwizzler) { |
643 SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcR
ow)); | 647 SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcR
ow)); |
644 return fSwizzler; | 648 return fSwizzler; |
645 } | 649 } |
646 | 650 |
647 this->initializeSwizzler(this->dstInfo(), this->options()); | 651 this->initializeSwizzler(this->dstInfo(), this->options()); |
648 this->allocateStorage(this->dstInfo()); | 652 this->allocateStorage(this->dstInfo()); |
649 return fSwizzler; | 653 return fSwizzler; |
650 } | 654 } |
651 | 655 |
652 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, | 656 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
653 const Options& options, SkPMColor ctable[], int* ctableCount) { | 657 const Options& options, SkPMColor ctable[], int* ctableCount) { |
654 // Set the jump location for libjpeg errors | 658 // Set the jump location for libjpeg errors |
655 if (setjmp(fDecoderMgr->getJmpBuf())) { | 659 if (setjmp(fDecoderMgr->getJmpBuf())) { |
656 SkCodecPrintf("setjmp: Error from libjpeg\n"); | 660 SkCodecPrintf("setjmp: Error from libjpeg\n"); |
657 return kInvalidInput; | 661 return kInvalidInput; |
658 } | 662 } |
659 | 663 |
660 this->initializeColorXform(dstInfo); | 664 // Check if we can decode to the requested destination and set the output co
lor space |
| 665 bool needsColorXform = needs_color_xform(dstInfo, this->getInfo()); |
| 666 if (!this->setOutputColorSpace(dstInfo, needsColorXform)) { |
| 667 return kInvalidConversion; |
| 668 } |
661 | 669 |
662 // Check if we can decode to the requested destination and set the output co
lor space | 670 if (!this->initializeColorXform(dstInfo, needsColorXform)) { |
663 if (!this->setOutputColorSpace(dstInfo)) { | 671 return fDecoderMgr->returnFailure("initializeColorXform", kInvalidParame
ters); |
664 return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConvers
ion); | |
665 } | 672 } |
666 | 673 |
667 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { | 674 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { |
668 SkCodecPrintf("start decompress failed\n"); | 675 SkCodecPrintf("start decompress failed\n"); |
669 return kInvalidInput; | 676 return kInvalidInput; |
670 } | 677 } |
671 | 678 |
672 if (options.fSubset) { | 679 if (options.fSubset) { |
673 uint32_t startX = options.fSubset->x(); | 680 uint32_t startX = options.fSubset->x(); |
674 uint32_t width = options.fSubset->width(); | 681 uint32_t width = options.fSubset->width(); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
925 | 932 |
926 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); | 933 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); |
927 if (linesRead < remainingRows) { | 934 if (linesRead < remainingRows) { |
928 // FIXME: Handle incomplete YUV decodes without signalling an error. | 935 // FIXME: Handle incomplete YUV decodes without signalling an error. |
929 return kInvalidInput; | 936 return kInvalidInput; |
930 } | 937 } |
931 } | 938 } |
932 | 939 |
933 return kSuccess; | 940 return kSuccess; |
934 } | 941 } |
OLD | NEW |