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