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 "SkBmpCodec.h" | 8 #include "SkBmpCodec.h" |
9 #include "SkCodec.h" | 9 #include "SkCodec.h" |
10 #include "SkData.h" | 10 #include "SkData.h" |
11 #include "SkCodec_libgif.h" | 11 #include "SkCodec_libgif.h" |
12 #include "SkCodec_libico.h" | 12 #include "SkCodec_libico.h" |
13 #include "SkCodec_libpng.h" | 13 #include "SkCodec_libpng.h" |
14 #include "SkCodec_wbmp.h" | 14 #include "SkCodec_wbmp.h" |
15 #include "SkCodecPriv.h" | 15 #include "SkCodecPriv.h" |
16 #if !defined(GOOGLE3) | 16 #if !defined(GOOGLE3) |
17 #include "SkJpegCodec.h" | 17 #include "SkJpegCodec.h" |
18 #endif | 18 #endif |
19 #include "SkStream.h" | 19 #include "SkStream.h" |
20 #include "SkWebpCodec.h" | 20 #include "SkWebpCodec.h" |
21 | 21 |
22 struct DecoderProc { | 22 struct DecoderProc { |
23 bool (*IsFormat)(SkStream*); | 23 bool (*IsFormat)(SkStream*); |
24 SkCodec* (*NewFromStream)(SkStream*); | 24 SkCodec* (*NewFromStream)(SkStream*); |
25 }; | 25 }; |
26 | 26 |
27 static const DecoderProc gDecoderProcs[] = { | 27 static const DecoderProc gDecoderProcs[] = { |
| 28 { SkPngCodec::IsPng, SkPngCodec::NewFromStream }, |
28 #if !defined(GOOGLE3) | 29 #if !defined(GOOGLE3) |
29 { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream }, | 30 { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream }, |
30 #endif | 31 #endif |
31 { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream }, | 32 { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream }, |
32 { SkGifCodec::IsGif, SkGifCodec::NewFromStream }, | 33 { SkGifCodec::IsGif, SkGifCodec::NewFromStream }, |
33 { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream }, | 34 { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream }, |
34 { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream }, | 35 { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream }, |
35 { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream } | 36 { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream } |
36 }; | 37 }; |
37 | 38 |
38 SkCodec* SkCodec::NewFromStream(SkStream* stream, | 39 SkCodec* SkCodec::NewFromStream(SkStream* stream) { |
39 SkPngChunkReader* chunkReader) { | |
40 if (!stream) { | 40 if (!stream) { |
41 return nullptr; | 41 return nullptr; |
42 } | 42 } |
43 | 43 |
44 SkAutoTDelete<SkStream> streamDeleter(stream); | 44 SkAutoTDelete<SkStream> streamDeleter(stream); |
45 | 45 |
46 SkAutoTDelete<SkCodec> codec(nullptr); | 46 SkAutoTDelete<SkCodec> codec(nullptr); |
47 // PNG is special, since we want to be able to supply an SkPngChunkReader. | 47 for (uint32_t i = 0; i < SK_ARRAY_COUNT(gDecoderProcs); i++) { |
48 // But this code follows the same pattern as the loop. | 48 DecoderProc proc = gDecoderProcs[i]; |
49 const bool isPng = SkPngCodec::IsPng(stream); | 49 const bool correctFormat = proc.IsFormat(stream); |
50 if (!stream->rewind()) { | 50 if (!stream->rewind()) { |
51 return NULL; | 51 return nullptr; |
52 } | 52 } |
53 if (isPng) { | 53 if (correctFormat) { |
54 codec.reset(SkPngCodec::NewFromStream(streamDeleter.detach(), chunkReade
r)); | 54 codec.reset(proc.NewFromStream(streamDeleter.detach())); |
55 } else { | 55 break; |
56 for (DecoderProc proc : gDecoderProcs) { | |
57 const bool correctFormat = proc.IsFormat(stream); | |
58 if (!stream->rewind()) { | |
59 return nullptr; | |
60 } | |
61 if (correctFormat) { | |
62 codec.reset(proc.NewFromStream(streamDeleter.detach())); | |
63 break; | |
64 } | |
65 } | 56 } |
66 } | 57 } |
67 | 58 |
68 // Set the max size at 128 megapixels (512 MB for kN32). | 59 // Set the max size at 128 megapixels (512 MB for kN32). |
69 // This is about 4x smaller than a test image that takes a few minutes for | 60 // This is about 4x smaller than a test image that takes a few minutes for |
70 // dm to decode and draw. | 61 // dm to decode and draw. |
71 const int32_t maxSize = 1 << 27; | 62 const int32_t maxSize = 1 << 27; |
72 if (codec && codec->getInfo().width() * codec->getInfo().height() > maxSize)
{ | 63 if (codec && codec->getInfo().width() * codec->getInfo().height() > maxSize)
{ |
73 SkCodecPrintf("Error: Image size too large, cannot decode.\n"); | 64 SkCodecPrintf("Error: Image size too large, cannot decode.\n"); |
74 return nullptr; | 65 return nullptr; |
75 } else { | 66 } else { |
76 return codec.detach(); | 67 return codec.detach(); |
77 } | 68 } |
78 } | 69 } |
79 | 70 |
80 SkCodec* SkCodec::NewFromData(SkData* data, SkPngChunkReader* reader) { | 71 SkCodec* SkCodec::NewFromData(SkData* data) { |
81 if (!data) { | 72 if (!data) { |
82 return nullptr; | 73 return nullptr; |
83 } | 74 } |
84 return NewFromStream(new SkMemoryStream(data), reader); | 75 return NewFromStream(new SkMemoryStream(data)); |
85 } | 76 } |
86 | 77 |
87 SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream) | 78 SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream) |
88 : fSrcInfo(info) | 79 : fSrcInfo(info) |
89 , fStream(stream) | 80 , fStream(stream) |
90 , fNeedsRewind(false) | 81 , fNeedsRewind(false) |
91 , fDstInfo() | 82 , fDstInfo() |
92 , fOptions() | 83 , fOptions() |
93 , fCurrScanline(-1) | 84 , fCurrScanline(-1) |
94 {} | 85 {} |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 SkASSERT(1 == linesRequested || this->getInfo().height() == linesReq
uested); | 343 SkASSERT(1 == linesRequested || this->getInfo().height() == linesReq
uested); |
353 const SkImageInfo fillInfo = info.makeWH(info.width(), 1); | 344 const SkImageInfo fillInfo = info.makeWH(info.width(), 1); |
354 for (int srcY = linesDecoded; srcY < linesRequested; srcY++) { | 345 for (int srcY = linesDecoded; srcY < linesRequested; srcY++) { |
355 fillDst = SkTAddOffset<void>(dst, this->outputScanline(srcY) * r
owBytes); | 346 fillDst = SkTAddOffset<void>(dst, this->outputScanline(srcY) * r
owBytes); |
356 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, samp
ler); | 347 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, samp
ler); |
357 } | 348 } |
358 break; | 349 break; |
359 } | 350 } |
360 } | 351 } |
361 } | 352 } |
OLD | NEW |