scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkCodec.h" |
| 9 | #include "SkData.h" |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 10 | #include "SkCodec_libbmp.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 11 | #include "SkCodec_libpng.h" |
| 12 | #include "SkStream.h" |
| 13 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 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 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 24 | SkCodec* SkCodec::NewFromStream(SkStream* stream) { |
| 25 | if (!stream) { |
| 26 | return NULL; |
| 27 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 28 | for (uint32_t i = 0; i < SK_ARRAY_COUNT(gDecoderProcs); i++) { |
| 29 | DecoderProc proc = gDecoderProcs[i]; |
| 30 | const bool correctFormat = proc.IsFormat(stream); |
| 31 | if (!stream->rewind()) { |
| 32 | return NULL; |
| 33 | } |
| 34 | if (correctFormat) { |
| 35 | return proc.NewFromStream(stream); |
| 36 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 37 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 38 | return NULL; |
| 39 | } |
| 40 | |
| 41 | SkCodec* SkCodec::NewFromData(SkData* data) { |
| 42 | if (!data) { |
| 43 | return NULL; |
| 44 | } |
| 45 | return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data))); |
| 46 | } |
| 47 | |
| 48 | SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream) |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame^] | 49 | : INHERITED(info) |
| 50 | , fInfo(info) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 51 | , fStream(stream) |
| 52 | , fNeedsRewind(false) |
| 53 | {} |
| 54 | |
| 55 | bool SkCodec::rewindIfNeeded() { |
| 56 | // Store the value of fNeedsRewind so we can update it. Next read will |
| 57 | // require a rewind. |
| 58 | const bool neededRewind = fNeedsRewind; |
| 59 | fNeedsRewind = true; |
| 60 | return !neededRewind || fStream->rewind(); |
| 61 | } |