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> |
37 | 39 |
38 extern "C" { | 40 extern "C" { |
39 #include <setjmp.h> | 41 #include <setjmp.h> |
40 #include <stdio.h> // jpeglib.h needs stdio.h FILE | 42 #include <stdio.h> // jpeglib.h needs stdio.h FILE |
41 #include "jpeglib.h" | 43 #include "jpeglib.h" |
42 } | 44 } |
43 | 45 |
44 namespace blink { | 46 namespace blink { |
45 | 47 |
46 struct JPEGOutputBuffer : public jpeg_destination_mgr { | 48 struct JPEGOutputBuffer : public jpeg_destination_mgr { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 } | 129 } |
128 } | 130 } |
129 | 131 |
130 #define SET_JUMP_BUFFER(jpeg_compress_struct_ptr, what_to_return) \ | 132 #define SET_JUMP_BUFFER(jpeg_compress_struct_ptr, what_to_return) \ |
131 jmp_buf jumpBuffer; \ | 133 jmp_buf jumpBuffer; \ |
132 jpeg_compress_struct_ptr->client_data = &jumpBuffer; \ | 134 jpeg_compress_struct_ptr->client_data = &jumpBuffer; \ |
133 if (setjmp(jumpBuffer)) { \ | 135 if (setjmp(jumpBuffer)) { \ |
134 return what_to_return; \ | 136 return what_to_return; \ |
135 } | 137 } |
136 | 138 |
137 PassOwnPtr<JPEGImageEncoderState> JPEGImageEncoderState::create(const IntSize& i
mageSize, const double& quality, Vector<unsigned char>* output) | 139 std::unique_ptr<JPEGImageEncoderState> JPEGImageEncoderState::create(const IntSi
ze& imageSize, const double& quality, Vector<unsigned char>* output) |
138 { | 140 { |
139 if (imageSize.width() <= 0 || imageSize.height() <= 0) | 141 if (imageSize.width() <= 0 || imageSize.height() <= 0) |
140 return nullptr; | 142 return nullptr; |
141 | 143 |
142 OwnPtr<JPEGImageEncoderStateImpl> encoderState = adoptPtr(new JPEGImageEncod
erStateImpl()); | 144 std::unique_ptr<JPEGImageEncoderStateImpl> encoderState = wrapUnique(new JPE
GImageEncoderStateImpl()); |
143 | 145 |
144 jpeg_compress_struct* cinfo = encoderState->cinfo(); | 146 jpeg_compress_struct* cinfo = encoderState->cinfo(); |
145 jpeg_error_mgr* error = encoderState->error(); | 147 jpeg_error_mgr* error = encoderState->error(); |
146 cinfo->err = jpeg_std_error(error); | 148 cinfo->err = jpeg_std_error(error); |
147 error->error_exit = handleError; | 149 error->error_exit = handleError; |
148 | 150 |
149 SET_JUMP_BUFFER(cinfo, nullptr); | 151 SET_JUMP_BUFFER(cinfo, nullptr); |
150 | 152 |
151 JPEGOutputBuffer* destination = encoderState->outputBuffer(); | 153 JPEGOutputBuffer* destination = encoderState->outputBuffer(); |
152 destination->output = output; | 154 destination->output = output; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 | 200 |
199 if (deadlineSeconds - SlackBeforeDeadline - monotonicallyIncreasingTime(
) <= 0) { | 201 if (deadlineSeconds - SlackBeforeDeadline - monotonicallyIncreasingTime(
) <= 0) { |
200 return currentRowsCompleted; | 202 return currentRowsCompleted; |
201 } | 203 } |
202 } | 204 } |
203 | 205 |
204 jpeg_finish_compress(encoderStateImpl->cinfo()); | 206 jpeg_finish_compress(encoderStateImpl->cinfo()); |
205 return currentRowsCompleted; | 207 return currentRowsCompleted; |
206 } | 208 } |
207 | 209 |
208 bool JPEGImageEncoder::encodeWithPreInitializedState(PassOwnPtr<JPEGImageEncoder
State> encoderState, const unsigned char* inputPixels, int numRowsCompleted) | 210 bool JPEGImageEncoder::encodeWithPreInitializedState(std::unique_ptr<JPEGImageEn
coderState> encoderState, const unsigned char* inputPixels, int numRowsCompleted
) |
209 { | 211 { |
210 JPEGImageEncoderStateImpl* encoderStateImpl = static_cast<JPEGImageEncoderSt
ateImpl*>(encoderState.get()); | 212 JPEGImageEncoderStateImpl* encoderStateImpl = static_cast<JPEGImageEncoderSt
ateImpl*>(encoderState.get()); |
211 | 213 |
212 Vector<JSAMPLE> row; | 214 Vector<JSAMPLE> row; |
213 row.resize(encoderStateImpl->cinfo()->image_width * encoderStateImpl->cinfo(
)->input_components); | 215 row.resize(encoderStateImpl->cinfo()->image_width * encoderStateImpl->cinfo(
)->input_components); |
214 | 216 |
215 SET_JUMP_BUFFER(encoderStateImpl->cinfo(), false); | 217 SET_JUMP_BUFFER(encoderStateImpl->cinfo(), false); |
216 | 218 |
217 const size_t pixelRowStride = encoderStateImpl->cinfo()->image_width * 4; | 219 const size_t pixelRowStride = encoderStateImpl->cinfo()->image_width * 4; |
218 unsigned char* pixels = const_cast<unsigned char*>(inputPixels) + pixelRowSt
ride * numRowsCompleted; | 220 unsigned char* pixels = const_cast<unsigned char*>(inputPixels) + pixelRowSt
ride * numRowsCompleted; |
219 while (encoderStateImpl->cinfo()->next_scanline < encoderStateImpl->cinfo()-
>image_height) { | 221 while (encoderStateImpl->cinfo()->next_scanline < encoderStateImpl->cinfo()-
>image_height) { |
220 JSAMPLE* rowData = row.data(); | 222 JSAMPLE* rowData = row.data(); |
221 RGBAtoRGB(pixels, encoderStateImpl->cinfo()->image_width, rowData); | 223 RGBAtoRGB(pixels, encoderStateImpl->cinfo()->image_width, rowData); |
222 jpeg_write_scanlines(encoderStateImpl->cinfo(), &rowData, 1); | 224 jpeg_write_scanlines(encoderStateImpl->cinfo(), &rowData, 1); |
223 pixels += pixelRowStride; | 225 pixels += pixelRowStride; |
224 } | 226 } |
225 | 227 |
226 jpeg_finish_compress(encoderStateImpl->cinfo()); | 228 jpeg_finish_compress(encoderStateImpl->cinfo()); |
227 return true; | 229 return true; |
228 } | 230 } |
229 | 231 |
230 bool JPEGImageEncoder::encode(const ImageDataBuffer& imageData, const double& qu
ality, Vector<unsigned char>* output) | 232 bool JPEGImageEncoder::encode(const ImageDataBuffer& imageData, const double& qu
ality, Vector<unsigned char>* output) |
231 { | 233 { |
232 if (!imageData.pixels()) | 234 if (!imageData.pixels()) |
233 return false; | 235 return false; |
234 | 236 |
235 OwnPtr<JPEGImageEncoderState> encoderState = JPEGImageEncoderState::create(i
mageData.size(), quality, output); | 237 std::unique_ptr<JPEGImageEncoderState> encoderState = JPEGImageEncoderState:
:create(imageData.size(), quality, output); |
236 if (!encoderState) | 238 if (!encoderState) |
237 return false; | 239 return false; |
238 | 240 |
239 return JPEGImageEncoder::encodeWithPreInitializedState(std::move(encoderStat
e), imageData.pixels()); | 241 return JPEGImageEncoder::encodeWithPreInitializedState(std::move(encoderStat
e), imageData.pixels()); |
240 } | 242 } |
241 | 243 |
242 } // namespace blink | 244 } // namespace blink |
OLD | NEW |