blob: 4a5951020efd3d35289d23b7d3c496ee1b0517e7 [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
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000039// 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
50static const size_t WEBP_VP8_HEADER_SIZE = 64;
51static const size_t WEBP_IDECODE_BUFFER_SZ = (1 << 16);
52
53// Parse headers of RIFF container, and check for valid Webp (VP8) content.
54static bool webp_parse_header(SkStream* stream, int* width, int* height, int* alpha) {
55 unsigned char buffer[WEBP_VP8_HEADER_SIZE];
scroggo@google.com3c8730a2013-08-21 14:56:09 +000056 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 // Could not read any bytes. Check to see if we are at the end (exit
63 // condition), and continue reading if not. Important for streams
64 // that do not have all the data ready.
65 continue;
66 }
67 bytesToRead -= bytesRead;
68 totalBytesRead += bytesRead;
69 SkASSERT(bytesToRead + totalBytesRead == WEBP_VP8_HEADER_SIZE);
70 } while (!stream->isAtEnd() && bytesToRead > 0);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000071
72 WebPBitstreamFeatures features;
scroggo@google.com3c8730a2013-08-21 14:56:09 +000073 VP8StatusCode status = WebPGetFeatures(buffer, totalBytesRead, &features);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000074 if (VP8_STATUS_OK != status) {
75 return false; // Invalid WebP file.
76 }
77 *width = features.width;
78 *height = features.height;
79 *alpha = features.has_alpha;
80
81 // sanity check for image size that's about to be decoded.
82 {
83 Sk64 size;
84 size.setMul(*width, *height);
85 if (size.isNeg() || !size.is32()) {
86 return false;
87 }
88 // now check that if we are 4-bytes per pixel, we also don't overflow
89 if (size.get32() > (0x7FFFFFFF >> 2)) {
90 return false;
91 }
92 }
93 return true;
94}
95
96class SkWEBPImageDecoder: public SkImageDecoder {
97public:
98 SkWEBPImageDecoder() {
99 fInputStream = NULL;
100 fOrigWidth = 0;
101 fOrigHeight = 0;
102 fHasAlpha = 0;
103 }
104 virtual ~SkWEBPImageDecoder() {
105 SkSafeUnref(fInputStream);
106 }
107
108 virtual Format getFormat() const SK_OVERRIDE {
109 return kWEBP_Format;
110 }
111
112protected:
scroggo@google.comb5571b32013-09-25 21:34:24 +0000113 virtual bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *height) SK_OVERRIDE;
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000114 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRIDE;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000115 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
116
117private:
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000118 /**
119 * Called when determining the output config to request to webp.
120 * If the image does not have alpha, there is no need to premultiply.
121 * If the caller wants unpremultiplied colors, that is respected.
122 */
123 bool shouldPremultiply() const {
124 return SkToBool(fHasAlpha) && !this->getRequireUnpremultipliedColors();
125 }
126
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000127 bool setDecodeConfig(SkBitmap* decodedBitmap, int width, int height);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000128
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000129 SkStream* fInputStream;
130 int fOrigWidth;
131 int fOrigHeight;
132 int fHasAlpha;
133
134 typedef SkImageDecoder INHERITED;
135};
136
137//////////////////////////////////////////////////////////////////////////
138
139#ifdef TIME_DECODE
140
141#include "SkTime.h"
142
143class AutoTimeMillis {
144public:
145 AutoTimeMillis(const char label[]) :
146 fLabel(label) {
147 if (NULL == fLabel) {
148 fLabel = "";
149 }
150 fNow = SkTime::GetMSecs();
151 }
152 ~AutoTimeMillis() {
153 SkDebugf("---- Time (ms): %s %d\n", fLabel, SkTime::GetMSecs() - fNow);
154 }
155private:
156 const char* fLabel;
157 SkMSec fNow;
158};
159
160#endif
161
162///////////////////////////////////////////////////////////////////////////////
163
164// This guy exists just to aid in debugging, as it allows debuggers to just
165// set a break-point in one place to see all error exists.
166static bool return_false(const SkBitmap& bm, const char msg[]) {
167 SkDEBUGF(("libwebp error %s [%d %d]", msg, bm.width(), bm.height()));
168 return false; // must always return false
169}
170
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000171static WEBP_CSP_MODE webp_decode_mode(const SkBitmap* decodedBitmap, bool premultiply) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000172 WEBP_CSP_MODE mode = MODE_LAST;
173 SkBitmap::Config config = decodedBitmap->config();
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000174
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000175 if (config == SkBitmap::kARGB_8888_Config) {
halcanary@google.comdedd44a2013-12-20 16:35:22 +0000176 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
177 mode = premultiply ? MODE_bgrA : MODE_BGRA;
178 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
179 mode = premultiply ? MODE_rgbA : MODE_RGBA;
180 #else
181 #error "Skia uses BGRA or RGBA byte order"
182 #endif
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000183 } else if (config == SkBitmap::kARGB_4444_Config) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000184 mode = premultiply ? MODE_rgbA_4444 : MODE_RGBA_4444;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000185 } else if (config == SkBitmap::kRGB_565_Config) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000186 mode = MODE_RGB_565;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000187 }
188 SkASSERT(MODE_LAST != mode);
189 return mode;
190}
191
192// Incremental WebP image decoding. Reads input buffer of 64K size iteratively
193// and decodes this block to appropriate color-space as per config object.
194static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) {
195 WebPIDecoder* idec = WebPIDecode(NULL, 0, config);
196 if (NULL == idec) {
197 WebPFreeDecBuffer(&config->output);
198 return false;
199 }
200
scroggo@google.com4d213ab2013-08-28 13:08:54 +0000201 if (!stream->rewind()) {
202 SkDebugf("Failed to rewind webp stream!");
203 return false;
204 }
scroggo@google.com3c8730a2013-08-21 14:56:09 +0000205 const size_t readBufferSize = stream->hasLength() ?
206 SkTMin(stream->getLength(), WEBP_IDECODE_BUFFER_SZ) : WEBP_IDECODE_BUFFER_SZ;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000207 SkAutoMalloc srcStorage(readBufferSize);
208 unsigned char* input = (uint8_t*)srcStorage.get();
209 if (NULL == input) {
210 WebPIDelete(idec);
211 WebPFreeDecBuffer(&config->output);
212 return false;
213 }
214
scroggo@google.com80e18c92013-08-09 19:22:00 +0000215 bool success = true;
216 VP8StatusCode status = VP8_STATUS_SUSPENDED;
217 do {
218 const size_t bytesRead = stream->read(input, readBufferSize);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000219 if (0 == bytesRead) {
scroggo@google.com80e18c92013-08-09 19:22:00 +0000220 success = false;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000221 break;
222 }
223
scroggo@google.com80e18c92013-08-09 19:22:00 +0000224 status = WebPIAppend(idec, input, bytesRead);
225 if (VP8_STATUS_OK != status && VP8_STATUS_SUSPENDED != status) {
226 success = false;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000227 break;
228 }
scroggo@google.com80e18c92013-08-09 19:22:00 +0000229 } while (VP8_STATUS_OK != status);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000230 srcStorage.free();
231 WebPIDelete(idec);
232 WebPFreeDecBuffer(&config->output);
233
scroggo@google.com80e18c92013-08-09 19:22:00 +0000234 return success;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000235}
236
237static bool webp_get_config_resize(WebPDecoderConfig* config,
238 SkBitmap* decodedBitmap,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000239 int width, int height, bool premultiply) {
240 WEBP_CSP_MODE mode = webp_decode_mode(decodedBitmap, premultiply);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000241 if (MODE_LAST == mode) {
242 return false;
243 }
244
245 if (0 == WebPInitDecoderConfig(config)) {
246 return false;
247 }
248
249 config->output.colorspace = mode;
250 config->output.u.RGBA.rgba = (uint8_t*)decodedBitmap->getPixels();
robertphillips@google.com8b169312013-10-15 17:47:36 +0000251 config->output.u.RGBA.stride = (int) decodedBitmap->rowBytes();
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000252 config->output.u.RGBA.size = decodedBitmap->getSize();
253 config->output.is_external_memory = 1;
254
255 if (width != decodedBitmap->width() || height != decodedBitmap->height()) {
256 config->options.use_scaling = 1;
257 config->options.scaled_width = decodedBitmap->width();
258 config->options.scaled_height = decodedBitmap->height();
259 }
260
261 return true;
262}
263
264static bool webp_get_config_resize_crop(WebPDecoderConfig* config,
265 SkBitmap* decodedBitmap,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000266 const SkIRect& region, bool premultiply) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000267
268 if (!webp_get_config_resize(config, decodedBitmap, region.width(),
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000269 region.height(), premultiply)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000270 return false;
271 }
272
273 config->options.use_cropping = 1;
274 config->options.crop_left = region.fLeft;
275 config->options.crop_top = region.fTop;
276 config->options.crop_width = region.width();
277 config->options.crop_height = region.height();
278
279 return true;
280}
281
282bool SkWEBPImageDecoder::setDecodeConfig(SkBitmap* decodedBitmap,
283 int width, int height) {
commit-bot@chromium.orgbff83f62013-03-14 15:18:08 +0000284 SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, SkToBool(fHasAlpha));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000285
286 // YUV converter supports output in RGB565, RGBA4444 and RGBA8888 formats.
287 if (fHasAlpha) {
288 if (config != SkBitmap::kARGB_4444_Config) {
289 config = SkBitmap::kARGB_8888_Config;
290 }
291 } else {
292 if (config != SkBitmap::kRGB_565_Config &&
293 config != SkBitmap::kARGB_4444_Config) {
294 config = SkBitmap::kARGB_8888_Config;
295 }
296 }
297
298 if (!this->chooseFromOneChoice(config, width, height)) {
299 return false;
300 }
301
reed@google.com383a6972013-10-21 14:00:07 +0000302 return decodedBitmap->setConfig(config, width, height, 0,
303 fHasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000304}
305
scroggo@google.comb5571b32013-09-25 21:34:24 +0000306bool SkWEBPImageDecoder::onBuildTileIndex(SkStreamRewindable* stream,
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000307 int *width, int *height) {
308 int origWidth, origHeight, hasAlpha;
309 if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
310 return false;
311 }
312
scroggo@google.com4d213ab2013-08-28 13:08:54 +0000313 if (!stream->rewind()) {
314 SkDebugf("Failed to rewind webp stream!");
315 return false;
316 }
317
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000318 *width = origWidth;
319 *height = origHeight;
320
321 SkRefCnt_SafeAssign(this->fInputStream, stream);
322 this->fOrigWidth = origWidth;
323 this->fOrigHeight = origHeight;
324 this->fHasAlpha = hasAlpha;
325
326 return true;
327}
328
329static bool is_config_compatible(const SkBitmap& bitmap) {
330 SkBitmap::Config config = bitmap.config();
331 return config == SkBitmap::kARGB_4444_Config ||
332 config == SkBitmap::kRGB_565_Config ||
333 config == SkBitmap::kARGB_8888_Config;
334}
335
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000336bool SkWEBPImageDecoder::onDecodeSubset(SkBitmap* decodedBitmap,
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000337 const SkIRect& region) {
338 SkIRect rect = SkIRect::MakeWH(fOrigWidth, fOrigHeight);
339
340 if (!rect.intersect(region)) {
341 // If the requested region is entirely outsides the image, return false
342 return false;
343 }
344
345 const int sampleSize = this->getSampleSize();
346 SkScaledBitmapSampler sampler(rect.width(), rect.height(), sampleSize);
347 const int width = sampler.scaledWidth();
348 const int height = sampler.scaledHeight();
349
350 // The image can be decoded directly to decodedBitmap if
351 // 1. the region is within the image range
352 // 2. bitmap's config is compatible
353 // 3. bitmap's size is same as the required region (after sampled)
354 bool directDecode = (rect == region) &&
355 (decodedBitmap->isNull() ||
356 (is_config_compatible(*decodedBitmap) &&
357 (decodedBitmap->width() == width) &&
358 (decodedBitmap->height() == height)));
commit-bot@chromium.orga9f142e2013-05-09 16:15:20 +0000359
360 SkBitmap tmpBitmap;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000361 SkBitmap *bitmap = decodedBitmap;
362
363 if (!directDecode) {
commit-bot@chromium.orga9f142e2013-05-09 16:15:20 +0000364 bitmap = &tmpBitmap;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000365 }
366
367 if (bitmap->isNull()) {
368 if (!setDecodeConfig(bitmap, width, height)) {
369 return false;
370 }
371 // alloc from native heap if it is a temp bitmap. (prevent GC)
372 bool allocResult = (bitmap == decodedBitmap)
373 ? allocPixelRef(bitmap, NULL)
374 : bitmap->allocPixels();
375 if (!allocResult) {
376 return return_false(*decodedBitmap, "allocPixelRef");
377 }
378 } else {
379 // This is also called in setDecodeConfig in above block.
380 // i.e., when bitmap->isNull() is true.
381 if (!chooseFromOneChoice(bitmap->config(), width, height)) {
382 return false;
383 }
384 }
385
386 SkAutoLockPixels alp(*bitmap);
387 WebPDecoderConfig config;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000388 if (!webp_get_config_resize_crop(&config, bitmap, rect,
389 this->shouldPremultiply())) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000390 return false;
391 }
392
393 // Decode the WebP image data stream using WebP incremental decoding for
394 // the specified cropped image-region.
395 if (!webp_idecode(this->fInputStream, &config)) {
396 return false;
397 }
398
399 if (!directDecode) {
400 cropBitmap(decodedBitmap, bitmap, sampleSize, region.x(), region.y(),
401 region.width(), region.height(), rect.x(), rect.y());
402 }
403 return true;
404}
405
406bool SkWEBPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap,
407 Mode mode) {
408#ifdef TIME_DECODE
409 AutoTimeMillis atm("WEBP Decode");
410#endif
411
412 int origWidth, origHeight, hasAlpha;
413 if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
414 return false;
415 }
416 this->fHasAlpha = hasAlpha;
417
418 const int sampleSize = this->getSampleSize();
419 SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000420 if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(),
421 sampler.scaledHeight())) {
422 return false;
423 }
424
scroggo@google.combc69ce92013-07-09 15:45:14 +0000425 // If only bounds are requested, done
426 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
427 return true;
428 }
429
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000430 if (!this->allocPixelRef(decodedBitmap, NULL)) {
431 return return_false(*decodedBitmap, "allocPixelRef");
432 }
433
434 SkAutoLockPixels alp(*decodedBitmap);
435
436 WebPDecoderConfig config;
437 if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000438 this->shouldPremultiply())) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000439 return false;
440 }
441
442 // Decode the WebP image data stream using WebP incremental decoding.
443 return webp_idecode(stream, &config);
444}
445
446///////////////////////////////////////////////////////////////////////////////
447
448typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width,
449 const SkPMColor* SK_RESTRICT ctable);
450
451static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
452 const SkPMColor*) {
453 const uint32_t* SK_RESTRICT src = (const uint32_t*)in;
454 for (int i = 0; i < width; ++i) {
455 const uint32_t c = *src++;
456 rgb[0] = SkGetPackedR32(c);
457 rgb[1] = SkGetPackedG32(c);
458 rgb[2] = SkGetPackedB32(c);
459 rgb += 3;
460 }
461}
462
463static void RGB_565_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
464 const SkPMColor*) {
465 const uint16_t* SK_RESTRICT src = (const uint16_t*)in;
466 for (int i = 0; i < width; ++i) {
467 const uint16_t c = *src++;
468 rgb[0] = SkPacked16ToR32(c);
469 rgb[1] = SkPacked16ToG32(c);
470 rgb[2] = SkPacked16ToB32(c);
471 rgb += 3;
472 }
473}
474
475static void ARGB_4444_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
476 const SkPMColor*) {
477 const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in;
478 for (int i = 0; i < width; ++i) {
479 const SkPMColor16 c = *src++;
480 rgb[0] = SkPacked4444ToR32(c);
481 rgb[1] = SkPacked4444ToG32(c);
482 rgb[2] = SkPacked4444ToB32(c);
483 rgb += 3;
484 }
485}
486
487static void Index8_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
488 const SkPMColor* SK_RESTRICT ctable) {
489 const uint8_t* SK_RESTRICT src = (const uint8_t*)in;
490 for (int i = 0; i < width; ++i) {
491 const uint32_t c = ctable[*src++];
492 rgb[0] = SkGetPackedR32(c);
493 rgb[1] = SkGetPackedG32(c);
494 rgb[2] = SkGetPackedB32(c);
495 rgb += 3;
496 }
497}
498
499static ScanlineImporter ChooseImporter(const SkBitmap::Config& config) {
500 switch (config) {
501 case SkBitmap::kARGB_8888_Config:
502 return ARGB_8888_To_RGB;
503 case SkBitmap::kRGB_565_Config:
504 return RGB_565_To_RGB;
505 case SkBitmap::kARGB_4444_Config:
506 return ARGB_4444_To_RGB;
507 case SkBitmap::kIndex8_Config:
508 return Index8_To_RGB;
509 default:
510 return NULL;
511 }
512}
513
514static int stream_writer(const uint8_t* data, size_t data_size,
515 const WebPPicture* const picture) {
516 SkWStream* const stream = (SkWStream*)picture->custom_ptr;
517 return stream->write(data, data_size) ? 1 : 0;
518}
519
520class SkWEBPImageEncoder : public SkImageEncoder {
521protected:
522 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE;
523
524private:
525 typedef SkImageEncoder INHERITED;
526};
527
528bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm,
529 int quality) {
reed@google.com44699382013-10-31 17:28:30 +0000530 const SkBitmap::Config config = bm.config();
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000531 const ScanlineImporter scanline_import = ChooseImporter(config);
532 if (NULL == scanline_import) {
533 return false;
534 }
535
536 SkAutoLockPixels alp(bm);
537 SkAutoLockColors ctLocker;
538 if (NULL == bm.getPixels()) {
539 return false;
540 }
541
542 WebPConfig webp_config;
commit-bot@chromium.orgbff83f62013-03-14 15:18:08 +0000543 if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000544 return false;
545 }
546
547 WebPPicture pic;
548 WebPPictureInit(&pic);
549 pic.width = bm.width();
550 pic.height = bm.height();
551 pic.writer = stream_writer;
552 pic.custom_ptr = (void*)stream;
553
554 const SkPMColor* colors = ctLocker.lockColors(bm);
555 const uint8_t* src = (uint8_t*)bm.getPixels();
556 const int rgbStride = pic.width * 3;
557
558 // Import (for each scanline) the bit-map image (in appropriate color-space)
559 // to RGB color space.
560 uint8_t* rgb = new uint8_t[rgbStride * pic.height];
561 for (int y = 0; y < pic.height; ++y) {
562 scanline_import(src + y * bm.rowBytes(), rgb + y * rgbStride,
563 pic.width, colors);
564 }
565
reed@google.combda74d32013-03-14 15:25:45 +0000566 bool ok = SkToBool(WebPPictureImportRGB(&pic, rgb, rgbStride));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000567 delete[] rgb;
568
569 ok = ok && WebPEncode(&webp_config, &pic);
570 WebPPictureFree(&pic);
571
572 return ok;
573}
574
575
576///////////////////////////////////////////////////////////////////////////////
577DEFINE_DECODER_CREATOR(WEBPImageDecoder);
578DEFINE_ENCODER_CREATOR(WEBPImageEncoder);
579///////////////////////////////////////////////////////////////////////////////
580
scroggo@google.comb5571b32013-09-25 21:34:24 +0000581static SkImageDecoder* sk_libwebp_dfactory(SkStreamRewindable* stream) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000582 int width, height, hasAlpha;
583 if (!webp_parse_header(stream, &width, &height, &hasAlpha)) {
584 return NULL;
585 }
586
587 // Magic matches, call decoder
588 return SkNEW(SkWEBPImageDecoder);
589}
590
scroggo@google.comb5571b32013-09-25 21:34:24 +0000591static SkImageDecoder::Format get_format_webp(SkStreamRewindable* stream) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000592 int width, height, hasAlpha;
593 if (webp_parse_header(stream, &width, &height, &hasAlpha)) {
594 return SkImageDecoder::kWEBP_Format;
595 }
596 return SkImageDecoder::kUnknown_Format;
597}
598
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000599static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) {
600 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NULL;
601}
602
mtklein@google.combd6343b2013-09-04 17:20:18 +0000603static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory);
604static SkImageDecoder_FormatReg gFormatReg(get_format_webp);
605static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory);