blob: 4b5081b436151b294680ba03ee97c932f8c85a69 [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
djsollen@google.com1f617a72013-08-09 12:32:48 +0000206 bool success = true;
207 VP8StatusCode status = VP8_STATUS_SUSPENDED;
208 do {
209 const uint32_t bytesToRead = WEBP_IDECODE_BUFFER_SZ;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000210 const size_t bytesRead = stream->read(input, bytesToRead);
211 if (0 == bytesRead) {
djsollen@google.com1f617a72013-08-09 12:32:48 +0000212 success = false;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000213 break;
214 }
215
djsollen@google.com1f617a72013-08-09 12:32:48 +0000216 status = WebPIAppend(idec, input, bytesRead);
217 if (VP8_STATUS_OK != status && VP8_STATUS_SUSPENDED != status) {
218 success = false;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000219 break;
220 }
djsollen@google.com1f617a72013-08-09 12:32:48 +0000221 } while (VP8_STATUS_OK != status);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000222 srcStorage.free();
223 WebPIDelete(idec);
224 WebPFreeDecBuffer(&config->output);
225
djsollen@google.com1f617a72013-08-09 12:32:48 +0000226 return success;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000227}
228
229static bool webp_get_config_resize(WebPDecoderConfig* config,
230 SkBitmap* decodedBitmap,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000231 int width, int height, bool premultiply) {
232 WEBP_CSP_MODE mode = webp_decode_mode(decodedBitmap, premultiply);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000233 if (MODE_LAST == mode) {
234 return false;
235 }
236
237 if (0 == WebPInitDecoderConfig(config)) {
238 return false;
239 }
240
241 config->output.colorspace = mode;
242 config->output.u.RGBA.rgba = (uint8_t*)decodedBitmap->getPixels();
243 config->output.u.RGBA.stride = decodedBitmap->rowBytes();
244 config->output.u.RGBA.size = decodedBitmap->getSize();
245 config->output.is_external_memory = 1;
246
247 if (width != decodedBitmap->width() || height != decodedBitmap->height()) {
248 config->options.use_scaling = 1;
249 config->options.scaled_width = decodedBitmap->width();
250 config->options.scaled_height = decodedBitmap->height();
251 }
252
253 return true;
254}
255
256static bool webp_get_config_resize_crop(WebPDecoderConfig* config,
257 SkBitmap* decodedBitmap,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000258 const SkIRect& region, bool premultiply) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000259
260 if (!webp_get_config_resize(config, decodedBitmap, region.width(),
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000261 region.height(), premultiply)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000262 return false;
263 }
264
265 config->options.use_cropping = 1;
266 config->options.crop_left = region.fLeft;
267 config->options.crop_top = region.fTop;
268 config->options.crop_width = region.width();
269 config->options.crop_height = region.height();
270
271 return true;
272}
273
274bool SkWEBPImageDecoder::setDecodeConfig(SkBitmap* decodedBitmap,
275 int width, int height) {
commit-bot@chromium.orgbff83f62013-03-14 15:18:08 +0000276 SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, SkToBool(fHasAlpha));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000277
278 // YUV converter supports output in RGB565, RGBA4444 and RGBA8888 formats.
279 if (fHasAlpha) {
280 if (config != SkBitmap::kARGB_4444_Config) {
281 config = SkBitmap::kARGB_8888_Config;
282 }
283 } else {
284 if (config != SkBitmap::kRGB_565_Config &&
285 config != SkBitmap::kARGB_4444_Config) {
286 config = SkBitmap::kARGB_8888_Config;
287 }
288 }
289
290 if (!this->chooseFromOneChoice(config, width, height)) {
291 return false;
292 }
293
294 decodedBitmap->setConfig(config, width, height, 0);
295
296 decodedBitmap->setIsOpaque(!fHasAlpha);
297
298 return true;
299}
300
301bool SkWEBPImageDecoder::onBuildTileIndex(SkStream* stream,
302 int *width, int *height) {
303 int origWidth, origHeight, hasAlpha;
304 if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
305 return false;
306 }
307
308 stream->rewind();
309 *width = origWidth;
310 *height = origHeight;
311
312 SkRefCnt_SafeAssign(this->fInputStream, stream);
313 this->fOrigWidth = origWidth;
314 this->fOrigHeight = origHeight;
315 this->fHasAlpha = hasAlpha;
316
317 return true;
318}
319
320static bool is_config_compatible(const SkBitmap& bitmap) {
321 SkBitmap::Config config = bitmap.config();
322 return config == SkBitmap::kARGB_4444_Config ||
323 config == SkBitmap::kRGB_565_Config ||
324 config == SkBitmap::kARGB_8888_Config;
325}
326
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000327bool SkWEBPImageDecoder::onDecodeSubset(SkBitmap* decodedBitmap,
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000328 const SkIRect& region) {
329 SkIRect rect = SkIRect::MakeWH(fOrigWidth, fOrigHeight);
330
331 if (!rect.intersect(region)) {
332 // If the requested region is entirely outsides the image, return false
333 return false;
334 }
335
336 const int sampleSize = this->getSampleSize();
337 SkScaledBitmapSampler sampler(rect.width(), rect.height(), sampleSize);
338 const int width = sampler.scaledWidth();
339 const int height = sampler.scaledHeight();
340
341 // The image can be decoded directly to decodedBitmap if
342 // 1. the region is within the image range
343 // 2. bitmap's config is compatible
344 // 3. bitmap's size is same as the required region (after sampled)
345 bool directDecode = (rect == region) &&
346 (decodedBitmap->isNull() ||
347 (is_config_compatible(*decodedBitmap) &&
348 (decodedBitmap->width() == width) &&
349 (decodedBitmap->height() == height)));
commit-bot@chromium.orga9f142e2013-05-09 16:15:20 +0000350
351 SkBitmap tmpBitmap;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000352 SkBitmap *bitmap = decodedBitmap;
353
354 if (!directDecode) {
commit-bot@chromium.orga9f142e2013-05-09 16:15:20 +0000355 bitmap = &tmpBitmap;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000356 }
357
358 if (bitmap->isNull()) {
359 if (!setDecodeConfig(bitmap, width, height)) {
360 return false;
361 }
362 // alloc from native heap if it is a temp bitmap. (prevent GC)
363 bool allocResult = (bitmap == decodedBitmap)
364 ? allocPixelRef(bitmap, NULL)
365 : bitmap->allocPixels();
366 if (!allocResult) {
367 return return_false(*decodedBitmap, "allocPixelRef");
368 }
369 } else {
370 // This is also called in setDecodeConfig in above block.
371 // i.e., when bitmap->isNull() is true.
372 if (!chooseFromOneChoice(bitmap->config(), width, height)) {
373 return false;
374 }
375 }
376
377 SkAutoLockPixels alp(*bitmap);
378 WebPDecoderConfig config;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000379 if (!webp_get_config_resize_crop(&config, bitmap, rect,
380 this->shouldPremultiply())) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000381 return false;
382 }
383
384 // Decode the WebP image data stream using WebP incremental decoding for
385 // the specified cropped image-region.
386 if (!webp_idecode(this->fInputStream, &config)) {
387 return false;
388 }
389
390 if (!directDecode) {
391 cropBitmap(decodedBitmap, bitmap, sampleSize, region.x(), region.y(),
392 region.width(), region.height(), rect.x(), rect.y());
393 }
394 return true;
395}
396
397bool SkWEBPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap,
398 Mode mode) {
399#ifdef TIME_DECODE
400 AutoTimeMillis atm("WEBP Decode");
401#endif
402
403 int origWidth, origHeight, hasAlpha;
404 if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
405 return false;
406 }
407 this->fHasAlpha = hasAlpha;
408
409 const int sampleSize = this->getSampleSize();
410 SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000411 if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(),
412 sampler.scaledHeight())) {
413 return false;
414 }
415
scroggo@google.combc69ce92013-07-09 15:45:14 +0000416 // If only bounds are requested, done
417 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
418 return true;
419 }
420
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000421 if (!this->allocPixelRef(decodedBitmap, NULL)) {
422 return return_false(*decodedBitmap, "allocPixelRef");
423 }
424
425 SkAutoLockPixels alp(*decodedBitmap);
426
427 WebPDecoderConfig config;
428 if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000429 this->shouldPremultiply())) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000430 return false;
431 }
432
433 // Decode the WebP image data stream using WebP incremental decoding.
434 return webp_idecode(stream, &config);
435}
436
437///////////////////////////////////////////////////////////////////////////////
438
439typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width,
440 const SkPMColor* SK_RESTRICT ctable);
441
442static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
443 const SkPMColor*) {
444 const uint32_t* SK_RESTRICT src = (const uint32_t*)in;
445 for (int i = 0; i < width; ++i) {
446 const uint32_t c = *src++;
447 rgb[0] = SkGetPackedR32(c);
448 rgb[1] = SkGetPackedG32(c);
449 rgb[2] = SkGetPackedB32(c);
450 rgb += 3;
451 }
452}
453
454static void RGB_565_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
455 const SkPMColor*) {
456 const uint16_t* SK_RESTRICT src = (const uint16_t*)in;
457 for (int i = 0; i < width; ++i) {
458 const uint16_t c = *src++;
459 rgb[0] = SkPacked16ToR32(c);
460 rgb[1] = SkPacked16ToG32(c);
461 rgb[2] = SkPacked16ToB32(c);
462 rgb += 3;
463 }
464}
465
466static void ARGB_4444_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
467 const SkPMColor*) {
468 const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in;
469 for (int i = 0; i < width; ++i) {
470 const SkPMColor16 c = *src++;
471 rgb[0] = SkPacked4444ToR32(c);
472 rgb[1] = SkPacked4444ToG32(c);
473 rgb[2] = SkPacked4444ToB32(c);
474 rgb += 3;
475 }
476}
477
478static void Index8_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
479 const SkPMColor* SK_RESTRICT ctable) {
480 const uint8_t* SK_RESTRICT src = (const uint8_t*)in;
481 for (int i = 0; i < width; ++i) {
482 const uint32_t c = ctable[*src++];
483 rgb[0] = SkGetPackedR32(c);
484 rgb[1] = SkGetPackedG32(c);
485 rgb[2] = SkGetPackedB32(c);
486 rgb += 3;
487 }
488}
489
490static ScanlineImporter ChooseImporter(const SkBitmap::Config& config) {
491 switch (config) {
492 case SkBitmap::kARGB_8888_Config:
493 return ARGB_8888_To_RGB;
494 case SkBitmap::kRGB_565_Config:
495 return RGB_565_To_RGB;
496 case SkBitmap::kARGB_4444_Config:
497 return ARGB_4444_To_RGB;
498 case SkBitmap::kIndex8_Config:
499 return Index8_To_RGB;
500 default:
501 return NULL;
502 }
503}
504
505static int stream_writer(const uint8_t* data, size_t data_size,
506 const WebPPicture* const picture) {
507 SkWStream* const stream = (SkWStream*)picture->custom_ptr;
508 return stream->write(data, data_size) ? 1 : 0;
509}
510
511class SkWEBPImageEncoder : public SkImageEncoder {
512protected:
513 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE;
514
515private:
516 typedef SkImageEncoder INHERITED;
517};
518
519bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm,
520 int quality) {
521 const SkBitmap::Config config = bm.getConfig();
522 const ScanlineImporter scanline_import = ChooseImporter(config);
523 if (NULL == scanline_import) {
524 return false;
525 }
526
527 SkAutoLockPixels alp(bm);
528 SkAutoLockColors ctLocker;
529 if (NULL == bm.getPixels()) {
530 return false;
531 }
532
533 WebPConfig webp_config;
commit-bot@chromium.orgbff83f62013-03-14 15:18:08 +0000534 if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000535 return false;
536 }
537
538 WebPPicture pic;
539 WebPPictureInit(&pic);
540 pic.width = bm.width();
541 pic.height = bm.height();
542 pic.writer = stream_writer;
543 pic.custom_ptr = (void*)stream;
544
545 const SkPMColor* colors = ctLocker.lockColors(bm);
546 const uint8_t* src = (uint8_t*)bm.getPixels();
547 const int rgbStride = pic.width * 3;
548
549 // Import (for each scanline) the bit-map image (in appropriate color-space)
550 // to RGB color space.
551 uint8_t* rgb = new uint8_t[rgbStride * pic.height];
552 for (int y = 0; y < pic.height; ++y) {
553 scanline_import(src + y * bm.rowBytes(), rgb + y * rgbStride,
554 pic.width, colors);
555 }
556
reed@google.combda74d32013-03-14 15:25:45 +0000557 bool ok = SkToBool(WebPPictureImportRGB(&pic, rgb, rgbStride));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000558 delete[] rgb;
559
560 ok = ok && WebPEncode(&webp_config, &pic);
561 WebPPictureFree(&pic);
562
563 return ok;
564}
565
566
567///////////////////////////////////////////////////////////////////////////////
568DEFINE_DECODER_CREATOR(WEBPImageDecoder);
569DEFINE_ENCODER_CREATOR(WEBPImageEncoder);
570///////////////////////////////////////////////////////////////////////////////
571
572#include "SkTRegistry.h"
573
574static SkImageDecoder* sk_libwebp_dfactory(SkStream* stream) {
575 int width, height, hasAlpha;
576 if (!webp_parse_header(stream, &width, &height, &hasAlpha)) {
577 return NULL;
578 }
579
580 // Magic matches, call decoder
581 return SkNEW(SkWEBPImageDecoder);
582}
583
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000584static SkImageDecoder::Format get_format_webp(SkStream* stream) {
585 int width, height, hasAlpha;
586 if (webp_parse_header(stream, &width, &height, &hasAlpha)) {
587 return SkImageDecoder::kWEBP_Format;
588 }
589 return SkImageDecoder::kUnknown_Format;
590}
591
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000592static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) {
593 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NULL;
594}
595
596static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libwebp_dfactory);
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000597static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_webp);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000598static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libwebp_efactory);