michael@0: /* michael@0: * Copyright 2010, The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #include "SkImageDecoder.h" michael@0: #include "SkImageEncoder.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkScaledBitmapSampler.h" michael@0: #include "SkStream.h" michael@0: #include "SkTemplates.h" michael@0: #include "SkUtils.h" michael@0: michael@0: // A WebP decoder only, on top of (subset of) libwebp michael@0: // For more information on WebP image format, and libwebp library, see: michael@0: // http://code.google.com/speed/webp/ michael@0: // http://www.webmproject.org/code/#libwebp_webp_image_decoder_library michael@0: // http://review.webmproject.org/gitweb?p=libwebp.git michael@0: michael@0: #include michael@0: extern "C" { michael@0: // If moving libwebp out of skia source tree, path for webp headers must be michael@0: // updated accordingly. Here, we enforce using local copy in webp sub-directory. michael@0: #include "webp/decode.h" michael@0: #include "webp/encode.h" michael@0: } michael@0: michael@0: // this enables timing code to report milliseconds for a decode michael@0: //#define TIME_DECODE michael@0: michael@0: ////////////////////////////////////////////////////////////////////////// michael@0: ////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // Define VP8 I/O on top of Skia stream michael@0: michael@0: ////////////////////////////////////////////////////////////////////////// michael@0: ////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static const size_t WEBP_VP8_HEADER_SIZE = 64; michael@0: static const size_t WEBP_IDECODE_BUFFER_SZ = (1 << 16); michael@0: michael@0: // Parse headers of RIFF container, and check for valid Webp (VP8) content. michael@0: static bool webp_parse_header(SkStream* stream, int* width, int* height, int* alpha) { michael@0: unsigned char buffer[WEBP_VP8_HEADER_SIZE]; michael@0: size_t bytesToRead = WEBP_VP8_HEADER_SIZE; michael@0: size_t totalBytesRead = 0; michael@0: do { michael@0: unsigned char* dst = buffer + totalBytesRead; michael@0: const size_t bytesRead = stream->read(dst, bytesToRead); michael@0: if (0 == bytesRead) { michael@0: // Could not read any bytes. Check to see if we are at the end (exit michael@0: // condition), and continue reading if not. Important for streams michael@0: // that do not have all the data ready. michael@0: continue; michael@0: } michael@0: bytesToRead -= bytesRead; michael@0: totalBytesRead += bytesRead; michael@0: SkASSERT(bytesToRead + totalBytesRead == WEBP_VP8_HEADER_SIZE); michael@0: } while (!stream->isAtEnd() && bytesToRead > 0); michael@0: michael@0: WebPBitstreamFeatures features; michael@0: VP8StatusCode status = WebPGetFeatures(buffer, totalBytesRead, &features); michael@0: if (VP8_STATUS_OK != status) { michael@0: return false; // Invalid WebP file. michael@0: } michael@0: *width = features.width; michael@0: *height = features.height; michael@0: *alpha = features.has_alpha; michael@0: michael@0: // sanity check for image size that's about to be decoded. michael@0: { michael@0: int64_t size = sk_64_mul(*width, *height); michael@0: if (!sk_64_isS32(size)) { michael@0: return false; michael@0: } michael@0: // now check that if we are 4-bytes per pixel, we also don't overflow michael@0: if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: class SkWEBPImageDecoder: public SkImageDecoder { michael@0: public: michael@0: SkWEBPImageDecoder() { michael@0: fInputStream = NULL; michael@0: fOrigWidth = 0; michael@0: fOrigHeight = 0; michael@0: fHasAlpha = 0; michael@0: } michael@0: virtual ~SkWEBPImageDecoder() { michael@0: SkSafeUnref(fInputStream); michael@0: } michael@0: michael@0: virtual Format getFormat() const SK_OVERRIDE { michael@0: return kWEBP_Format; michael@0: } michael@0: michael@0: protected: michael@0: virtual bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *height) SK_OVERRIDE; michael@0: virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRIDE; michael@0: virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; michael@0: michael@0: private: michael@0: /** michael@0: * Called when determining the output config to request to webp. michael@0: * If the image does not have alpha, there is no need to premultiply. michael@0: * If the caller wants unpremultiplied colors, that is respected. michael@0: */ michael@0: bool shouldPremultiply() const { michael@0: return SkToBool(fHasAlpha) && !this->getRequireUnpremultipliedColors(); michael@0: } michael@0: michael@0: bool setDecodeConfig(SkBitmap* decodedBitmap, int width, int height); michael@0: michael@0: SkStream* fInputStream; michael@0: int fOrigWidth; michael@0: int fOrigHeight; michael@0: int fHasAlpha; michael@0: michael@0: typedef SkImageDecoder INHERITED; michael@0: }; michael@0: michael@0: ////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifdef TIME_DECODE michael@0: michael@0: #include "SkTime.h" michael@0: michael@0: class AutoTimeMillis { michael@0: public: michael@0: AutoTimeMillis(const char label[]) : michael@0: fLabel(label) { michael@0: if (NULL == fLabel) { michael@0: fLabel = ""; michael@0: } michael@0: fNow = SkTime::GetMSecs(); michael@0: } michael@0: ~AutoTimeMillis() { michael@0: SkDebugf("---- Time (ms): %s %d\n", fLabel, SkTime::GetMSecs() - fNow); michael@0: } michael@0: private: michael@0: const char* fLabel; michael@0: SkMSec fNow; michael@0: }; michael@0: michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // This guy exists just to aid in debugging, as it allows debuggers to just michael@0: // set a break-point in one place to see all error exists. michael@0: static bool return_false(const SkBitmap& bm, const char msg[]) { michael@0: SkDEBUGF(("libwebp error %s [%d %d]", msg, bm.width(), bm.height())); michael@0: return false; // must always return false michael@0: } michael@0: michael@0: static WEBP_CSP_MODE webp_decode_mode(const SkBitmap* decodedBitmap, bool premultiply) { michael@0: WEBP_CSP_MODE mode = MODE_LAST; michael@0: SkBitmap::Config config = decodedBitmap->config(); michael@0: michael@0: if (config == SkBitmap::kARGB_8888_Config) { michael@0: #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) michael@0: mode = premultiply ? MODE_bgrA : MODE_BGRA; michael@0: #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) michael@0: mode = premultiply ? MODE_rgbA : MODE_RGBA; michael@0: #else michael@0: #error "Skia uses BGRA or RGBA byte order" michael@0: #endif michael@0: } else if (config == SkBitmap::kARGB_4444_Config) { michael@0: mode = premultiply ? MODE_rgbA_4444 : MODE_RGBA_4444; michael@0: } else if (config == SkBitmap::kRGB_565_Config) { michael@0: mode = MODE_RGB_565; michael@0: } michael@0: SkASSERT(MODE_LAST != mode); michael@0: return mode; michael@0: } michael@0: michael@0: // Incremental WebP image decoding. Reads input buffer of 64K size iteratively michael@0: // and decodes this block to appropriate color-space as per config object. michael@0: static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) { michael@0: WebPIDecoder* idec = WebPIDecode(NULL, 0, config); michael@0: if (NULL == idec) { michael@0: WebPFreeDecBuffer(&config->output); michael@0: return false; michael@0: } michael@0: michael@0: if (!stream->rewind()) { michael@0: SkDebugf("Failed to rewind webp stream!"); michael@0: return false; michael@0: } michael@0: const size_t readBufferSize = stream->hasLength() ? michael@0: SkTMin(stream->getLength(), WEBP_IDECODE_BUFFER_SZ) : WEBP_IDECODE_BUFFER_SZ; michael@0: SkAutoMalloc srcStorage(readBufferSize); michael@0: unsigned char* input = (uint8_t*)srcStorage.get(); michael@0: if (NULL == input) { michael@0: WebPIDelete(idec); michael@0: WebPFreeDecBuffer(&config->output); michael@0: return false; michael@0: } michael@0: michael@0: bool success = true; michael@0: VP8StatusCode status = VP8_STATUS_SUSPENDED; michael@0: do { michael@0: const size_t bytesRead = stream->read(input, readBufferSize); michael@0: if (0 == bytesRead) { michael@0: success = false; michael@0: break; michael@0: } michael@0: michael@0: status = WebPIAppend(idec, input, bytesRead); michael@0: if (VP8_STATUS_OK != status && VP8_STATUS_SUSPENDED != status) { michael@0: success = false; michael@0: break; michael@0: } michael@0: } while (VP8_STATUS_OK != status); michael@0: srcStorage.free(); michael@0: WebPIDelete(idec); michael@0: WebPFreeDecBuffer(&config->output); michael@0: michael@0: return success; michael@0: } michael@0: michael@0: static bool webp_get_config_resize(WebPDecoderConfig* config, michael@0: SkBitmap* decodedBitmap, michael@0: int width, int height, bool premultiply) { michael@0: WEBP_CSP_MODE mode = webp_decode_mode(decodedBitmap, premultiply); michael@0: if (MODE_LAST == mode) { michael@0: return false; michael@0: } michael@0: michael@0: if (0 == WebPInitDecoderConfig(config)) { michael@0: return false; michael@0: } michael@0: michael@0: config->output.colorspace = mode; michael@0: config->output.u.RGBA.rgba = (uint8_t*)decodedBitmap->getPixels(); michael@0: config->output.u.RGBA.stride = (int) decodedBitmap->rowBytes(); michael@0: config->output.u.RGBA.size = decodedBitmap->getSize(); michael@0: config->output.is_external_memory = 1; michael@0: michael@0: if (width != decodedBitmap->width() || height != decodedBitmap->height()) { michael@0: config->options.use_scaling = 1; michael@0: config->options.scaled_width = decodedBitmap->width(); michael@0: config->options.scaled_height = decodedBitmap->height(); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool webp_get_config_resize_crop(WebPDecoderConfig* config, michael@0: SkBitmap* decodedBitmap, michael@0: const SkIRect& region, bool premultiply) { michael@0: michael@0: if (!webp_get_config_resize(config, decodedBitmap, region.width(), michael@0: region.height(), premultiply)) { michael@0: return false; michael@0: } michael@0: michael@0: config->options.use_cropping = 1; michael@0: config->options.crop_left = region.fLeft; michael@0: config->options.crop_top = region.fTop; michael@0: config->options.crop_width = region.width(); michael@0: config->options.crop_height = region.height(); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool SkWEBPImageDecoder::setDecodeConfig(SkBitmap* decodedBitmap, michael@0: int width, int height) { michael@0: SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, SkToBool(fHasAlpha)); michael@0: michael@0: // YUV converter supports output in RGB565, RGBA4444 and RGBA8888 formats. michael@0: if (fHasAlpha) { michael@0: if (config != SkBitmap::kARGB_4444_Config) { michael@0: config = SkBitmap::kARGB_8888_Config; michael@0: } michael@0: } else { michael@0: if (config != SkBitmap::kRGB_565_Config && michael@0: config != SkBitmap::kARGB_4444_Config) { michael@0: config = SkBitmap::kARGB_8888_Config; michael@0: } michael@0: } michael@0: michael@0: if (!this->chooseFromOneChoice(config, width, height)) { michael@0: return false; michael@0: } michael@0: michael@0: return decodedBitmap->setConfig(config, width, height, 0, michael@0: fHasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType); michael@0: } michael@0: michael@0: bool SkWEBPImageDecoder::onBuildTileIndex(SkStreamRewindable* stream, michael@0: int *width, int *height) { michael@0: int origWidth, origHeight, hasAlpha; michael@0: if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) { michael@0: return false; michael@0: } michael@0: michael@0: if (!stream->rewind()) { michael@0: SkDebugf("Failed to rewind webp stream!"); michael@0: return false; michael@0: } michael@0: michael@0: *width = origWidth; michael@0: *height = origHeight; michael@0: michael@0: SkRefCnt_SafeAssign(this->fInputStream, stream); michael@0: this->fOrigWidth = origWidth; michael@0: this->fOrigHeight = origHeight; michael@0: this->fHasAlpha = hasAlpha; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool is_config_compatible(const SkBitmap& bitmap) { michael@0: SkBitmap::Config config = bitmap.config(); michael@0: return config == SkBitmap::kARGB_4444_Config || michael@0: config == SkBitmap::kRGB_565_Config || michael@0: config == SkBitmap::kARGB_8888_Config; michael@0: } michael@0: michael@0: bool SkWEBPImageDecoder::onDecodeSubset(SkBitmap* decodedBitmap, michael@0: const SkIRect& region) { michael@0: SkIRect rect = SkIRect::MakeWH(fOrigWidth, fOrigHeight); michael@0: michael@0: if (!rect.intersect(region)) { michael@0: // If the requested region is entirely outsides the image, return false michael@0: return false; michael@0: } michael@0: michael@0: const int sampleSize = this->getSampleSize(); michael@0: SkScaledBitmapSampler sampler(rect.width(), rect.height(), sampleSize); michael@0: const int width = sampler.scaledWidth(); michael@0: const int height = sampler.scaledHeight(); michael@0: michael@0: // The image can be decoded directly to decodedBitmap if michael@0: // 1. the region is within the image range michael@0: // 2. bitmap's config is compatible michael@0: // 3. bitmap's size is same as the required region (after sampled) michael@0: bool directDecode = (rect == region) && michael@0: (decodedBitmap->isNull() || michael@0: (is_config_compatible(*decodedBitmap) && michael@0: (decodedBitmap->width() == width) && michael@0: (decodedBitmap->height() == height))); michael@0: michael@0: SkBitmap tmpBitmap; michael@0: SkBitmap *bitmap = decodedBitmap; michael@0: michael@0: if (!directDecode) { michael@0: bitmap = &tmpBitmap; michael@0: } michael@0: michael@0: if (bitmap->isNull()) { michael@0: if (!setDecodeConfig(bitmap, width, height)) { michael@0: return false; michael@0: } michael@0: // alloc from native heap if it is a temp bitmap. (prevent GC) michael@0: bool allocResult = (bitmap == decodedBitmap) michael@0: ? allocPixelRef(bitmap, NULL) michael@0: : bitmap->allocPixels(); michael@0: if (!allocResult) { michael@0: return return_false(*decodedBitmap, "allocPixelRef"); michael@0: } michael@0: } else { michael@0: // This is also called in setDecodeConfig in above block. michael@0: // i.e., when bitmap->isNull() is true. michael@0: if (!chooseFromOneChoice(bitmap->config(), width, height)) { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: SkAutoLockPixels alp(*bitmap); michael@0: WebPDecoderConfig config; michael@0: if (!webp_get_config_resize_crop(&config, bitmap, rect, michael@0: this->shouldPremultiply())) { michael@0: return false; michael@0: } michael@0: michael@0: // Decode the WebP image data stream using WebP incremental decoding for michael@0: // the specified cropped image-region. michael@0: if (!webp_idecode(this->fInputStream, &config)) { michael@0: return false; michael@0: } michael@0: michael@0: if (!directDecode) { michael@0: cropBitmap(decodedBitmap, bitmap, sampleSize, region.x(), region.y(), michael@0: region.width(), region.height(), rect.x(), rect.y()); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SkWEBPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap, michael@0: Mode mode) { michael@0: #ifdef TIME_DECODE michael@0: AutoTimeMillis atm("WEBP Decode"); michael@0: #endif michael@0: michael@0: int origWidth, origHeight, hasAlpha; michael@0: if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) { michael@0: return false; michael@0: } michael@0: this->fHasAlpha = hasAlpha; michael@0: michael@0: const int sampleSize = this->getSampleSize(); michael@0: SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize); michael@0: if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(), michael@0: sampler.scaledHeight())) { michael@0: return false; michael@0: } michael@0: michael@0: // If only bounds are requested, done michael@0: if (SkImageDecoder::kDecodeBounds_Mode == mode) { michael@0: return true; michael@0: } michael@0: michael@0: if (!this->allocPixelRef(decodedBitmap, NULL)) { michael@0: return return_false(*decodedBitmap, "allocPixelRef"); michael@0: } michael@0: michael@0: SkAutoLockPixels alp(*decodedBitmap); michael@0: michael@0: WebPDecoderConfig config; michael@0: if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight, michael@0: this->shouldPremultiply())) { michael@0: return false; michael@0: } michael@0: michael@0: // Decode the WebP image data stream using WebP incremental decoding. michael@0: return webp_idecode(stream, &config); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkUnPreMultiply.h" michael@0: michael@0: typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width, michael@0: const SkPMColor* SK_RESTRICT ctable); michael@0: michael@0: static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width, michael@0: const SkPMColor*) { michael@0: const uint32_t* SK_RESTRICT src = (const uint32_t*)in; michael@0: for (int i = 0; i < width; ++i) { michael@0: const uint32_t c = *src++; michael@0: rgb[0] = SkGetPackedR32(c); michael@0: rgb[1] = SkGetPackedG32(c); michael@0: rgb[2] = SkGetPackedB32(c); michael@0: rgb += 3; michael@0: } michael@0: } michael@0: michael@0: static void ARGB_8888_To_RGBA(const uint8_t* in, uint8_t* rgb, int width, michael@0: const SkPMColor*) { michael@0: const uint32_t* SK_RESTRICT src = (const uint32_t*)in; michael@0: const SkUnPreMultiply::Scale* SK_RESTRICT table = michael@0: SkUnPreMultiply::GetScaleTable(); michael@0: for (int i = 0; i < width; ++i) { michael@0: const uint32_t c = *src++; michael@0: uint8_t a = SkGetPackedA32(c); michael@0: uint8_t r = SkGetPackedR32(c); michael@0: uint8_t g = SkGetPackedG32(c); michael@0: uint8_t b = SkGetPackedB32(c); michael@0: if (0 != a && 255 != a) { michael@0: SkUnPreMultiply::Scale scale = table[a]; michael@0: r = SkUnPreMultiply::ApplyScale(scale, r); michael@0: g = SkUnPreMultiply::ApplyScale(scale, g); michael@0: b = SkUnPreMultiply::ApplyScale(scale, b); michael@0: } michael@0: rgb[0] = r; michael@0: rgb[1] = g; michael@0: rgb[2] = b; michael@0: rgb[3] = a; michael@0: rgb += 4; michael@0: } michael@0: } michael@0: michael@0: static void RGB_565_To_RGB(const uint8_t* in, uint8_t* rgb, int width, michael@0: const SkPMColor*) { michael@0: const uint16_t* SK_RESTRICT src = (const uint16_t*)in; michael@0: for (int i = 0; i < width; ++i) { michael@0: const uint16_t c = *src++; michael@0: rgb[0] = SkPacked16ToR32(c); michael@0: rgb[1] = SkPacked16ToG32(c); michael@0: rgb[2] = SkPacked16ToB32(c); michael@0: rgb += 3; michael@0: } michael@0: } michael@0: michael@0: static void ARGB_4444_To_RGB(const uint8_t* in, uint8_t* rgb, int width, michael@0: const SkPMColor*) { michael@0: const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in; michael@0: for (int i = 0; i < width; ++i) { michael@0: const SkPMColor16 c = *src++; michael@0: rgb[0] = SkPacked4444ToR32(c); michael@0: rgb[1] = SkPacked4444ToG32(c); michael@0: rgb[2] = SkPacked4444ToB32(c); michael@0: rgb += 3; michael@0: } michael@0: } michael@0: michael@0: static void ARGB_4444_To_RGBA(const uint8_t* in, uint8_t* rgb, int width, michael@0: const SkPMColor*) { michael@0: const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in; michael@0: const SkUnPreMultiply::Scale* SK_RESTRICT table = michael@0: SkUnPreMultiply::GetScaleTable(); michael@0: for (int i = 0; i < width; ++i) { michael@0: const SkPMColor16 c = *src++; michael@0: uint8_t a = SkPacked4444ToA32(c); michael@0: uint8_t r = SkPacked4444ToR32(c); michael@0: uint8_t g = SkPacked4444ToG32(c); michael@0: uint8_t b = SkPacked4444ToB32(c); michael@0: if (0 != a && 255 != a) { michael@0: SkUnPreMultiply::Scale scale = table[a]; michael@0: r = SkUnPreMultiply::ApplyScale(scale, r); michael@0: g = SkUnPreMultiply::ApplyScale(scale, g); michael@0: b = SkUnPreMultiply::ApplyScale(scale, b); michael@0: } michael@0: rgb[0] = r; michael@0: rgb[1] = g; michael@0: rgb[2] = b; michael@0: rgb[3] = a; michael@0: rgb += 4; michael@0: } michael@0: } michael@0: michael@0: static void Index8_To_RGB(const uint8_t* in, uint8_t* rgb, int width, michael@0: const SkPMColor* SK_RESTRICT ctable) { michael@0: const uint8_t* SK_RESTRICT src = (const uint8_t*)in; michael@0: for (int i = 0; i < width; ++i) { michael@0: const uint32_t c = ctable[*src++]; michael@0: rgb[0] = SkGetPackedR32(c); michael@0: rgb[1] = SkGetPackedG32(c); michael@0: rgb[2] = SkGetPackedB32(c); michael@0: rgb += 3; michael@0: } michael@0: } michael@0: michael@0: static ScanlineImporter ChooseImporter(const SkBitmap::Config& config, michael@0: bool hasAlpha, michael@0: int* bpp) { michael@0: switch (config) { michael@0: case SkBitmap::kARGB_8888_Config: michael@0: if (hasAlpha) { michael@0: *bpp = 4; michael@0: return ARGB_8888_To_RGBA; michael@0: } else { michael@0: *bpp = 3; michael@0: return ARGB_8888_To_RGB; michael@0: } michael@0: case SkBitmap::kARGB_4444_Config: michael@0: if (hasAlpha) { michael@0: *bpp = 4; michael@0: return ARGB_4444_To_RGBA; michael@0: } else { michael@0: *bpp = 3; michael@0: return ARGB_4444_To_RGB; michael@0: } michael@0: case SkBitmap::kRGB_565_Config: michael@0: *bpp = 3; michael@0: return RGB_565_To_RGB; michael@0: case SkBitmap::kIndex8_Config: michael@0: *bpp = 3; michael@0: return Index8_To_RGB; michael@0: default: michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: static int stream_writer(const uint8_t* data, size_t data_size, michael@0: const WebPPicture* const picture) { michael@0: SkWStream* const stream = (SkWStream*)picture->custom_ptr; michael@0: return stream->write(data, data_size) ? 1 : 0; michael@0: } michael@0: michael@0: class SkWEBPImageEncoder : public SkImageEncoder { michael@0: protected: michael@0: virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE; michael@0: michael@0: private: michael@0: typedef SkImageEncoder INHERITED; michael@0: }; michael@0: michael@0: bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm, michael@0: int quality) { michael@0: const SkBitmap::Config config = bm.config(); michael@0: const bool hasAlpha = !bm.isOpaque(); michael@0: int bpp = -1; michael@0: const ScanlineImporter scanline_import = ChooseImporter(config, hasAlpha, michael@0: &bpp); michael@0: if (NULL == scanline_import) { michael@0: return false; michael@0: } michael@0: if (-1 == bpp) { michael@0: return false; michael@0: } michael@0: michael@0: SkAutoLockPixels alp(bm); michael@0: SkAutoLockColors ctLocker; michael@0: if (NULL == bm.getPixels()) { michael@0: return false; michael@0: } michael@0: michael@0: WebPConfig webp_config; michael@0: if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) { michael@0: return false; michael@0: } michael@0: michael@0: WebPPicture pic; michael@0: WebPPictureInit(&pic); michael@0: pic.width = bm.width(); michael@0: pic.height = bm.height(); michael@0: pic.writer = stream_writer; michael@0: pic.custom_ptr = (void*)stream; michael@0: michael@0: const SkPMColor* colors = ctLocker.lockColors(bm); michael@0: const uint8_t* src = (uint8_t*)bm.getPixels(); michael@0: const int rgbStride = pic.width * bpp; michael@0: michael@0: // Import (for each scanline) the bit-map image (in appropriate color-space) michael@0: // to RGB color space. michael@0: uint8_t* rgb = new uint8_t[rgbStride * pic.height]; michael@0: for (int y = 0; y < pic.height; ++y) { michael@0: scanline_import(src + y * bm.rowBytes(), rgb + y * rgbStride, michael@0: pic.width, colors); michael@0: } michael@0: michael@0: bool ok; michael@0: if (bpp == 3) { michael@0: ok = SkToBool(WebPPictureImportRGB(&pic, rgb, rgbStride)); michael@0: } else { michael@0: ok = SkToBool(WebPPictureImportRGBA(&pic, rgb, rgbStride)); michael@0: } michael@0: delete[] rgb; michael@0: michael@0: ok = ok && WebPEncode(&webp_config, &pic); michael@0: WebPPictureFree(&pic); michael@0: michael@0: return ok; michael@0: } michael@0: michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: DEFINE_DECODER_CREATOR(WEBPImageDecoder); michael@0: DEFINE_ENCODER_CREATOR(WEBPImageEncoder); michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static SkImageDecoder* sk_libwebp_dfactory(SkStreamRewindable* stream) { michael@0: int width, height, hasAlpha; michael@0: if (!webp_parse_header(stream, &width, &height, &hasAlpha)) { michael@0: return NULL; michael@0: } michael@0: michael@0: // Magic matches, call decoder michael@0: return SkNEW(SkWEBPImageDecoder); michael@0: } michael@0: michael@0: static SkImageDecoder::Format get_format_webp(SkStreamRewindable* stream) { michael@0: int width, height, hasAlpha; michael@0: if (webp_parse_header(stream, &width, &height, &hasAlpha)) { michael@0: return SkImageDecoder::kWEBP_Format; michael@0: } michael@0: return SkImageDecoder::kUnknown_Format; michael@0: } michael@0: michael@0: static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { michael@0: return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NULL; michael@0: } michael@0: michael@0: static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory); michael@0: static SkImageDecoder_FormatReg gFormatReg(get_format_webp); michael@0: static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory);