Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: src/codec/SkJpegCodec.cpp

Issue 2319293003: Checking for valid colorType, alphaType, colorSpace in SkCodec (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 bool needsColorXform = needs_color_xform(dstInfo, this->getInfo());
552 if (needsColorXform) {
553 if (!this->initializeColorXform(dstInfo)) {
554 return fDecoderMgr->returnFailure("initializeColorXform", kInvalidPa rameters);
555 }
556 }
557
548 // Check if we can decode to the requested destination and set the output co lor space 558 // 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()); 559 if (!this->setOutputColorSpace(dstInfo)) {
550 if (!this->setOutputColorSpace(dstInfo, needsColorXform)) {
551 return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConvers ion); 560 return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConvers ion);
552 } 561 }
553 562
554 if (!this->initializeColorXform(dstInfo, needsColorXform)) {
555 return fDecoderMgr->returnFailure("initializeColorXform", kInvalidParame ters);
556 }
557
558 if (!jpeg_start_decompress(dinfo)) { 563 if (!jpeg_start_decompress(dinfo)) {
559 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); 564 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
560 } 565 }
561 566
562 // The recommended output buffer height should always be 1 in high quality m odes. 567 // 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 . 568 // If it's not, we want to know because it means our strategy is not optimal .
564 SkASSERT(1 == dinfo->rec_outbuf_height); 569 SkASSERT(1 == dinfo->rec_outbuf_height);
565 570
566 J_COLOR_SPACE colorSpace = dinfo->out_color_space; 571 J_COLOR_SPACE colorSpace = dinfo->out_color_space;
567 if (JCS_CMYK == colorSpace) { 572 if (JCS_CMYK == colorSpace) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 // Also, verify that fSwizzlerSubset is initialized and valid. 628 // Also, verify that fSwizzlerSubset is initialized and valid.
624 SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fS ubset->x() && 629 SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fS ubset->x() &&
625 fSwizzlerSubset.width() == options.fSubset->width()); 630 fSwizzlerSubset.width() == options.fSubset->width());
626 swizzlerOptions.fSubset = &fSwizzlerSubset; 631 swizzlerOptions.fSubset = &fSwizzlerSubset;
627 } 632 }
628 fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, nullptr, dstInfo, s wizzlerOptions, 633 fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, nullptr, dstInfo, s wizzlerOptions,
629 nullptr, preSwizzled)); 634 nullptr, preSwizzled));
630 SkASSERT(fSwizzler); 635 SkASSERT(fSwizzler);
631 } 636 }
632 637
633 bool SkJpegCodec::initializeColorXform(const SkImageInfo& dstInfo, bool needsCol orXform) { 638 bool SkJpegCodec::initializeColorXform(const SkImageInfo& dstInfo) {
634 if (needsColorXform) { 639 fColorXform = SkColorSpaceXform::New(sk_ref_sp(this->getInfo().colorSpace()) ,
635 fColorXform = SkColorSpaceXform::New(sk_ref_sp(this->getInfo().colorSpac e()), 640 sk_ref_sp(dstInfo.colorSpace()));
636 sk_ref_sp(dstInfo.colorSpace())); 641 // TODO: Can we assert this instead?
msarett 2016/09/08 16:01:58 This can be an assert when Brian's CL lands: https
637 if (!fColorXform && kRGBA_F16_SkColorType == dstInfo.colorType()) { 642 if (!fColorXform) {
638 return false; 643 return false;
639 }
640 } 644 }
641 645
642 return true; 646 return true;
643 } 647 }
644 648
645 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { 649 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) {
646 if (!createIfNecessary || fSwizzler) { 650 if (!createIfNecessary || fSwizzler) {
647 SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcR ow)); 651 SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcR ow));
648 return fSwizzler; 652 return fSwizzler;
649 } 653 }
650 654
651 this->initializeSwizzler(this->dstInfo(), this->options()); 655 this->initializeSwizzler(this->dstInfo(), this->options());
652 this->allocateStorage(this->dstInfo()); 656 this->allocateStorage(this->dstInfo());
653 return fSwizzler; 657 return fSwizzler;
654 } 658 }
655 659
656 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, 660 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
657 const Options& options, SkPMColor ctable[], int* ctableCount) { 661 const Options& options, SkPMColor ctable[], int* ctableCount) {
658 // Set the jump location for libjpeg errors 662 // Set the jump location for libjpeg errors
659 if (setjmp(fDecoderMgr->getJmpBuf())) { 663 if (setjmp(fDecoderMgr->getJmpBuf())) {
660 SkCodecPrintf("setjmp: Error from libjpeg\n"); 664 SkCodecPrintf("setjmp: Error from libjpeg\n");
661 return kInvalidInput; 665 return kInvalidInput;
662 } 666 }
663 667
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()); 668 bool needsColorXform = needs_color_xform(dstInfo, this->getInfo());
scroggo 2016/09/08 16:33:11 Both callers check this first and otherwise don't
msarett 2016/09/08 16:49:45 Yes, sgtm.
666 if (!this->setOutputColorSpace(dstInfo, needsColorXform)) { 669 if (needsColorXform) {
667 return kInvalidConversion; 670 if (!this->initializeColorXform(dstInfo)) {
671 return fDecoderMgr->returnFailure("initializeColorXform", kInvalidPa rameters);
672 }
668 } 673 }
669 674
670 if (!this->initializeColorXform(dstInfo, needsColorXform)) { 675 // Check if we can decode to the requested destination and set the output co lor space
671 return fDecoderMgr->returnFailure("initializeColorXform", kInvalidParame ters); 676 if (!this->setOutputColorSpace(dstInfo)) {
677 return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConvers ion);
672 } 678 }
673 679
674 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { 680 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) {
675 SkCodecPrintf("start decompress failed\n"); 681 SkCodecPrintf("start decompress failed\n");
676 return kInvalidInput; 682 return kInvalidInput;
677 } 683 }
678 684
679 if (options.fSubset) { 685 if (options.fSubset) {
680 uint32_t startX = options.fSubset->x(); 686 uint32_t startX = options.fSubset->x();
681 uint32_t width = options.fSubset->width(); 687 uint32_t width = options.fSubset->width();
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
932 938
933 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); 939 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
934 if (linesRead < remainingRows) { 940 if (linesRead < remainingRows) {
935 // FIXME: Handle incomplete YUV decodes without signalling an error. 941 // FIXME: Handle incomplete YUV decodes without signalling an error.
936 return kInvalidInput; 942 return kInvalidInput;
937 } 943 }
938 } 944 }
939 945
940 return kSuccess; 946 return kSuccess;
941 } 947 }
OLDNEW
« src/codec/SkCodecPriv.h ('K') | « src/codec/SkJpegCodec.h ('k') | src/codec/SkPngCodec.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698