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" |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
382 turbo_jpeg_finish_decompress(dinfo); | 382 turbo_jpeg_finish_decompress(dinfo); |
383 | 383 |
384 return kSuccess; | 384 return kSuccess; |
385 } | 385 } |
386 | 386 |
387 /* | 387 /* |
388 * Enable scanline decoding for jpegs | 388 * Enable scanline decoding for jpegs |
389 */ | 389 */ |
390 class SkJpegScanlineDecoder : public SkScanlineDecoder { | 390 class SkJpegScanlineDecoder : public SkScanlineDecoder { |
391 public: | 391 public: |
392 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec, | 392 SkJpegScanlineDecoder(const SkImageInfo& srcInfo, SkJpegCodec* codec) |
393 const SkCodec::Options& opts) | 393 : INHERITED(srcInfo) |
394 : INHERITED(dstInfo) | |
395 , fCodec(codec) | 394 , fCodec(codec) |
396 , fOpts(opts) | 395 , fOpts() |
scroggo_chromium
2015/08/03 18:35:58
The latest patch is a rebase, but I had to make so
| |
397 {} | 396 {} |
398 | 397 |
398 SkCodec::Result onStart(const SkImageInfo& dstInfo, const SkCodec::Options& options, | |
399 SkPMColor ctable[], int* ctableCount) override { | |
400 | |
401 // Rewind the stream if needed | |
402 if (!fCodec->handleRewind()) { | |
403 return SkCodec::kCouldNotRewind; | |
404 } | |
405 | |
406 // Set the jump location for libjpeg errors | |
407 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | |
408 SkCodecPrintf("setjmp: Error from libjpeg\n"); | |
409 return SkCodec::kInvalidInput; | |
410 } | |
411 | |
412 // Check if we can decode to the requested destination and set the outpu t color space | |
413 if (!fCodec->setOutputColorSpace(dstInfo)) { | |
414 return SkCodec::kInvalidConversion; | |
415 } | |
416 | |
417 // Perform the necessary scaling | |
418 if (!fCodec->scaleToDimensions(dstInfo.width(), dstInfo.height())) { | |
419 return SkCodec::kInvalidScale; | |
420 } | |
421 | |
422 // Now, given valid output dimensions, we can start the decompress | |
423 if (!turbo_jpeg_start_decompress(fCodec->fDecoderMgr->dinfo())) { | |
424 SkCodecPrintf("start decompress failed\n"); | |
425 return SkCodec::kInvalidInput; | |
426 } | |
427 | |
428 fOpts = options; | |
429 | |
430 return SkCodec::kSuccess; | |
431 } | |
432 | |
399 virtual ~SkJpegScanlineDecoder() { | 433 virtual ~SkJpegScanlineDecoder() { |
400 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 434 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
401 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); | 435 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); |
402 return; | 436 return; |
403 } | 437 } |
404 | 438 |
405 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a | 439 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a |
406 // partial decode. | 440 // partial decode. |
407 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height (); | 441 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height (); |
408 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); | 442 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
457 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput); | 491 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput); |
458 } | 492 } |
459 | 493 |
460 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); | 494 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); |
461 | 495 |
462 return SkCodec::kSuccess; | 496 return SkCodec::kSuccess; |
463 } | 497 } |
464 | 498 |
465 private: | 499 private: |
466 SkAutoTDelete<SkJpegCodec> fCodec; | 500 SkAutoTDelete<SkJpegCodec> fCodec; |
467 const SkCodec::Options& fOpts; | 501 SkCodec::Options fOpts; |
468 | 502 |
469 typedef SkScanlineDecoder INHERITED; | 503 typedef SkScanlineDecoder INHERITED; |
470 }; | 504 }; |
471 | 505 |
472 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, | 506 SkScanlineDecoder* SkJpegCodec::NewSDFromStream(SkStream* stream) { |
473 const Options& options, SkPMColor ctable[], int* ctableCount) { | |
474 | |
475 // Rewind the stream if needed | |
476 if (!this->handleRewind()) { | |
477 SkCodecPrintf("Could not rewind\n"); | |
478 return NULL; | |
479 } | |
480 | |
481 // Set the jump location for libjpeg errors | |
482 if (setjmp(fDecoderMgr->getJmpBuf())) { | |
483 SkCodecPrintf("setjmp: Error from libjpeg\n"); | |
484 return NULL; | |
485 } | |
486 | |
487 SkStream* stream = this->stream()->duplicate(); | |
488 if (!stream) { | |
489 return NULL; | |
490 } | |
491 SkAutoTDelete<SkJpegCodec> codec(static_cast<SkJpegCodec*>(SkJpegCodec::NewF romStream(stream))); | 507 SkAutoTDelete<SkJpegCodec> codec(static_cast<SkJpegCodec*>(SkJpegCodec::NewF romStream(stream))); |
492 if (!codec) { | 508 if (!codec) { |
493 return NULL; | 509 return NULL; |
494 } | 510 } |
495 | 511 |
496 // Check if we can decode to the requested destination and set the output co lor space | 512 const SkImageInfo& srcInfo = codec->getInfo(); |
497 if (!codec->setOutputColorSpace(dstInfo)) { | |
498 SkCodecPrintf("Cannot convert to output type\n"); | |
499 return NULL; | |
500 } | |
501 | |
502 // Perform the necessary scaling | |
503 if (!codec->scaleToDimensions(dstInfo.width(), dstInfo.height())) { | |
504 SkCodecPrintf("Cannot scale to output dimensions\n"); | |
505 return NULL; | |
506 } | |
507 | |
508 // Now, given valid output dimensions, we can start the decompress | |
509 if (!turbo_jpeg_start_decompress(codec->fDecoderMgr->dinfo())) { | |
510 SkCodecPrintf("start decompress failed\n"); | |
511 return NULL; | |
512 } | |
513 | |
514 // Return the new scanline decoder | 513 // Return the new scanline decoder |
515 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, codec.detach(), options)) ; | 514 return SkNEW_ARGS(SkJpegScanlineDecoder, (srcInfo, codec.detach())); |
516 } | 515 } |
OLD | NEW |