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" |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 11 | #include "SkCodec_libico.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 12 | #include "SkCodec_libpng.h" |
| 13 | #include "SkStream.h" |
| 14 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 15 | struct DecoderProc { |
| 16 | bool (*IsFormat)(SkStream*); |
| 17 | SkCodec* (*NewFromStream)(SkStream*); |
| 18 | }; |
| 19 | |
| 20 | static const DecoderProc gDecoderProcs[] = { |
| 21 | { SkPngCodec::IsPng, SkPngCodec::NewFromStream }, |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 22 | { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream }, |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 23 | { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream } |
| 24 | }; |
| 25 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 26 | SkCodec* SkCodec::NewFromStream(SkStream* stream) { |
| 27 | if (!stream) { |
| 28 | return NULL; |
| 29 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 30 | for (uint32_t i = 0; i < SK_ARRAY_COUNT(gDecoderProcs); i++) { |
| 31 | DecoderProc proc = gDecoderProcs[i]; |
| 32 | const bool correctFormat = proc.IsFormat(stream); |
| 33 | if (!stream->rewind()) { |
| 34 | return NULL; |
| 35 | } |
| 36 | if (correctFormat) { |
| 37 | return proc.NewFromStream(stream); |
| 38 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 39 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | SkCodec* SkCodec::NewFromData(SkData* data) { |
| 44 | if (!data) { |
| 45 | return NULL; |
| 46 | } |
| 47 | return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data))); |
| 48 | } |
| 49 | |
| 50 | SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream) |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 51 | : INHERITED(info) |
halcanary | b880d7f | 2015-03-26 06:29:03 -0700 | [diff] [blame^] | 52 | #ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 53 | , fInfo(info) |
halcanary | b880d7f | 2015-03-26 06:29:03 -0700 | [diff] [blame^] | 54 | #endif |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 55 | , fStream(stream) |
| 56 | , fNeedsRewind(false) |
| 57 | {} |
| 58 | |
| 59 | bool SkCodec::rewindIfNeeded() { |
| 60 | // Store the value of fNeedsRewind so we can update it. Next read will |
| 61 | // require a rewind. |
| 62 | const bool neededRewind = fNeedsRewind; |
| 63 | fNeedsRewind = true; |
| 64 | return !neededRewind || fStream->rewind(); |
| 65 | } |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 66 | |
| 67 | SkScanlineDecoder* SkCodec::getScanlineDecoder(const SkImageInfo& dstInfo) { |
| 68 | fScanlineDecoder.reset(NULL); |
| 69 | if (!rewindIfNeeded()) { |
| 70 | return NULL; |
| 71 | } |
| 72 | fScanlineDecoder.reset(this->onGetScanlineDecoder(dstInfo)); |
| 73 | return fScanlineDecoder.get(); |
| 74 | } |