Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / image-encoders / skia / WEBPImageEncoder.cpp
1 /*
2  * Copyright (c) 2011, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "platform/image-encoders/skia/WEBPImageEncoder.h"
33
34 #include "SkBitmap.h"
35 #include "platform/geometry/IntSize.h"
36 #include "platform/graphics/ImageBuffer.h"
37 #include "webp/encode.h"
38
39 typedef int (*WebPImporter)(WebPPicture* const, const uint8_t* const data, int rowStride);
40
41 namespace blink {
42
43 static int writeOutput(const uint8_t* data, size_t size, const WebPPicture* const picture)
44 {
45     static_cast<Vector<unsigned char>*>(picture->custom_ptr)->append(data, size);
46     return 1;
47 }
48
49 static bool rgbPictureImport(const unsigned char* pixels, bool premultiplied, WebPImporter importRGBX, WebPImporter importRGB, WebPPicture* picture)
50 {
51     if (premultiplied)
52         return importRGBX(picture, pixels, picture->width * 4);
53
54     // Write the RGB pixels to an rgb data buffer, alpha premultiplied, then import the rgb data.
55
56     Vector<unsigned char> rgb;
57     size_t pixelCount = picture->height * picture->width;
58     rgb.reserveInitialCapacity(pixelCount * 3);
59
60     for (unsigned char* data = rgb.data(); pixelCount-- > 0; pixels += 4) {
61         unsigned char alpha = pixels[3];
62         if (alpha != 255) {
63             *data++ = SkMulDiv255Round(pixels[0], alpha);
64             *data++ = SkMulDiv255Round(pixels[1], alpha);
65             *data++ = SkMulDiv255Round(pixels[2], alpha);
66         } else {
67             *data++ = pixels[0];
68             *data++ = pixels[1];
69             *data++ = pixels[2];
70         }
71     }
72
73     return importRGB(picture, rgb.data(), picture->width * 3);
74 }
75
76 template <bool Premultiplied> inline bool importPictureBGRX(const unsigned char* pixels, WebPPicture* picture)
77 {
78     return rgbPictureImport(pixels, Premultiplied, &WebPPictureImportBGRX, &WebPPictureImportBGR, picture);
79 }
80
81 template <bool Premultiplied> inline bool importPictureRGBX(const unsigned char* pixels, WebPPicture* picture)
82 {
83     return rgbPictureImport(pixels, Premultiplied, &WebPPictureImportRGBX, &WebPPictureImportRGB, picture);
84 }
85
86 static bool platformPremultipliedImportPicture(const unsigned char* pixels, WebPPicture* picture)
87 {
88     if (SK_B32_SHIFT) // Android
89         return importPictureRGBX<true>(pixels, picture);
90
91     return importPictureBGRX<true>(pixels, picture);
92 }
93
94 static bool encodePixels(IntSize imageSize, const unsigned char* pixels, bool premultiplied, int quality, Vector<unsigned char>* output)
95 {
96     WebPConfig config;
97     if (!WebPConfigInit(&config))
98         return false;
99     WebPPicture picture;
100     if (!WebPPictureInit(&picture))
101         return false;
102
103     imageSize.clampNegativeToZero();
104     if (!imageSize.width() || imageSize.width() > WEBP_MAX_DIMENSION)
105         return false;
106     picture.width = imageSize.width();
107     if (!imageSize.height() || imageSize.height() > WEBP_MAX_DIMENSION)
108         return false;
109     picture.height = imageSize.height();
110
111     if (premultiplied && !platformPremultipliedImportPicture(pixels, &picture))
112         return false;
113     if (!premultiplied && !importPictureRGBX<false>(pixels, &picture))
114         return false;
115
116     picture.custom_ptr = output;
117     picture.writer = &writeOutput;
118     config.quality = quality;
119     config.method = 3;
120
121     bool success = WebPEncode(&config, &picture);
122     WebPPictureFree(&picture);
123     return success;
124 }
125
126 bool WEBPImageEncoder::encode(const SkBitmap& bitmap, int quality, Vector<unsigned char>* output)
127 {
128     SkAutoLockPixels bitmapLock(bitmap);
129
130     if (bitmap.colorType() != kN32_SkColorType || !bitmap.getPixels())
131         return false; // Only support 32 bit/pixel skia bitmaps.
132
133     return encodePixels(IntSize(bitmap.width(), bitmap.height()), static_cast<unsigned char *>(bitmap.getPixels()), true, quality, output);
134 }
135
136 bool WEBPImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vector<unsigned char>* output)
137 {
138     return encodePixels(imageData.size(), imageData.data(), false, quality, output);
139 }
140
141 } // namespace blink