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

Side by Side Diff: src/images/SkImageDecoder_libwebp.cpp

Issue 22841005: Remove dependency on getLength from webp decoder. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | 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 2010, The Android Open Source Project 2 * Copyright 2010, The Android Open Source Project
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at 6 * You may obtain a copy of the License at
7 * 7 *
8 * http://www.apache.org/licenses/LICENSE-2.0 8 * http://www.apache.org/licenses/LICENSE-2.0
9 * 9 *
10 * Unless required by applicable law or agreed to in writing, software 10 * Unless required by applicable law or agreed to in writing, software
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 53
54 ////////////////////////////////////////////////////////////////////////// 54 //////////////////////////////////////////////////////////////////////////
55 ////////////////////////////////////////////////////////////////////////// 55 //////////////////////////////////////////////////////////////////////////
56 56
57 static const size_t WEBP_VP8_HEADER_SIZE = 64; 57 static const size_t WEBP_VP8_HEADER_SIZE = 64;
58 static const size_t WEBP_IDECODE_BUFFER_SZ = (1 << 16); 58 static const size_t WEBP_IDECODE_BUFFER_SZ = (1 << 16);
59 59
60 // Parse headers of RIFF container, and check for valid Webp (VP8) content. 60 // Parse headers of RIFF container, and check for valid Webp (VP8) content.
61 static bool webp_parse_header(SkStream* stream, int* width, int* height, int* al pha) { 61 static bool webp_parse_header(SkStream* stream, int* width, int* height, int* al pha) {
62 unsigned char buffer[WEBP_VP8_HEADER_SIZE]; 62 unsigned char buffer[WEBP_VP8_HEADER_SIZE];
63 const uint32_t contentSize = stream->getLength(); 63 size_t bytesToRead = WEBP_VP8_HEADER_SIZE;
64 const size_t len = stream->read(buffer, WEBP_VP8_HEADER_SIZE); 64 size_t totalBytesRead = 0;
65 const uint32_t read_bytes = 65 do {
66 (contentSize < WEBP_VP8_HEADER_SIZE) ? contentSize : WEBP_VP8_HEADER _SIZE; 66 unsigned char* dst = buffer + totalBytesRead;
67 if (len != read_bytes) { 67 const size_t bytesRead = stream->read(dst, bytesToRead);
68 return false; // can't read enough 68 if ((size_t)-1 == bytesRead) {
djsollen 2013/08/12 17:54:57 I don't think our stream should ever return -1. I
scroggo 2013/08/12 19:08:10 Done.
69 } 69 // Could not read any bytes, so we have read the entire stream.
70 break;
71 }
72 bytesToRead -= bytesRead;
73 totalBytesRead += bytesRead;
74 SkASSERT(bytesToRead + totalBytesRead == WEBP_VP8_HEADER_SIZE);
75 } while (!stream->isAtEnd() && bytesToRead > 0);
vikasa 2013/08/12 18:25:41 Assumption here is that 'stream->isAtEnd()' works
djsollen 2013/08/12 18:46:52 Yes, As part of this we are also updating the code
70 76
71 WebPBitstreamFeatures features; 77 WebPBitstreamFeatures features;
72 VP8StatusCode status = WebPGetFeatures(buffer, read_bytes, &features); 78 VP8StatusCode status = WebPGetFeatures(buffer, totalBytesRead, &features);
73 if (VP8_STATUS_OK != status) { 79 if (VP8_STATUS_OK != status) {
74 return false; // Invalid WebP file. 80 return false; // Invalid WebP file.
75 } 81 }
76 *width = features.width; 82 *width = features.width;
77 *height = features.height; 83 *height = features.height;
78 *alpha = features.has_alpha; 84 *alpha = features.has_alpha;
79 85
80 // sanity check for image size that's about to be decoded. 86 // sanity check for image size that's about to be decoded.
81 { 87 {
82 Sk64 size; 88 Sk64 size;
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 // Incremental WebP image decoding. Reads input buffer of 64K size iteratively 191 // Incremental WebP image decoding. Reads input buffer of 64K size iteratively
186 // and decodes this block to appropriate color-space as per config object. 192 // and decodes this block to appropriate color-space as per config object.
187 static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) { 193 static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) {
188 WebPIDecoder* idec = WebPIDecode(NULL, 0, config); 194 WebPIDecoder* idec = WebPIDecode(NULL, 0, config);
189 if (NULL == idec) { 195 if (NULL == idec) {
190 WebPFreeDecBuffer(&config->output); 196 WebPFreeDecBuffer(&config->output);
191 return false; 197 return false;
192 } 198 }
193 199
194 stream->rewind(); 200 stream->rewind();
195 const uint32_t contentSize = stream->getLength(); 201 const size_t readBufferSize = stream->hasLength() ?
196 const uint32_t readBufferSize = (contentSize < WEBP_IDECODE_BUFFER_SZ) ? 202 SkTMin(stream->getLength(), WEBP_IDECODE_BUFFER_SZ) : WEBP_IDECODE_B UFFER_SZ;
vikasa 2013/08/12 18:25:41 As mentioned by Craig (w.r.t https://codereview.ch
scroggo 2013/08/12 18:44:57 Right. That is why I only check stream->getLength(
197 contentSize : WEBP_IDECODE_BUFFER_SZ;
198 SkAutoMalloc srcStorage(readBufferSize); 203 SkAutoMalloc srcStorage(readBufferSize);
199 unsigned char* input = (uint8_t*)srcStorage.get(); 204 unsigned char* input = (uint8_t*)srcStorage.get();
200 if (NULL == input) { 205 if (NULL == input) {
201 WebPIDelete(idec); 206 WebPIDelete(idec);
202 WebPFreeDecBuffer(&config->output); 207 WebPFreeDecBuffer(&config->output);
203 return false; 208 return false;
204 } 209 }
205 210
206 bool success = true; 211 bool success = true;
207 VP8StatusCode status = VP8_STATUS_SUSPENDED; 212 VP8StatusCode status = VP8_STATUS_SUSPENDED;
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 return SkImageDecoder::kUnknown_Format; 593 return SkImageDecoder::kUnknown_Format;
589 } 594 }
590 595
591 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { 596 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) {
592 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NUL L; 597 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NUL L;
593 } 598 }
594 599
595 static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libwebp_dfactory); 600 static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libwebp_dfactory);
596 static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_webp ); 601 static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_webp );
597 static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libwebp_efact ory); 602 static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libwebp_efact ory);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698