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

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

Issue 1472123002: Make SkCodec support peek() and read() (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Add comments, test, and accessor for needed buffer bytes Created 5 years 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 "SkBmpCodec.h" 8 #include "SkBmpCodec.h"
9 #include "SkCodec_libico.h" 9 #include "SkCodec_libico.h"
10 #include "SkCodec_libpng.h" 10 #include "SkCodec_libpng.h"
11 #include "SkCodecPriv.h" 11 #include "SkCodecPriv.h"
12 #include "SkColorPriv.h" 12 #include "SkColorPriv.h"
13 #include "SkData.h" 13 #include "SkData.h"
14 #include "SkStream.h" 14 #include "SkStream.h"
15 #include "SkTDArray.h" 15 #include "SkTDArray.h"
16 #include "SkTSort.h" 16 #include "SkTSort.h"
17 17
18 /* 18 /*
19 * Checks the start of the stream to see if the image is an Ico or Cur 19 * Checks the start of the stream to see if the image is an Ico or Cur
20 */ 20 */
21 bool SkIcoCodec::IsIco(SkStream* stream) { 21 bool SkIcoCodec::IsIco(const char* buffer, size_t bytesRead) {
22 const char icoSig[] = { '\x00', '\x00', '\x01', '\x00' }; 22 const char icoSig[] = { '\x00', '\x00', '\x01', '\x00' };
23 const char curSig[] = { '\x00', '\x00', '\x02', '\x00' }; 23 const char curSig[] = { '\x00', '\x00', '\x02', '\x00' };
24 char buffer[sizeof(icoSig)]; 24 return bytesRead >= sizeof(icoSig) &&
25 return stream->read(buffer, sizeof(icoSig)) == sizeof(icoSig) &&
26 (!memcmp(buffer, icoSig, sizeof(icoSig)) || 25 (!memcmp(buffer, icoSig, sizeof(icoSig)) ||
27 !memcmp(buffer, curSig, sizeof(curSig))); 26 !memcmp(buffer, curSig, sizeof(curSig)));
28 } 27 }
29 28
30 /* 29 /*
31 * Assumes IsIco was called and returned true 30 * Assumes IsIco was called and returned true
32 * Creates an Ico decoder 31 * Creates an Ico decoder
33 * Reads enough of the stream to determine the image format 32 * Reads enough of the stream to determine the image format
34 */ 33 */
35 SkCodec* SkIcoCodec::NewFromStream(SkStream* stream) { 34 SkCodec* SkIcoCodec::NewFromStream(SkStream* stream) {
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 SkAutoTUnref<SkData> data( 131 SkAutoTUnref<SkData> data(
133 SkData::NewFromStream(inputStream.get(), size)); 132 SkData::NewFromStream(inputStream.get(), size));
134 if (nullptr == data.get()) { 133 if (nullptr == data.get()) {
135 SkCodecPrintf("Warning: could not create embedded stream.\n"); 134 SkCodecPrintf("Warning: could not create embedded stream.\n");
136 break; 135 break;
137 } 136 }
138 SkAutoTDelete<SkMemoryStream> embeddedStream(new SkMemoryStream(data.get ())); 137 SkAutoTDelete<SkMemoryStream> embeddedStream(new SkMemoryStream(data.get ()));
139 bytesRead += size; 138 bytesRead += size;
140 139
141 // Check if the embedded codec is bmp or png and create the codec 140 // Check if the embedded codec is bmp or png and create the codec
142 const bool isPng = SkPngCodec::IsPng(embeddedStream);
143 SkAssertResult(embeddedStream->rewind());
144 SkCodec* codec = nullptr; 141 SkCodec* codec = nullptr;
145 if (isPng) { 142 if (SkPngCodec::IsPng((const char*) data->bytes(), data->size())) {
146 codec = SkPngCodec::NewFromStream(embeddedStream.detach()); 143 codec = SkPngCodec::NewFromStream(embeddedStream.detach());
147 } else { 144 } else {
148 codec = SkBmpCodec::NewFromIco(embeddedStream.detach()); 145 codec = SkBmpCodec::NewFromIco(embeddedStream.detach());
149 } 146 }
150 147
151 // Save a valid codec 148 // Save a valid codec
152 if (nullptr != codec) { 149 if (nullptr != codec) {
153 codecs->push_back().reset(codec); 150 codecs->push_back().reset(codec);
154 } 151 }
155 } 152 }
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 } 290 }
294 291
295 // On success or partial success, return the result 292 // On success or partial success, return the result
296 return result; 293 return result;
297 } 294 }
298 } 295 }
299 296
300 SkCodecPrintf("Error: No matching candidate image in ico.\n"); 297 SkCodecPrintf("Error: No matching candidate image in ico.\n");
301 return result; 298 return result;
302 } 299 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698