blob: 9cf84493ad89b4c555539fc0b24825a212b24a5d [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);
415
416 // If only bounds are requested, done
417 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
418 if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(),
419 sampler.scaledHeight())) {
420 return false;
421 }
422 return true;
423 }
424
425 // No Bitmap reuse supported for this format
426 if (!decodedBitmap->isNull()) {
427 return false;
428 }
429 if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(),
430 sampler.scaledHeight())) {
431 return false;
432 }
433
434 if (!this->allocPixelRef(decodedBitmap, NULL)) {
435 return return_false(*decodedBitmap, "allocPixelRef");
436 }
437
438 SkAutoLockPixels alp(*decodedBitmap);
439
440 WebPDecoderConfig config;
441 if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000442 this->shouldPremultiply())) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000443 return false;
444 }
445
446 // Decode the WebP image data stream using WebP incremental decoding.
447 return webp_idecode(stream, &config);
448}
449
450///////////////////////////////////////////////////////////////////////////////
451
452typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width,
453 const SkPMColor* SK_RESTRICT ctable);
454
455static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
456 const SkPMColor*) {
457 const uint32_t* SK_RESTRICT src = (const uint32_t*)in;
458 for (int i = 0; i < width; ++i) {
459 const uint32_t c = *src++;
460 rgb[0] = SkGetPackedR32(c);
461 rgb[1] = SkGetPackedG32(c);
462 rgb[2] = SkGetPackedB32(c);
463 rgb += 3;
464 }
465}
466
467static void RGB_565_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
468 const SkPMColor*) {
469 const uint16_t* SK_RESTRICT src = (const uint16_t*)in;
470 for (int i = 0; i < width; ++i) {
471 const uint16_t c = *src++;
472 rgb[0] = SkPacked16ToR32(c);
473 rgb[1] = SkPacked16ToG32(c);
474 rgb[2] = SkPacked16ToB32(c);
475 rgb += 3;
476 }
477}
478
479static void ARGB_4444_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
480 const SkPMColor*) {
481 const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in;
482 for (int i = 0; i < width; ++i) {
483 const SkPMColor16 c = *src++;
484 rgb[0] = SkPacked4444ToR32(c);
485 rgb[1] = SkPacked4444ToG32(c);
486 rgb[2] = SkPacked4444ToB32(c);
487 rgb += 3;
488 }
489}
490
491static void Index8_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
492 const SkPMColor* SK_RESTRICT ctable) {
493 const uint8_t* SK_RESTRICT src = (const uint8_t*)in;
494 for (int i = 0; i < width; ++i) {
495 const uint32_t c = ctable[*src++];
496 rgb[0] = SkGetPackedR32(c);
497 rgb[1] = SkGetPackedG32(c);
498 rgb[2] = SkGetPackedB32(c);
499 rgb += 3;
500 }
501}
502
503static ScanlineImporter ChooseImporter(const SkBitmap::Config& config) {
504 switch (config) {
505 case SkBitmap::kARGB_8888_Config:
506 return ARGB_8888_To_RGB;
507 case SkBitmap::kRGB_565_Config:
508 return RGB_565_To_RGB;
509 case SkBitmap::kARGB_4444_Config:
510 return ARGB_4444_To_RGB;
511 case SkBitmap::kIndex8_Config:
512 return Index8_To_RGB;
513 default:
514 return NULL;
515 }
516}
517
518static int stream_writer(const uint8_t* data, size_t data_size,
519 const WebPPicture* const picture) {
520 SkWStream* const stream = (SkWStream*)picture->custom_ptr;
521 return stream->write(data, data_size) ? 1 : 0;
522}
523
524class SkWEBPImageEncoder : public SkImageEncoder {
525protected:
526 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE;
527
528private:
529 typedef SkImageEncoder INHERITED;
530};
531
532bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm,
533 int quality) {
534 const SkBitmap::Config config = bm.getConfig();
535 const ScanlineImporter scanline_import = ChooseImporter(config);
536 if (NULL == scanline_import) {
537 return false;
538 }
539
540 SkAutoLockPixels alp(bm);
541 SkAutoLockColors ctLocker;
542 if (NULL == bm.getPixels()) {
543 return false;
544 }
545
546 WebPConfig webp_config;
commit-bot@chromium.orgbff83f62013-03-14 15:18:08 +0000547 if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000548 return false;
549 }
550
551 WebPPicture pic;
552 WebPPictureInit(&pic);
553 pic.width = bm.width();
554 pic.height = bm.height();
555 pic.writer = stream_writer;
556 pic.custom_ptr = (void*)stream;
557
558 const SkPMColor* colors = ctLocker.lockColors(bm);
559 const uint8_t* src = (uint8_t*)bm.getPixels();
560 const int rgbStride = pic.width * 3;
561
562 // Import (for each scanline) the bit-map image (in appropriate color-space)
563 // to RGB color space.
564 uint8_t* rgb = new uint8_t[rgbStride * pic.height];
565 for (int y = 0; y < pic.height; ++y) {
566 scanline_import(src + y * bm.rowBytes(), rgb + y * rgbStride,
567 pic.width, colors);
568 }
569
reed@google.combda74d32013-03-14 15:25:45 +0000570 bool ok = SkToBool(WebPPictureImportRGB(&pic, rgb, rgbStride));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000571 delete[] rgb;
572
573 ok = ok && WebPEncode(&webp_config, &pic);
574 WebPPictureFree(&pic);
575
576 return ok;
577}
578
579
580///////////////////////////////////////////////////////////////////////////////
581DEFINE_DECODER_CREATOR(WEBPImageDecoder);
582DEFINE_ENCODER_CREATOR(WEBPImageEncoder);
583///////////////////////////////////////////////////////////////////////////////
584
585#include "SkTRegistry.h"
586
587static SkImageDecoder* sk_libwebp_dfactory(SkStream* stream) {
588 int width, height, hasAlpha;
589 if (!webp_parse_header(stream, &width, &height, &hasAlpha)) {
590 return NULL;
591 }
592
593 // Magic matches, call decoder
594 return SkNEW(SkWEBPImageDecoder);
595}
596
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000597static SkImageDecoder::Format get_format_webp(SkStream* stream) {
598 int width, height, hasAlpha;
599 if (webp_parse_header(stream, &width, &height, &hasAlpha)) {
600 return SkImageDecoder::kWEBP_Format;
601 }
602 return SkImageDecoder::kUnknown_Format;
603}
604
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000605static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) {
606 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NULL;
607}
608
609static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libwebp_dfactory);
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000610static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_webp);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000611static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libwebp_efactory);