2 * Copyright (c) 2011, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "platform/image-encoders/skia/WEBPImageEncoder.h"
35 #include "platform/geometry/IntSize.h"
36 #include "platform/graphics/ImageBuffer.h"
37 #include "webp/encode.h"
39 typedef int (*WebPImporter)(WebPPicture* const, const uint8_t* const data, int rowStride);
43 static int writeOutput(const uint8_t* data, size_t size, const WebPPicture* const picture)
45 static_cast<Vector<unsigned char>*>(picture->custom_ptr)->append(data, size);
49 static bool rgbPictureImport(const unsigned char* pixels, bool premultiplied, WebPImporter importRGBX, WebPImporter importRGB, WebPPicture* picture)
52 return importRGBX(picture, pixels, picture->width * 4);
54 // Write the RGB pixels to an rgb data buffer, alpha premultiplied, then import the rgb data.
56 Vector<unsigned char> rgb;
57 size_t pixelCount = picture->height * picture->width;
58 rgb.reserveInitialCapacity(pixelCount * 3);
60 for (unsigned char* data = rgb.data(); pixelCount-- > 0; pixels += 4) {
61 unsigned char alpha = pixels[3];
63 *data++ = SkMulDiv255Round(pixels[0], alpha);
64 *data++ = SkMulDiv255Round(pixels[1], alpha);
65 *data++ = SkMulDiv255Round(pixels[2], alpha);
73 return importRGB(picture, rgb.data(), picture->width * 3);
76 template <bool Premultiplied> inline bool importPictureBGRX(const unsigned char* pixels, WebPPicture* picture)
78 return rgbPictureImport(pixels, Premultiplied, &WebPPictureImportBGRX, &WebPPictureImportBGR, picture);
81 template <bool Premultiplied> inline bool importPictureRGBX(const unsigned char* pixels, WebPPicture* picture)
83 return rgbPictureImport(pixels, Premultiplied, &WebPPictureImportRGBX, &WebPPictureImportRGB, picture);
86 static bool platformPremultipliedImportPicture(const unsigned char* pixels, WebPPicture* picture)
88 if (SK_B32_SHIFT) // Android
89 return importPictureRGBX<true>(pixels, picture);
91 return importPictureBGRX<true>(pixels, picture);
94 static bool encodePixels(IntSize imageSize, const unsigned char* pixels, bool premultiplied, int quality, Vector<unsigned char>* output)
97 if (!WebPConfigInit(&config))
100 if (!WebPPictureInit(&picture))
103 imageSize.clampNegativeToZero();
104 if (!imageSize.width() || imageSize.width() > WEBP_MAX_DIMENSION)
106 picture.width = imageSize.width();
107 if (!imageSize.height() || imageSize.height() > WEBP_MAX_DIMENSION)
109 picture.height = imageSize.height();
111 if (premultiplied && !platformPremultipliedImportPicture(pixels, &picture))
113 if (!premultiplied && !importPictureRGBX<false>(pixels, &picture))
116 picture.custom_ptr = output;
117 picture.writer = &writeOutput;
118 config.quality = quality;
121 bool success = WebPEncode(&config, &picture);
122 WebPPictureFree(&picture);
126 bool WEBPImageEncoder::encode(const SkBitmap& bitmap, int quality, Vector<unsigned char>* output)
128 SkAutoLockPixels bitmapLock(bitmap);
130 if (bitmap.colorType() != kN32_SkColorType || !bitmap.getPixels())
131 return false; // Only support 32 bit/pixel skia bitmaps.
133 return encodePixels(IntSize(bitmap.width(), bitmap.height()), static_cast<unsigned char *>(bitmap.getPixels()), true, quality, output);
136 bool WEBPImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vector<unsigned char>* output)
138 return encodePixels(imageData.size(), imageData.data(), false, quality, output);