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

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

Issue 1472123002: Make SkCodec support peek() and read() (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase 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
« no previous file with comments | « src/codec/SkWebpCodec.h ('k') | tests/CodexTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "SkCodecPriv.h" 8 #include "SkCodecPriv.h"
9 #include "SkWebpCodec.h" 9 #include "SkWebpCodec.h"
10 #include "SkTemplates.h" 10 #include "SkTemplates.h"
11 11
12 // A WebP decoder on top of (subset of) libwebp 12 // A WebP decoder on top of (subset of) libwebp
13 // For more information on WebP image format, and libwebp library, see: 13 // For more information on WebP image format, and libwebp library, see:
14 // https://code.google.com/speed/webp/ 14 // https://code.google.com/speed/webp/
15 // http://www.webmproject.org/code/#libwebp-webp-image-library 15 // http://www.webmproject.org/code/#libwebp-webp-image-library
16 // https://chromium.googlesource.com/webm/libwebp 16 // https://chromium.googlesource.com/webm/libwebp
17 17
18 // If moving libwebp out of skia source tree, path for webp headers must be 18 // If moving libwebp out of skia source tree, path for webp headers must be
19 // updated accordingly. Here, we enforce using local copy in webp sub-directory. 19 // updated accordingly. Here, we enforce using local copy in webp sub-directory.
20 #include "webp/decode.h" 20 #include "webp/decode.h"
21 #include "webp/encode.h" 21 #include "webp/encode.h"
22 22
23 bool SkWebpCodec::IsWebp(SkStream* stream) { 23 bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) {
24 // WEBP starts with the following: 24 // WEBP starts with the following:
25 // RIFFXXXXWEBPVP 25 // RIFFXXXXWEBPVP
26 // Where XXXX is unspecified. 26 // Where XXXX is unspecified.
27 const char LENGTH = 14; 27 const char* bytes = static_cast<const char*>(buf);
28 char bytes[LENGTH]; 28 return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "W EBPVP", 6);
29 if (stream->read(&bytes, LENGTH) != LENGTH) {
30 return false;
31 }
32 return !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6);
33 } 29 }
34 30
35 static const size_t WEBP_VP8_HEADER_SIZE = 30;
36
37 // Parse headers of RIFF container, and check for valid Webp (VP8) content. 31 // Parse headers of RIFF container, and check for valid Webp (VP8) content.
38 // NOTE: This calls peek instead of read, since onGetPixels will need these 32 // NOTE: This calls peek instead of read, since onGetPixels will need these
39 // bytes again. 33 // bytes again.
40 static bool webp_parse_header(SkStream* stream, SkImageInfo* info) { 34 static bool webp_parse_header(SkStream* stream, SkImageInfo* info) {
41 unsigned char buffer[WEBP_VP8_HEADER_SIZE]; 35 unsigned char buffer[WEBP_VP8_HEADER_SIZE];
42 if (!stream->peek(buffer, WEBP_VP8_HEADER_SIZE)) { 36 SkASSERT(WEBP_VP8_HEADER_SIZE <= SkCodec::MinBufferedBytesNeeded());
37
38 const size_t bytesPeeked = stream->peek(buffer, WEBP_VP8_HEADER_SIZE);
39 if (bytesPeeked != WEBP_VP8_HEADER_SIZE) {
40 // Use read + rewind as a backup
41 if (stream->read(buffer, WEBP_VP8_HEADER_SIZE) != WEBP_VP8_HEADER_SIZE
42 || !stream->rewind())
43 return false; 43 return false;
44 } 44 }
45 45
46 WebPBitstreamFeatures features; 46 WebPBitstreamFeatures features;
47 VP8StatusCode status = WebPGetFeatures(buffer, WEBP_VP8_HEADER_SIZE, &featur es); 47 VP8StatusCode status = WebPGetFeatures(buffer, WEBP_VP8_HEADER_SIZE, &featur es);
48 if (VP8_STATUS_OK != status) { 48 if (VP8_STATUS_OK != status) {
49 return false; // Invalid WebP file. 49 return false; // Invalid WebP file.
50 } 50 }
51 51
52 // sanity check for image size that's about to be decoded. 52 // sanity check for image size that's about to be decoded.
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 // Break out of the switch statement. Continue the loop. 246 // Break out of the switch statement. Continue the loop.
247 break; 247 break;
248 default: 248 default:
249 return kInvalidInput; 249 return kInvalidInput;
250 } 250 }
251 } 251 }
252 } 252 }
253 253
254 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) 254 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream)
255 : INHERITED(info, stream) {} 255 : INHERITED(info, stream) {}
OLDNEW
« no previous file with comments | « src/codec/SkWebpCodec.h ('k') | tests/CodexTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698