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 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 } | 256 } |
257 return nullptr; | 257 return nullptr; |
258 } | 258 } |
259 | 259 |
260 SkJpegCodec::SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStr eam* stream, | 260 SkJpegCodec::SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStr eam* stream, |
261 JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origi n, | 261 JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origi n, |
262 sk_sp<SkData> iccData) | 262 sk_sp<SkData> iccData) |
263 : INHERITED(width, height, info, stream, std::move(colorSpace), origin) | 263 : INHERITED(width, height, info, stream, std::move(colorSpace), origin) |
264 , fDecoderMgr(decoderMgr) | 264 , fDecoderMgr(decoderMgr) |
265 , fReadyState(decoderMgr->dinfo()->global_state) | 265 , fReadyState(decoderMgr->dinfo()->global_state) |
266 , fSrcRow(nullptr) | 266 , fSwizzleSrcRow(nullptr) |
267 , fColorXformSrcRow(nullptr) | |
267 , fSwizzlerSubset(SkIRect::MakeEmpty()) | 268 , fSwizzlerSubset(SkIRect::MakeEmpty()) |
268 , fICCData(std::move(iccData)) | 269 , fICCData(std::move(iccData)) |
269 {} | 270 {} |
270 | 271 |
271 /* | 272 /* |
272 * Return the row bytes of a particular image type and width | 273 * Return the row bytes of a particular image type and width |
273 */ | 274 */ |
274 static size_t get_row_bytes(const j_decompress_ptr dinfo) { | 275 static size_t get_row_bytes(const j_decompress_ptr dinfo) { |
275 #ifdef TURBO_HAS_565 | 276 #ifdef TURBO_HAS_565 |
276 const size_t colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 : | 277 const size_t colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 : |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 | 336 |
336 bool SkJpegCodec::onRewind() { | 337 bool SkJpegCodec::onRewind() { |
337 JpegDecoderMgr* decoderMgr = nullptr; | 338 JpegDecoderMgr* decoderMgr = nullptr; |
338 if (!ReadHeader(this->stream(), nullptr, &decoderMgr)) { | 339 if (!ReadHeader(this->stream(), nullptr, &decoderMgr)) { |
339 return fDecoderMgr->returnFalse("could not rewind"); | 340 return fDecoderMgr->returnFalse("could not rewind"); |
340 } | 341 } |
341 SkASSERT(nullptr != decoderMgr); | 342 SkASSERT(nullptr != decoderMgr); |
342 fDecoderMgr.reset(decoderMgr); | 343 fDecoderMgr.reset(decoderMgr); |
343 | 344 |
344 fSwizzler.reset(nullptr); | 345 fSwizzler.reset(nullptr); |
345 fSrcRow = nullptr; | 346 fSwizzleSrcRow = nullptr; |
347 fColorXformSrcRow = nullptr; | |
346 fStorage.reset(); | 348 fStorage.reset(); |
349 fColorXform.reset(nullptr); | |
347 | 350 |
348 return true; | 351 return true; |
349 } | 352 } |
350 | 353 |
351 /* | 354 /* |
352 * Checks if the conversion between the input image and the requested output | 355 * Checks if the conversion between the input image and the requested output |
353 * image has been implemented | 356 * image has been implemented |
354 * Sets the output color space | 357 * Sets the output color space |
355 */ | 358 */ |
356 bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dst) { | 359 bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dstInfo, bool needsColo rXform) { |
357 if (kUnknown_SkAlphaType == dst.alphaType()) { | 360 if (kUnknown_SkAlphaType == dstInfo.alphaType()) { |
358 return false; | 361 return false; |
359 } | 362 } |
360 | 363 |
361 if (kOpaque_SkAlphaType != dst.alphaType()) { | 364 if (kOpaque_SkAlphaType != dstInfo.alphaType()) { |
362 SkCodecPrintf("Warning: an opaque image should be decoded as opaque " | 365 SkCodecPrintf("Warning: an opaque image should be decoded as opaque " |
363 "- it is being decoded as non-opaque, which will draw slow er\n"); | 366 "- it is being decoded as non-opaque, which will draw slow er\n"); |
364 } | 367 } |
365 | 368 |
366 // Check if we will decode to CMYK because a conversion to RGBA is not suppo rted | 369 // Check if we will decode to CMYK. libjpeg-turbo does not convert CMYK to RGBA, so |
367 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->jpeg_color_space; | 370 // we must do it ourselves. |
368 bool isCMYK = JCS_CMYK == colorSpace || JCS_YCCK == colorSpace; | 371 J_COLOR_SPACE encodedColorType = fDecoderMgr->dinfo()->jpeg_color_space; |
372 bool isCMYK = (JCS_CMYK == encodedColorType || JCS_YCCK == encodedColorType) ; | |
369 | 373 |
370 // Check for valid color types and set the output color space | 374 // Check for valid color types and set the output color space |
371 switch (dst.colorType()) { | 375 switch (dstInfo.colorType()) { |
372 case kRGBA_8888_SkColorType: | 376 case kRGBA_8888_SkColorType: |
373 if (isCMYK) { | 377 if (isCMYK) { |
374 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; | 378 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
375 } else { | 379 } else { |
376 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; | 380 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; |
377 } | 381 } |
378 return true; | 382 return true; |
379 case kBGRA_8888_SkColorType: | 383 case kBGRA_8888_SkColorType: |
380 if (isCMYK) { | 384 if (isCMYK) { |
381 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; | 385 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
386 } else if (needsColorXform) { | |
387 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; | |
mtklein
2016/07/27 13:51:06
// Our color transformation code requires RGBA ord
msarett
2016/07/27 15:13:50
Done.
| |
382 } else { | 388 } else { |
383 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA; | 389 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA; |
384 } | 390 } |
385 return true; | 391 return true; |
386 case kRGB_565_SkColorType: | 392 case kRGB_565_SkColorType: |
393 if (needsColorXform) { | |
394 return false; | |
395 } | |
396 | |
387 if (isCMYK) { | 397 if (isCMYK) { |
388 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; | 398 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
389 } else { | 399 } else { |
390 #ifdef TURBO_HAS_565 | 400 #ifdef TURBO_HAS_565 |
391 fDecoderMgr->dinfo()->dither_mode = JDITHER_NONE; | 401 fDecoderMgr->dinfo()->dither_mode = JDITHER_NONE; |
392 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565; | 402 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565; |
393 #else | 403 #else |
394 fDecoderMgr->dinfo()->out_color_space = JCS_RGB; | 404 fDecoderMgr->dinfo()->out_color_space = JCS_RGB; |
395 #endif | 405 #endif |
396 } | 406 } |
397 return true; | 407 return true; |
398 case kGray_8_SkColorType: | 408 case kGray_8_SkColorType: |
409 if (needsColorXform || JCS_GRAYSCALE != encodedColorType) { | |
410 return false; | |
411 } | |
412 | |
413 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE; | |
414 return true; | |
415 case kRGBA_F16_SkColorType: | |
416 SkASSERT(needsColorXform); | |
417 if (!dstInfo.colorSpace()) { | |
418 // Disable F16 in legacy mode. | |
mtklein
2016/07/27 13:51:06
Yeah, the new standard line is
// F16 makes no se
msarett
2016/07/27 15:13:50
Deleting this whole block. It's unnecessary becau
| |
419 // TODO (msarett): | |
420 // Alternatively, should this mean "xform to half floats, but do n't xform | |
421 // the gamut"? | |
422 return false; | |
423 } | |
424 | |
399 if (isCMYK) { | 425 if (isCMYK) { |
400 return false; | 426 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
401 } else { | 427 } else { |
402 // We will enable decodes to gray even if the image is color bec ause this is | 428 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; |
403 // much faster than decoding to color and then converting | |
404 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE; | |
405 } | 429 } |
406 return true; | 430 return true; |
407 default: | 431 default: |
408 return false; | 432 return false; |
409 } | 433 } |
410 } | 434 } |
411 | 435 |
412 /* | 436 /* |
413 * Checks if we can natively scale to the requested dimensions and natively scal es the | 437 * Checks if we can natively scale to the requested dimensions and natively scal es the |
414 * dimensions if possible | 438 * dimensions if possible |
(...skipping 28 matching lines...) Expand all Loading... | |
443 // Try the next scale | 467 // Try the next scale |
444 num -= 1; | 468 num -= 1; |
445 calc_output_dimensions(&dinfo, num, denom); | 469 calc_output_dimensions(&dinfo, num, denom); |
446 } | 470 } |
447 | 471 |
448 fDecoderMgr->dinfo()->scale_num = num; | 472 fDecoderMgr->dinfo()->scale_num = num; |
449 fDecoderMgr->dinfo()->scale_denom = denom; | 473 fDecoderMgr->dinfo()->scale_denom = denom; |
450 return true; | 474 return true; |
451 } | 475 } |
452 | 476 |
477 static bool needs_color_xform(const SkImageInfo& dstInfo, const SkImageInfo& src Info) { | |
478 // FIXME (msarett): | |
479 // Do a better check for color space equality. | |
480 return (kRGBA_F16_SkColorType == dstInfo.colorType()) || | |
mtklein
2016/07/27 13:51:06
?
Is this, we know we need to end up in F16 by th
msarett
2016/07/27 15:13:50
The transform will never be an identity transform
| |
481 (dstInfo.colorSpace() && (dstInfo.colorSpace() != srcInfo.colorSpace( ))); | |
482 } | |
483 | |
484 int SkJpegCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes , int count) { | |
485 int width = dstInfo.width(); | |
486 | |
487 JSAMPLE* decodeDst = (JSAMPLE*) dst; | |
mtklein
2016/07/27 13:51:05
Some comments about what aliases what, when, and w
msarett
2016/07/27 15:13:50
SGTM
| |
488 uint32_t* swizzleDst = (uint32_t*) dst; | |
489 size_t decodeDstRowBytes = rowBytes; | |
490 size_t swizzleDstRowBytes = rowBytes; | |
491 if (fSwizzleSrcRow && fColorXformSrcRow) { | |
492 decodeDst = (JSAMPLE*) fSwizzleSrcRow; | |
493 swizzleDst = fColorXformSrcRow; | |
494 decodeDstRowBytes = 0; | |
495 swizzleDstRowBytes = 0; | |
496 } else if (fColorXformSrcRow) { | |
497 decodeDst = (JSAMPLE*) fColorXformSrcRow; | |
498 swizzleDst = fColorXformSrcRow; | |
499 decodeDstRowBytes = 0; | |
500 swizzleDstRowBytes = 0; | |
501 } else if (fSwizzleSrcRow) { | |
502 decodeDst = (JSAMPLE*) fSwizzleSrcRow; | |
503 decodeDstRowBytes = 0; | |
504 } | |
505 | |
506 for (int y = 0; y < count; y++) { | |
507 uint32_t lines = jpeg_read_scanlines(fDecoderMgr->dinfo(), &decodeDst, 1 ); | |
508 sk_msan_mark_initialized(decodeDst, decodeDst + rowBytes, "skbug.com/455 0"); | |
509 if (lines != 1) { | |
mtklein
2016/07/27 13:51:06
// i.e. == 0 ?
msarett
2016/07/27 15:13:50
Done.
| |
510 return y; | |
511 } | |
512 | |
513 if (fSwizzler) { | |
514 fSwizzler->swizzle(swizzleDst, decodeDst); | |
mtklein
2016/07/27 13:51:05
... oh hey, this would be a good pipeline stage to
msarett
2016/07/27 15:13:50
The swizzler performs sampling and subsetting. It
| |
515 } | |
516 | |
517 if (fColorXform) { | |
518 switch (dstInfo.colorType()) { | |
519 case kRGBA_8888_SkColorType: | |
520 fColorXform->applyToRGBA((uint32_t*) dst, swizzleDst, width) ; | |
521 break; | |
522 case kBGRA_8888_SkColorType: | |
523 fColorXform->applyToBGRA((uint32_t*) dst, swizzleDst, width) ; | |
524 break; | |
525 case kRGBA_F16_SkColorType: | |
526 fColorXform->applyToF16((uint64_t*) dst, swizzleDst, width); | |
527 break; | |
528 default: | |
529 SkASSERT(false); | |
530 break; | |
531 } | |
532 | |
533 dst = SkTAddOffset<void>(dst, rowBytes); | |
534 } | |
535 | |
536 decodeDst = SkTAddOffset<JSAMPLE>(decodeDst, decodeDstRowBytes); | |
537 swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes); | |
538 } | |
539 | |
540 return count; | |
541 } | |
542 | |
453 /* | 543 /* |
454 * Performs the jpeg decode | 544 * Performs the jpeg decode |
455 */ | 545 */ |
456 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, | 546 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, |
457 void* dst, size_t dstRowBytes, | 547 void* dst, size_t dstRowBytes, |
458 const Options& options, SkPMColor*, int *, | 548 const Options& options, SkPMColor*, int *, |
459 int* rowsDecoded) { | 549 int* rowsDecoded) { |
460 if (options.fSubset) { | 550 if (options.fSubset) { |
461 // Subsets are not supported. | 551 // Subsets are not supported. |
462 return kUnimplemented; | 552 return kUnimplemented; |
463 } | 553 } |
464 | 554 |
465 // Get a pointer to the decompress info since we will use it quite frequentl y | 555 // Get a pointer to the decompress info since we will use it quite frequentl y |
466 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); | 556 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); |
467 | 557 |
468 // Set the jump location for libjpeg errors | 558 // Set the jump location for libjpeg errors |
469 if (setjmp(fDecoderMgr->getJmpBuf())) { | 559 if (setjmp(fDecoderMgr->getJmpBuf())) { |
470 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 560 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
471 } | 561 } |
472 | 562 |
473 // Check if we can decode to the requested destination and set the output co lor space | 563 // Check if we can decode to the requested destination and set the output co lor space |
474 if (!this->setOutputColorSpace(dstInfo)) { | 564 bool needsColorXform = needs_color_xform(dstInfo, this->getInfo()); |
475 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers ion); | 565 if (!this->setOutputColorSpace(dstInfo, needsColorXform)) { |
566 return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConvers ion); | |
476 } | 567 } |
477 | 568 |
478 // Now, given valid output dimensions, we can start the decompress | 569 if (needsColorXform) { |
570 fColorXform = SkColorSpaceXform::New(std::move(sk_ref_sp(this->getInfo() .colorSpace())), | |
571 std::move(sk_ref_sp(dstInfo.colorSp ace()))); | |
mtklein
2016/07/27 13:51:06
Aren't these moves redundant?
msarett
2016/07/27 15:13:50
Because we ref them as parameters? Sure, seems li
| |
572 if (!fColorXform && kRGBA_F16_SkColorType == dstInfo.colorType()) { | |
573 return kInvalidInput; | |
574 } | |
575 } | |
576 | |
479 if (!jpeg_start_decompress(dinfo)) { | 577 if (!jpeg_start_decompress(dinfo)) { |
480 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); | 578 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); |
481 } | 579 } |
482 | 580 |
483 // The recommended output buffer height should always be 1 in high quality m odes. | 581 // The recommended output buffer height should always be 1 in high quality m odes. |
484 // If it's not, we want to know because it means our strategy is not optimal . | 582 // If it's not, we want to know because it means our strategy is not optimal . |
485 SkASSERT(1 == dinfo->rec_outbuf_height); | 583 SkASSERT(1 == dinfo->rec_outbuf_height); |
486 | 584 |
487 J_COLOR_SPACE colorSpace = dinfo->out_color_space; | 585 J_COLOR_SPACE colorSpace = dinfo->out_color_space; |
488 if (JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { | 586 if (JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { |
489 this->initializeSwizzler(dstInfo, options); | 587 this->initializeSwizzler(dstInfo, options); |
490 } | 588 } |
491 | 589 |
492 // Perform the decode a single row at a time | 590 this->allocateStorage(dstInfo); |
493 uint32_t dstHeight = dstInfo.height(); | |
494 | 591 |
495 JSAMPLE* dstRow; | 592 int rows = this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height()); |
496 if (fSwizzler) { | 593 if (rows < dstInfo.height()) { |
497 // write data to storage row, then sample using swizzler | 594 *rowsDecoded = rows; |
498 dstRow = fSrcRow; | 595 return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteIn put); |
499 } else { | |
500 // write data directly to dst | |
501 dstRow = (JSAMPLE*) dst; | |
502 } | |
503 | |
504 for (uint32_t y = 0; y < dstHeight; y++) { | |
505 // Read rows of the image | |
506 uint32_t lines = jpeg_read_scanlines(dinfo, &dstRow, 1); | |
507 sk_msan_mark_initialized(dstRow, dstRow + dstRowBytes, "skbug.com/4550") ; | |
508 | |
509 // If we cannot read enough rows, assume the input is incomplete | |
510 if (lines != 1) { | |
511 *rowsDecoded = y; | |
512 return fDecoderMgr->returnFailure("Incomplete image data", kIncomple teInput); | |
513 } | |
514 | |
515 if (fSwizzler) { | |
516 // use swizzler to sample row | |
517 fSwizzler->swizzle(dst, dstRow); | |
518 dst = SkTAddOffset<JSAMPLE>(dst, dstRowBytes); | |
519 } else { | |
520 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes); | |
521 } | |
522 } | 596 } |
523 | 597 |
524 return kSuccess; | 598 return kSuccess; |
525 } | 599 } |
526 | 600 |
601 void SkJpegCodec::allocateStorage(const SkImageInfo& dstInfo) { | |
602 size_t swizzleBytes = 0; | |
603 if (fSwizzler) { | |
604 swizzleBytes = get_row_bytes(fDecoderMgr->dinfo()); | |
605 } | |
606 | |
607 size_t xformBytes = 0; | |
608 if (kRGBA_F16_SkColorType == dstInfo.colorType()) { | |
609 SkASSERT(fColorXform); | |
610 xformBytes = dstInfo.width() * sizeof(SkColorSpaceXform::RGBA32); | |
611 } | |
612 | |
613 size_t totalBytes = swizzleBytes + xformBytes; | |
614 if (totalBytes > 0) { | |
615 fStorage.reset(totalBytes); | |
616 fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr; | |
617 fColorXformSrcRow = (xformBytes > 0) ? | |
618 SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr; | |
619 } | |
620 } | |
621 | |
527 void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& options) { | 622 void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& options) { |
528 // libjpeg-turbo may have already performed color conversion. We must indic ate the | 623 // libjpeg-turbo may have already performed color conversion. We must indic ate the |
529 // appropriate format to the swizzler. | 624 // appropriate format to the swizzler. |
530 SkEncodedInfo swizzlerInfo = this->getEncodedInfo(); | 625 SkEncodedInfo swizzlerInfo = this->getEncodedInfo(); |
531 bool preSwizzled = true; | 626 bool preSwizzled = true; |
532 switch (fDecoderMgr->dinfo()->out_color_space) { | 627 switch (fDecoderMgr->dinfo()->out_color_space) { |
533 case JCS_RGB: | 628 case JCS_RGB: |
534 preSwizzled = false; | 629 preSwizzled = false; |
535 swizzlerInfo = SkEncodedInfo::Make(SkEncodedInfo::kRGB_Color, | 630 swizzlerInfo = SkEncodedInfo::Make(SkEncodedInfo::kRGB_Color, |
536 swizzlerInfo.alpha(), | 631 swizzlerInfo.alpha(), |
537 swizzlerInfo.bitsPerComponent()); | 632 swizzlerInfo.bitsPerComponent()); |
538 break; | 633 break; |
539 case JCS_CMYK: | 634 case JCS_CMYK: |
540 preSwizzled = false; | 635 preSwizzled = false; |
541 swizzlerInfo = SkEncodedInfo::Make( | 636 swizzlerInfo = SkEncodedInfo::Make(SkEncodedInfo::kInvertedCMYK_Colo r, |
542 SkEncodedInfo::kInvertedCMYK_Color, swizzlerInfo.alpha(), | 637 swizzlerInfo.alpha(), |
543 swizzlerInfo.bitsPerComponent()); | 638 swizzlerInfo.bitsPerComponent()); |
544 break; | 639 break; |
545 default: | 640 default: |
546 break; | 641 break; |
547 } | 642 } |
548 | 643 |
549 Options swizzlerOptions = options; | 644 Options swizzlerOptions = options; |
550 if (options.fSubset) { | 645 if (options.fSubset) { |
551 // Use fSwizzlerSubset if this is a subset decode. This is necessary in the case | 646 // Use fSwizzlerSubset if this is a subset decode. This is necessary in the case |
552 // where libjpeg-turbo provides a subset and then we need to subset it f urther. | 647 // where libjpeg-turbo provides a subset and then we need to subset it f urther. |
553 // Also, verify that fSwizzlerSubset is initialized and valid. | 648 // Also, verify that fSwizzlerSubset is initialized and valid. |
554 SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fS ubset->x() && | 649 SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fS ubset->x() && |
555 fSwizzlerSubset.width() == options.fSubset->width()); | 650 fSwizzlerSubset.width() == options.fSubset->width()); |
556 swizzlerOptions.fSubset = &fSwizzlerSubset; | 651 swizzlerOptions.fSubset = &fSwizzlerSubset; |
557 } | 652 } |
558 fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, nullptr, dstInfo, s wizzlerOptions, | 653 fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, nullptr, dstInfo, s wizzlerOptions, |
559 nullptr, preSwizzled)); | 654 nullptr, preSwizzled)); |
560 SkASSERT(fSwizzler); | 655 SkASSERT(fSwizzler); |
561 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); | |
562 fSrcRow = fStorage.get(); | |
563 } | 656 } |
564 | 657 |
565 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { | 658 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { |
566 if (!createIfNecessary || fSwizzler) { | 659 if (!createIfNecessary || fSwizzler) { |
567 SkASSERT(!fSwizzler || (fSrcRow && fStorage.get() == fSrcRow)); | 660 SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcR ow)); |
568 return fSwizzler; | 661 return fSwizzler; |
569 } | 662 } |
570 | 663 |
571 this->initializeSwizzler(this->dstInfo(), this->options()); | 664 this->initializeSwizzler(this->dstInfo(), this->options()); |
665 this->allocateStorage(this->dstInfo()); | |
572 return fSwizzler; | 666 return fSwizzler; |
573 } | 667 } |
574 | 668 |
575 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, | 669 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
576 const Options& options, SkPMColor ctable[], int* ctableCount) { | 670 const Options& options, SkPMColor ctable[], int* ctableCount) { |
577 // Set the jump location for libjpeg errors | 671 // Set the jump location for libjpeg errors |
578 if (setjmp(fDecoderMgr->getJmpBuf())) { | 672 if (setjmp(fDecoderMgr->getJmpBuf())) { |
579 SkCodecPrintf("setjmp: Error from libjpeg\n"); | 673 SkCodecPrintf("setjmp: Error from libjpeg\n"); |
580 return kInvalidInput; | 674 return kInvalidInput; |
581 } | 675 } |
582 | 676 |
583 // Check if we can decode to the requested destination and set the output co lor space | 677 // Check if we can decode to the requested destination and set the output co lor space |
584 if (!this->setOutputColorSpace(dstInfo)) { | 678 bool needsColorXform = needs_color_xform(dstInfo, this->getInfo()); |
679 if (!this->setOutputColorSpace(dstInfo, needsColorXform)) { | |
585 return kInvalidConversion; | 680 return kInvalidConversion; |
586 } | 681 } |
587 | 682 |
588 // Now, given valid output dimensions, we can start the decompress | 683 if (needsColorXform) { |
mtklein
2016/07/27 13:51:06
Hmm. I feel like I've read this before. Think th
msarett
2016/07/27 15:13:50
Done.
| |
684 fColorXform = SkColorSpaceXform::New(std::move(sk_ref_sp(this->getInfo() .colorSpace())), | |
685 std::move(sk_ref_sp(dstInfo.colorSp ace()))); | |
686 if (!fColorXform && kRGBA_F16_SkColorType == dstInfo.colorType()) { | |
687 return kInvalidInput; | |
688 } | |
689 } | |
690 | |
589 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { | 691 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { |
590 SkCodecPrintf("start decompress failed\n"); | 692 SkCodecPrintf("start decompress failed\n"); |
591 return kInvalidInput; | 693 return kInvalidInput; |
592 } | 694 } |
593 | 695 |
594 #ifdef TURBO_HAS_CROP | 696 #ifdef TURBO_HAS_CROP |
595 if (options.fSubset) { | 697 if (options.fSubset) { |
596 uint32_t startX = options.fSubset->x(); | 698 uint32_t startX = options.fSubset->x(); |
597 uint32_t width = options.fSubset->width(); | 699 uint32_t width = options.fSubset->width(); |
598 | 700 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
639 } | 741 } |
640 | 742 |
641 // We will need a swizzler if we are performing a subset decode or | 743 // We will need a swizzler if we are performing a subset decode or |
642 // converting from CMYK. | 744 // converting from CMYK. |
643 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->out_color_space; | 745 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->out_color_space; |
644 if (options.fSubset || JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { | 746 if (options.fSubset || JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { |
645 this->initializeSwizzler(dstInfo, options); | 747 this->initializeSwizzler(dstInfo, options); |
646 } | 748 } |
647 #endif | 749 #endif |
648 | 750 |
751 this->allocateStorage(dstInfo); | |
752 | |
649 return kSuccess; | 753 return kSuccess; |
650 } | 754 } |
651 | 755 |
652 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { | 756 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { |
653 // Set the jump location for libjpeg errors | 757 // Set the jump location for libjpeg errors |
654 if (setjmp(fDecoderMgr->getJmpBuf())) { | 758 if (setjmp(fDecoderMgr->getJmpBuf())) { |
655 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 759 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
656 } | 760 } |
657 // Read rows one at a time | 761 |
658 JSAMPLE* dstRow; | 762 int rows = this->readRows(this->dstInfo(), dst, dstRowBytes, count); |
659 size_t srcRowBytes = get_row_bytes(fDecoderMgr->dinfo()); | 763 if (rows < count) { |
660 if (fSwizzler) { | 764 // This allows us to skip calling jpeg_finish_decompress(). |
661 // write data to storage row, then sample using swizzler | 765 fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height(); |
662 dstRow = fSrcRow; | |
663 } else { | |
664 // write data directly to dst | |
665 SkASSERT(count == 1 || dstRowBytes >= srcRowBytes); | |
666 dstRow = (JSAMPLE*) dst; | |
667 } | 766 } |
668 | 767 |
669 for (int y = 0; y < count; y++) { | 768 return rows; |
670 // Read row of the image | |
671 uint32_t rowsDecoded = jpeg_read_scanlines(fDecoderMgr->dinfo(), &dstRow , 1); | |
672 sk_msan_mark_initialized(dstRow, dstRow + srcRowBytes, "skbug.com/4550") ; | |
673 if (rowsDecoded != 1) { | |
674 fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height(); | |
675 return y; | |
676 } | |
677 | |
678 if (fSwizzler) { | |
679 // use swizzler to sample row | |
680 fSwizzler->swizzle(dst, dstRow); | |
681 dst = SkTAddOffset<JSAMPLE>(dst, dstRowBytes); | |
682 } else { | |
683 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes); | |
684 } | |
685 } | |
686 return count; | |
687 } | 769 } |
688 | 770 |
689 bool SkJpegCodec::onSkipScanlines(int count) { | 771 bool SkJpegCodec::onSkipScanlines(int count) { |
690 // Set the jump location for libjpeg errors | 772 // Set the jump location for libjpeg errors |
691 if (setjmp(fDecoderMgr->getJmpBuf())) { | 773 if (setjmp(fDecoderMgr->getJmpBuf())) { |
692 return fDecoderMgr->returnFalse("setjmp"); | 774 return fDecoderMgr->returnFalse("setjmp"); |
693 } | 775 } |
694 | 776 |
695 #ifdef TURBO_HAS_SKIP | 777 #ifdef TURBO_HAS_SKIP |
696 return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count); | 778 return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count); |
697 #else | 779 #else |
698 if (!fSrcRow) { | 780 if (!fSwizzleSrcRow) { |
699 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); | 781 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); |
700 fSrcRow = fStorage.get(); | 782 fSwizzleSrcRow = fStorage.get(); |
701 } | 783 } |
702 | 784 |
703 for (int y = 0; y < count; y++) { | 785 for (int y = 0; y < count; y++) { |
704 if (1 != jpeg_read_scanlines(fDecoderMgr->dinfo(), &fSrcRow, 1)) { | 786 if (1 != jpeg_read_scanlines(fDecoderMgr->dinfo(), &fSwizzleSrcRow, 1)) { |
705 return false; | 787 return false; |
706 } | 788 } |
707 } | 789 } |
708 return true; | 790 return true; |
709 #endif | 791 #endif |
710 } | 792 } |
711 | 793 |
712 static bool is_yuv_supported(jpeg_decompress_struct* dinfo) { | 794 static bool is_yuv_supported(jpeg_decompress_struct* dinfo) { |
713 // Scaling is not supported in raw data mode. | 795 // Scaling is not supported in raw data mode. |
714 SkASSERT(dinfo->scale_num == dinfo->scale_denom); | 796 SkASSERT(dinfo->scale_num == dinfo->scale_denom); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
899 | 981 |
900 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); | 982 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); |
901 if (linesRead < remainingRows) { | 983 if (linesRead < remainingRows) { |
902 // FIXME: Handle incomplete YUV decodes without signalling an error. | 984 // FIXME: Handle incomplete YUV decodes without signalling an error. |
903 return kInvalidInput; | 985 return kInvalidInput; |
904 } | 986 } |
905 } | 987 } |
906 | 988 |
907 return kSuccess; | 989 return kSuccess; |
908 } | 990 } |
OLD | NEW |