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 "SkData.h" | 9 #include "SkData.h" |
10 #include "SkCodec_libbmp.h" | |
11 #include "SkCodec_libpng.h" | 10 #include "SkCodec_libpng.h" |
12 #include "SkStream.h" | 11 #include "SkStream.h" |
13 | 12 |
14 struct DecoderProc { | |
15 bool (*IsFormat)(SkStream*); | |
16 SkCodec* (*NewFromStream)(SkStream*); | |
17 }; | |
18 | |
19 static const uint32_t gNumDecoderProcs = 2; | |
20 | |
21 static const DecoderProc gDecoderProcs[] = { | |
22 { SkPngCodec::IsPng, SkPngCodec::NewFromStream }, | |
23 { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream } | |
24 }; | |
25 | |
26 SkCodec* SkCodec::NewFromStream(SkStream* stream) { | 13 SkCodec* SkCodec::NewFromStream(SkStream* stream) { |
27 if (!stream) { | 14 if (!stream) { |
28 return NULL; | 15 return NULL; |
29 } | 16 } |
30 for (uint32_t i = 0; i < gNumDecoderProcs; i++) { | 17 SkAutoTDelete<SkStream> streamDeleter(stream); |
31 DecoderProc proc = gDecoderProcs[i]; | 18 const bool isPng = SkPngCodec::IsPng(stream); |
32 const bool correctFormat = proc.IsFormat(stream); | 19 // TODO: Avoid rewinding. |
33 if (!stream->rewind()) { | 20 if (!stream->rewind()) { |
34 return NULL; | 21 return NULL; |
35 } | |
36 if (correctFormat) { | |
37 return proc.NewFromStream(stream); | |
38 } | |
39 } | 22 } |
| 23 if (isPng) { |
| 24 streamDeleter.detach(); |
| 25 return SkPngCodec::NewFromStream(stream); |
| 26 } |
| 27 // TODO: Check other image types. |
40 return NULL; | 28 return NULL; |
41 } | 29 } |
42 | 30 |
43 SkCodec* SkCodec::NewFromData(SkData* data) { | 31 SkCodec* SkCodec::NewFromData(SkData* data) { |
44 if (!data) { | 32 if (!data) { |
45 return NULL; | 33 return NULL; |
46 } | 34 } |
47 return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data))); | 35 return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data))); |
48 } | 36 } |
49 | 37 |
50 SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream) | 38 SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream) |
51 : fInfo(info) | 39 : fInfo(info) |
52 , fStream(stream) | 40 , fStream(stream) |
53 , fNeedsRewind(false) | 41 , fNeedsRewind(false) |
54 {} | 42 {} |
55 | 43 |
56 bool SkCodec::rewindIfNeeded() { | 44 bool SkCodec::rewindIfNeeded() { |
57 // Store the value of fNeedsRewind so we can update it. Next read will | 45 // Store the value of fNeedsRewind so we can update it. Next read will |
58 // require a rewind. | 46 // require a rewind. |
59 const bool neededRewind = fNeedsRewind; | 47 const bool neededRewind = fNeedsRewind; |
60 fNeedsRewind = true; | 48 fNeedsRewind = true; |
61 return !neededRewind || fStream->rewind(); | 49 return !neededRewind || fStream->rewind(); |
62 } | 50 } |
OLD | NEW |