blob: 9f1116e5d5bbbd51c8e168f12d8db99d585f07f6 [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];
scroggo@google.com3c8730a2013-08-21 14:56:09 +000063 size_t bytesToRead = WEBP_VP8_HEADER_SIZE;
64 size_t totalBytesRead = 0;
65 do {
66 unsigned char* dst = buffer + totalBytesRead;
67 const size_t bytesRead = stream->read(dst, bytesToRead);
68 if (0 == bytesRead) {
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);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000078
79 WebPBitstreamFeatures features;
scroggo@google.com3c8730a2013-08-21 14:56:09 +000080 VP8StatusCode status = WebPGetFeatures(buffer, totalBytesRead, &features);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000081 if (VP8_STATUS_OK != status) {
82 return false; // Invalid WebP file.
83 }
84 *width = features.width;
85 *height = features.height;
86 *alpha = features.has_alpha;
87
88 // sanity check for image size that's about to be decoded.
89 {
90 Sk64 size;
91 size.setMul(*width, *height);
92 if (size.isNeg() || !size.is32()) {
93 return false;
94 }
95 // now check that if we are 4-bytes per pixel, we also don't overflow
96 if (size.get32() > (0x7FFFFFFF >> 2)) {
97 return false;
98 }
99 }
100 return true;
101}
102
103class SkWEBPImageDecoder: public SkImageDecoder {
104public:
105 SkWEBPImageDecoder() {
106 fInputStream = NULL;
107 fOrigWidth = 0;
108 fOrigHeight = 0;
109 fHasAlpha = 0;
110 }
111 virtual ~SkWEBPImageDecoder() {
112 SkSafeUnref(fInputStream);
113 }
114
115 virtual Format getFormat() const SK_OVERRIDE {
116 return kWEBP_Format;
117 }
118
119protected:
120 virtual bool onBuildTileIndex(SkStream *stream, int *width, int *height) SK_OVERRIDE;
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000121 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRIDE;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000122 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
123
124private:
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000125 /**
126 * Called when determining the output config to request to webp.
127 * If the image does not have alpha, there is no need to premultiply.
128 * If the caller wants unpremultiplied colors, that is respected.
129 */
130 bool shouldPremultiply() const {
131 return SkToBool(fHasAlpha) && !this->getRequireUnpremultipliedColors();
132 }
133
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000134 bool setDecodeConfig(SkBitmap* decodedBitmap, int width, int height);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000135
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000136 SkStream* fInputStream;
137 int fOrigWidth;
138 int fOrigHeight;
139 int fHasAlpha;
140
141 typedef SkImageDecoder INHERITED;
142};
143
144//////////////////////////////////////////////////////////////////////////
145
146#ifdef TIME_DECODE
147
148#include "SkTime.h"
149
150class AutoTimeMillis {
151public:
152 AutoTimeMillis(const char label[]) :
153 fLabel(label) {
154 if (NULL == fLabel) {
155 fLabel = "";
156 }
157 fNow = SkTime::GetMSecs();
158 }
159 ~AutoTimeMillis() {
160 SkDebugf("---- Time (ms): %s %d\n", fLabel, SkTime::GetMSecs() - fNow);
161 }
162private:
163 const char* fLabel;
164 SkMSec fNow;
165};
166
167#endif
168
169///////////////////////////////////////////////////////////////////////////////
170
171// This guy exists just to aid in debugging, as it allows debuggers to just
172// set a break-point in one place to see all error exists.
173static bool return_false(const SkBitmap& bm, const char msg[]) {
174 SkDEBUGF(("libwebp error %s [%d %d]", msg, bm.width(), bm.height()));
175 return false; // must always return false
176}
177
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000178static WEBP_CSP_MODE webp_decode_mode(const SkBitmap* decodedBitmap, bool premultiply) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000179 WEBP_CSP_MODE mode = MODE_LAST;
180 SkBitmap::Config config = decodedBitmap->config();
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000181
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000182 if (config == SkBitmap::kARGB_8888_Config) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000183 mode = premultiply ? MODE_rgbA : MODE_RGBA;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000184 } else if (config == SkBitmap::kARGB_4444_Config) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000185 mode = premultiply ? MODE_rgbA_4444 : MODE_RGBA_4444;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000186 } else if (config == SkBitmap::kRGB_565_Config) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000187 mode = MODE_RGB_565;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000188 }
189 SkASSERT(MODE_LAST != mode);
190 return mode;
191}
192
193// Incremental WebP image decoding. Reads input buffer of 64K size iteratively
194// and decodes this block to appropriate color-space as per config object.
195static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) {
196 WebPIDecoder* idec = WebPIDecode(NULL, 0, config);
197 if (NULL == idec) {
198 WebPFreeDecBuffer(&config->output);
199 return false;
200 }
201
202 stream->rewind();
scroggo@google.com3c8730a2013-08-21 14:56:09 +0000203 const size_t readBufferSize = stream->hasLength() ?
204 SkTMin(stream->getLength(), WEBP_IDECODE_BUFFER_SZ) : WEBP_IDECODE_BUFFER_SZ;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000205 SkAutoMalloc srcStorage(readBufferSize);
206 unsigned char* input = (uint8_t*)srcStorage.get();
207 if (NULL == input) {
208 WebPIDelete(idec);
209 WebPFreeDecBuffer(&config->output);
210 return false;
211 }
212
scroggo@google.com80e18c92013-08-09 19:22:00 +0000213 bool success = true;
214 VP8StatusCode status = VP8_STATUS_SUSPENDED;
215 do {
216 const size_t bytesRead = stream->read(input, readBufferSize);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000217 if (0 == bytesRead) {
scroggo@google.com80e18c92013-08-09 19:22:00 +0000218 success = false;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000219 break;
220 }
221
scroggo@google.com80e18c92013-08-09 19:22:00 +0000222 status = WebPIAppend(idec, input, bytesRead);
223 if (VP8_STATUS_OK != status && VP8_STATUS_SUSPENDED != status) {
224 success = false;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000225 break;
226 }
scroggo@google.com80e18c92013-08-09 19:22:00 +0000227 } while (VP8_STATUS_OK != status);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000228 srcStorage.free();
229 WebPIDelete(idec);
230 WebPFreeDecBuffer(&config->output);
231
scroggo@google.com80e18c92013-08-09 19:22:00 +0000232 return success;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000233}
234
235static bool webp_get_config_resize(WebPDecoderConfig* config,
236 SkBitmap* decodedBitmap,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000237 int width, int height, bool premultiply) {
238 WEBP_CSP_MODE mode = webp_decode_mode(decodedBitmap, premultiply);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000239 if (MODE_LAST == mode) {
240 return false;
241 }
242
243 if (0 == WebPInitDecoderConfig(config)) {
244 return false;
245 }
246
247 config->output.colorspace = mode;
248 config->output.u.RGBA.rgba = (uint8_t*)decodedBitmap->getPixels();
249 config->output.u.RGBA.stride = decodedBitmap->rowBytes();
250 config->output.u.RGBA.size = decodedBitmap->getSize();
251 config->output.is_external_memory = 1;
252
253 if (width != decodedBitmap->width() || height != decodedBitmap->height()) {
254 config->options.use_scaling = 1;
255 config->options.scaled_width = decodedBitmap->width();
256 config->options.scaled_height = decodedBitmap->height();
257 }
258
259 return true;
260}
261
262static bool webp_get_config_resize_crop(WebPDecoderConfig* config,
263 SkBitmap* decodedBitmap,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000264 const SkIRect& region, bool premultiply) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000265
266 if (!webp_get_config_resize(config, decodedBitmap, region.width(),
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000267 region.height(), premultiply)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000268 return false;
269 }
270
271 config->options.use_cropping = 1;
272 config->options.crop_left = region.fLeft;
273 config->options.crop_top = region.fTop;
274 config->options.crop_width = region.width();
275 config->options.crop_height = region.height();
276
277 return true;
278}
279
280bool SkWEBPImageDecoder::setDecodeConfig(SkBitmap* decodedBitmap,
281 int width, int height) {
commit-bot@chromium.orgbff83f62013-03-14 15:18:08 +0000282 SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, SkToBool(fHasAlpha));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000283
284 // YUV converter supports output in RGB565, RGBA4444 and RGBA8888 formats.
285 if (fHasAlpha) {
286 if (config != SkBitmap::kARGB_4444_Config) {
287 config = SkBitmap::kARGB_8888_Config;
288 }
289 } else {
290 if (config != SkBitmap::kRGB_565_Config &&
291 config != SkBitmap::kARGB_4444_Config) {
292 config = SkBitmap::kARGB_8888_Config;
293 }
294 }
295
296 if (!this->chooseFromOneChoice(config, width, height)) {
297 return false;
298 }
299
300 decodedBitmap->setConfig(config, width, height, 0);
301
302 decodedBitmap->setIsOpaque(!fHasAlpha);
303
304 return true;
305}
306
307bool SkWEBPImageDecoder::onBuildTileIndex(SkStream* stream,
308 int *width, int *height) {
309 int origWidth, origHeight, hasAlpha;
310 if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
311 return false;
312 }
313
314 stream->rewind();
315 *width = origWidth;
316 *height = origHeight;
317
318 SkRefCnt_SafeAssign(this->fInputStream, stream);
319 this->fOrigWidth = origWidth;
320 this->fOrigHeight = origHeight;
321 this->fHasAlpha = hasAlpha;
322
323 return true;
324}
325
326static bool is_config_compatible(const SkBitmap& bitmap) {
327 SkBitmap::Config config = bitmap.config();
328 return config == SkBitmap::kARGB_4444_Config ||
329 config == SkBitmap::kRGB_565_Config ||
330 config == SkBitmap::kARGB_8888_Config;
331}
332
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000333bool SkWEBPImageDecoder::onDecodeSubset(SkBitmap* decodedBitmap,
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000334 const SkIRect& region) {
335 SkIRect rect = SkIRect::MakeWH(fOrigWidth, fOrigHeight);
336
337 if (!rect.intersect(region)) {
338 // If the requested region is entirely outsides the image, return false
339 return false;
340 }
341
342 const int sampleSize = this->getSampleSize();
343 SkScaledBitmapSampler sampler(rect.width(), rect.height(), sampleSize);
344 const int width = sampler.scaledWidth();
345 const int height = sampler.scaledHeight();
346
347 // The image can be decoded directly to decodedBitmap if
348 // 1. the region is within the image range
349 // 2. bitmap's config is compatible
350 // 3. bitmap's size is same as the required region (after sampled)
351 bool directDecode = (rect == region) &&
352 (decodedBitmap->isNull() ||
353 (is_config_compatible(*decodedBitmap) &&
354 (decodedBitmap->width() == width) &&
355 (decodedBitmap->height() == height)));
commit-bot@chromium.orga9f142e2013-05-09 16:15:20 +0000356
357 SkBitmap tmpBitmap;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000358 SkBitmap *bitmap = decodedBitmap;
359
360 if (!directDecode) {
commit-bot@chromium.orga9f142e2013-05-09 16:15:20 +0000361 bitmap = &tmpBitmap;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000362 }
363
364 if (bitmap->isNull()) {
365 if (!setDecodeConfig(bitmap, width, height)) {
366 return false;
367 }
368 // alloc from native heap if it is a temp bitmap. (prevent GC)
369 bool allocResult = (bitmap == decodedBitmap)
370 ? allocPixelRef(bitmap, NULL)
371 : bitmap->allocPixels();
372 if (!allocResult) {
373 return return_false(*decodedBitmap, "allocPixelRef");
374 }
375 } else {
376 // This is also called in setDecodeConfig in above block.
377 // i.e., when bitmap->isNull() is true.
378 if (!chooseFromOneChoice(bitmap->config(), width, height)) {
379 return false;
380 }
381 }
382
383 SkAutoLockPixels alp(*bitmap);
384 WebPDecoderConfig config;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000385 if (!webp_get_config_resize_crop(&config, bitmap, rect,
386 this->shouldPremultiply())) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000387 return false;
388 }
389
390 // Decode the WebP image data stream using WebP incremental decoding for
391 // the specified cropped image-region.
392 if (!webp_idecode(this->fInputStream, &config)) {
393 return false;
394 }
395
396 if (!directDecode) {
397 cropBitmap(decodedBitmap, bitmap, sampleSize, region.x(), region.y(),
398 region.width(), region.height(), rect.x(), rect.y());
399 }
400 return true;
401}
402
403bool SkWEBPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap,
404 Mode mode) {
405#ifdef TIME_DECODE
406 AutoTimeMillis atm("WEBP Decode");
407#endif
408
409 int origWidth, origHeight, hasAlpha;
410 if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
411 return false;
412 }
413 this->fHasAlpha = hasAlpha;
414
415 const int sampleSize = this->getSampleSize();
416 SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000417 if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(),
418 sampler.scaledHeight())) {
419 return false;
420 }
421
scroggo@google.combc69ce92013-07-09 15:45:14 +0000422 // If only bounds are requested, done
423 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
424 return true;
425 }
426
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000427 if (!this->allocPixelRef(decodedBitmap, NULL)) {
428 return return_false(*decodedBitmap, "allocPixelRef");
429 }
430
431 SkAutoLockPixels alp(*decodedBitmap);
432
433 WebPDecoderConfig config;
434 if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000435 this->shouldPremultiply())) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000436 return false;
437 }
438
439 // Decode the WebP image data stream using WebP incremental decoding.
440 return webp_idecode(stream, &config);
441}
442
443///////////////////////////////////////////////////////////////////////////////
444
445typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width,
446 const SkPMColor* SK_RESTRICT ctable);
447
448static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
449 const SkPMColor*) {
450 const uint32_t* SK_RESTRICT src = (const uint32_t*)in;
451 for (int i = 0; i < width; ++i) {
452 const uint32_t c = *src++;
453 rgb[0] = SkGetPackedR32(c);
454 rgb[1] = SkGetPackedG32(c);
455 rgb[2] = SkGetPackedB32(c);
456 rgb += 3;
457 }
458}
459
460static void RGB_565_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
461 const SkPMColor*) {
462 const uint16_t* SK_RESTRICT src = (const uint16_t*)in;
463 for (int i = 0; i < width; ++i) {
464 const uint16_t c = *src++;
465 rgb[0] = SkPacked16ToR32(c);
466 rgb[1] = SkPacked16ToG32(c);
467 rgb[2] = SkPacked16ToB32(c);
468 rgb += 3;
469 }
470}
471
472static void ARGB_4444_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
473 const SkPMColor*) {
474 const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in;
475 for (int i = 0; i < width; ++i) {
476 const SkPMColor16 c = *src++;
477 rgb[0] = SkPacked4444ToR32(c);
478 rgb[1] = SkPacked4444ToG32(c);
479 rgb[2] = SkPacked4444ToB32(c);
480 rgb += 3;
481 }
482}
483
484static void Index8_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
485 const SkPMColor* SK_RESTRICT ctable) {
486 const uint8_t* SK_RESTRICT src = (const uint8_t*)in;
487 for (int i = 0; i < width; ++i) {
488 const uint32_t c = ctable[*src++];
489 rgb[0] = SkGetPackedR32(c);
490 rgb[1] = SkGetPackedG32(c);
491 rgb[2] = SkGetPackedB32(c);
492 rgb += 3;
493 }
494}
495
496static ScanlineImporter ChooseImporter(const SkBitmap::Config& config) {
497 switch (config) {
498 case SkBitmap::kARGB_8888_Config:
499 return ARGB_8888_To_RGB;
500 case SkBitmap::kRGB_565_Config:
501 return RGB_565_To_RGB;
502 case SkBitmap::kARGB_4444_Config:
503 return ARGB_4444_To_RGB;
504 case SkBitmap::kIndex8_Config:
505 return Index8_To_RGB;
506 default:
507 return NULL;
508 }
509}
510
511static int stream_writer(const uint8_t* data, size_t data_size,
512 const WebPPicture* const picture) {
513 SkWStream* const stream = (SkWStream*)picture->custom_ptr;
514 return stream->write(data, data_size) ? 1 : 0;
515}
516
517class SkWEBPImageEncoder : public SkImageEncoder {
518protected:
519 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE;
520
521private:
522 typedef SkImageEncoder INHERITED;
523};
524
525bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm,
526 int quality) {
527 const SkBitmap::Config config = bm.getConfig();
528 const ScanlineImporter scanline_import = ChooseImporter(config);
529 if (NULL == scanline_import) {
530 return false;
531 }
532
533 SkAutoLockPixels alp(bm);
534 SkAutoLockColors ctLocker;
535 if (NULL == bm.getPixels()) {
536 return false;
537 }
538
539 WebPConfig webp_config;
commit-bot@chromium.orgbff83f62013-03-14 15:18:08 +0000540 if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000541 return false;
542 }
543
544 WebPPicture pic;
545 WebPPictureInit(&pic);
546 pic.width = bm.width();
547 pic.height = bm.height();
548 pic.writer = stream_writer;
549 pic.custom_ptr = (void*)stream;
550
551 const SkPMColor* colors = ctLocker.lockColors(bm);
552 const uint8_t* src = (uint8_t*)bm.getPixels();
553 const int rgbStride = pic.width * 3;
554
555 // Import (for each scanline) the bit-map image (in appropriate color-space)
556 // to RGB color space.
557 uint8_t* rgb = new uint8_t[rgbStride * pic.height];
558 for (int y = 0; y < pic.height; ++y) {
559 scanline_import(src + y * bm.rowBytes(), rgb + y * rgbStride,
560 pic.width, colors);
561 }
562
reed@google.combda74d32013-03-14 15:25:45 +0000563 bool ok = SkToBool(WebPPictureImportRGB(&pic, rgb, rgbStride));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000564 delete[] rgb;
565
566 ok = ok && WebPEncode(&webp_config, &pic);
567 WebPPictureFree(&pic);
568
569 return ok;
570}
571
572
573///////////////////////////////////////////////////////////////////////////////
574DEFINE_DECODER_CREATOR(WEBPImageDecoder);
575DEFINE_ENCODER_CREATOR(WEBPImageEncoder);
576///////////////////////////////////////////////////////////////////////////////
577
578#include "SkTRegistry.h"
579
580static SkImageDecoder* sk_libwebp_dfactory(SkStream* stream) {
581 int width, height, hasAlpha;
582 if (!webp_parse_header(stream, &width, &height, &hasAlpha)) {
583 return NULL;
584 }
585
586 // Magic matches, call decoder
587 return SkNEW(SkWEBPImageDecoder);
588}
589
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000590static SkImageDecoder::Format get_format_webp(SkStream* stream) {
591 int width, height, hasAlpha;
592 if (webp_parse_header(stream, &width, &height, &hasAlpha)) {
593 return SkImageDecoder::kWEBP_Format;
594 }
595 return SkImageDecoder::kUnknown_Format;
596}
597
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000598static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) {
599 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NULL;
600}
601
602static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libwebp_dfactory);
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000603static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_webp);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000604static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libwebp_efactory);