OLD | NEW |
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 Loading... |
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 (0 == bytesRead) { |
69 } | 69 // Could not read any bytes. Check to see if we are at the end (exit |
| 70 // condition), and continue reading if not. Important for streams |
| 71 // that do not have all the data ready. |
| 72 continue; |
| 73 } |
| 74 bytesToRead -= bytesRead; |
| 75 totalBytesRead += bytesRead; |
| 76 SkASSERT(bytesToRead + totalBytesRead == WEBP_VP8_HEADER_SIZE); |
| 77 } while (!stream->isAtEnd() && bytesToRead > 0); |
70 | 78 |
71 WebPBitstreamFeatures features; | 79 WebPBitstreamFeatures features; |
72 VP8StatusCode status = WebPGetFeatures(buffer, read_bytes, &features); | 80 VP8StatusCode status = WebPGetFeatures(buffer, totalBytesRead, &features); |
73 if (VP8_STATUS_OK != status) { | 81 if (VP8_STATUS_OK != status) { |
74 return false; // Invalid WebP file. | 82 return false; // Invalid WebP file. |
75 } | 83 } |
76 *width = features.width; | 84 *width = features.width; |
77 *height = features.height; | 85 *height = features.height; |
78 *alpha = features.has_alpha; | 86 *alpha = features.has_alpha; |
79 | 87 |
80 // sanity check for image size that's about to be decoded. | 88 // sanity check for image size that's about to be decoded. |
81 { | 89 { |
82 Sk64 size; | 90 Sk64 size; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 // Incremental WebP image decoding. Reads input buffer of 64K size iteratively | 193 // Incremental WebP image decoding. Reads input buffer of 64K size iteratively |
186 // and decodes this block to appropriate color-space as per config object. | 194 // and decodes this block to appropriate color-space as per config object. |
187 static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) { | 195 static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) { |
188 WebPIDecoder* idec = WebPIDecode(NULL, 0, config); | 196 WebPIDecoder* idec = WebPIDecode(NULL, 0, config); |
189 if (NULL == idec) { | 197 if (NULL == idec) { |
190 WebPFreeDecBuffer(&config->output); | 198 WebPFreeDecBuffer(&config->output); |
191 return false; | 199 return false; |
192 } | 200 } |
193 | 201 |
194 stream->rewind(); | 202 stream->rewind(); |
195 const uint32_t contentSize = stream->getLength(); | 203 const size_t readBufferSize = stream->hasLength() ? |
196 const uint32_t readBufferSize = (contentSize < WEBP_IDECODE_BUFFER_SZ) ? | 204 SkTMin(stream->getLength(), WEBP_IDECODE_BUFFER_SZ) : WEBP_IDECODE_B
UFFER_SZ; |
197 contentSize : WEBP_IDECODE_BUFFER_SZ; | |
198 SkAutoMalloc srcStorage(readBufferSize); | 205 SkAutoMalloc srcStorage(readBufferSize); |
199 unsigned char* input = (uint8_t*)srcStorage.get(); | 206 unsigned char* input = (uint8_t*)srcStorage.get(); |
200 if (NULL == input) { | 207 if (NULL == input) { |
201 WebPIDelete(idec); | 208 WebPIDelete(idec); |
202 WebPFreeDecBuffer(&config->output); | 209 WebPFreeDecBuffer(&config->output); |
203 return false; | 210 return false; |
204 } | 211 } |
205 | 212 |
206 bool success = true; | 213 bool success = true; |
207 VP8StatusCode status = VP8_STATUS_SUSPENDED; | 214 VP8StatusCode status = VP8_STATUS_SUSPENDED; |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
588 return SkImageDecoder::kUnknown_Format; | 595 return SkImageDecoder::kUnknown_Format; |
589 } | 596 } |
590 | 597 |
591 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { | 598 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { |
592 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NUL
L; | 599 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NUL
L; |
593 } | 600 } |
594 | 601 |
595 static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libwebp_dfactory); | 602 static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libwebp_dfactory); |
596 static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_webp
); | 603 static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_webp
); |
597 static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libwebp_efact
ory); | 604 static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libwebp_efact
ory); |
OLD | NEW |