OLD | NEW |
| (Empty) |
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 "platform/image-encoders/skia/JPEGImageEncoder.h" | |
32 | |
33 #include "SkColorPriv.h" | |
34 #include "platform/geometry/IntSize.h" | |
35 #include "platform/graphics/ImageBuffer.h" | |
36 | |
37 extern "C" { | |
38 #include <setjmp.h> | |
39 #include <stdio.h> // jpeglib.h needs stdio.h FILE | |
40 #include "jpeglib.h" | |
41 } | |
42 | |
43 namespace blink { | |
44 | |
45 struct JPEGOutputBuffer : public jpeg_destination_mgr { | |
46 DISALLOW_NEW(); | |
47 Vector<unsigned char>* output; | |
48 Vector<unsigned char> buffer; | |
49 }; | |
50 | |
51 class JPEGImageEncoderStateImpl final : public JPEGImageEncoderState { | |
52 public: | |
53 JPEGImageEncoderStateImpl() {} | |
54 ~JPEGImageEncoderStateImpl() override | |
55 { | |
56 jpeg_destroy_compress(&m_cinfo); | |
57 m_cinfo.client_data = 0; | |
58 } | |
59 JPEGOutputBuffer* outputBuffer() { return &m_outputBuffer; } | |
60 jpeg_compress_struct* cinfo() { return &m_cinfo; } | |
61 jpeg_error_mgr* error() { return &m_error; } | |
62 | |
63 private: | |
64 JPEGOutputBuffer m_outputBuffer; | |
65 jpeg_compress_struct m_cinfo; | |
66 jpeg_error_mgr m_error; | |
67 }; | |
68 | |
69 static void prepareOutput(j_compress_ptr cinfo) | |
70 { | |
71 JPEGOutputBuffer* out = static_cast<JPEGOutputBuffer*>(cinfo->dest); | |
72 const size_t internalBufferSize = 8192; | |
73 out->buffer.resize(internalBufferSize); | |
74 out->next_output_byte = out->buffer.data(); | |
75 out->free_in_buffer = out->buffer.size(); | |
76 } | |
77 | |
78 static boolean writeOutput(j_compress_ptr cinfo) | |
79 { | |
80 JPEGOutputBuffer* out = static_cast<JPEGOutputBuffer*>(cinfo->dest); | |
81 out->output->append(out->buffer.data(), out->buffer.size()); | |
82 out->next_output_byte = out->buffer.data(); | |
83 out->free_in_buffer = out->buffer.size(); | |
84 return TRUE; | |
85 } | |
86 | |
87 static void finishOutput(j_compress_ptr cinfo) | |
88 { | |
89 JPEGOutputBuffer* out = static_cast<JPEGOutputBuffer*>(cinfo->dest); | |
90 const size_t size = out->buffer.size() - out->free_in_buffer; | |
91 out->output->append(out->buffer.data(), size); | |
92 } | |
93 | |
94 static void handleError(j_common_ptr common) | |
95 { | |
96 jmp_buf* jumpBufferPtr = static_cast<jmp_buf*>(common->client_data); | |
97 longjmp(*jumpBufferPtr, -1); | |
98 } | |
99 | |
100 static void RGBAtoRGB(const unsigned char* pixels, unsigned pixelCount, unsigned
char* output) | |
101 { | |
102 // Per <canvas> spec, composite the input image pixels source-over on black. | |
103 | |
104 for (; pixelCount-- > 0; pixels += 4) { | |
105 unsigned char alpha = pixels[3]; | |
106 if (alpha != 255) { | |
107 *output++ = SkMulDiv255Round(pixels[0], alpha); | |
108 *output++ = SkMulDiv255Round(pixels[1], alpha); | |
109 *output++ = SkMulDiv255Round(pixels[2], alpha); | |
110 } else { | |
111 *output++ = pixels[0]; | |
112 *output++ = pixels[1]; | |
113 *output++ = pixels[2]; | |
114 } | |
115 } | |
116 } | |
117 | |
118 static void disableSubsamplingForHighQuality(jpeg_compress_struct* cinfo, int qu
ality) | |
119 { | |
120 if (quality < 100) | |
121 return; | |
122 | |
123 for (int i = 0; i < MAX_COMPONENTS; ++i) { | |
124 cinfo->comp_info[i].h_samp_factor = 1; | |
125 cinfo->comp_info[i].v_samp_factor = 1; | |
126 } | |
127 } | |
128 | |
129 PassOwnPtr<JPEGImageEncoderState> JPEGImageEncoderState::create(const IntSize& i
mageSize, const double& quality, Vector<unsigned char>* output) | |
130 { | |
131 if (imageSize.width() <= 0 || imageSize.height() <= 0) | |
132 return nullptr; | |
133 | |
134 OwnPtr<JPEGImageEncoderStateImpl> encoderState = adoptPtr(new JPEGImageEncod
erStateImpl()); | |
135 | |
136 jpeg_compress_struct* cinfo = encoderState->cinfo(); | |
137 jpeg_error_mgr* error = encoderState->error(); | |
138 cinfo->err = jpeg_std_error(error); | |
139 error->error_exit = handleError; | |
140 | |
141 jmp_buf jumpBuffer; | |
142 cinfo->client_data = &jumpBuffer; | |
143 | |
144 if (setjmp(jumpBuffer)) { | |
145 return nullptr; | |
146 } | |
147 | |
148 JPEGOutputBuffer* destination = encoderState->outputBuffer(); | |
149 destination->output = output; | |
150 | |
151 jpeg_create_compress(cinfo); | |
152 cinfo->dest = destination; | |
153 cinfo->dest->init_destination = prepareOutput; | |
154 cinfo->dest->empty_output_buffer = writeOutput; | |
155 cinfo->dest->term_destination = finishOutput; | |
156 | |
157 cinfo->image_height = imageSize.height(); | |
158 cinfo->image_width = imageSize.width(); | |
159 cinfo->in_color_space = JCS_RGB; | |
160 cinfo->input_components = 3; | |
161 | |
162 jpeg_set_defaults(cinfo); | |
163 int compressionQuality = JPEGImageEncoder::computeCompressionQuality(quality
); | |
164 jpeg_set_quality(cinfo, compressionQuality, TRUE); | |
165 disableSubsamplingForHighQuality(cinfo, compressionQuality); | |
166 jpeg_start_compress(cinfo, TRUE); | |
167 | |
168 cinfo->client_data = 0; | |
169 return encoderState.release(); | |
170 } | |
171 | |
172 int JPEGImageEncoder::computeCompressionQuality(const double& quality) | |
173 { | |
174 int compressionQuality = JPEGImageEncoder::DefaultCompressionQuality; | |
175 if (quality >= 0.0 && quality <= 1.0) | |
176 compressionQuality = static_cast<int>(quality * 100 + 0.5); | |
177 return compressionQuality; | |
178 } | |
179 | |
180 bool JPEGImageEncoder::encodeWithPreInitializedState(PassOwnPtr<JPEGImageEncoder
State> encoderState, const unsigned char* inputPixels) | |
181 { | |
182 JPEGImageEncoderStateImpl* encoderStateImpl = static_cast<JPEGImageEncoderSt
ateImpl*>(encoderState.get()); | |
183 | |
184 Vector<JSAMPLE> row; | |
185 row.resize(encoderStateImpl->cinfo()->image_width * encoderStateImpl->cinfo(
)->input_components); | |
186 | |
187 jmp_buf jumpBuffer; | |
188 encoderStateImpl->cinfo()->client_data = &jumpBuffer; | |
189 | |
190 if (setjmp(jumpBuffer)) { | |
191 return false; | |
192 } | |
193 | |
194 unsigned char* pixels = const_cast<unsigned char*>(inputPixels); | |
195 const size_t pixelRowStride = encoderStateImpl->cinfo()->image_width * 4; | |
196 while (encoderStateImpl->cinfo()->next_scanline < encoderStateImpl->cinfo()-
>image_height) { | |
197 JSAMPLE* rowData = row.data(); | |
198 RGBAtoRGB(pixels, encoderStateImpl->cinfo()->image_width, rowData); | |
199 jpeg_write_scanlines(encoderStateImpl->cinfo(), &rowData, 1); | |
200 pixels += pixelRowStride; | |
201 } | |
202 | |
203 jpeg_finish_compress(encoderStateImpl->cinfo()); | |
204 return true; | |
205 } | |
206 | |
207 bool JPEGImageEncoder::encode(const ImageDataBuffer& imageData, const double& qu
ality, Vector<unsigned char>* output) | |
208 { | |
209 if (!imageData.pixels()) | |
210 return false; | |
211 | |
212 OwnPtr<JPEGImageEncoderState> encoderState = JPEGImageEncoderState::create(i
mageData.size(), quality, output); | |
213 if (!encoderState) | |
214 return false; | |
215 | |
216 return JPEGImageEncoder::encodeWithPreInitializedState(encoderState.release(
), imageData.pixels()); | |
217 } | |
218 | |
219 } // namespace blink | |
OLD | NEW |