blob: c56933dd2a6c015b366908f0240fae31dc0677bd [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 {
reed@google.com57212f92013-12-30 14:40:38 +000083 int64_t size = sk_64_mul(*width, *height);
84 if (!sk_64_isS32(size)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000085 return false;
86 }
87 // now check that if we are 4-bytes per pixel, we also don't overflow
reed@google.com57212f92013-12-30 14:40:38 +000088 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000089 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
mtklein72c9faa2015-01-09 10:06:39 -0800107 Format getFormat() const SK_OVERRIDE {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000108 return kWEBP_Format;
109 }
110
111protected:
mtklein72c9faa2015-01-09 10:06:39 -0800112 bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *height) SK_OVERRIDE;
113 bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRIDE;
114 Result onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000115
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.
scroggo2a120802014-10-22 12:07:00 -0700165static void print_webp_error(const SkBitmap& bm, const char msg[]) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000166 SkDEBUGF(("libwebp error %s [%d %d]", msg, bm.width(), bm.height()));
scroggo2a120802014-10-22 12:07:00 -0700167}
168
169static bool return_false(const SkBitmap& bm, const char msg[]) {
170 print_webp_error(bm, msg);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000171 return false; // must always return false
172}
173
scroggo2a120802014-10-22 12:07:00 -0700174static SkImageDecoder::Result return_failure(const SkBitmap& bm, const char msg[]) {
175 print_webp_error(bm, msg);
176 return SkImageDecoder::kFailure; // must always return kFailure
177}
178
179///////////////////////////////////////////////////////////////////////////////
180
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000181static WEBP_CSP_MODE webp_decode_mode(const SkBitmap* decodedBitmap, bool premultiply) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000182 WEBP_CSP_MODE mode = MODE_LAST;
reed0689d7b2014-06-14 05:30:20 -0700183 const SkColorType ct = decodedBitmap->colorType();
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000184
reed0689d7b2014-06-14 05:30:20 -0700185 if (ct == kN32_SkColorType) {
halcanary@google.comdedd44a2013-12-20 16:35:22 +0000186 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
187 mode = premultiply ? MODE_bgrA : MODE_BGRA;
188 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
189 mode = premultiply ? MODE_rgbA : MODE_RGBA;
190 #else
191 #error "Skia uses BGRA or RGBA byte order"
192 #endif
reed0689d7b2014-06-14 05:30:20 -0700193 } else if (ct == kARGB_4444_SkColorType) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000194 mode = premultiply ? MODE_rgbA_4444 : MODE_RGBA_4444;
reed0689d7b2014-06-14 05:30:20 -0700195 } else if (ct == kRGB_565_SkColorType) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000196 mode = MODE_RGB_565;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000197 }
198 SkASSERT(MODE_LAST != mode);
199 return mode;
200}
201
202// Incremental WebP image decoding. Reads input buffer of 64K size iteratively
203// and decodes this block to appropriate color-space as per config object.
204static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) {
205 WebPIDecoder* idec = WebPIDecode(NULL, 0, config);
206 if (NULL == idec) {
207 WebPFreeDecBuffer(&config->output);
208 return false;
209 }
210
scroggo@google.com4d213ab2013-08-28 13:08:54 +0000211 if (!stream->rewind()) {
212 SkDebugf("Failed to rewind webp stream!");
213 return false;
214 }
scroggo@google.com3c8730a2013-08-21 14:56:09 +0000215 const size_t readBufferSize = stream->hasLength() ?
216 SkTMin(stream->getLength(), WEBP_IDECODE_BUFFER_SZ) : WEBP_IDECODE_BUFFER_SZ;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000217 SkAutoMalloc srcStorage(readBufferSize);
218 unsigned char* input = (uint8_t*)srcStorage.get();
219 if (NULL == input) {
220 WebPIDelete(idec);
221 WebPFreeDecBuffer(&config->output);
222 return false;
223 }
224
scroggo@google.com80e18c92013-08-09 19:22:00 +0000225 bool success = true;
226 VP8StatusCode status = VP8_STATUS_SUSPENDED;
227 do {
228 const size_t bytesRead = stream->read(input, readBufferSize);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000229 if (0 == bytesRead) {
scroggo@google.com80e18c92013-08-09 19:22:00 +0000230 success = false;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000231 break;
232 }
233
scroggo@google.com80e18c92013-08-09 19:22:00 +0000234 status = WebPIAppend(idec, input, bytesRead);
235 if (VP8_STATUS_OK != status && VP8_STATUS_SUSPENDED != status) {
236 success = false;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000237 break;
238 }
scroggo@google.com80e18c92013-08-09 19:22:00 +0000239 } while (VP8_STATUS_OK != status);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000240 srcStorage.free();
241 WebPIDelete(idec);
242 WebPFreeDecBuffer(&config->output);
243
scroggo@google.com80e18c92013-08-09 19:22:00 +0000244 return success;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000245}
246
247static bool webp_get_config_resize(WebPDecoderConfig* config,
248 SkBitmap* decodedBitmap,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000249 int width, int height, bool premultiply) {
250 WEBP_CSP_MODE mode = webp_decode_mode(decodedBitmap, premultiply);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000251 if (MODE_LAST == mode) {
252 return false;
253 }
254
255 if (0 == WebPInitDecoderConfig(config)) {
256 return false;
257 }
258
259 config->output.colorspace = mode;
260 config->output.u.RGBA.rgba = (uint8_t*)decodedBitmap->getPixels();
robertphillips@google.com8b169312013-10-15 17:47:36 +0000261 config->output.u.RGBA.stride = (int) decodedBitmap->rowBytes();
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000262 config->output.u.RGBA.size = decodedBitmap->getSize();
263 config->output.is_external_memory = 1;
264
265 if (width != decodedBitmap->width() || height != decodedBitmap->height()) {
266 config->options.use_scaling = 1;
267 config->options.scaled_width = decodedBitmap->width();
268 config->options.scaled_height = decodedBitmap->height();
269 }
270
271 return true;
272}
273
274static bool webp_get_config_resize_crop(WebPDecoderConfig* config,
275 SkBitmap* decodedBitmap,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000276 const SkIRect& region, bool premultiply) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000277
278 if (!webp_get_config_resize(config, decodedBitmap, region.width(),
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000279 region.height(), premultiply)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000280 return false;
281 }
282
283 config->options.use_cropping = 1;
284 config->options.crop_left = region.fLeft;
285 config->options.crop_top = region.fTop;
286 config->options.crop_width = region.width();
287 config->options.crop_height = region.height();
288
289 return true;
290}
291
reed6c225732014-06-09 19:52:07 -0700292bool SkWEBPImageDecoder::setDecodeConfig(SkBitmap* decodedBitmap, int width, int height) {
293 SkColorType colorType = this->getPrefColorType(k32Bit_SrcDepth, SkToBool(fHasAlpha));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000294
295 // YUV converter supports output in RGB565, RGBA4444 and RGBA8888 formats.
296 if (fHasAlpha) {
reed6c225732014-06-09 19:52:07 -0700297 if (colorType != kARGB_4444_SkColorType) {
298 colorType = kN32_SkColorType;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000299 }
300 } else {
reed6c225732014-06-09 19:52:07 -0700301 if (colorType != kRGB_565_SkColorType && colorType != kARGB_4444_SkColorType) {
302 colorType = kN32_SkColorType;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000303 }
304 }
305
reed6c225732014-06-09 19:52:07 -0700306 SkAlphaType alphaType = kOpaque_SkAlphaType;
commit-bot@chromium.org915b9722014-04-24 18:55:13 +0000307 if (SkToBool(fHasAlpha)) {
308 if (this->getRequireUnpremultipliedColors()) {
reed6c225732014-06-09 19:52:07 -0700309 alphaType = kUnpremul_SkAlphaType;
commit-bot@chromium.org915b9722014-04-24 18:55:13 +0000310 } else {
reed6c225732014-06-09 19:52:07 -0700311 alphaType = kPremul_SkAlphaType;
commit-bot@chromium.org915b9722014-04-24 18:55:13 +0000312 }
commit-bot@chromium.org915b9722014-04-24 18:55:13 +0000313 }
reed6c225732014-06-09 19:52:07 -0700314 return decodedBitmap->setInfo(SkImageInfo::Make(width, height, colorType, alphaType));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000315}
316
scroggo@google.comb5571b32013-09-25 21:34:24 +0000317bool SkWEBPImageDecoder::onBuildTileIndex(SkStreamRewindable* stream,
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000318 int *width, int *height) {
319 int origWidth, origHeight, hasAlpha;
320 if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
321 return false;
322 }
323
scroggo@google.com4d213ab2013-08-28 13:08:54 +0000324 if (!stream->rewind()) {
325 SkDebugf("Failed to rewind webp stream!");
326 return false;
327 }
328
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000329 *width = origWidth;
330 *height = origHeight;
331
332 SkRefCnt_SafeAssign(this->fInputStream, stream);
333 this->fOrigWidth = origWidth;
334 this->fOrigHeight = origHeight;
335 this->fHasAlpha = hasAlpha;
336
337 return true;
338}
339
340static bool is_config_compatible(const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -0700341 const SkColorType ct = bitmap.colorType();
342 return ct == kARGB_4444_SkColorType || ct == kRGB_565_SkColorType || ct == kN32_SkColorType;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000343}
344
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000345bool SkWEBPImageDecoder::onDecodeSubset(SkBitmap* decodedBitmap,
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000346 const SkIRect& region) {
347 SkIRect rect = SkIRect::MakeWH(fOrigWidth, fOrigHeight);
348
349 if (!rect.intersect(region)) {
350 // If the requested region is entirely outsides the image, return false
351 return false;
352 }
353
354 const int sampleSize = this->getSampleSize();
355 SkScaledBitmapSampler sampler(rect.width(), rect.height(), sampleSize);
356 const int width = sampler.scaledWidth();
357 const int height = sampler.scaledHeight();
358
359 // The image can be decoded directly to decodedBitmap if
360 // 1. the region is within the image range
361 // 2. bitmap's config is compatible
362 // 3. bitmap's size is same as the required region (after sampled)
363 bool directDecode = (rect == region) &&
364 (decodedBitmap->isNull() ||
365 (is_config_compatible(*decodedBitmap) &&
366 (decodedBitmap->width() == width) &&
367 (decodedBitmap->height() == height)));
commit-bot@chromium.orga9f142e2013-05-09 16:15:20 +0000368
369 SkBitmap tmpBitmap;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000370 SkBitmap *bitmap = decodedBitmap;
371
372 if (!directDecode) {
commit-bot@chromium.orga9f142e2013-05-09 16:15:20 +0000373 bitmap = &tmpBitmap;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000374 }
375
376 if (bitmap->isNull()) {
377 if (!setDecodeConfig(bitmap, width, height)) {
378 return false;
379 }
380 // alloc from native heap if it is a temp bitmap. (prevent GC)
381 bool allocResult = (bitmap == decodedBitmap)
382 ? allocPixelRef(bitmap, NULL)
reed84825042014-09-02 12:50:45 -0700383 : bitmap->tryAllocPixels();
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000384 if (!allocResult) {
385 return return_false(*decodedBitmap, "allocPixelRef");
386 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000387 }
388
389 SkAutoLockPixels alp(*bitmap);
390 WebPDecoderConfig config;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000391 if (!webp_get_config_resize_crop(&config, bitmap, rect,
392 this->shouldPremultiply())) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000393 return false;
394 }
395
396 // Decode the WebP image data stream using WebP incremental decoding for
397 // the specified cropped image-region.
398 if (!webp_idecode(this->fInputStream, &config)) {
399 return false;
400 }
401
402 if (!directDecode) {
403 cropBitmap(decodedBitmap, bitmap, sampleSize, region.x(), region.y(),
404 region.width(), region.height(), rect.x(), rect.y());
405 }
406 return true;
407}
408
scroggo2a120802014-10-22 12:07:00 -0700409SkImageDecoder::Result SkWEBPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap,
410 Mode mode) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000411#ifdef TIME_DECODE
412 AutoTimeMillis atm("WEBP Decode");
413#endif
414
415 int origWidth, origHeight, hasAlpha;
416 if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
scroggo2a120802014-10-22 12:07:00 -0700417 return kFailure;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000418 }
419 this->fHasAlpha = hasAlpha;
420
421 const int sampleSize = this->getSampleSize();
422 SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000423 if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(),
424 sampler.scaledHeight())) {
scroggo2a120802014-10-22 12:07:00 -0700425 return kFailure;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000426 }
427
scroggo@google.combc69ce92013-07-09 15:45:14 +0000428 // If only bounds are requested, done
429 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
scroggo2a120802014-10-22 12:07:00 -0700430 return kSuccess;
scroggo@google.combc69ce92013-07-09 15:45:14 +0000431 }
432
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000433 if (!this->allocPixelRef(decodedBitmap, NULL)) {
scroggo2a120802014-10-22 12:07:00 -0700434 return return_failure(*decodedBitmap, "allocPixelRef");
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000435 }
436
437 SkAutoLockPixels alp(*decodedBitmap);
438
439 WebPDecoderConfig config;
440 if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000441 this->shouldPremultiply())) {
scroggo2a120802014-10-22 12:07:00 -0700442 return kFailure;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000443 }
444
445 // Decode the WebP image data stream using WebP incremental decoding.
scroggo2a120802014-10-22 12:07:00 -0700446 return webp_idecode(stream, &config) ? kSuccess : kFailure;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000447}
448
449///////////////////////////////////////////////////////////////////////////////
450
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000451#include "SkUnPreMultiply.h"
452
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000453typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width,
454 const SkPMColor* SK_RESTRICT ctable);
455
456static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
457 const SkPMColor*) {
458 const uint32_t* SK_RESTRICT src = (const uint32_t*)in;
459 for (int i = 0; i < width; ++i) {
460 const uint32_t c = *src++;
461 rgb[0] = SkGetPackedR32(c);
462 rgb[1] = SkGetPackedG32(c);
463 rgb[2] = SkGetPackedB32(c);
464 rgb += 3;
465 }
466}
467
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000468static void ARGB_8888_To_RGBA(const uint8_t* in, uint8_t* rgb, int width,
469 const SkPMColor*) {
470 const uint32_t* SK_RESTRICT src = (const uint32_t*)in;
471 const SkUnPreMultiply::Scale* SK_RESTRICT table =
472 SkUnPreMultiply::GetScaleTable();
473 for (int i = 0; i < width; ++i) {
474 const uint32_t c = *src++;
475 uint8_t a = SkGetPackedA32(c);
476 uint8_t r = SkGetPackedR32(c);
477 uint8_t g = SkGetPackedG32(c);
478 uint8_t b = SkGetPackedB32(c);
479 if (0 != a && 255 != a) {
480 SkUnPreMultiply::Scale scale = table[a];
481 r = SkUnPreMultiply::ApplyScale(scale, r);
482 g = SkUnPreMultiply::ApplyScale(scale, g);
483 b = SkUnPreMultiply::ApplyScale(scale, b);
484 }
485 rgb[0] = r;
486 rgb[1] = g;
487 rgb[2] = b;
488 rgb[3] = a;
489 rgb += 4;
490 }
491}
492
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000493static void RGB_565_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
494 const SkPMColor*) {
495 const uint16_t* SK_RESTRICT src = (const uint16_t*)in;
496 for (int i = 0; i < width; ++i) {
497 const uint16_t c = *src++;
498 rgb[0] = SkPacked16ToR32(c);
499 rgb[1] = SkPacked16ToG32(c);
500 rgb[2] = SkPacked16ToB32(c);
501 rgb += 3;
502 }
503}
504
505static void ARGB_4444_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
506 const SkPMColor*) {
507 const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in;
508 for (int i = 0; i < width; ++i) {
509 const SkPMColor16 c = *src++;
510 rgb[0] = SkPacked4444ToR32(c);
511 rgb[1] = SkPacked4444ToG32(c);
512 rgb[2] = SkPacked4444ToB32(c);
513 rgb += 3;
514 }
515}
516
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000517static void ARGB_4444_To_RGBA(const uint8_t* in, uint8_t* rgb, int width,
518 const SkPMColor*) {
519 const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in;
520 const SkUnPreMultiply::Scale* SK_RESTRICT table =
521 SkUnPreMultiply::GetScaleTable();
522 for (int i = 0; i < width; ++i) {
523 const SkPMColor16 c = *src++;
524 uint8_t a = SkPacked4444ToA32(c);
525 uint8_t r = SkPacked4444ToR32(c);
526 uint8_t g = SkPacked4444ToG32(c);
527 uint8_t b = SkPacked4444ToB32(c);
528 if (0 != a && 255 != a) {
529 SkUnPreMultiply::Scale scale = table[a];
530 r = SkUnPreMultiply::ApplyScale(scale, r);
531 g = SkUnPreMultiply::ApplyScale(scale, g);
532 b = SkUnPreMultiply::ApplyScale(scale, b);
533 }
534 rgb[0] = r;
535 rgb[1] = g;
536 rgb[2] = b;
537 rgb[3] = a;
538 rgb += 4;
539 }
540}
541
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000542static void Index8_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
543 const SkPMColor* SK_RESTRICT ctable) {
544 const uint8_t* SK_RESTRICT src = (const uint8_t*)in;
545 for (int i = 0; i < width; ++i) {
546 const uint32_t c = ctable[*src++];
547 rgb[0] = SkGetPackedR32(c);
548 rgb[1] = SkGetPackedG32(c);
549 rgb[2] = SkGetPackedB32(c);
550 rgb += 3;
551 }
552}
553
reed0689d7b2014-06-14 05:30:20 -0700554static ScanlineImporter ChooseImporter(SkColorType ct, bool hasAlpha, int* bpp) {
555 switch (ct) {
556 case kN32_SkColorType:
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000557 if (hasAlpha) {
558 *bpp = 4;
559 return ARGB_8888_To_RGBA;
560 } else {
561 *bpp = 3;
562 return ARGB_8888_To_RGB;
563 }
reed0689d7b2014-06-14 05:30:20 -0700564 case kARGB_4444_SkColorType:
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000565 if (hasAlpha) {
566 *bpp = 4;
567 return ARGB_4444_To_RGBA;
568 } else {
569 *bpp = 3;
570 return ARGB_4444_To_RGB;
571 }
reed0689d7b2014-06-14 05:30:20 -0700572 case kRGB_565_SkColorType:
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000573 *bpp = 3;
574 return RGB_565_To_RGB;
reed0689d7b2014-06-14 05:30:20 -0700575 case kIndex_8_SkColorType:
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000576 *bpp = 3;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000577 return Index8_To_RGB;
578 default:
579 return NULL;
580 }
581}
582
583static int stream_writer(const uint8_t* data, size_t data_size,
584 const WebPPicture* const picture) {
585 SkWStream* const stream = (SkWStream*)picture->custom_ptr;
586 return stream->write(data, data_size) ? 1 : 0;
587}
588
589class SkWEBPImageEncoder : public SkImageEncoder {
590protected:
mtklein72c9faa2015-01-09 10:06:39 -0800591 bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000592
593private:
594 typedef SkImageEncoder INHERITED;
595};
596
597bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm,
598 int quality) {
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000599 const bool hasAlpha = !bm.isOpaque();
600 int bpp = -1;
reed0689d7b2014-06-14 05:30:20 -0700601 const ScanlineImporter scanline_import = ChooseImporter(bm.colorType(), hasAlpha, &bpp);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000602 if (NULL == scanline_import) {
603 return false;
604 }
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000605 if (-1 == bpp) {
606 return false;
607 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000608
609 SkAutoLockPixels alp(bm);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000610 if (NULL == bm.getPixels()) {
611 return false;
612 }
613
614 WebPConfig webp_config;
commit-bot@chromium.orgbff83f62013-03-14 15:18:08 +0000615 if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000616 return false;
617 }
618
619 WebPPicture pic;
620 WebPPictureInit(&pic);
621 pic.width = bm.width();
622 pic.height = bm.height();
623 pic.writer = stream_writer;
624 pic.custom_ptr = (void*)stream;
625
mtklein775b8192014-12-02 09:11:25 -0800626 const SkPMColor* colors = bm.getColorTable() ? bm.getColorTable()->readColors() : NULL;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000627 const uint8_t* src = (uint8_t*)bm.getPixels();
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000628 const int rgbStride = pic.width * bpp;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000629
630 // Import (for each scanline) the bit-map image (in appropriate color-space)
631 // to RGB color space.
632 uint8_t* rgb = new uint8_t[rgbStride * pic.height];
633 for (int y = 0; y < pic.height; ++y) {
634 scanline_import(src + y * bm.rowBytes(), rgb + y * rgbStride,
635 pic.width, colors);
636 }
637
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000638 bool ok;
639 if (bpp == 3) {
640 ok = SkToBool(WebPPictureImportRGB(&pic, rgb, rgbStride));
641 } else {
642 ok = SkToBool(WebPPictureImportRGBA(&pic, rgb, rgbStride));
643 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000644 delete[] rgb;
645
646 ok = ok && WebPEncode(&webp_config, &pic);
647 WebPPictureFree(&pic);
648
649 return ok;
650}
651
652
653///////////////////////////////////////////////////////////////////////////////
654DEFINE_DECODER_CREATOR(WEBPImageDecoder);
655DEFINE_ENCODER_CREATOR(WEBPImageEncoder);
656///////////////////////////////////////////////////////////////////////////////
657
scroggo@google.comb5571b32013-09-25 21:34:24 +0000658static SkImageDecoder* sk_libwebp_dfactory(SkStreamRewindable* stream) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000659 int width, height, hasAlpha;
660 if (!webp_parse_header(stream, &width, &height, &hasAlpha)) {
661 return NULL;
662 }
663
664 // Magic matches, call decoder
665 return SkNEW(SkWEBPImageDecoder);
666}
667
scroggo@google.comb5571b32013-09-25 21:34:24 +0000668static SkImageDecoder::Format get_format_webp(SkStreamRewindable* stream) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000669 int width, height, hasAlpha;
670 if (webp_parse_header(stream, &width, &height, &hasAlpha)) {
671 return SkImageDecoder::kWEBP_Format;
672 }
673 return SkImageDecoder::kUnknown_Format;
674}
675
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000676static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) {
677 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NULL;
678}
679
mtklein@google.combd6343b2013-09-04 17:20:18 +0000680static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory);
681static SkImageDecoder_FormatReg gFormatReg(get_format_webp);
682static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory);