OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010, Google Inc. All rights reserved. | 2 * Copyright (c) 2010, Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 16 matching lines...) Expand all Loading... |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "platform/image-encoders/JPEGImageEncoder.h" | 31 #include "platform/image-encoders/JPEGImageEncoder.h" |
32 | 32 |
33 #include "SkColorPriv.h" | 33 #include "SkColorPriv.h" |
34 #include "platform/geometry/IntSize.h" | 34 #include "platform/geometry/IntSize.h" |
35 #include "platform/graphics/ImageBuffer.h" | 35 #include "platform/graphics/ImageBuffer.h" |
36 #include "wtf/CurrentTime.h" | 36 #include "wtf/CurrentTime.h" |
37 #include "wtf/PtrUtil.h" | |
38 #include <memory> | |
39 | 37 |
40 extern "C" { | 38 extern "C" { |
41 #include <setjmp.h> | 39 #include <setjmp.h> |
42 #include <stdio.h> // jpeglib.h needs stdio.h FILE | 40 #include <stdio.h> // jpeglib.h needs stdio.h FILE |
43 #include "jpeglib.h" | 41 #include "jpeglib.h" |
44 } | 42 } |
45 | 43 |
46 namespace blink { | 44 namespace blink { |
47 | 45 |
48 struct JPEGOutputBuffer : public jpeg_destination_mgr { | 46 struct JPEGOutputBuffer : public jpeg_destination_mgr { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 } | 127 } |
130 } | 128 } |
131 | 129 |
132 #define SET_JUMP_BUFFER(jpeg_compress_struct_ptr, what_to_return) \ | 130 #define SET_JUMP_BUFFER(jpeg_compress_struct_ptr, what_to_return) \ |
133 jmp_buf jumpBuffer; \ | 131 jmp_buf jumpBuffer; \ |
134 jpeg_compress_struct_ptr->client_data = &jumpBuffer; \ | 132 jpeg_compress_struct_ptr->client_data = &jumpBuffer; \ |
135 if (setjmp(jumpBuffer)) { \ | 133 if (setjmp(jumpBuffer)) { \ |
136 return what_to_return; \ | 134 return what_to_return; \ |
137 } | 135 } |
138 | 136 |
139 std::unique_ptr<JPEGImageEncoderState> JPEGImageEncoderState::create(const IntSi
ze& imageSize, const double& quality, Vector<unsigned char>* output) | 137 PassOwnPtr<JPEGImageEncoderState> JPEGImageEncoderState::create(const IntSize& i
mageSize, const double& quality, Vector<unsigned char>* output) |
140 { | 138 { |
141 if (imageSize.width() <= 0 || imageSize.height() <= 0) | 139 if (imageSize.width() <= 0 || imageSize.height() <= 0) |
142 return nullptr; | 140 return nullptr; |
143 | 141 |
144 std::unique_ptr<JPEGImageEncoderStateImpl> encoderState = wrapUnique(new JPE
GImageEncoderStateImpl()); | 142 OwnPtr<JPEGImageEncoderStateImpl> encoderState = adoptPtr(new JPEGImageEncod
erStateImpl()); |
145 | 143 |
146 jpeg_compress_struct* cinfo = encoderState->cinfo(); | 144 jpeg_compress_struct* cinfo = encoderState->cinfo(); |
147 jpeg_error_mgr* error = encoderState->error(); | 145 jpeg_error_mgr* error = encoderState->error(); |
148 cinfo->err = jpeg_std_error(error); | 146 cinfo->err = jpeg_std_error(error); |
149 error->error_exit = handleError; | 147 error->error_exit = handleError; |
150 | 148 |
151 SET_JUMP_BUFFER(cinfo, nullptr); | 149 SET_JUMP_BUFFER(cinfo, nullptr); |
152 | 150 |
153 JPEGOutputBuffer* destination = encoderState->outputBuffer(); | 151 JPEGOutputBuffer* destination = encoderState->outputBuffer(); |
154 destination->output = output; | 152 destination->output = output; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 | 198 |
201 if (deadlineSeconds - SlackBeforeDeadline - monotonicallyIncreasingTime(
) <= 0) { | 199 if (deadlineSeconds - SlackBeforeDeadline - monotonicallyIncreasingTime(
) <= 0) { |
202 return currentRowsCompleted; | 200 return currentRowsCompleted; |
203 } | 201 } |
204 } | 202 } |
205 | 203 |
206 jpeg_finish_compress(encoderStateImpl->cinfo()); | 204 jpeg_finish_compress(encoderStateImpl->cinfo()); |
207 return currentRowsCompleted; | 205 return currentRowsCompleted; |
208 } | 206 } |
209 | 207 |
210 bool JPEGImageEncoder::encodeWithPreInitializedState(std::unique_ptr<JPEGImageEn
coderState> encoderState, const unsigned char* inputPixels, int numRowsCompleted
) | 208 bool JPEGImageEncoder::encodeWithPreInitializedState(PassOwnPtr<JPEGImageEncoder
State> encoderState, const unsigned char* inputPixels, int numRowsCompleted) |
211 { | 209 { |
212 JPEGImageEncoderStateImpl* encoderStateImpl = static_cast<JPEGImageEncoderSt
ateImpl*>(encoderState.get()); | 210 JPEGImageEncoderStateImpl* encoderStateImpl = static_cast<JPEGImageEncoderSt
ateImpl*>(encoderState.get()); |
213 | 211 |
214 Vector<JSAMPLE> row; | 212 Vector<JSAMPLE> row; |
215 row.resize(encoderStateImpl->cinfo()->image_width * encoderStateImpl->cinfo(
)->input_components); | 213 row.resize(encoderStateImpl->cinfo()->image_width * encoderStateImpl->cinfo(
)->input_components); |
216 | 214 |
217 SET_JUMP_BUFFER(encoderStateImpl->cinfo(), false); | 215 SET_JUMP_BUFFER(encoderStateImpl->cinfo(), false); |
218 | 216 |
219 const size_t pixelRowStride = encoderStateImpl->cinfo()->image_width * 4; | 217 const size_t pixelRowStride = encoderStateImpl->cinfo()->image_width * 4; |
220 unsigned char* pixels = const_cast<unsigned char*>(inputPixels) + pixelRowSt
ride * numRowsCompleted; | 218 unsigned char* pixels = const_cast<unsigned char*>(inputPixels) + pixelRowSt
ride * numRowsCompleted; |
221 while (encoderStateImpl->cinfo()->next_scanline < encoderStateImpl->cinfo()-
>image_height) { | 219 while (encoderStateImpl->cinfo()->next_scanline < encoderStateImpl->cinfo()-
>image_height) { |
222 JSAMPLE* rowData = row.data(); | 220 JSAMPLE* rowData = row.data(); |
223 RGBAtoRGB(pixels, encoderStateImpl->cinfo()->image_width, rowData); | 221 RGBAtoRGB(pixels, encoderStateImpl->cinfo()->image_width, rowData); |
224 jpeg_write_scanlines(encoderStateImpl->cinfo(), &rowData, 1); | 222 jpeg_write_scanlines(encoderStateImpl->cinfo(), &rowData, 1); |
225 pixels += pixelRowStride; | 223 pixels += pixelRowStride; |
226 } | 224 } |
227 | 225 |
228 jpeg_finish_compress(encoderStateImpl->cinfo()); | 226 jpeg_finish_compress(encoderStateImpl->cinfo()); |
229 return true; | 227 return true; |
230 } | 228 } |
231 | 229 |
232 bool JPEGImageEncoder::encode(const ImageDataBuffer& imageData, const double& qu
ality, Vector<unsigned char>* output) | 230 bool JPEGImageEncoder::encode(const ImageDataBuffer& imageData, const double& qu
ality, Vector<unsigned char>* output) |
233 { | 231 { |
234 if (!imageData.pixels()) | 232 if (!imageData.pixels()) |
235 return false; | 233 return false; |
236 | 234 |
237 std::unique_ptr<JPEGImageEncoderState> encoderState = JPEGImageEncoderState:
:create(imageData.size(), quality, output); | 235 OwnPtr<JPEGImageEncoderState> encoderState = JPEGImageEncoderState::create(i
mageData.size(), quality, output); |
238 if (!encoderState) | 236 if (!encoderState) |
239 return false; | 237 return false; |
240 | 238 |
241 return JPEGImageEncoder::encodeWithPreInitializedState(std::move(encoderStat
e), imageData.pixels()); | 239 return JPEGImageEncoder::encodeWithPreInitializedState(std::move(encoderStat
e), imageData.pixels()); |
242 } | 240 } |
243 | 241 |
244 } // namespace blink | 242 } // namespace blink |
OLD | NEW |