blob: b0fa7f7053d36d7050dbbd3fd407fa1329bead06 [file] [log] [blame]
commit-bot@chromium.orga936e372013-03-14 14:42:18 +00001/*
2 * Copyright 2010, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "SkImageDecoder.h"
18#include "SkImageEncoder.h"
19#include "SkColorPriv.h"
20#include "SkScaledBitmapSampler.h"
21#include "SkStream.h"
22#include "SkTemplates.h"
23#include "SkUtils.h"
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000024
25// A WebP decoder only, on top of (subset of) libwebp
26// For more information on WebP image format, and libwebp library, see:
27// http://code.google.com/speed/webp/
28// http://www.webmproject.org/code/#libwebp_webp_image_decoder_library
29// http://review.webmproject.org/gitweb?p=libwebp.git
30
31#include <stdio.h>
32extern "C" {
33// If moving libwebp out of skia source tree, path for webp headers must be
34// updated accordingly. Here, we enforce using local copy in webp sub-directory.
35#include "webp/decode.h"
36#include "webp/encode.h"
37}
38
39#ifdef ANDROID
40#include <cutils/properties.h>
41
42// Key to lookup the size of memory buffer set in system property
43static const char KEY_MEM_CAP[] = "ro.media.dec.webp.memcap";
44#endif
45
46// this enables timing code to report milliseconds for a decode
47//#define TIME_DECODE
48
49//////////////////////////////////////////////////////////////////////////
50//////////////////////////////////////////////////////////////////////////
51
52// Define VP8 I/O on top of Skia stream
53
54//////////////////////////////////////////////////////////////////////////
55//////////////////////////////////////////////////////////////////////////
56
57static const size_t WEBP_VP8_HEADER_SIZE = 64;
58static const size_t WEBP_IDECODE_BUFFER_SZ = (1 << 16);
59
60// Parse headers of RIFF container, and check for valid Webp (VP8) content.
61static bool webp_parse_header(SkStream* stream, int* width, int* height, int* alpha) {
62 unsigned char buffer[WEBP_VP8_HEADER_SIZE];
63 const uint32_t contentSize = stream->getLength();
64 const size_t len = stream->read(buffer, WEBP_VP8_HEADER_SIZE);
65 const uint32_t read_bytes =
66 (contentSize < WEBP_VP8_HEADER_SIZE) ? contentSize : WEBP_VP8_HEADER_SIZE;
67 if (len != read_bytes) {
68 return false; // can't read enough
69 }
70
71 WebPBitstreamFeatures features;
72 VP8StatusCode status = WebPGetFeatures(buffer, read_bytes, &features);
73 if (VP8_STATUS_OK != status) {
74 return false; // Invalid WebP file.
75 }
76 *width = features.width;
77 *height = features.height;
78 *alpha = features.has_alpha;
79
80 // sanity check for image size that's about to be decoded.
81 {
82 Sk64 size;
83 size.setMul(*width, *height);
84 if (size.isNeg() || !size.is32()) {
85 return false;
86 }
87 // now check that if we are 4-bytes per pixel, we also don't overflow
88 if (size.get32() > (0x7FFFFFFF >> 2)) {
89 return false;
90 }
91 }
92 return true;
93}
94
95class SkWEBPImageDecoder: public SkImageDecoder {
96public:
97 SkWEBPImageDecoder() {
98 fInputStream = NULL;
99 fOrigWidth = 0;
100 fOrigHeight = 0;
101 fHasAlpha = 0;
102 }
103 virtual ~SkWEBPImageDecoder() {
104 SkSafeUnref(fInputStream);
105 }
106
107 virtual Format getFormat() const SK_OVERRIDE {
108 return kWEBP_Format;
109 }
110
111protected:
112 virtual bool onBuildTileIndex(SkStream *stream, int *width, int *height) SK_OVERRIDE;
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000113 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRIDE;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000114 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
115
116private:
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000117 /**
118 * Called when determining the output config to request to webp.
119 * If the image does not have alpha, there is no need to premultiply.
120 * If the caller wants unpremultiplied colors, that is respected.
121 */
122 bool shouldPremultiply() const {
123 return SkToBool(fHasAlpha) && !this->getRequireUnpremultipliedColors();
124 }
125
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000126 bool setDecodeConfig(SkBitmap* decodedBitmap, int width, int height);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000127
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000128 SkStream* fInputStream;
129 int fOrigWidth;
130 int fOrigHeight;
131 int fHasAlpha;
132
133 typedef SkImageDecoder INHERITED;
134};
135
136//////////////////////////////////////////////////////////////////////////
137
138#ifdef TIME_DECODE
139
140#include "SkTime.h"
141
142class AutoTimeMillis {
143public:
144 AutoTimeMillis(const char label[]) :
145 fLabel(label) {
146 if (NULL == fLabel) {
147 fLabel = "";
148 }
149 fNow = SkTime::GetMSecs();
150 }
151 ~AutoTimeMillis() {
152 SkDebugf("---- Time (ms): %s %d\n", fLabel, SkTime::GetMSecs() - fNow);
153 }
154private:
155 const char* fLabel;
156 SkMSec fNow;
157};
158
159#endif
160
161///////////////////////////////////////////////////////////////////////////////
162
163// This guy exists just to aid in debugging, as it allows debuggers to just
164// set a break-point in one place to see all error exists.
165static bool return_false(const SkBitmap& bm, const char msg[]) {
166 SkDEBUGF(("libwebp error %s [%d %d]", msg, bm.width(), bm.height()));
167 return false; // must always return false
168}
169
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000170static WEBP_CSP_MODE webp_decode_mode(const SkBitmap* decodedBitmap, bool premultiply) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000171 WEBP_CSP_MODE mode = MODE_LAST;
172 SkBitmap::Config config = decodedBitmap->config();
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000173
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000174 if (config == SkBitmap::kARGB_8888_Config) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000175 mode = premultiply ? MODE_rgbA : MODE_RGBA;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000176 } else if (config == SkBitmap::kARGB_4444_Config) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000177 mode = premultiply ? MODE_rgbA_4444 : MODE_RGBA_4444;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000178 } else if (config == SkBitmap::kRGB_565_Config) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000179 mode = MODE_RGB_565;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000180 }
181 SkASSERT(MODE_LAST != mode);
182 return mode;
183}
184
185// Incremental WebP image decoding. Reads input buffer of 64K size iteratively
186// and decodes this block to appropriate color-space as per config object.
187static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) {
188 WebPIDecoder* idec = WebPIDecode(NULL, 0, config);
189 if (NULL == idec) {
190 WebPFreeDecBuffer(&config->output);
191 return false;
192 }
193
194 stream->rewind();
195 const uint32_t contentSize = stream->getLength();
196 const uint32_t readBufferSize = (contentSize < WEBP_IDECODE_BUFFER_SZ) ?
197 contentSize : WEBP_IDECODE_BUFFER_SZ;
198 SkAutoMalloc srcStorage(readBufferSize);
199 unsigned char* input = (uint8_t*)srcStorage.get();
200 if (NULL == input) {
201 WebPIDelete(idec);
202 WebPFreeDecBuffer(&config->output);
203 return false;
204 }
205
206 uint32_t bytesRemaining = contentSize;
207 while (bytesRemaining > 0) {
208 const uint32_t bytesToRead = (bytesRemaining < WEBP_IDECODE_BUFFER_SZ) ?
209 bytesRemaining : WEBP_IDECODE_BUFFER_SZ;
210 const size_t bytesRead = stream->read(input, bytesToRead);
211 if (0 == bytesRead) {
212 break;
213 }
214
215 VP8StatusCode status = WebPIAppend(idec, input, bytesRead);
216 if (VP8_STATUS_OK == status || VP8_STATUS_SUSPENDED == status) {
217 bytesRemaining -= bytesRead;
218 } else {
219 break;
220 }
221 }
222 srcStorage.free();
223 WebPIDelete(idec);
224 WebPFreeDecBuffer(&config->output);
225
226 if (bytesRemaining > 0) {
227 return false;
228 } else {
229 return true;
230 }
231}
232
233static bool webp_get_config_resize(WebPDecoderConfig* config,
234 SkBitmap* decodedBitmap,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000235 int width, int height, bool premultiply) {
236 WEBP_CSP_MODE mode = webp_decode_mode(decodedBitmap, premultiply);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000237 if (MODE_LAST == mode) {
238 return false;
239 }
240
241 if (0 == WebPInitDecoderConfig(config)) {
242 return false;
243 }
244
245 config->output.colorspace = mode;
246 config->output.u.RGBA.rgba = (uint8_t*)decodedBitmap->getPixels();
247 config->output.u.RGBA.stride = decodedBitmap->rowBytes();
248 config->output.u.RGBA.size = decodedBitmap->getSize();
249 config->output.is_external_memory = 1;
250
251 if (width != decodedBitmap->width() || height != decodedBitmap->height()) {
252 config->options.use_scaling = 1;
253 config->options.scaled_width = decodedBitmap->width();
254 config->options.scaled_height = decodedBitmap->height();
255 }
256
257 return true;
258}
259
260static bool webp_get_config_resize_crop(WebPDecoderConfig* config,
261 SkBitmap* decodedBitmap,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000262 const SkIRect& region, bool premultiply) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000263
264 if (!webp_get_config_resize(config, decodedBitmap, region.width(),
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000265 region.height(), premultiply)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000266 return false;
267 }
268
269 config->options.use_cropping = 1;
270 config->options.crop_left = region.fLeft;
271 config->options.crop_top = region.fTop;
272 config->options.crop_width = region.width();
273 config->options.crop_height = region.height();
274
275 return true;
276}
277
278bool SkWEBPImageDecoder::setDecodeConfig(SkBitmap* decodedBitmap,
279 int width, int height) {
commit-bot@chromium.orgbff83f62013-03-14 15:18:08 +0000280 SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, SkToBool(fHasAlpha));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000281
282 // YUV converter supports output in RGB565, RGBA4444 and RGBA8888 formats.
283 if (fHasAlpha) {
284 if (config != SkBitmap::kARGB_4444_Config) {
285 config = SkBitmap::kARGB_8888_Config;
286 }
287 } else {
288 if (config != SkBitmap::kRGB_565_Config &&
289 config != SkBitmap::kARGB_4444_Config) {
290 config = SkBitmap::kARGB_8888_Config;
291 }
292 }
293
294 if (!this->chooseFromOneChoice(config, width, height)) {
295 return false;
296 }
297
298 decodedBitmap->setConfig(config, width, height, 0);
299
300 decodedBitmap->setIsOpaque(!fHasAlpha);
301
302 return true;
303}
304
305bool SkWEBPImageDecoder::onBuildTileIndex(SkStream* stream,
306 int *width, int *height) {
307 int origWidth, origHeight, hasAlpha;
308 if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
309 return false;
310 }
311
312 stream->rewind();
313 *width = origWidth;
314 *height = origHeight;
315
316 SkRefCnt_SafeAssign(this->fInputStream, stream);
317 this->fOrigWidth = origWidth;
318 this->fOrigHeight = origHeight;
319 this->fHasAlpha = hasAlpha;
320
321 return true;
322}
323
324static bool is_config_compatible(const SkBitmap& bitmap) {
325 SkBitmap::Config config = bitmap.config();
326 return config == SkBitmap::kARGB_4444_Config ||
327 config == SkBitmap::kRGB_565_Config ||
328 config == SkBitmap::kARGB_8888_Config;
329}
330
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000331bool SkWEBPImageDecoder::onDecodeSubset(SkBitmap* decodedBitmap,
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000332 const SkIRect& region) {
333 SkIRect rect = SkIRect::MakeWH(fOrigWidth, fOrigHeight);
334
335 if (!rect.intersect(region)) {
336 // If the requested region is entirely outsides the image, return false
337 return false;
338 }
339
340 const int sampleSize = this->getSampleSize();
341 SkScaledBitmapSampler sampler(rect.width(), rect.height(), sampleSize);
342 const int width = sampler.scaledWidth();
343 const int height = sampler.scaledHeight();
344
345 // The image can be decoded directly to decodedBitmap if
346 // 1. the region is within the image range
347 // 2. bitmap's config is compatible
348 // 3. bitmap's size is same as the required region (after sampled)
349 bool directDecode = (rect == region) &&
350 (decodedBitmap->isNull() ||
351 (is_config_compatible(*decodedBitmap) &&
352 (decodedBitmap->width() == width) &&
353 (decodedBitmap->height() == height)));
commit-bot@chromium.orga9f142e2013-05-09 16:15:20 +0000354
355 SkBitmap tmpBitmap;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000356 SkBitmap *bitmap = decodedBitmap;
357
358 if (!directDecode) {
commit-bot@chromium.orga9f142e2013-05-09 16:15:20 +0000359 bitmap = &tmpBitmap;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000360 }
361
362 if (bitmap->isNull()) {
363 if (!setDecodeConfig(bitmap, width, height)) {
364 return false;
365 }
366 // alloc from native heap if it is a temp bitmap. (prevent GC)
367 bool allocResult = (bitmap == decodedBitmap)
368 ? allocPixelRef(bitmap, NULL)
369 : bitmap->allocPixels();
370 if (!allocResult) {
371 return return_false(*decodedBitmap, "allocPixelRef");
372 }
373 } else {
374 // This is also called in setDecodeConfig in above block.
375 // i.e., when bitmap->isNull() is true.
376 if (!chooseFromOneChoice(bitmap->config(), width, height)) {
377 return false;
378 }
379 }
380
381 SkAutoLockPixels alp(*bitmap);
382 WebPDecoderConfig config;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000383 if (!webp_get_config_resize_crop(&config, bitmap, rect,
384 this->shouldPremultiply())) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000385 return false;
386 }
387
388 // Decode the WebP image data stream using WebP incremental decoding for
389 // the specified cropped image-region.
390 if (!webp_idecode(this->fInputStream, &config)) {
391 return false;
392 }
393
394 if (!directDecode) {
395 cropBitmap(decodedBitmap, bitmap, sampleSize, region.x(), region.y(),
396 region.width(), region.height(), rect.x(), rect.y());
397 }
398 return true;
399}
400
401bool SkWEBPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap,
402 Mode mode) {
403#ifdef TIME_DECODE
404 AutoTimeMillis atm("WEBP Decode");
405#endif
406
407 int origWidth, origHeight, hasAlpha;
408 if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
409 return false;
410 }
411 this->fHasAlpha = hasAlpha;
412
413 const int sampleSize = this->getSampleSize();
414 SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000415 if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(),
416 sampler.scaledHeight())) {
417 return false;
418 }
419
scroggo@google.combc69ce92013-07-09 15:45:14 +0000420 // If only bounds are requested, done
421 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
422 return true;
423 }
424
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000425 if (!this->allocPixelRef(decodedBitmap, NULL)) {
426 return return_false(*decodedBitmap, "allocPixelRef");
427 }
428
429 SkAutoLockPixels alp(*decodedBitmap);
430
431 WebPDecoderConfig config;
432 if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000433 this->shouldPremultiply())) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000434 return false;
435 }
436
437 // Decode the WebP image data stream using WebP incremental decoding.
438 return webp_idecode(stream, &config);
439}
440
441///////////////////////////////////////////////////////////////////////////////
442
443typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width,
444 const SkPMColor* SK_RESTRICT ctable);
445
446static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
447 const SkPMColor*) {
448 const uint32_t* SK_RESTRICT src = (const uint32_t*)in;
449 for (int i = 0; i < width; ++i) {
450 const uint32_t c = *src++;
451 rgb[0] = SkGetPackedR32(c);
452 rgb[1] = SkGetPackedG32(c);
453 rgb[2] = SkGetPackedB32(c);
454 rgb += 3;
455 }
456}
457
458static void RGB_565_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
459 const SkPMColor*) {
460 const uint16_t* SK_RESTRICT src = (const uint16_t*)in;
461 for (int i = 0; i < width; ++i) {
462 const uint16_t c = *src++;
463 rgb[0] = SkPacked16ToR32(c);
464 rgb[1] = SkPacked16ToG32(c);
465 rgb[2] = SkPacked16ToB32(c);
466 rgb += 3;
467 }
468}
469
470static void ARGB_4444_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
471 const SkPMColor*) {
472 const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in;
473 for (int i = 0; i < width; ++i) {
474 const SkPMColor16 c = *src++;
475 rgb[0] = SkPacked4444ToR32(c);
476 rgb[1] = SkPacked4444ToG32(c);
477 rgb[2] = SkPacked4444ToB32(c);
478 rgb += 3;
479 }
480}
481
482static void Index8_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
483 const SkPMColor* SK_RESTRICT ctable) {
484 const uint8_t* SK_RESTRICT src = (const uint8_t*)in;
485 for (int i = 0; i < width; ++i) {
486 const uint32_t c = ctable[*src++];
487 rgb[0] = SkGetPackedR32(c);
488 rgb[1] = SkGetPackedG32(c);
489 rgb[2] = SkGetPackedB32(c);
490 rgb += 3;
491 }
492}
493
494static ScanlineImporter ChooseImporter(const SkBitmap::Config& config) {
495 switch (config) {
496 case SkBitmap::kARGB_8888_Config:
497 return ARGB_8888_To_RGB;
498 case SkBitmap::kRGB_565_Config:
499 return RGB_565_To_RGB;
500 case SkBitmap::kARGB_4444_Config:
501 return ARGB_4444_To_RGB;
502 case SkBitmap::kIndex8_Config:
503 return Index8_To_RGB;
504 default:
505 return NULL;
506 }
507}
508
509static int stream_writer(const uint8_t* data, size_t data_size,
510 const WebPPicture* const picture) {
511 SkWStream* const stream = (SkWStream*)picture->custom_ptr;
512 return stream->write(data, data_size) ? 1 : 0;
513}
514
515class SkWEBPImageEncoder : public SkImageEncoder {
516protected:
517 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE;
518
519private:
520 typedef SkImageEncoder INHERITED;
521};
522
523bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm,
524 int quality) {
525 const SkBitmap::Config config = bm.getConfig();
526 const ScanlineImporter scanline_import = ChooseImporter(config);
527 if (NULL == scanline_import) {
528 return false;
529 }
530
531 SkAutoLockPixels alp(bm);
532 SkAutoLockColors ctLocker;
533 if (NULL == bm.getPixels()) {
534 return false;
535 }
536
537 WebPConfig webp_config;
commit-bot@chromium.orgbff83f62013-03-14 15:18:08 +0000538 if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000539 return false;
540 }
541
542 WebPPicture pic;
543 WebPPictureInit(&pic);
544 pic.width = bm.width();
545 pic.height = bm.height();
546 pic.writer = stream_writer;
547 pic.custom_ptr = (void*)stream;
548
549 const SkPMColor* colors = ctLocker.lockColors(bm);
550 const uint8_t* src = (uint8_t*)bm.getPixels();
551 const int rgbStride = pic.width * 3;
552
553 // Import (for each scanline) the bit-map image (in appropriate color-space)
554 // to RGB color space.
555 uint8_t* rgb = new uint8_t[rgbStride * pic.height];
556 for (int y = 0; y < pic.height; ++y) {
557 scanline_import(src + y * bm.rowBytes(), rgb + y * rgbStride,
558 pic.width, colors);
559 }
560
reed@google.combda74d32013-03-14 15:25:45 +0000561 bool ok = SkToBool(WebPPictureImportRGB(&pic, rgb, rgbStride));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000562 delete[] rgb;
563
564 ok = ok && WebPEncode(&webp_config, &pic);
565 WebPPictureFree(&pic);
566
567 return ok;
568}
569
570
571///////////////////////////////////////////////////////////////////////////////
572DEFINE_DECODER_CREATOR(WEBPImageDecoder);
573DEFINE_ENCODER_CREATOR(WEBPImageEncoder);
574///////////////////////////////////////////////////////////////////////////////
575
576#include "SkTRegistry.h"
577
578static SkImageDecoder* sk_libwebp_dfactory(SkStream* stream) {
579 int width, height, hasAlpha;
580 if (!webp_parse_header(stream, &width, &height, &hasAlpha)) {
581 return NULL;
582 }
583
584 // Magic matches, call decoder
585 return SkNEW(SkWEBPImageDecoder);
586}
587
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000588static SkImageDecoder::Format get_format_webp(SkStream* stream) {
589 int width, height, hasAlpha;
590 if (webp_parse_header(stream, &width, &height, &hasAlpha)) {
591 return SkImageDecoder::kWEBP_Format;
592 }
593 return SkImageDecoder::kUnknown_Format;
594}
595
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000596static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) {
597 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NULL;
598}
599
600static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libwebp_dfactory);
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000601static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_webp);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000602static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libwebp_efactory);