OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2015 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SkScanlineDecoder.h" |
| 9 #include "SkCodec_libpng.h" |
| 10 #include "SkCodecPriv.h" |
| 11 #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 12 #include "SkJpegCodec.h" |
| 13 #endif |
| 14 |
| 15 struct DecoderProc { |
| 16 bool (*IsFormat)(SkStream*); |
| 17 SkScanlineDecoder* (*NewFromStream)(SkStream*); |
| 18 }; |
| 19 |
| 20 static const DecoderProc gDecoderProcs[] = { |
| 21 { SkPngCodec::IsPng, SkPngCodec::NewSDFromStream }, |
| 22 #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 23 { SkJpegCodec::IsJpeg, SkJpegCodec::NewSDFromStream }, |
| 24 #endif |
| 25 }; |
| 26 |
| 27 SkScanlineDecoder* SkScanlineDecoder::NewFromStream(SkStream* stream) { |
| 28 if (!stream) { |
| 29 return NULL; |
| 30 } |
| 31 |
| 32 SkAutoTDelete<SkStream> streamDeleter(stream); |
| 33 |
| 34 SkAutoTDelete<SkScanlineDecoder> codec(NULL); |
| 35 for (uint32_t i = 0; i < SK_ARRAY_COUNT(gDecoderProcs); i++) { |
| 36 DecoderProc proc = gDecoderProcs[i]; |
| 37 const bool correctFormat = proc.IsFormat(stream); |
| 38 if (!stream->rewind()) { |
| 39 return NULL; |
| 40 } |
| 41 if (correctFormat) { |
| 42 codec.reset(proc.NewFromStream(streamDeleter.detach())); |
| 43 break; |
| 44 } |
| 45 } |
| 46 |
| 47 // Set the max size at 128 megapixels (512 MB for kN32). |
| 48 // This is about 4x smaller than a test image that takes a few minutes for |
| 49 // dm to decode and draw. |
| 50 const int32_t maxSize = 1 << 27; |
| 51 if (codec && codec->getInfo().width() * codec->getInfo().height() > maxSize)
{ |
| 52 SkCodecPrintf("Error: Image size too large, cannot decode.\n"); |
| 53 return NULL; |
| 54 } else { |
| 55 return codec.detach(); |
| 56 } |
| 57 } |
| 58 |
| 59 SkScanlineDecoder* SkScanlineDecoder::NewFromData(SkData* data) { |
| 60 if (!data) { |
| 61 return NULL; |
| 62 } |
| 63 return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data))); |
| 64 } |
| 65 |
| 66 |
| 67 SkCodec::Result SkScanlineDecoder::start(const SkImageInfo& dstInfo, |
| 68 const SkCodec::Options* options, SkPMColor ctable[], int* ctableCount) { |
| 69 // Set options. |
| 70 SkCodec::Options optsStorage; |
| 71 if (NULL == options) { |
| 72 options = &optsStorage; |
| 73 } |
| 74 |
| 75 const SkCodec::Result result = this->onStart(dstInfo, *options, ctable, ctab
leCount); |
| 76 if (result != SkCodec::kSuccess) { |
| 77 return result; |
| 78 } |
| 79 |
| 80 fCurrScanline = 0; |
| 81 fDstInfo = dstInfo; |
| 82 return SkCodec::kSuccess; |
| 83 } |
| 84 |
| 85 SkCodec::Result SkScanlineDecoder::start(const SkImageInfo& dstInfo) { |
| 86 SkASSERT(kIndex_8_SkColorType != dstInfo.colorType()); |
| 87 if (kIndex_8_SkColorType == dstInfo.colorType()) { |
| 88 return SkCodec::kInvalidConversion; |
| 89 } |
| 90 return this->start(dstInfo, NULL, NULL, NULL); |
| 91 } |
| 92 |
OLD | NEW |