| 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 initialized and valid. |
| 380 SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fS
ubset->x() && |
| 381 fSwizzlerSubset.width() == options.fSubset->width()); |
| 382 swizzlerOptions.fSubset = &fSwizzlerSubset; |
| 383 } |
| 384 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, dstInfo, swiz
zlerOptions)); |
| 375 SkASSERT(fSwizzler); | 385 SkASSERT(fSwizzler); |
| 376 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); | 386 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); |
| 377 fSrcRow = fStorage.get(); | 387 fSrcRow = fStorage.get(); |
| 378 } | 388 } |
| 379 | 389 |
| 380 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { | 390 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { |
| 381 if (!createIfNecessary || fSwizzler) { | 391 if (!createIfNecessary || fSwizzler) { |
| 382 SkASSERT(!fSwizzler || (fSrcRow && fStorage.get() == fSrcRow)); | 392 SkASSERT(!fSwizzler || (fSrcRow && fStorage.get() == fSrcRow)); |
| 383 return fSwizzler; | 393 return fSwizzler; |
| 384 } | 394 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 404 fSwizzler.reset(nullptr); | 414 fSwizzler.reset(nullptr); |
| 405 fSrcRow = nullptr; | 415 fSrcRow = nullptr; |
| 406 fStorage.free(); | 416 fStorage.free(); |
| 407 | 417 |
| 408 // Now, given valid output dimensions, we can start the decompress | 418 // Now, given valid output dimensions, we can start the decompress |
| 409 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { | 419 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { |
| 410 SkCodecPrintf("start decompress failed\n"); | 420 SkCodecPrintf("start decompress failed\n"); |
| 411 return kInvalidInput; | 421 return kInvalidInput; |
| 412 } | 422 } |
| 413 | 423 |
| 424 if (options.fSubset) { |
| 425 fSwizzlerSubset = *options.fSubset; |
| 426 } |
| 427 |
| 428 #ifdef TURBO_HAS_CROP |
| 429 if (options.fSubset) { |
| 430 uint32_t startX = options.fSubset->x(); |
| 431 uint32_t width = options.fSubset->width(); |
| 432 |
| 433 // libjpeg-turbo may need to align startX to a multiple of the IDCT |
| 434 // block size. If this is the case, it will decrease the value of |
| 435 // startX to the appropriate alignment and also increase the value |
| 436 // of width so that the right edge of the requested subset remains |
| 437 // the same. |
| 438 jpeg_crop_scanline(fDecoderMgr->dinfo(), &startX, &width); |
| 439 |
| 440 SkASSERT(startX <= (uint32_t) options.fSubset->x()); |
| 441 SkASSERT(width >= (uint32_t) options.fSubset->width()); |
| 442 SkASSERT(startX + width >= (uint32_t) options.fSubset->right()); |
| 443 |
| 444 // Instruct the swizzler (if it is necessary) to further subset the |
| 445 // output provided by libjpeg-turbo. |
| 446 // |
| 447 // We set this here (rather than in the if statement below), so that |
| 448 // if (1) we don't need a swizzler for the subset, and (2) we need a |
| 449 // swizzler for CMYK, the swizzler will still use the proper subset |
| 450 // dimensions. |
| 451 // |
| 452 // Note that the swizzler will ignore the y and height parameters of |
| 453 // the subset. Since the scanline decoder (and the swizzler) handle |
| 454 // one row at a time, only the subsetting in the x-dimension matters. |
| 455 fSwizzlerSubset.setXYWH(options.fSubset->x() - startX, 0, |
| 456 options.fSubset->width(), options.fSubset->height()); |
| 457 |
| 458 // We will need a swizzler if libjpeg-turbo cannot provide the exact |
| 459 // subset that we request. |
| 460 if (startX != (uint32_t) options.fSubset->x() || |
| 461 width != (uint32_t) options.fSubset->width()) { |
| 462 this->initializeSwizzler(dstInfo, options); |
| 463 } |
| 464 } |
| 465 |
| 466 // Make sure we have a swizzler if we are converting from CMYK. |
| 467 if (!fSwizzler && JCS_CMYK == fDecoderMgr->dinfo()->out_color_space) { |
| 468 this->initializeSwizzler(dstInfo, options); |
| 469 } |
| 470 #else |
| 414 // We will need a swizzler if we are performing a subset decode or | 471 // We will need a swizzler if we are performing a subset decode or |
| 415 // converting from CMYK. | 472 // converting from CMYK. |
| 416 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->out_color_space; | 473 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->out_color_space; |
| 417 if (options.fSubset || JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { | 474 if (options.fSubset || JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { |
| 418 this->initializeSwizzler(dstInfo, options); | 475 this->initializeSwizzler(dstInfo, options); |
| 419 } | 476 } |
| 477 #endif |
| 420 | 478 |
| 421 return kSuccess; | 479 return kSuccess; |
| 422 } | 480 } |
| 423 | 481 |
| 424 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { | 482 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { |
| 425 // Set the jump location for libjpeg errors | 483 // Set the jump location for libjpeg errors |
| 426 if (setjmp(fDecoderMgr->getJmpBuf())) { | 484 if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 427 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 485 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
| 428 } | 486 } |
| 429 // Read rows one at a time | 487 // Read rows one at a time |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 | 724 |
| 667 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); | 725 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); |
| 668 if (linesRead < remainingRows) { | 726 if (linesRead < remainingRows) { |
| 669 // FIXME: Handle incomplete YUV decodes without signalling an error. | 727 // FIXME: Handle incomplete YUV decodes without signalling an error. |
| 670 return kInvalidInput; | 728 return kInvalidInput; |
| 671 } | 729 } |
| 672 } | 730 } |
| 673 | 731 |
| 674 return kSuccess; | 732 return kSuccess; |
| 675 } | 733 } |
| OLD | NEW |