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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 return codec; | 73 return codec; |
74 } | 74 } |
75 return nullptr; | 75 return nullptr; |
76 } | 76 } |
77 | 77 |
78 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, | 78 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, |
79 JpegDecoderMgr* decoderMgr) | 79 JpegDecoderMgr* decoderMgr) |
80 : INHERITED(srcInfo, stream) | 80 : INHERITED(srcInfo, stream) |
81 , fDecoderMgr(decoderMgr) | 81 , fDecoderMgr(decoderMgr) |
82 , fReadyState(decoderMgr->dinfo()->global_state) | 82 , fReadyState(decoderMgr->dinfo()->global_state) |
| 83 , fSwizzlerSubset(SkIRect::MakeEmpty()) |
83 {} | 84 {} |
84 | 85 |
85 /* | 86 /* |
86 * Return the row bytes of a particular image type and width | 87 * Return the row bytes of a particular image type and width |
87 */ | 88 */ |
88 static size_t get_row_bytes(const j_decompress_ptr dinfo) { | 89 static size_t get_row_bytes(const j_decompress_ptr dinfo) { |
89 #ifdef TURBO_HAS_565 | 90 #ifdef TURBO_HAS_565 |
90 const size_t colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 : | 91 const size_t colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 : |
91 dinfo->out_color_components; | 92 dinfo->out_color_components; |
92 #else | 93 #else |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 default: | 365 default: |
365 // This function should only be called if the colorType is suppo
rted by jpeg | 366 // This function should only be called if the colorType is suppo
rted by jpeg |
366 SkASSERT(false); | 367 SkASSERT(false); |
367 } | 368 } |
368 } | 369 } |
369 | 370 |
370 if (JCS_RGB == fDecoderMgr->dinfo()->out_color_space) { | 371 if (JCS_RGB == fDecoderMgr->dinfo()->out_color_space) { |
371 srcConfig = SkSwizzler::kRGB; | 372 srcConfig = SkSwizzler::kRGB; |
372 } | 373 } |
373 | 374 |
374 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, dstInfo, opti
ons)); | 375 Options swizzlerOptions = options; |
| 376 if (options.fSubset) { |
| 377 // Use fSwizzlerSubset if this is a subset decode. This is necessary in
the case |
| 378 // where libjpeg-turbo provides a subset and then we need to subset it f
urther. |
| 379 // Also, verify that fSwizzlerSubset is non-empty, meaning that we've in
itialized it. |
| 380 SkASSERT(!fSwizzlerSubset.isEmpty()); |
| 381 swizzlerOptions.fSubset = &fSwizzlerSubset; |
| 382 } |
| 383 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, dstInfo, swiz
zlerOptions)); |
375 SkASSERT(fSwizzler); | 384 SkASSERT(fSwizzler); |
376 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); | 385 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); |
377 fSrcRow = fStorage.get(); | 386 fSrcRow = fStorage.get(); |
378 } | 387 } |
379 | 388 |
380 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { | 389 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { |
381 if (!createIfNecessary || fSwizzler) { | 390 if (!createIfNecessary || fSwizzler) { |
382 SkASSERT(!fSwizzler || (fSrcRow && fStorage.get() == fSrcRow)); | 391 SkASSERT(!fSwizzler || (fSrcRow && fStorage.get() == fSrcRow)); |
383 return fSwizzler; | 392 return fSwizzler; |
384 } | 393 } |
(...skipping 19 matching lines...) Expand all Loading... |
404 fSwizzler.reset(nullptr); | 413 fSwizzler.reset(nullptr); |
405 fSrcRow = nullptr; | 414 fSrcRow = nullptr; |
406 fStorage.free(); | 415 fStorage.free(); |
407 | 416 |
408 // Now, given valid output dimensions, we can start the decompress | 417 // Now, given valid output dimensions, we can start the decompress |
409 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { | 418 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { |
410 SkCodecPrintf("start decompress failed\n"); | 419 SkCodecPrintf("start decompress failed\n"); |
411 return kInvalidInput; | 420 return kInvalidInput; |
412 } | 421 } |
413 | 422 |
| 423 if (options.fSubset) { |
| 424 fSwizzlerSubset = *options.fSubset; |
| 425 } |
| 426 |
| 427 #ifdef TURBO_HAS_CROP |
| 428 if (options.fSubset) { |
| 429 uint32_t startX = options.fSubset->x(); |
| 430 uint32_t width = options.fSubset->width(); |
| 431 |
| 432 // libjpeg-turbo may need to align startX to a multiple of the IDCT |
| 433 // block size. If this is the case, it will decrease the value of |
| 434 // startX to the appropriate alignment and also increase the value |
| 435 // of width so that the right edge of the requested subset remains |
| 436 // the same. |
| 437 jpeg_crop_scanline(fDecoderMgr->dinfo(), &startX, &width); |
| 438 |
| 439 SkASSERT(startX <= (uint32_t) options.fSubset->x()); |
| 440 SkASSERT(width >= (uint32_t) options.fSubset->width()); |
| 441 SkASSERT(startX + width >= (uint32_t) options.fSubset->right()); |
| 442 |
| 443 // Instruct the swizzler (if it is necessary) to further subset the |
| 444 // output provided by libjpeg-turbo |
| 445 fSwizzlerSubset.setXYWH(options.fSubset->x() - startX, 0, |
| 446 options.fSubset->width(), options.fSubset->height()); |
| 447 |
| 448 // We will need a swizzler if libjpeg-turbo cannot provide the exact |
| 449 // subset that we request. |
| 450 if (startX != (uint32_t) options.fSubset->x() || |
| 451 width != (uint32_t) options.fSubset->width()) { |
| 452 this->initializeSwizzler(dstInfo, options); |
| 453 } |
| 454 } |
| 455 |
| 456 // Make sure we have a swizzler if we are converting from CMYK. |
| 457 if (!fSwizzler && JCS_CMYK == fDecoderMgr->dinfo()->out_color_space) { |
| 458 this->initializeSwizzler(dstInfo, options); |
| 459 } |
| 460 #else |
414 // We will need a swizzler if we are performing a subset decode or | 461 // We will need a swizzler if we are performing a subset decode or |
415 // converting from CMYK. | 462 // converting from CMYK. |
416 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->out_color_space; | 463 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->out_color_space; |
417 if (options.fSubset || JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { | 464 if (options.fSubset || JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { |
418 this->initializeSwizzler(dstInfo, options); | 465 this->initializeSwizzler(dstInfo, options); |
419 } | 466 } |
| 467 #endif |
420 | 468 |
421 return kSuccess; | 469 return kSuccess; |
422 } | 470 } |
423 | 471 |
424 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { | 472 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { |
425 // Set the jump location for libjpeg errors | 473 // Set the jump location for libjpeg errors |
426 if (setjmp(fDecoderMgr->getJmpBuf())) { | 474 if (setjmp(fDecoderMgr->getJmpBuf())) { |
427 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 475 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
428 } | 476 } |
429 // Read rows one at a time | 477 // Read rows one at a time |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 | 714 |
667 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); | 715 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); |
668 if (linesRead < remainingRows) { | 716 if (linesRead < remainingRows) { |
669 // FIXME: Handle incomplete YUV decodes without signalling an error. | 717 // FIXME: Handle incomplete YUV decodes without signalling an error. |
670 return kInvalidInput; | 718 return kInvalidInput; |
671 } | 719 } |
672 } | 720 } |
673 | 721 |
674 return kSuccess; | 722 return kSuccess; |
675 } | 723 } |
OLD | NEW |