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

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

Issue 1830943002: Revert of Delete SkImageDecoder (Closed) Base URL: https://skia.googlesource.com/skia.git@fix-animator
Patch Set: Created 4 years, 9 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
« no previous file with comments | « src/images/SkImageDecoder_libpng.cpp ('k') | src/images/SkImageDecoder_pkm.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 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
11 * distributed under the License is distributed on an "AS IS" BASIS, 11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 #include "SkBitmap.h" 17 #include "SkImageDecoder.h"
18 #include "SkImageEncoder.h" 18 #include "SkImageEncoder.h"
19 #include "SkColorPriv.h" 19 #include "SkColorPriv.h"
20 #include "SkScaledBitmapSampler.h"
20 #include "SkStream.h" 21 #include "SkStream.h"
21 #include "SkTemplates.h" 22 #include "SkTemplates.h"
22 #include "SkUtils.h" 23 #include "SkUtils.h"
23 24
24 // A WebP decoder only, on top of (subset of) libwebp 25 // A WebP decoder only, on top of (subset of) libwebp
25 // For more information on WebP image format, and libwebp library, see: 26 // For more information on WebP image format, and libwebp library, see:
26 // http://code.google.com/speed/webp/ 27 // http://code.google.com/speed/webp/
27 // http://www.webmproject.org/code/#libwebp_webp_image_decoder_library 28 // http://www.webmproject.org/code/#libwebp_webp_image_decoder_library
28 // http://review.webmproject.org/gitweb?p=libwebp.git 29 // http://review.webmproject.org/gitweb?p=libwebp.git
29 30
30 #include <stdio.h> 31 #include <stdio.h>
31 extern "C" { 32 extern "C" {
32 // If moving libwebp out of skia source tree, path for webp headers must be 33 // If moving libwebp out of skia source tree, path for webp headers must be
33 // updated accordingly. Here, we enforce using local copy in webp sub-directory. 34 // updated accordingly. Here, we enforce using local copy in webp sub-directory.
35 #include "webp/decode.h"
34 #include "webp/encode.h" 36 #include "webp/encode.h"
35 } 37 }
36 38
39 // this enables timing code to report milliseconds for a decode
40 //#define TIME_DECODE
41
42 //////////////////////////////////////////////////////////////////////////
43 //////////////////////////////////////////////////////////////////////////
44
45 // Define VP8 I/O on top of Skia stream
46
47 //////////////////////////////////////////////////////////////////////////
48 //////////////////////////////////////////////////////////////////////////
49
50 static const size_t WEBP_VP8_HEADER_SIZE = 64;
51 static const size_t WEBP_IDECODE_BUFFER_SZ = (1 << 16);
52
53 // Parse headers of RIFF container, and check for valid Webp (VP8) content.
54 static bool webp_parse_header(SkStream* stream, int* width, int* height, int* al pha) {
55 unsigned char buffer[WEBP_VP8_HEADER_SIZE];
56 size_t bytesToRead = WEBP_VP8_HEADER_SIZE;
57 size_t totalBytesRead = 0;
58 do {
59 unsigned char* dst = buffer + totalBytesRead;
60 const size_t bytesRead = stream->read(dst, bytesToRead);
61 if (0 == bytesRead) {
62 SkASSERT(stream->isAtEnd());
63 break;
64 }
65 bytesToRead -= bytesRead;
66 totalBytesRead += bytesRead;
67 SkASSERT(bytesToRead + totalBytesRead == WEBP_VP8_HEADER_SIZE);
68 } while (!stream->isAtEnd() && bytesToRead > 0);
69
70 WebPBitstreamFeatures features;
71 VP8StatusCode status = WebPGetFeatures(buffer, totalBytesRead, &features);
72 if (VP8_STATUS_OK != status) {
73 return false; // Invalid WebP file.
74 }
75 *width = features.width;
76 *height = features.height;
77 *alpha = features.has_alpha;
78
79 // sanity check for image size that's about to be decoded.
80 {
81 int64_t size = sk_64_mul(*width, *height);
82 if (!sk_64_isS32(size)) {
83 return false;
84 }
85 // now check that if we are 4-bytes per pixel, we also don't overflow
86 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) {
87 return false;
88 }
89 }
90 return true;
91 }
92
93 class SkWEBPImageDecoder: public SkImageDecoder {
94 public:
95 SkWEBPImageDecoder() {
96 fOrigWidth = 0;
97 fOrigHeight = 0;
98 fHasAlpha = 0;
99 }
100
101 Format getFormat() const override {
102 return kWEBP_Format;
103 }
104
105 protected:
106 Result onDecode(SkStream* stream, SkBitmap* bm, Mode) override;
107
108 private:
109 /**
110 * Called when determining the output config to request to webp.
111 * If the image does not have alpha, there is no need to premultiply.
112 * If the caller wants unpremultiplied colors, that is respected.
113 */
114 bool shouldPremultiply() const {
115 return SkToBool(fHasAlpha) && !this->getRequireUnpremultipliedColors();
116 }
117
118 bool setDecodeConfig(SkBitmap* decodedBitmap, int width, int height);
119
120 SkAutoTDelete<SkStream> fInputStream;
121 int fOrigWidth;
122 int fOrigHeight;
123 int fHasAlpha;
124
125 typedef SkImageDecoder INHERITED;
126 };
127
128 //////////////////////////////////////////////////////////////////////////
129
130 #ifdef TIME_DECODE
131
132 #include "SkTime.h"
133
134 class AutoTimeMillis {
135 public:
136 AutoTimeMillis(const char label[]) :
137 fLabel(label) {
138 if (nullptr == fLabel) {
139 fLabel = "";
140 }
141 fNow = SkTime::GetMSecs();
142 }
143 ~AutoTimeMillis() {
144 SkDebugf("---- Time (ms): %s %d\n", fLabel, SkTime::GetMSecs() - fNow);
145 }
146 private:
147 const char* fLabel;
148 SkMSec fNow;
149 };
150
151 #endif
152
153 ///////////////////////////////////////////////////////////////////////////////
154
155 // This guy exists just to aid in debugging, as it allows debuggers to just
156 // set a break-point in one place to see all error exists.
157 static void print_webp_error(const SkBitmap& bm, const char msg[]) {
158 SkDEBUGF(("libwebp error %s [%d %d]", msg, bm.width(), bm.height()));
159 }
160
161 static SkImageDecoder::Result return_failure(const SkBitmap& bm, const char msg[ ]) {
162 print_webp_error(bm, msg);
163 return SkImageDecoder::kFailure; // must always return kFailure
164 }
165
166 ///////////////////////////////////////////////////////////////////////////////
167
168 static WEBP_CSP_MODE webp_decode_mode(const SkBitmap* decodedBitmap, bool premul tiply) {
169 WEBP_CSP_MODE mode = MODE_LAST;
170 const SkColorType ct = decodedBitmap->colorType();
171
172 if (ct == kN32_SkColorType) {
173 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
174 mode = premultiply ? MODE_bgrA : MODE_BGRA;
175 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
176 mode = premultiply ? MODE_rgbA : MODE_RGBA;
177 #else
178 #error "Skia uses BGRA or RGBA byte order"
179 #endif
180 } else if (ct == kARGB_4444_SkColorType) {
181 mode = premultiply ? MODE_rgbA_4444 : MODE_RGBA_4444;
182 } else if (ct == kRGB_565_SkColorType) {
183 mode = MODE_RGB_565;
184 }
185 SkASSERT(MODE_LAST != mode);
186 return mode;
187 }
188
189 // Incremental WebP image decoding. Reads input buffer of 64K size iteratively
190 // and decodes this block to appropriate color-space as per config object.
191 static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) {
192 WebPIDecoder* idec = WebPIDecode(nullptr, 0, config);
193 if (nullptr == idec) {
194 WebPFreeDecBuffer(&config->output);
195 return false;
196 }
197
198 if (!stream->rewind()) {
199 SkDebugf("Failed to rewind webp stream!");
200 return false;
201 }
202 const size_t readBufferSize = stream->hasLength() ?
203 SkTMin(stream->getLength(), WEBP_IDECODE_BUFFER_SZ) : WEBP_IDECODE_B UFFER_SZ;
204 SkAutoTMalloc<unsigned char> srcStorage(readBufferSize);
205 unsigned char* input = srcStorage.get();
206 if (nullptr == input) {
207 WebPIDelete(idec);
208 WebPFreeDecBuffer(&config->output);
209 return false;
210 }
211
212 bool success = true;
213 VP8StatusCode status = VP8_STATUS_SUSPENDED;
214 do {
215 const size_t bytesRead = stream->read(input, readBufferSize);
216 if (0 == bytesRead) {
217 success = false;
218 break;
219 }
220
221 status = WebPIAppend(idec, input, bytesRead);
222 if (VP8_STATUS_OK != status && VP8_STATUS_SUSPENDED != status) {
223 success = false;
224 break;
225 }
226 } while (VP8_STATUS_OK != status);
227 srcStorage.reset();
228 WebPIDelete(idec);
229 WebPFreeDecBuffer(&config->output);
230
231 return success;
232 }
233
234 static bool webp_get_config_resize(WebPDecoderConfig* config,
235 SkBitmap* decodedBitmap,
236 int width, int height, bool premultiply) {
237 WEBP_CSP_MODE mode = webp_decode_mode(decodedBitmap, premultiply);
238 if (MODE_LAST == mode) {
239 return false;
240 }
241
242 if (0 == WebPInitDecoderConfig(config)) {
243 return false;
244 }
245
246 config->output.colorspace = mode;
247 config->output.u.RGBA.rgba = (uint8_t*)decodedBitmap->getPixels();
248 config->output.u.RGBA.stride = (int) decodedBitmap->rowBytes();
249 config->output.u.RGBA.size = decodedBitmap->getSize();
250 config->output.is_external_memory = 1;
251
252 if (width != decodedBitmap->width() || height != decodedBitmap->height()) {
253 config->options.use_scaling = 1;
254 config->options.scaled_width = decodedBitmap->width();
255 config->options.scaled_height = decodedBitmap->height();
256 }
257
258 return true;
259 }
260
261 bool SkWEBPImageDecoder::setDecodeConfig(SkBitmap* decodedBitmap, int width, int height) {
262 SkColorType colorType = this->getPrefColorType(k32Bit_SrcDepth, SkToBool(fHa sAlpha));
263
264 // YUV converter supports output in RGB565, RGBA4444 and RGBA8888 formats.
265 if (fHasAlpha) {
266 if (colorType != kARGB_4444_SkColorType) {
267 colorType = kN32_SkColorType;
268 }
269 } else {
270 if (colorType != kRGB_565_SkColorType && colorType != kARGB_4444_SkColor Type) {
271 colorType = kN32_SkColorType;
272 }
273 }
274
275 SkAlphaType alphaType = kOpaque_SkAlphaType;
276 if (SkToBool(fHasAlpha)) {
277 if (this->getRequireUnpremultipliedColors()) {
278 alphaType = kUnpremul_SkAlphaType;
279 } else {
280 alphaType = kPremul_SkAlphaType;
281 }
282 }
283 return decodedBitmap->setInfo(SkImageInfo::Make(width, height, colorType, al phaType));
284 }
285
286 SkImageDecoder::Result SkWEBPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap,
287 Mode mode) {
288 #ifdef TIME_DECODE
289 AutoTimeMillis atm("WEBP Decode");
290 #endif
291
292 int origWidth, origHeight, hasAlpha;
293 if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
294 return kFailure;
295 }
296 this->fHasAlpha = hasAlpha;
297
298 const int sampleSize = this->getSampleSize();
299 SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize);
300 if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(),
301 sampler.scaledHeight())) {
302 return kFailure;
303 }
304
305 // If only bounds are requested, done
306 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
307 return kSuccess;
308 }
309
310 if (!this->allocPixelRef(decodedBitmap, nullptr)) {
311 return return_failure(*decodedBitmap, "allocPixelRef");
312 }
313
314 SkAutoLockPixels alp(*decodedBitmap);
315
316 WebPDecoderConfig config;
317 if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight,
318 this->shouldPremultiply())) {
319 return kFailure;
320 }
321
322 // Decode the WebP image data stream using WebP incremental decoding.
323 return webp_idecode(stream, &config) ? kSuccess : kFailure;
324 }
325
326 ///////////////////////////////////////////////////////////////////////////////
327
37 #include "SkUnPreMultiply.h" 328 #include "SkUnPreMultiply.h"
38 329
39 typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width, 330 typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width,
40 const SkPMColor* SK_RESTRICT ctable); 331 const SkPMColor* SK_RESTRICT ctable);
41 332
42 static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width, 333 static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
43 const SkPMColor*) { 334 const SkPMColor*) {
44 const uint32_t* SK_RESTRICT src = (const uint32_t*)in; 335 const uint32_t* SK_RESTRICT src = (const uint32_t*)in;
45 for (int i = 0; i < width; ++i) { 336 for (int i = 0; i < width; ++i) {
46 const uint32_t c = *src++; 337 const uint32_t c = *src++;
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 delete[] rgb; 521 delete[] rgb;
231 522
232 ok = ok && WebPEncode(&webp_config, &pic); 523 ok = ok && WebPEncode(&webp_config, &pic);
233 WebPPictureFree(&pic); 524 WebPPictureFree(&pic);
234 525
235 return ok; 526 return ok;
236 } 527 }
237 528
238 529
239 /////////////////////////////////////////////////////////////////////////////// 530 ///////////////////////////////////////////////////////////////////////////////
531 DEFINE_DECODER_CREATOR(WEBPImageDecoder);
240 DEFINE_ENCODER_CREATOR(WEBPImageEncoder); 532 DEFINE_ENCODER_CREATOR(WEBPImageEncoder);
241 /////////////////////////////////////////////////////////////////////////////// 533 ///////////////////////////////////////////////////////////////////////////////
242 534
535 static SkImageDecoder* sk_libwebp_dfactory(SkStreamRewindable* stream) {
536 int width, height, hasAlpha;
537 if (!webp_parse_header(stream, &width, &height, &hasAlpha)) {
538 return nullptr;
539 }
540
541 // Magic matches, call decoder
542 return new SkWEBPImageDecoder;
543 }
544
545 static SkImageDecoder::Format get_format_webp(SkStreamRewindable* stream) {
546 int width, height, hasAlpha;
547 if (webp_parse_header(stream, &width, &height, &hasAlpha)) {
548 return SkImageDecoder::kWEBP_Format;
549 }
550 return SkImageDecoder::kUnknown_Format;
551 }
552
243 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { 553 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) {
244 return (SkImageEncoder::kWEBP_Type == t) ? new SkWEBPImageEncoder : nullptr; 554 return (SkImageEncoder::kWEBP_Type == t) ? new SkWEBPImageEncoder : nullptr;
245 } 555 }
246 556
557 static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory);
558 static SkImageDecoder_FormatReg gFormatReg(get_format_webp);
247 static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory); 559 static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory);
OLDNEW
« no previous file with comments | « src/images/SkImageDecoder_libpng.cpp ('k') | src/images/SkImageDecoder_pkm.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698