Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: src/codec/SkCodec.cpp

Issue 1040453002: Add SkPngChunkReader. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Peeker -> SkChunkReader, passed to constructor. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_libbmp.h"
11 #include "SkCodec_libico.h" 11 #include "SkCodec_libico.h"
12 #include "SkCodec_libpng.h" 12 #include "SkCodec_libpng.h"
13 #include "SkStream.h" 13 #include "SkStream.h"
14 14
15 struct DecoderProc { 15 struct DecoderProc {
16 bool (*IsFormat)(SkStream*); 16 bool (*IsFormat)(SkStream*);
17 SkCodec* (*NewFromStream)(SkStream*); 17 SkCodec* (*NewFromStream)(SkStream*);
18 }; 18 };
19 19
20 static const DecoderProc gDecoderProcs[] = { 20 static const DecoderProc gDecoderProcs[] = {
21 { SkPngCodec::IsPng, SkPngCodec::NewFromStream },
22 { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream }, 21 { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream },
23 { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream } 22 { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream }
24 }; 23 };
25 24
26 SkCodec* SkCodec::NewFromStream(SkStream* stream) { 25 SkCodec* SkCodec::NewFromStream(SkStream* stream, SkChunkReader* chunkReader) {
27 if (!stream) { 26 if (!stream) {
28 return NULL; 27 return NULL;
29 } 28 }
29
30 // PNG is special, since we want to be able to supply an SkChunkReader.
31 // But this code follows the same pattern as the loop.
32 const bool isPng = SkPngCodec::IsPng(stream);
33 if (!stream->rewind()) {
34 return NULL;
35 }
36 if (isPng) {
37 return SkPngCodec::NewFromStream(stream, chunkReader);
38 }
39
30 for (uint32_t i = 0; i < SK_ARRAY_COUNT(gDecoderProcs); i++) { 40 for (uint32_t i = 0; i < SK_ARRAY_COUNT(gDecoderProcs); i++) {
31 DecoderProc proc = gDecoderProcs[i]; 41 DecoderProc proc = gDecoderProcs[i];
32 const bool correctFormat = proc.IsFormat(stream); 42 const bool correctFormat = proc.IsFormat(stream);
33 if (!stream->rewind()) { 43 if (!stream->rewind()) {
34 return NULL; 44 return NULL;
35 } 45 }
36 if (correctFormat) { 46 if (correctFormat) {
37 return proc.NewFromStream(stream); 47 return proc.NewFromStream(stream);
38 } 48 }
39 } 49 }
40 return NULL; 50 return NULL;
41 } 51 }
42 52
43 SkCodec* SkCodec::NewFromData(SkData* data) { 53 SkCodec* SkCodec::NewFromData(SkData* data, SkChunkReader* reader) {
44 if (!data) { 54 if (!data) {
45 return NULL; 55 return NULL;
46 } 56 }
47 return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data))); 57 return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data)), reader);
48 } 58 }
49 59
50 SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream) 60 SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream)
51 : INHERITED(info) 61 : INHERITED(info)
52 #ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO 62 #ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
53 , fInfo(info) 63 , fInfo(info)
54 #endif 64 #endif
55 , fStream(stream) 65 , fStream(stream)
56 , fNeedsRewind(false) 66 , fNeedsRewind(false)
57 {} 67 {}
58 68
59 bool SkCodec::rewindIfNeeded() { 69 bool SkCodec::rewindIfNeeded() {
60 // Store the value of fNeedsRewind so we can update it. Next read will 70 // Store the value of fNeedsRewind so we can update it. Next read will
61 // require a rewind. 71 // require a rewind.
62 const bool neededRewind = fNeedsRewind; 72 const bool neededRewind = fNeedsRewind;
63 fNeedsRewind = true; 73 fNeedsRewind = true;
64 return !neededRewind || fStream->rewind(); 74 return !neededRewind || fStream->rewind();
65 } 75 }
66 76
67 SkScanlineDecoder* SkCodec::getScanlineDecoder(const SkImageInfo& dstInfo) { 77 SkScanlineDecoder* SkCodec::getScanlineDecoder(const SkImageInfo& dstInfo) {
68 fScanlineDecoder.reset(NULL); 78 fScanlineDecoder.reset(NULL);
69 if (!rewindIfNeeded()) { 79 if (!rewindIfNeeded()) {
70 return NULL; 80 return NULL;
71 } 81 }
72 fScanlineDecoder.reset(this->onGetScanlineDecoder(dstInfo)); 82 fScanlineDecoder.reset(this->onGetScanlineDecoder(dstInfo));
73 return fScanlineDecoder.get(); 83 return fScanlineDecoder.get();
74 } 84 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698