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 "SkJpegCodec.h" | 9 #include "SkJpegCodec.h" |
10 #include "SkJpegDecoderMgr.h" | 10 #include "SkJpegDecoderMgr.h" |
11 #include "SkJpegUtility_codec.h" | 11 #include "SkJpegUtility_codec.h" |
12 #include "SkCodecPriv.h" | 12 #include "SkCodecPriv.h" |
13 #include "SkColorPriv.h" | 13 #include "SkColorPriv.h" |
| 14 #include "SkScaledCodec.h" |
14 #include "SkScanlineDecoder.h" | 15 #include "SkScanlineDecoder.h" |
15 #include "SkStream.h" | 16 #include "SkStream.h" |
16 #include "SkTemplates.h" | 17 #include "SkTemplates.h" |
17 #include "SkTypes.h" | 18 #include "SkTypes.h" |
18 | 19 |
19 // stdio is needed for libjpeg-turbo | 20 // stdio is needed for libjpeg-turbo |
20 #include <stdio.h> | 21 #include <stdio.h> |
21 | 22 |
22 extern "C" { | 23 extern "C" { |
23 #include "jpeglibmangler.h" | 24 #include "jpeglibmangler.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 return NULL; | 143 return NULL; |
143 } | 144 } |
144 | 145 |
145 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, | 146 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, |
146 JpegDecoderMgr* decoderMgr) | 147 JpegDecoderMgr* decoderMgr) |
147 : INHERITED(srcInfo, stream) | 148 : INHERITED(srcInfo, stream) |
148 , fDecoderMgr(decoderMgr) | 149 , fDecoderMgr(decoderMgr) |
149 {} | 150 {} |
150 | 151 |
151 /* | 152 /* |
| 153 * Return the row bytes of a particular image type and width |
| 154 */ |
| 155 static int get_row_bytes(const j_decompress_ptr dinfo) { |
| 156 int colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 : dinfo->out_col
or_components; |
| 157 return dinfo->output_width * colorBytes; |
| 158 |
| 159 } |
| 160 /* |
152 * Return a valid set of output dimensions for this decoder, given an input scal
e | 161 * Return a valid set of output dimensions for this decoder, given an input scal
e |
153 */ | 162 */ |
154 SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const { | 163 SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const { |
155 // libjpeg-turbo supports scaling by 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and
1/1, so we will | 164 // libjpeg-turbo supports scaling by 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and
1/1, so we will |
156 // support these as well | 165 // support these as well |
157 long num; | 166 long num; |
158 long denom = 8; | 167 long denom = 8; |
159 if (desiredScale > 0.875f) { | 168 if (desiredScale > 0.875f) { |
160 num = 8; | 169 num = 8; |
161 } else if (desiredScale > 0.75f) { | 170 } else if (desiredScale > 0.75f) { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 // much faster than decoding to color and then converting | 274 // much faster than decoding to color and then converting |
266 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE; | 275 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE; |
267 } | 276 } |
268 return true; | 277 return true; |
269 default: | 278 default: |
270 return false; | 279 return false; |
271 } | 280 } |
272 } | 281 } |
273 | 282 |
274 /* | 283 /* |
275 * Checks if we can scale to the requested dimensions and scales the dimensions | 284 * Checks if we can natively scale to the requested dimensions and natively scal
es the |
276 * if possible | 285 * dimensions if possible |
277 */ | 286 */ |
278 bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) { | 287 bool SkJpegCodec::nativelyScaleToDimensions(uint32_t dstWidth, uint32_t dstHeigh
t) { |
279 // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1 | 288 // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1 |
280 fDecoderMgr->dinfo()->scale_denom = 8; | 289 fDecoderMgr->dinfo()->scale_denom = 8; |
281 fDecoderMgr->dinfo()->scale_num = 8; | 290 fDecoderMgr->dinfo()->scale_num = 8; |
282 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); | 291 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); |
283 while (fDecoderMgr->dinfo()->output_width != dstWidth || | 292 while (fDecoderMgr->dinfo()->output_width != dstWidth || |
284 fDecoderMgr->dinfo()->output_height != dstHeight) { | 293 fDecoderMgr->dinfo()->output_height != dstHeight) { |
285 | 294 |
286 // Return a failure if we have tried all of the possible scales | 295 // Return a failure if we have tried all of the possible scales |
287 if (1 == fDecoderMgr->dinfo()->scale_num || | 296 if (1 == fDecoderMgr->dinfo()->scale_num || |
288 dstWidth > fDecoderMgr->dinfo()->output_width || | 297 dstWidth > fDecoderMgr->dinfo()->output_width || |
289 dstHeight > fDecoderMgr->dinfo()->output_height) { | 298 dstHeight > fDecoderMgr->dinfo()->output_height) { |
290 return fDecoderMgr->returnFalse("could not scale to requested dimens
ions"); | 299 // reset native scale settings on failure because this may be suppor
ted by the swizzler |
| 300 this->fDecoderMgr->dinfo()->scale_num = 8; |
| 301 turbo_jpeg_calc_output_dimensions(this->fDecoderMgr->dinfo()); |
| 302 return false; |
291 } | 303 } |
292 | 304 |
293 // Try the next scale | 305 // Try the next scale |
294 fDecoderMgr->dinfo()->scale_num -= 1; | 306 fDecoderMgr->dinfo()->scale_num -= 1; |
295 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); | 307 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); |
296 } | 308 } |
297 return true; | 309 return true; |
298 } | 310 } |
299 | 311 |
300 /* | 312 /* |
(...skipping 19 matching lines...) Expand all Loading... |
320 if (setjmp(fDecoderMgr->getJmpBuf())) { | 332 if (setjmp(fDecoderMgr->getJmpBuf())) { |
321 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 333 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
322 } | 334 } |
323 | 335 |
324 // Check if we can decode to the requested destination and set the output co
lor space | 336 // Check if we can decode to the requested destination and set the output co
lor space |
325 if (!this->setOutputColorSpace(dstInfo)) { | 337 if (!this->setOutputColorSpace(dstInfo)) { |
326 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers
ion); | 338 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers
ion); |
327 } | 339 } |
328 | 340 |
329 // Perform the necessary scaling | 341 // Perform the necessary scaling |
330 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { | 342 if (!this->nativelyScaleToDimensions(dstInfo.width(), dstInfo.height())) { |
331 return fDecoderMgr->returnFailure("cannot scale to requested dims", kInv
alidScale); | 343 return fDecoderMgr->returnFailure("cannot scale to requested dims", kInv
alidScale); |
332 } | 344 } |
333 | 345 |
334 // Now, given valid output dimensions, we can start the decompress | 346 // Now, given valid output dimensions, we can start the decompress |
335 if (!turbo_jpeg_start_decompress(dinfo)) { | 347 if (!turbo_jpeg_start_decompress(dinfo)) { |
336 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); | 348 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); |
337 } | 349 } |
338 | 350 |
339 // The recommended output buffer height should always be 1 in high quality m
odes. | 351 // The recommended output buffer height should always be 1 in high quality m
odes. |
340 // If it's not, we want to know because it means our strategy is not optimal
. | 352 // If it's not, we want to know because it means our strategy is not optimal
. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 * Enable scanline decoding for jpegs | 400 * Enable scanline decoding for jpegs |
389 */ | 401 */ |
390 class SkJpegScanlineDecoder : public SkScanlineDecoder { | 402 class SkJpegScanlineDecoder : public SkScanlineDecoder { |
391 public: | 403 public: |
392 SkJpegScanlineDecoder(const SkImageInfo& srcInfo, SkJpegCodec* codec) | 404 SkJpegScanlineDecoder(const SkImageInfo& srcInfo, SkJpegCodec* codec) |
393 : INHERITED(srcInfo) | 405 : INHERITED(srcInfo) |
394 , fCodec(codec) | 406 , fCodec(codec) |
395 , fOpts() | 407 , fOpts() |
396 {} | 408 {} |
397 | 409 |
| 410 /* |
| 411 * Return a valid set of output dimensions for this decoder, given an input s
cale |
| 412 */ |
| 413 SkISize onGetScaledDimensions(float desiredScale) override { |
| 414 return fCodec->onGetScaledDimensions(desiredScale); |
| 415 } |
| 416 |
| 417 /* |
| 418 * Create the swizzler based on the encoded format. |
| 419 * The swizzler is only used for sampling in the x direction. |
| 420 */ |
| 421 |
| 422 SkCodec::Result initializeSwizzler(const SkImageInfo& info, const SkCodec::O
ptions& options) { |
| 423 SkSwizzler::SrcConfig srcConfig; |
| 424 switch (info.colorType()) { |
| 425 case kGray_8_SkColorType: |
| 426 srcConfig = SkSwizzler::kGray; |
| 427 break; |
| 428 case kRGBA_8888_SkColorType: |
| 429 srcConfig = SkSwizzler::kRGBX; |
| 430 break; |
| 431 case kBGRA_8888_SkColorType: |
| 432 srcConfig = SkSwizzler::kBGRX; |
| 433 break; |
| 434 case kRGB_565_SkColorType: |
| 435 srcConfig = SkSwizzler::kRGB_565; |
| 436 break; |
| 437 default: |
| 438 //would have exited before now if the colorType was supported by
jpeg |
| 439 SkASSERT(false); |
| 440 } |
| 441 |
| 442 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, NULL, info, option
s.fZeroInitialized, |
| 443 this->getInfo().width())); |
| 444 if (!fSwizzler) { |
| 445 // FIXME: CreateSwizzler could fail for another reason. |
| 446 return SkCodec::kUnimplemented; |
| 447 } |
| 448 return SkCodec::kSuccess; |
| 449 } |
| 450 |
398 SkCodec::Result onStart(const SkImageInfo& dstInfo, const SkCodec::Options&
options, | 451 SkCodec::Result onStart(const SkImageInfo& dstInfo, const SkCodec::Options&
options, |
399 SkPMColor ctable[], int* ctableCount) override { | 452 SkPMColor ctable[], int* ctableCount) override { |
400 | 453 |
401 // Rewind the stream if needed | 454 // Rewind the stream if needed |
402 if (!fCodec->handleRewind()) { | 455 if (!fCodec->handleRewind()) { |
403 return SkCodec::kCouldNotRewind; | 456 return SkCodec::kCouldNotRewind; |
404 } | 457 } |
405 | 458 |
406 // Set the jump location for libjpeg errors | 459 // Set the jump location for libjpeg errors |
407 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 460 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
408 SkCodecPrintf("setjmp: Error from libjpeg\n"); | 461 SkCodecPrintf("setjmp: Error from libjpeg\n"); |
409 return SkCodec::kInvalidInput; | 462 return SkCodec::kInvalidInput; |
410 } | 463 } |
411 | 464 |
412 // Check if we can decode to the requested destination and set the outpu
t color space | 465 // Check if we can decode to the requested destination and set the outpu
t color space |
413 if (!fCodec->setOutputColorSpace(dstInfo)) { | 466 if (!fCodec->setOutputColorSpace(dstInfo)) { |
414 return SkCodec::kInvalidConversion; | 467 return SkCodec::kInvalidConversion; |
415 } | 468 } |
416 | 469 |
417 // Perform the necessary scaling | 470 // Perform the necessary scaling |
418 if (!fCodec->scaleToDimensions(dstInfo.width(), dstInfo.height())) { | 471 if (!fCodec->nativelyScaleToDimensions(dstInfo.width(), dstInfo.height()
)) { |
419 return SkCodec::kInvalidScale; | 472 // full native scaling to dstInfo dimensions not supported |
| 473 |
| 474 if (!SkScaledCodec::DimensionsSupportedForSampling(this->getInfo(),
dstInfo)) { |
| 475 return SkCodec::kInvalidScale; |
| 476 } |
| 477 // create swizzler for sampling |
| 478 SkCodec::Result result = this->initializeSwizzler(dstInfo, options); |
| 479 if (SkCodec::kSuccess != result) { |
| 480 SkCodecPrintf("failed to initialize the swizzler.\n"); |
| 481 return result; |
| 482 } |
| 483 fStorage.reset(get_row_bytes(fCodec->fDecoderMgr->dinfo())); |
| 484 fSrcRow = static_cast<uint8_t*>(fStorage.get()); |
| 485 } else { |
| 486 fSrcRow = NULL; |
| 487 fSwizzler.reset(NULL); |
420 } | 488 } |
421 | 489 |
422 // Now, given valid output dimensions, we can start the decompress | 490 // Now, given valid output dimensions, we can start the decompress |
423 if (!turbo_jpeg_start_decompress(fCodec->fDecoderMgr->dinfo())) { | 491 if (!turbo_jpeg_start_decompress(fCodec->fDecoderMgr->dinfo())) { |
424 SkCodecPrintf("start decompress failed\n"); | 492 SkCodecPrintf("start decompress failed\n"); |
425 return SkCodec::kInvalidInput; | 493 return SkCodec::kInvalidInput; |
426 } | 494 } |
427 | 495 |
428 fOpts = options; | 496 fOpts = options; |
429 | 497 |
(...skipping 10 matching lines...) Expand all Loading... |
440 // partial decode. | 508 // partial decode. |
441 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height
(); | 509 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height
(); |
442 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); | 510 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); |
443 } | 511 } |
444 | 512 |
445 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri
de { | 513 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri
de { |
446 // Set the jump location for libjpeg errors | 514 // Set the jump location for libjpeg errors |
447 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 515 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
448 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali
dInput); | 516 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali
dInput); |
449 } | 517 } |
| 518 // Read rows one at a time |
| 519 JSAMPLE* dstRow; |
| 520 if (fSwizzler) { |
| 521 // write data to storage row, then sample using swizzler |
| 522 dstRow = fSrcRow; |
| 523 } else { |
| 524 // write data directly to dst |
| 525 dstRow = (JSAMPLE*) dst; |
| 526 } |
450 | 527 |
451 // Read rows one at a time | |
452 JSAMPLE* dstRow = (JSAMPLE*) dst; | |
453 for (int y = 0; y < count; y++) { | 528 for (int y = 0; y < count; y++) { |
454 // Read row of the image | 529 // Read row of the image |
455 uint32_t rowsDecoded = | 530 uint32_t rowsDecoded = |
456 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dst
Row, 1); | 531 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dst
Row, 1); |
457 if (rowsDecoded != 1) { | 532 if (rowsDecoded != 1) { |
458 if (SkCodec::kNo_ZeroInitialized == fOpts.fZeroInitialized || | 533 if (SkCodec::kNo_ZeroInitialized == fOpts.fZeroInitialized || |
459 kN32_SkColorType == this->dstInfo().colorType()) { | 534 kN32_SkColorType == this->dstInfo().colorType()) { |
460 SkSwizzler::Fill(dstRow, this->dstInfo(), rowBytes, | 535 SkSwizzler::Fill(dstRow, this->dstInfo(), rowBytes, |
461 count - y, SK_ColorBLACK, NULL); | 536 count - y, SK_ColorBLACK, NULL); |
462 } | 537 } |
463 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo().
height(); | 538 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo().
height(); |
464 return SkCodec::kIncompleteInput; | 539 return SkCodec::kIncompleteInput; |
465 } | 540 } |
466 | 541 |
467 // Convert to RGBA if necessary | 542 // Convert to RGBA if necessary |
468 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { | 543 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { |
469 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width()); | 544 convert_CMYK_to_RGBA(dstRow, fCodec->fDecoderMgr->dinfo()->outpu
t_width); |
470 } | 545 } |
471 | 546 |
472 // Move to the next row | 547 if(fSwizzler) { |
473 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes); | 548 // use swizzler to sample row |
| 549 fSwizzler->swizzle(dst, dstRow); |
| 550 dst = SkTAddOffset<JSAMPLE>(dst, rowBytes); |
| 551 } else { |
| 552 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes); |
| 553 } |
474 } | 554 } |
475 | |
476 return SkCodec::kSuccess; | 555 return SkCodec::kSuccess; |
477 } | 556 } |
478 | 557 |
479 #ifndef TURBO_HAS_SKIP | 558 #ifndef TURBO_HAS_SKIP |
480 #define turbo_jpeg_skip_scanlines(dinfo, count) \ | 559 #define turbo_jpeg_skip_scanlines(dinfo, count)
\ |
481 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \ | 560 SkAutoMalloc storage(get_row_bytes(dinfo));
\ |
482 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \ | 561 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get());
\ |
483 for (int y = 0; y < count; y++) { \ | 562 for (int y = 0; y < count; y++) {
\ |
484 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \ | 563 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1);
\ |
485 } | 564 } |
486 #endif | 565 #endif |
487 | 566 |
488 SkCodec::Result onSkipScanlines(int count) override { | 567 SkCodec::Result onSkipScanlines(int count) override { |
489 // Set the jump location for libjpeg errors | 568 // Set the jump location for libjpeg errors |
490 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 569 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
491 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali
dInput); | 570 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali
dInput); |
492 } | 571 } |
493 | 572 |
494 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); | 573 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); |
495 | 574 |
496 return SkCodec::kSuccess; | 575 return SkCodec::kSuccess; |
497 } | 576 } |
498 | 577 |
| 578 SkEncodedFormat onGetEncodedFormat() const override { |
| 579 return kJPEG_SkEncodedFormat; |
| 580 } |
| 581 |
499 private: | 582 private: |
500 SkAutoTDelete<SkJpegCodec> fCodec; | 583 SkAutoTDelete<SkJpegCodec> fCodec; |
| 584 SkAutoMalloc fStorage; // Only used if sampling is needed |
| 585 uint8_t* fSrcRow; // Only used if sampling is needed |
501 SkCodec::Options fOpts; | 586 SkCodec::Options fOpts; |
| 587 SkAutoTDelete<SkSwizzler> fSwizzler; |
502 | 588 |
503 typedef SkScanlineDecoder INHERITED; | 589 typedef SkScanlineDecoder INHERITED; |
504 }; | 590 }; |
505 | 591 |
506 SkScanlineDecoder* SkJpegCodec::NewSDFromStream(SkStream* stream) { | 592 SkScanlineDecoder* SkJpegCodec::NewSDFromStream(SkStream* stream) { |
507 SkAutoTDelete<SkJpegCodec> codec(static_cast<SkJpegCodec*>(SkJpegCodec::NewF
romStream(stream))); | 593 SkAutoTDelete<SkJpegCodec> codec(static_cast<SkJpegCodec*>(SkJpegCodec::NewF
romStream(stream))); |
508 if (!codec) { | 594 if (!codec) { |
509 return NULL; | 595 return NULL; |
510 } | 596 } |
511 | 597 |
512 const SkImageInfo& srcInfo = codec->getInfo(); | 598 const SkImageInfo& srcInfo = codec->getInfo(); |
| 599 |
513 // Return the new scanline decoder | 600 // Return the new scanline decoder |
514 return SkNEW_ARGS(SkJpegScanlineDecoder, (srcInfo, codec.detach())); | 601 return SkNEW_ARGS(SkJpegScanlineDecoder, (srcInfo, codec.detach())); |
515 } | 602 } |
OLD | NEW |