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)(const char*, size_t); |
24 SkCodec* (*NewFromStream)(SkStream*); | 24 SkCodec* (*NewFromStream)(SkStream*); |
25 }; | 25 }; |
26 | 26 |
27 static const DecoderProc gDecoderProcs[] = { | 27 static const DecoderProc gDecoderProcs[] = { |
28 #if !defined(GOOGLE3) | 28 #if !defined(GOOGLE3) |
29 { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream }, | 29 { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream }, |
30 #endif | 30 #endif |
31 { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream }, | 31 { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream }, |
32 { SkGifCodec::IsGif, SkGifCodec::NewFromStream }, | 32 { SkGifCodec::IsGif, SkGifCodec::NewFromStream }, |
33 { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream }, | 33 { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream }, |
34 { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream }, | 34 { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream }, |
35 { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream } | 35 { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream } |
36 }; | 36 }; |
37 | 37 |
38 SkCodec* SkCodec::NewFromStream(SkStream* stream, | 38 SkCodec* SkCodec::NewFromStream(SkStream* stream, |
39 SkPngChunkReader* chunkReader) { | 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 // 14 is enough to read all of the supported types. | |
scroggo
2015/11/25 22:08:35
I've convinced myself that 14 bytes is enough to s
msarett
2015/11/30 14:08:11
This matches my understanding as well. Looks like
scroggo
2015/11/30 20:02:30
Added a test.
| |
47 size_t bytesRead = 14; | |
48 char buffer[bytesRead]; | |
49 if (!stream->peek(buffer, bytesRead)) { | |
50 SkCodecPrintf("Could not peek!\n"); | |
51 // It is possible the stream does not support peeking, but does support | |
52 // rewinding, so try reading. | |
53 // (Is there a valid image that is less than 14 bytes, causing peek to | |
54 // fail, even though we read enough?) | |
msarett
2015/11/30 14:08:11
Good question. I don't know, but I would guess th
scroggo
2015/11/30 20:02:30
Yeah, that should do it:
1 + 1 (headers) + 1 (wid
| |
55 bytesRead = stream->read(buffer, bytesRead); | |
56 if (!stream->rewind()) { | |
57 return nullptr; | |
58 } | |
59 } | |
46 SkAutoTDelete<SkCodec> codec(nullptr); | 60 SkAutoTDelete<SkCodec> codec(nullptr); |
47 // PNG is special, since we want to be able to supply an SkPngChunkReader. | 61 // PNG is special, since we want to be able to supply an SkPngChunkReader. |
48 // But this code follows the same pattern as the loop. | 62 // But this code follows the same pattern as the loop. |
49 const bool isPng = SkPngCodec::IsPng(stream); | 63 if (SkPngCodec::IsPng((const char*)buffer, bytesRead)) { |
50 if (!stream->rewind()) { | |
51 return NULL; | |
52 } | |
53 if (isPng) { | |
54 codec.reset(SkPngCodec::NewFromStream(streamDeleter.detach(), chunkReade r)); | 64 codec.reset(SkPngCodec::NewFromStream(streamDeleter.detach(), chunkReade r)); |
55 } else { | 65 } else { |
56 for (DecoderProc proc : gDecoderProcs) { | 66 for (DecoderProc proc : gDecoderProcs) { |
57 const bool correctFormat = proc.IsFormat(stream); | 67 if (proc.IsFormat(buffer, bytesRead)) { |
58 if (!stream->rewind()) { | |
59 return nullptr; | |
60 } | |
61 if (correctFormat) { | |
62 codec.reset(proc.NewFromStream(streamDeleter.detach())); | 68 codec.reset(proc.NewFromStream(streamDeleter.detach())); |
63 break; | 69 break; |
64 } | 70 } |
65 } | 71 } |
66 } | 72 } |
67 | 73 |
68 // Set the max size at 128 megapixels (512 MB for kN32). | 74 // 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 | 75 // This is about 4x smaller than a test image that takes a few minutes for |
70 // dm to decode and draw. | 76 // dm to decode and draw. |
71 const int32_t maxSize = 1 << 27; | 77 const int32_t maxSize = 1 << 27; |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
352 SkASSERT(1 == linesRequested || this->getInfo().height() == linesReq uested); | 358 SkASSERT(1 == linesRequested || this->getInfo().height() == linesReq uested); |
353 const SkImageInfo fillInfo = info.makeWH(info.width(), 1); | 359 const SkImageInfo fillInfo = info.makeWH(info.width(), 1); |
354 for (int srcY = linesDecoded; srcY < linesRequested; srcY++) { | 360 for (int srcY = linesDecoded; srcY < linesRequested; srcY++) { |
355 fillDst = SkTAddOffset<void>(dst, this->outputScanline(srcY) * r owBytes); | 361 fillDst = SkTAddOffset<void>(dst, this->outputScanline(srcY) * r owBytes); |
356 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, samp ler); | 362 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, samp ler); |
357 } | 363 } |
358 break; | 364 break; |
359 } | 365 } |
360 } | 366 } |
361 } | 367 } |
OLD | NEW |