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 "WEBPImageEncoder.h"
36
#include "ImageData.h"
39
#include "SkColorPriv.h"
40
#include "webp/encode.h"
42
typedef int (*WebPImporter)(WebPPicture* const, const uint8_t* const data, int rowStride);
46
static int writeOutput(const uint8_t* data, size_t size, const WebPPicture* const picture)
48
static_cast<Vector<unsigned char>*>(picture->custom_ptr)->append(data, size);
52
static bool rgbPictureImport(const unsigned char* pixels, bool premultiplied, WebPImporter importRGBX, WebPImporter importRGB, WebPPicture* picture)
55
return importRGBX(picture, pixels, picture->width * 4);
57
// Write the RGB pixels to an rgb data buffer, alpha premultiplied, then import the rgb data.
59
Vector<unsigned char> rgb;
60
size_t pixelCount = picture->height * picture->width;
61
rgb.reserveInitialCapacity(pixelCount * 3);
63
for (unsigned char* data = rgb.data(); pixelCount-- > 0; pixels += 4) {
64
unsigned char alpha = pixels[3];
66
*data++ = SkMulDiv255Round(pixels[0], alpha);
67
*data++ = SkMulDiv255Round(pixels[1], alpha);
68
*data++ = SkMulDiv255Round(pixels[2], alpha);
76
return importRGB(picture, rgb.data(), picture->width * 3);
79
template <bool Premultiplied> inline bool importPictureBGRX(const unsigned char* pixels, WebPPicture* picture)
81
return rgbPictureImport(pixels, Premultiplied, &WebPPictureImportBGRX, &WebPPictureImportBGR, picture);
84
template <bool Premultiplied> inline bool importPictureRGBX(const unsigned char* pixels, WebPPicture* picture)
86
return rgbPictureImport(pixels, Premultiplied, &WebPPictureImportRGBX, &WebPPictureImportRGB, picture);
89
static bool encodePixels(IntSize imageSize, const unsigned char* pixels, bool premultiplied, int quality, Vector<unsigned char>* output)
92
if (!WebPConfigInit(&config))
95
if (!WebPPictureInit(&picture))
98
imageSize.clampNegativeToZero();
99
if (!imageSize.width() || imageSize.width() > WEBP_MAX_DIMENSION)
101
picture.width = imageSize.width();
102
if (!imageSize.height() || imageSize.height() > WEBP_MAX_DIMENSION)
104
picture.height = imageSize.height();
106
if (premultiplied && !importPictureBGRX<true>(pixels, &picture))
108
if (!premultiplied && !importPictureRGBX<false>(pixels, &picture))
111
picture.custom_ptr = output;
112
picture.writer = &writeOutput;
113
config.quality = quality;
116
bool success = WebPEncode(&config, &picture);
117
WebPPictureFree(&picture);
121
bool WEBPImageEncoder::encode(const SkBitmap& bitmap, int quality, Vector<unsigned char>* output)
123
SkAutoLockPixels bitmapLock(bitmap);
125
if (bitmap.config() != SkBitmap::kARGB_8888_Config)
126
return false; // Only support 32 bit/pixel skia bitmaps.
128
return encodePixels(IntSize(bitmap.width(), bitmap.height()), static_cast<unsigned char *>(bitmap.getPixels()), true, quality, output);
131
bool WEBPImageEncoder::encode(const ImageData& imageData, int quality, Vector<unsigned char>* output)
133
return encodePixels(imageData.size(), imageData.data()->data(), false, quality, output);
136
} // namespace WebCore