| 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 (DecoderProc proc : gDecoderProcs) { |
| 18 const bool isPng = SkPngCodec::IsPng(stream); | 29 const bool correctFormat = proc.IsFormat(stream); |
| 19 if (!stream->rewind()) { | 30 if (!stream->rewind()) { |
| 20 return NULL; | 31 return NULL; |
| 32 } |
| 33 if (correctFormat) { |
| 34 return proc.NewFromStream(stream); |
| 35 } |
| 21 } | 36 } |
| 22 if (isPng) { | |
| 23 streamDeleter.detach(); | |
| 24 return SkPngCodec::NewFromStream(stream); | |
| 25 } | |
| 26 // TODO: Check other image types. | |
| 27 return NULL; | 37 return NULL; |
| 28 } | 38 } |
| 29 | 39 |
| 30 SkCodec* SkCodec::NewFromData(SkData* data) { | 40 SkCodec* SkCodec::NewFromData(SkData* data) { |
| 31 if (!data) { | 41 if (!data) { |
| 32 return NULL; | 42 return NULL; |
| 33 } | 43 } |
| 34 return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data))); | 44 return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data))); |
| 35 } | 45 } |
| 36 | 46 |
| 37 SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream) | 47 SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream) |
| 38 : fInfo(info) | 48 : fInfo(info) |
| 39 , fStream(stream) | 49 , fStream(stream) |
| 40 , fNeedsRewind(false) | 50 , fNeedsRewind(false) |
| 41 {} | 51 {} |
| 42 | 52 |
| 43 bool SkCodec::couldRewindIfNeeded() { | 53 bool SkCodec::couldRewindIfNeeded() { |
| 44 // Store the value of fNeedsRewind so we can update it. Next read will | 54 // Store the value of fNeedsRewind so we can update it. Next read will |
| 45 // require a rewind. | 55 // require a rewind. |
| 46 const bool neededRewind = fNeedsRewind; | 56 const bool neededRewind = fNeedsRewind; |
| 47 fNeedsRewind = true; | 57 fNeedsRewind = true; |
| 48 return !neededRewind || fStream->rewind(); | 58 return !neededRewind || fStream->rewind(); |
| 49 } | 59 } |
| OLD | NEW |