Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / image-encoders / skia / JPEGImageEncoder.cpp
1 /*
2  * Copyright (c) 2010, 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/JPEGImageEncoder.h"
33
34 #include "SkBitmap.h"
35 #include "SkColorPriv.h"
36 #include "platform/geometry/IntSize.h"
37 #include "platform/graphics/ImageBuffer.h"
38 extern "C" {
39 #include <setjmp.h>
40 #include <stdio.h> // jpeglib.h needs stdio.h FILE
41 #include "jpeglib.h"
42 }
43
44 namespace blink {
45
46 struct JPEGOutputBuffer : public jpeg_destination_mgr {
47     Vector<unsigned char>* output;
48     Vector<unsigned char> buffer;
49 };
50
51 static void prepareOutput(j_compress_ptr cinfo)
52 {
53     JPEGOutputBuffer* out = static_cast<JPEGOutputBuffer*>(cinfo->dest);
54     const size_t internalBufferSize = 8192;
55     out->buffer.resize(internalBufferSize);
56     out->next_output_byte = out->buffer.data();
57     out->free_in_buffer = out->buffer.size();
58 }
59
60 static boolean writeOutput(j_compress_ptr cinfo)
61 {
62     JPEGOutputBuffer* out = static_cast<JPEGOutputBuffer*>(cinfo->dest);
63     out->output->append(out->buffer.data(), out->buffer.size());
64     out->next_output_byte = out->buffer.data();
65     out->free_in_buffer = out->buffer.size();
66     return TRUE;
67 }
68
69 static void finishOutput(j_compress_ptr cinfo)
70 {
71     JPEGOutputBuffer* out = static_cast<JPEGOutputBuffer*>(cinfo->dest);
72     const size_t size = out->buffer.size() - out->free_in_buffer;
73     out->output->append(out->buffer.data(), size);
74 }
75
76 static void handleError(j_common_ptr common)
77 {
78     jmp_buf* jumpBufferPtr = static_cast<jmp_buf*>(common->client_data);
79     longjmp(*jumpBufferPtr, -1);
80 }
81
82 static void preMultipliedBGRAtoRGB(const unsigned char* pixels, unsigned pixelCount, unsigned char* output)
83 {
84     const SkPMColor* input = reinterpret_cast_ptr<const SkPMColor*>(pixels);
85     for (; pixelCount-- > 0; ++input) {
86         *output++ = SkGetPackedR32(*input);
87         *output++ = SkGetPackedG32(*input);
88         *output++ = SkGetPackedB32(*input);
89     }
90 }
91
92 static void RGBAtoRGB(const unsigned char* pixels, unsigned pixelCount, unsigned char* output)
93 {
94     for (; pixelCount-- > 0; pixels += 4) {
95         // Do source-over composition on black.
96         unsigned char alpha = pixels[3];
97         if (alpha != 255) {
98             *output++ = SkMulDiv255Round(pixels[0], alpha);
99             *output++ = SkMulDiv255Round(pixels[1], alpha);
100             *output++ = SkMulDiv255Round(pixels[2], alpha);
101         } else {
102             *output++ = pixels[0];
103             *output++ = pixels[1];
104             *output++ = pixels[2];
105         }
106     }
107 }
108
109 static void disableSubsamplingForHighQuality(jpeg_compress_struct* cinfo, int quality)
110 {
111     if (quality < 100)
112         return;
113
114     for (int i = 0; i < MAX_COMPONENTS; ++i) {
115         cinfo->comp_info[i].h_samp_factor = 1;
116         cinfo->comp_info[i].v_samp_factor = 1;
117     }
118 }
119
120 static bool encodePixels(IntSize imageSize, unsigned char* inputPixels, bool premultiplied, int quality, Vector<unsigned char>* output)
121 {
122     JPEGOutputBuffer destination;
123     destination.output = output;
124     Vector<JSAMPLE> row;
125
126     jpeg_compress_struct cinfo;
127     jpeg_error_mgr error;
128     cinfo.err = jpeg_std_error(&error);
129     error.error_exit = handleError;
130     jmp_buf jumpBuffer;
131     cinfo.client_data = &jumpBuffer;
132
133     if (setjmp(jumpBuffer)) {
134         jpeg_destroy_compress(&cinfo);
135         return false;
136     }
137
138     jpeg_create_compress(&cinfo);
139     cinfo.dest = &destination;
140     cinfo.dest->init_destination = prepareOutput;
141     cinfo.dest->empty_output_buffer = writeOutput;
142     cinfo.dest->term_destination = finishOutput;
143
144     imageSize.clampNegativeToZero();
145     cinfo.image_height = imageSize.height();
146     cinfo.image_width = imageSize.width();
147
148 #if defined(JCS_EXTENSIONS)
149     if (premultiplied) {
150         cinfo.in_color_space = SK_B32_SHIFT ? JCS_EXT_RGBX : JCS_EXT_BGRX;
151
152         cinfo.input_components = 4;
153
154         jpeg_set_defaults(&cinfo);
155         jpeg_set_quality(&cinfo, quality, TRUE);
156         disableSubsamplingForHighQuality(&cinfo, quality);
157         jpeg_start_compress(&cinfo, TRUE);
158
159         unsigned char* pixels = inputPixels;
160         const size_t pixelRowStride = cinfo.image_width * 4;
161         while (cinfo.next_scanline < cinfo.image_height) {
162             jpeg_write_scanlines(&cinfo, &pixels, 1);
163             pixels += pixelRowStride;
164         }
165
166         jpeg_finish_compress(&cinfo);
167         jpeg_destroy_compress(&cinfo);
168         return true;
169     }
170 #endif
171
172     cinfo.in_color_space = JCS_RGB;
173     cinfo.input_components = 3;
174
175     void (*extractRowRGB)(const unsigned char*, unsigned, unsigned char* output);
176     extractRowRGB = &RGBAtoRGB;
177     if (premultiplied)
178         extractRowRGB = &preMultipliedBGRAtoRGB;
179
180     jpeg_set_defaults(&cinfo);
181     jpeg_set_quality(&cinfo, quality, TRUE);
182     disableSubsamplingForHighQuality(&cinfo, quality);
183     jpeg_start_compress(&cinfo, TRUE);
184
185     unsigned char* pixels = inputPixels;
186     row.resize(cinfo.image_width * cinfo.input_components);
187     const size_t pixelRowStride = cinfo.image_width * 4;
188     while (cinfo.next_scanline < cinfo.image_height) {
189         JSAMPLE* rowData = row.data();
190         extractRowRGB(pixels, cinfo.image_width, rowData);
191         jpeg_write_scanlines(&cinfo, &rowData, 1);
192         pixels += pixelRowStride;
193     }
194
195     jpeg_finish_compress(&cinfo);
196     jpeg_destroy_compress(&cinfo);
197     return true;
198 }
199
200 bool JPEGImageEncoder::encode(const SkBitmap& bitmap, int quality, Vector<unsigned char>* output)
201 {
202     SkAutoLockPixels bitmapLock(bitmap);
203
204     if (bitmap.colorType() != kN32_SkColorType || !bitmap.getPixels())
205         return false; // Only support 32 bit/pixel skia bitmaps.
206
207     return encodePixels(IntSize(bitmap.width(), bitmap.height()), static_cast<unsigned char *>(bitmap.getPixels()), true, quality, output);
208 }
209
210 bool JPEGImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vector<unsigned char>* output)
211 {
212     return encodePixels(imageData.size(), imageData.data(), false, quality, output);
213 }
214
215 } // namespace blink