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, bool needsColo
rXform) { | 360 bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dstInfo) { |
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 (needsColorXform) { | 387 } else if (fColorXform) { |
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 (needsColorXform) { | 396 if (fColorXform) { |
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 (needsColorXform || JCS_GRAYSCALE != encodedColorType) { | 408 if (fColorXform || 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(needsColorXform); | 415 SkASSERT(fColorXform); |
| 416 if (!dstInfo.colorSpace()->gammaIsLinear()) { |
| 417 return false; |
| 418 } |
416 | 419 |
417 if (isCMYK) { | 420 if (isCMYK) { |
418 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; | 421 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
419 } else { | 422 } else { |
420 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; | 423 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; |
421 } | 424 } |
422 return true; | 425 return true; |
423 default: | 426 default: |
424 return false; | 427 return false; |
425 } | 428 } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 } | 541 } |
539 | 542 |
540 // Get a pointer to the decompress info since we will use it quite frequentl
y | 543 // Get a pointer to the decompress info since we will use it quite frequentl
y |
541 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); | 544 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); |
542 | 545 |
543 // Set the jump location for libjpeg errors | 546 // Set the jump location for libjpeg errors |
544 if (setjmp(fDecoderMgr->getJmpBuf())) { | 547 if (setjmp(fDecoderMgr->getJmpBuf())) { |
545 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 548 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
546 } | 549 } |
547 | 550 |
| 551 this->initializeColorXform(dstInfo); |
| 552 |
548 // Check if we can decode to the requested destination and set the output co
lor space | 553 // Check if we can decode to the requested destination and set the output co
lor space |
549 bool needsColorXform = needs_color_xform(dstInfo, this->getInfo()); | 554 if (!this->setOutputColorSpace(dstInfo)) { |
550 if (!this->setOutputColorSpace(dstInfo, needsColorXform)) { | |
551 return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConvers
ion); | 555 return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConvers
ion); |
552 } | 556 } |
553 | 557 |
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 bool SkJpegCodec::initializeColorXform(const SkImageInfo& dstInfo, bool needsCol
orXform) { | 633 void SkJpegCodec::initializeColorXform(const SkImageInfo& dstInfo) { |
634 if (needsColorXform) { | 634 if (needs_color_xform(dstInfo, this->getInfo())) { |
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 if (!fColorXform && kRGBA_F16_SkColorType == dstInfo.colorType()) { | 637 SkASSERT(fColorXform); |
638 return false; | |
639 } | |
640 } | 638 } |
641 | |
642 return true; | |
643 } | 639 } |
644 | 640 |
645 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { | 641 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { |
646 if (!createIfNecessary || fSwizzler) { | 642 if (!createIfNecessary || fSwizzler) { |
647 SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcR
ow)); | 643 SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcR
ow)); |
648 return fSwizzler; | 644 return fSwizzler; |
649 } | 645 } |
650 | 646 |
651 this->initializeSwizzler(this->dstInfo(), this->options()); | 647 this->initializeSwizzler(this->dstInfo(), this->options()); |
652 this->allocateStorage(this->dstInfo()); | 648 this->allocateStorage(this->dstInfo()); |
653 return fSwizzler; | 649 return fSwizzler; |
654 } | 650 } |
655 | 651 |
656 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, | 652 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
657 const Options& options, SkPMColor ctable[], int* ctableCount) { | 653 const Options& options, SkPMColor ctable[], int* ctableCount) { |
658 // Set the jump location for libjpeg errors | 654 // Set the jump location for libjpeg errors |
659 if (setjmp(fDecoderMgr->getJmpBuf())) { | 655 if (setjmp(fDecoderMgr->getJmpBuf())) { |
660 SkCodecPrintf("setjmp: Error from libjpeg\n"); | 656 SkCodecPrintf("setjmp: Error from libjpeg\n"); |
661 return kInvalidInput; | 657 return kInvalidInput; |
662 } | 658 } |
663 | 659 |
| 660 this->initializeColorXform(dstInfo); |
| 661 |
664 // Check if we can decode to the requested destination and set the output co
lor space | 662 // 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()); | 663 if (!this->setOutputColorSpace(dstInfo)) { |
666 if (!this->setOutputColorSpace(dstInfo, needsColorXform)) { | 664 return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConvers
ion); |
667 return kInvalidConversion; | |
668 } | |
669 | |
670 if (!this->initializeColorXform(dstInfo, needsColorXform)) { | |
671 return fDecoderMgr->returnFailure("initializeColorXform", kInvalidParame
ters); | |
672 } | 665 } |
673 | 666 |
674 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { | 667 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { |
675 SkCodecPrintf("start decompress failed\n"); | 668 SkCodecPrintf("start decompress failed\n"); |
676 return kInvalidInput; | 669 return kInvalidInput; |
677 } | 670 } |
678 | 671 |
679 if (options.fSubset) { | 672 if (options.fSubset) { |
680 uint32_t startX = options.fSubset->x(); | 673 uint32_t startX = options.fSubset->x(); |
681 uint32_t width = options.fSubset->width(); | 674 uint32_t width = options.fSubset->width(); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
932 | 925 |
933 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); | 926 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); |
934 if (linesRead < remainingRows) { | 927 if (linesRead < remainingRows) { |
935 // FIXME: Handle incomplete YUV decodes without signalling an error. | 928 // FIXME: Handle incomplete YUV decodes without signalling an error. |
936 return kInvalidInput; | 929 return kInvalidInput; |
937 } | 930 } |
938 } | 931 } |
939 | 932 |
940 return kSuccess; | 933 return kSuccess; |
941 } | 934 } |
OLD | NEW |