1/*
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"
24
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;
113    virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRIDE;
114    virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
115
116private:
117    /**
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
126    bool setDecodeConfig(SkBitmap* decodedBitmap, int width, int height);
127
128    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
170static WEBP_CSP_MODE webp_decode_mode(const SkBitmap* decodedBitmap, bool premultiply) {
171    WEBP_CSP_MODE mode = MODE_LAST;
172    SkBitmap::Config config = decodedBitmap->config();
173
174    if (config == SkBitmap::kARGB_8888_Config) {
175        mode = premultiply ? MODE_rgbA : MODE_RGBA;
176    } else if (config == SkBitmap::kARGB_4444_Config) {
177        mode = premultiply ? MODE_rgbA_4444 : MODE_RGBA_4444;
178    } else if (config == SkBitmap::kRGB_565_Config) {
179        mode = MODE_RGB_565;
180    }
181    SkASSERT(MODE_LAST != mode);
182    return mode;
183}
184
185// Incremental WebP image decoding. Reads input buffer of 64K size iteratively
186// and decodes this block to appropriate color-space as per config object.
187static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) {
188    WebPIDecoder* idec = WebPIDecode(NULL, 0, config);
189    if (NULL == idec) {
190        WebPFreeDecBuffer(&config->output);
191        return false;
192    }
193
194    stream->rewind();
195    const uint32_t contentSize = stream->getLength();
196    const uint32_t readBufferSize = (contentSize < WEBP_IDECODE_BUFFER_SZ) ?
197                                       contentSize : WEBP_IDECODE_BUFFER_SZ;
198    SkAutoMalloc srcStorage(readBufferSize);
199    unsigned char* input = (uint8_t*)srcStorage.get();
200    if (NULL == input) {
201        WebPIDelete(idec);
202        WebPFreeDecBuffer(&config->output);
203        return false;
204    }
205
206    bool success = true;
207    VP8StatusCode status = VP8_STATUS_SUSPENDED;
208    do {
209        const size_t bytesRead = stream->read(input, readBufferSize);
210        if (0 == bytesRead) {
211            success = false;
212            break;
213        }
214
215        status = WebPIAppend(idec, input, bytesRead);
216        if (VP8_STATUS_OK != status && VP8_STATUS_SUSPENDED != status) {
217            success = false;
218            break;
219        }
220    } while (VP8_STATUS_OK != status);
221    srcStorage.free();
222    WebPIDelete(idec);
223    WebPFreeDecBuffer(&config->output);
224
225    return success;
226}
227
228static bool webp_get_config_resize(WebPDecoderConfig* config,
229                                   SkBitmap* decodedBitmap,
230                                   int width, int height, bool premultiply) {
231    WEBP_CSP_MODE mode = webp_decode_mode(decodedBitmap, premultiply);
232    if (MODE_LAST == mode) {
233        return false;
234    }
235
236    if (0 == WebPInitDecoderConfig(config)) {
237        return false;
238    }
239
240    config->output.colorspace = mode;
241    config->output.u.RGBA.rgba = (uint8_t*)decodedBitmap->getPixels();
242    config->output.u.RGBA.stride = decodedBitmap->rowBytes();
243    config->output.u.RGBA.size = decodedBitmap->getSize();
244    config->output.is_external_memory = 1;
245
246    if (width != decodedBitmap->width() || height != decodedBitmap->height()) {
247        config->options.use_scaling = 1;
248        config->options.scaled_width = decodedBitmap->width();
249        config->options.scaled_height = decodedBitmap->height();
250    }
251
252    return true;
253}
254
255static bool webp_get_config_resize_crop(WebPDecoderConfig* config,
256                                        SkBitmap* decodedBitmap,
257                                        const SkIRect& region, bool premultiply) {
258
259    if (!webp_get_config_resize(config, decodedBitmap, region.width(),
260                                region.height(), premultiply)) {
261      return false;
262    }
263
264    config->options.use_cropping = 1;
265    config->options.crop_left = region.fLeft;
266    config->options.crop_top = region.fTop;
267    config->options.crop_width = region.width();
268    config->options.crop_height = region.height();
269
270    return true;
271}
272
273bool SkWEBPImageDecoder::setDecodeConfig(SkBitmap* decodedBitmap,
274                                         int width, int height) {
275    SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, SkToBool(fHasAlpha));
276
277    // YUV converter supports output in RGB565, RGBA4444 and RGBA8888 formats.
278    if (fHasAlpha) {
279        if (config != SkBitmap::kARGB_4444_Config) {
280            config = SkBitmap::kARGB_8888_Config;
281        }
282    } else {
283        if (config != SkBitmap::kRGB_565_Config &&
284            config != SkBitmap::kARGB_4444_Config) {
285            config = SkBitmap::kARGB_8888_Config;
286        }
287    }
288
289    if (!this->chooseFromOneChoice(config, width, height)) {
290        return false;
291    }
292
293    decodedBitmap->setConfig(config, width, height, 0);
294
295    decodedBitmap->setIsOpaque(!fHasAlpha);
296
297    return true;
298}
299
300bool SkWEBPImageDecoder::onBuildTileIndex(SkStream* stream,
301                                          int *width, int *height) {
302    int origWidth, origHeight, hasAlpha;
303    if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
304        return false;
305    }
306
307    stream->rewind();
308    *width = origWidth;
309    *height = origHeight;
310
311    SkRefCnt_SafeAssign(this->fInputStream, stream);
312    this->fOrigWidth = origWidth;
313    this->fOrigHeight = origHeight;
314    this->fHasAlpha = hasAlpha;
315
316    return true;
317}
318
319static bool is_config_compatible(const SkBitmap& bitmap) {
320    SkBitmap::Config config = bitmap.config();
321    return config == SkBitmap::kARGB_4444_Config ||
322           config == SkBitmap::kRGB_565_Config ||
323           config == SkBitmap::kARGB_8888_Config;
324}
325
326bool SkWEBPImageDecoder::onDecodeSubset(SkBitmap* decodedBitmap,
327                                        const SkIRect& region) {
328    SkIRect rect = SkIRect::MakeWH(fOrigWidth, fOrigHeight);
329
330    if (!rect.intersect(region)) {
331        // If the requested region is entirely outsides the image, return false
332        return false;
333    }
334
335    const int sampleSize = this->getSampleSize();
336    SkScaledBitmapSampler sampler(rect.width(), rect.height(), sampleSize);
337    const int width = sampler.scaledWidth();
338    const int height = sampler.scaledHeight();
339
340    // The image can be decoded directly to decodedBitmap if
341    //   1. the region is within the image range
342    //   2. bitmap's config is compatible
343    //   3. bitmap's size is same as the required region (after sampled)
344    bool directDecode = (rect == region) &&
345                        (decodedBitmap->isNull() ||
346                         (is_config_compatible(*decodedBitmap) &&
347                         (decodedBitmap->width() == width) &&
348                         (decodedBitmap->height() == height)));
349
350    SkBitmap tmpBitmap;
351    SkBitmap *bitmap = decodedBitmap;
352
353    if (!directDecode) {
354        bitmap = &tmpBitmap;
355    }
356
357    if (bitmap->isNull()) {
358        if (!setDecodeConfig(bitmap, width, height)) {
359            return false;
360        }
361        // alloc from native heap if it is a temp bitmap. (prevent GC)
362        bool allocResult = (bitmap == decodedBitmap)
363                               ? allocPixelRef(bitmap, NULL)
364                               : bitmap->allocPixels();
365        if (!allocResult) {
366            return return_false(*decodedBitmap, "allocPixelRef");
367        }
368    } else {
369        // This is also called in setDecodeConfig in above block.
370        // i.e., when bitmap->isNull() is true.
371        if (!chooseFromOneChoice(bitmap->config(), width, height)) {
372            return false;
373        }
374    }
375
376    SkAutoLockPixels alp(*bitmap);
377    WebPDecoderConfig config;
378    if (!webp_get_config_resize_crop(&config, bitmap, rect,
379                                     this->shouldPremultiply())) {
380        return false;
381    }
382
383    // Decode the WebP image data stream using WebP incremental decoding for
384    // the specified cropped image-region.
385    if (!webp_idecode(this->fInputStream, &config)) {
386        return false;
387    }
388
389    if (!directDecode) {
390        cropBitmap(decodedBitmap, bitmap, sampleSize, region.x(), region.y(),
391                   region.width(), region.height(), rect.x(), rect.y());
392    }
393    return true;
394}
395
396bool SkWEBPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap,
397                                  Mode mode) {
398#ifdef TIME_DECODE
399    AutoTimeMillis atm("WEBP Decode");
400#endif
401
402    int origWidth, origHeight, hasAlpha;
403    if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
404        return false;
405    }
406    this->fHasAlpha = hasAlpha;
407
408    const int sampleSize = this->getSampleSize();
409    SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize);
410    if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(),
411                         sampler.scaledHeight())) {
412        return false;
413    }
414
415    // If only bounds are requested, done
416    if (SkImageDecoder::kDecodeBounds_Mode == mode) {
417        return true;
418    }
419
420    if (!this->allocPixelRef(decodedBitmap, NULL)) {
421        return return_false(*decodedBitmap, "allocPixelRef");
422    }
423
424    SkAutoLockPixels alp(*decodedBitmap);
425
426    WebPDecoderConfig config;
427    if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight,
428                                this->shouldPremultiply())) {
429        return false;
430    }
431
432    // Decode the WebP image data stream using WebP incremental decoding.
433    return webp_idecode(stream, &config);
434}
435
436///////////////////////////////////////////////////////////////////////////////
437
438typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width,
439                                 const SkPMColor* SK_RESTRICT ctable);
440
441static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
442                             const SkPMColor*) {
443  const uint32_t* SK_RESTRICT src = (const uint32_t*)in;
444  for (int i = 0; i < width; ++i) {
445      const uint32_t c = *src++;
446      rgb[0] = SkGetPackedR32(c);
447      rgb[1] = SkGetPackedG32(c);
448      rgb[2] = SkGetPackedB32(c);
449      rgb += 3;
450  }
451}
452
453static void RGB_565_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
454                           const SkPMColor*) {
455  const uint16_t* SK_RESTRICT src = (const uint16_t*)in;
456  for (int i = 0; i < width; ++i) {
457      const uint16_t c = *src++;
458      rgb[0] = SkPacked16ToR32(c);
459      rgb[1] = SkPacked16ToG32(c);
460      rgb[2] = SkPacked16ToB32(c);
461      rgb += 3;
462  }
463}
464
465static void ARGB_4444_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
466                             const SkPMColor*) {
467  const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in;
468  for (int i = 0; i < width; ++i) {
469      const SkPMColor16 c = *src++;
470      rgb[0] = SkPacked4444ToR32(c);
471      rgb[1] = SkPacked4444ToG32(c);
472      rgb[2] = SkPacked4444ToB32(c);
473      rgb += 3;
474  }
475}
476
477static void Index8_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
478                          const SkPMColor* SK_RESTRICT ctable) {
479  const uint8_t* SK_RESTRICT src = (const uint8_t*)in;
480  for (int i = 0; i < width; ++i) {
481      const uint32_t c = ctable[*src++];
482      rgb[0] = SkGetPackedR32(c);
483      rgb[1] = SkGetPackedG32(c);
484      rgb[2] = SkGetPackedB32(c);
485      rgb += 3;
486  }
487}
488
489static ScanlineImporter ChooseImporter(const SkBitmap::Config& config) {
490    switch (config) {
491        case SkBitmap::kARGB_8888_Config:
492            return ARGB_8888_To_RGB;
493        case SkBitmap::kRGB_565_Config:
494            return RGB_565_To_RGB;
495        case SkBitmap::kARGB_4444_Config:
496            return ARGB_4444_To_RGB;
497        case SkBitmap::kIndex8_Config:
498            return Index8_To_RGB;
499        default:
500            return NULL;
501    }
502}
503
504static int stream_writer(const uint8_t* data, size_t data_size,
505                         const WebPPicture* const picture) {
506  SkWStream* const stream = (SkWStream*)picture->custom_ptr;
507  return stream->write(data, data_size) ? 1 : 0;
508}
509
510class SkWEBPImageEncoder : public SkImageEncoder {
511protected:
512    virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE;
513
514private:
515    typedef SkImageEncoder INHERITED;
516};
517
518bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm,
519                                  int quality) {
520    const SkBitmap::Config config = bm.getConfig();
521    const ScanlineImporter scanline_import = ChooseImporter(config);
522    if (NULL == scanline_import) {
523        return false;
524    }
525
526    SkAutoLockPixels alp(bm);
527    SkAutoLockColors ctLocker;
528    if (NULL == bm.getPixels()) {
529        return false;
530    }
531
532    WebPConfig webp_config;
533    if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) {
534        return false;
535    }
536
537    WebPPicture pic;
538    WebPPictureInit(&pic);
539    pic.width = bm.width();
540    pic.height = bm.height();
541    pic.writer = stream_writer;
542    pic.custom_ptr = (void*)stream;
543
544    const SkPMColor* colors = ctLocker.lockColors(bm);
545    const uint8_t* src = (uint8_t*)bm.getPixels();
546    const int rgbStride = pic.width * 3;
547
548    // Import (for each scanline) the bit-map image (in appropriate color-space)
549    // to RGB color space.
550    uint8_t* rgb = new uint8_t[rgbStride * pic.height];
551    for (int y = 0; y < pic.height; ++y) {
552        scanline_import(src + y * bm.rowBytes(), rgb + y * rgbStride,
553                        pic.width, colors);
554    }
555
556    bool ok = SkToBool(WebPPictureImportRGB(&pic, rgb, rgbStride));
557    delete[] rgb;
558
559    ok = ok && WebPEncode(&webp_config, &pic);
560    WebPPictureFree(&pic);
561
562    return ok;
563}
564
565
566///////////////////////////////////////////////////////////////////////////////
567DEFINE_DECODER_CREATOR(WEBPImageDecoder);
568DEFINE_ENCODER_CREATOR(WEBPImageEncoder);
569///////////////////////////////////////////////////////////////////////////////
570
571#include "SkTRegistry.h"
572
573static SkImageDecoder* sk_libwebp_dfactory(SkStream* stream) {
574    int width, height, hasAlpha;
575    if (!webp_parse_header(stream, &width, &height, &hasAlpha)) {
576        return NULL;
577    }
578
579    // Magic matches, call decoder
580    return SkNEW(SkWEBPImageDecoder);
581}
582
583static SkImageDecoder::Format get_format_webp(SkStream* stream) {
584    int width, height, hasAlpha;
585    if (webp_parse_header(stream, &width, &height, &hasAlpha)) {
586        return SkImageDecoder::kWEBP_Format;
587    }
588    return SkImageDecoder::kUnknown_Format;
589}
590
591static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) {
592      return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NULL;
593}
594
595static SkTRegistry<SkImageDecoder*, SkStream*> gDReg(sk_libwebp_dfactory);
596static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_webp);
597static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libwebp_efactory);
598