Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/codec/SkJpegCodec.cpp

Issue 1180983002: Switch SkJpegCode to libjpeg-turbo (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: cros 3 Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkCodec.h" 8 #include "SkCodec.h"
9 #include "SkJpegCodec.h" 9 #include "SkJpegCodec.h"
10 #include "SkJpegDecoderMgr.h" 10 #include "SkJpegDecoderMgr.h"
11 #include "SkJpegUtility_codec.h" 11 #include "SkJpegUtility_codec.h"
12 #include "SkCodecPriv.h" 12 #include "SkCodecPriv.h"
13 #include "SkColorPriv.h" 13 #include "SkColorPriv.h"
14 #include "SkStream.h" 14 #include "SkStream.h"
15 #include "SkTemplates.h" 15 #include "SkTemplates.h"
16 #include "SkTypes.h" 16 #include "SkTypes.h"
17 17
18 // stdio is needed for jpeglib 18 // stdio is needed for libjpeg-turbo
19 #include <stdio.h> 19 #include <stdio.h>
20 20
21 extern "C" { 21 extern "C" {
22 #include "jpeglibmangler.h"
22 #include "jerror.h" 23 #include "jerror.h"
23 #include "jmorecfg.h"
24 #include "jpegint.h" 24 #include "jpegint.h"
25 #include "jpeglib.h" 25 #include "jpeglib.h"
26 } 26 }
27 27
28 // ANDROID_RGB
29 // If this is defined in the jpeg headers it indicates that jpeg offers
30 // support for two additional formats: JCS_RGBA_8888 and JCS_RGB_565.
31
32 /* 28 /*
33 * Get the source configuarion for the swizzler 29 * Convert a row of CMYK samples to RGBA in place.
34 */
35 SkSwizzler::SrcConfig get_src_config(const jpeg_decompress_struct& dinfo) {
36 if (JCS_CMYK == dinfo.out_color_space) {
37 // We will need to perform a manual conversion
38 return SkSwizzler::kRGBX;
39 }
40 if (3 == dinfo.out_color_components && JCS_RGB == dinfo.out_color_space) {
41 return SkSwizzler::kRGB;
42 }
43 #ifdef ANDROID_RGB
44 if (JCS_RGBA_8888 == dinfo.out_color_space) {
45 return SkSwizzler::kRGBX;
46 }
47
48 if (JCS_RGB_565 == dinfo.out_color_space) {
49 return SkSwizzler::kRGB_565;
50 }
51 #endif
52 if (1 == dinfo.out_color_components && JCS_GRAYSCALE == dinfo.out_color_spac e) {
53 return SkSwizzler::kGray;
54 }
55 return SkSwizzler::kUnknown;
56 }
57
58 /*
59 * Convert a row of CMYK samples to RGBX in place.
60 * Note that this method moves the row pointer. 30 * Note that this method moves the row pointer.
61 * @param width the number of pixels in the row that is being converted 31 * @param width the number of pixels in the row that is being converted
62 * CMYK is stored as four bytes per pixel 32 * CMYK is stored as four bytes per pixel
63 */ 33 */
64 static void convert_CMYK_to_RGB(uint8_t* row, uint32_t width) { 34 static void convert_CMYK_to_RGBA(uint8_t* row, uint32_t width) {
65 // We will implement a crude conversion from CMYK -> RGB using formulas 35 // We will implement a crude conversion from CMYK -> RGB using formulas
66 // from easyrgb.com. 36 // from easyrgb.com.
67 // 37 //
68 // CMYK -> CMY 38 // CMYK -> CMY
69 // C = C * (1 - K) + K 39 // C = C * (1 - K) + K
70 // M = M * (1 - K) + K 40 // M = M * (1 - K) + K
71 // Y = Y * (1 - K) + K 41 // Y = Y * (1 - K) + K
72 // 42 //
73 // libjpeg actually gives us inverted CMYK, so we must subtract the 43 // libjpeg actually gives us inverted CMYK, so we must subtract the
74 // original terms from 1. 44 // original terms from 1.
(...skipping 22 matching lines...) Expand all
97 // 67 //
98 // As a final note, we have treated the CMYK values as if they were on 68 // As a final note, we have treated the CMYK values as if they were on
99 // a scale from 0-1, when in fact they are 8-bit ints scaling from 0-255. 69 // a scale from 0-1, when in fact they are 8-bit ints scaling from 0-255.
100 // We must divide each CMYK component by 255 to obtain the true conversion 70 // We must divide each CMYK component by 255 to obtain the true conversion
101 // we should perform. 71 // we should perform.
102 // CMYK -> RGB 72 // CMYK -> RGB
103 // R = C * K / 255 73 // R = C * K / 255
104 // G = M * K / 255 74 // G = M * K / 255
105 // B = Y * K / 255 75 // B = Y * K / 255
106 for (uint32_t x = 0; x < width; x++, row += 4) { 76 for (uint32_t x = 0; x < width; x++, row += 4) {
77 #if defined(SK_PMCOLOR_IS_RGBA)
107 row[0] = SkMulDiv255Round(row[0], row[3]); 78 row[0] = SkMulDiv255Round(row[0], row[3]);
108 row[1] = SkMulDiv255Round(row[1], row[3]); 79 row[1] = SkMulDiv255Round(row[1], row[3]);
109 row[2] = SkMulDiv255Round(row[2], row[3]); 80 row[2] = SkMulDiv255Round(row[2], row[3]);
81 #else
82 uint8_t tmp = row[0];
83 row[0] = SkMulDiv255Round(row[2], row[3]);
84 row[1] = SkMulDiv255Round(row[1], row[3]);
85 row[2] = SkMulDiv255Round(tmp, row[3]);
86 #endif
110 row[3] = 0xFF; 87 row[3] = 0xFF;
111 } 88 }
112 } 89 }
113 90
114 bool SkJpegCodec::IsJpeg(SkStream* stream) { 91 bool SkJpegCodec::IsJpeg(SkStream* stream) {
115 static const uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF }; 92 static const uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF };
116 char buffer[sizeof(jpegSig)]; 93 char buffer[sizeof(jpegSig)];
117 return stream->read(buffer, sizeof(jpegSig)) == sizeof(jpegSig) && 94 return stream->read(buffer, sizeof(jpegSig)) == sizeof(jpegSig) &&
118 !memcmp(buffer, jpegSig, sizeof(jpegSig)); 95 !memcmp(buffer, jpegSig, sizeof(jpegSig));
119 } 96 }
120 97
121 bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut, 98 bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut,
122 JpegDecoderMgr** decoderMgrOut) { 99 JpegDecoderMgr** decoderMgrOut) {
123 100
124 // Create a JpegDecoderMgr to own all of the decompress information 101 // Create a JpegDecoderMgr to own all of the decompress information
125 SkAutoTDelete<JpegDecoderMgr> decoderMgr(SkNEW_ARGS(JpegDecoderMgr, (stream) )); 102 SkAutoTDelete<JpegDecoderMgr> decoderMgr(SkNEW_ARGS(JpegDecoderMgr, (stream) ));
126 103
127 // libjpeg errors will be caught and reported here 104 // libjpeg errors will be caught and reported here
128 if (setjmp(decoderMgr->getJmpBuf())) { 105 if (setjmp(decoderMgr->getJmpBuf())) {
129 return decoderMgr->returnFalse("setjmp"); 106 return decoderMgr->returnFalse("setjmp");
130 } 107 }
131 108
132 // Initialize the decompress info and the source manager 109 // Initialize the decompress info and the source manager
133 decoderMgr->init(); 110 decoderMgr->init();
134 111
135 // Read the jpeg header 112 // Read the jpeg header
136 if (JPEG_HEADER_OK != jpeg_read_header(decoderMgr->dinfo(), true)) { 113 if (JPEG_HEADER_OK != turbo_jpeg_read_header(decoderMgr->dinfo(), true)) {
137 return decoderMgr->returnFalse("read_header"); 114 return decoderMgr->returnFalse("read_header");
138 } 115 }
139 116
140 if (NULL != codecOut) { 117 if (NULL != codecOut) {
141 // Recommend the color type to decode to 118 // Recommend the color type to decode to
142 const SkColorType colorType = decoderMgr->getColorType(); 119 const SkColorType colorType = decoderMgr->getColorType();
143 120
144 // Create image info object and the codec 121 // Create image info object and the codec
145 const SkImageInfo& imageInfo = SkImageInfo::Make(decoderMgr->dinfo()->im age_width, 122 const SkImageInfo& imageInfo = SkImageInfo::Make(decoderMgr->dinfo()->im age_width,
146 decoderMgr->dinfo()->image_height, colorType, kOpaque_SkAlphaTyp e); 123 decoderMgr->dinfo()->image_height, colorType, kOpaque_SkAlphaTyp e);
(...skipping 14 matching lines...) Expand all
161 streamDeleter.detach(); 138 streamDeleter.detach();
162 return codec; 139 return codec;
163 } 140 }
164 return NULL; 141 return NULL;
165 } 142 }
166 143
167 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, 144 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream,
168 JpegDecoderMgr* decoderMgr) 145 JpegDecoderMgr* decoderMgr)
169 : INHERITED(srcInfo, stream) 146 : INHERITED(srcInfo, stream)
170 , fDecoderMgr(decoderMgr) 147 , fDecoderMgr(decoderMgr)
171 , fSwizzler(NULL)
172 , fSrcRowBytes(0)
173 {} 148 {}
174 149
175 /* 150 /*
176 * Return a valid set of output dimensions for this decoder, given an input scal e 151 * Return a valid set of output dimensions for this decoder, given an input scal e
177 */ 152 */
178 SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const { 153 SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const {
179 // libjpeg supports scaling by 1/1, 1/2, 1/4, and 1/8, so we will support th ese as well 154 // libjpeg-turbo supports scaling by 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1, so we will
180 long scale; 155 // support these as well
181 if (desiredScale > 0.75f) { 156 long num;
182 scale = 1; 157 long denom = 8;
158 if (desiredScale > 0.875f) {
159 num = 8;
160 } else if (desiredScale > 0.75f) {
161 num = 7;
162 } else if (desiredScale > 0.625f) {
163 num = 6;
164 } else if (desiredScale > 0.5f) {
165 num = 5;
183 } else if (desiredScale > 0.375f) { 166 } else if (desiredScale > 0.375f) {
184 scale = 2; 167 num = 4;
185 } else if (desiredScale > 0.1875f) { 168 } else if (desiredScale > 0.25f) {
186 scale = 4; 169 num = 3;
170 } else if (desiredScale > 0.125f) {
171 num = 2;
187 } else { 172 } else {
188 scale = 8; 173 num = 1;
189 } 174 }
190 175
191 // Set up a fake decompress struct in order to use libjpeg to calculate outp ut dimensions 176 // Set up a fake decompress struct in order to use libjpeg to calculate outp ut dimensions
192 jpeg_decompress_struct dinfo; 177 jpeg_decompress_struct dinfo;
193 sk_bzero(&dinfo, sizeof(dinfo)); 178 sk_bzero(&dinfo, sizeof(dinfo));
194 dinfo.image_width = this->getInfo().width(); 179 dinfo.image_width = this->getInfo().width();
195 dinfo.image_height = this->getInfo().height(); 180 dinfo.image_height = this->getInfo().height();
196 dinfo.global_state = DSTATE_READY; 181 dinfo.global_state = DSTATE_READY;
197 dinfo.num_components = 0; 182 dinfo.num_components = 0;
198 dinfo.scale_num = 1; 183 dinfo.scale_num = num;
199 dinfo.scale_denom = scale; 184 dinfo.scale_denom = denom;
200 jpeg_calc_output_dimensions(&dinfo); 185 turbo_jpeg_calc_output_dimensions(&dinfo);
201 186
202 // Return the calculated output dimensions for the given scale 187 // Return the calculated output dimensions for the given scale
203 return SkISize::Make(dinfo.output_width, dinfo.output_height); 188 return SkISize::Make(dinfo.output_width, dinfo.output_height);
204 } 189 }
205 190
206 /* 191 /*
207 * Checks if the conversion between the input image and the requested output
208 * image has been implemented
209 */
210 static bool conversion_possible(const SkImageInfo& dst,
211 const SkImageInfo& src) {
212 // Ensure that the profile type is unchanged
213 if (dst.profileType() != src.profileType()) {
214 return false;
215 }
216
217 // Ensure that the alpha type is opaque
218 if (kOpaque_SkAlphaType != dst.alphaType()) {
219 return false;
220 }
221
222 // Always allow kN32 as the color type
223 if (kN32_SkColorType == dst.colorType()) {
224 return true;
225 }
226
227 // Otherwise require that the destination color type match our recommendatio n
228 return dst.colorType() == src.colorType();
229 }
230
231 /*
232 * Handles rewinding the input stream if it is necessary 192 * Handles rewinding the input stream if it is necessary
233 */ 193 */
234 bool SkJpegCodec::handleRewind() { 194 bool SkJpegCodec::handleRewind() {
235 switch(this->rewindIfNeeded()) { 195 switch(this->rewindIfNeeded()) {
236 case kCouldNotRewind_RewindState: 196 case kCouldNotRewind_RewindState:
237 return fDecoderMgr->returnFalse("could not rewind"); 197 return fDecoderMgr->returnFalse("could not rewind");
238 case kRewound_RewindState: { 198 case kRewound_RewindState: {
239 JpegDecoderMgr* decoderMgr = NULL; 199 JpegDecoderMgr* decoderMgr = NULL;
240 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) { 200 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) {
241 return fDecoderMgr->returnFalse("could not rewind"); 201 return fDecoderMgr->returnFalse("could not rewind");
242 } 202 }
243 SkASSERT(NULL != decoderMgr); 203 SkASSERT(NULL != decoderMgr);
244 fDecoderMgr.reset(decoderMgr); 204 fDecoderMgr.reset(decoderMgr);
245 return true; 205 return true;
246 } 206 }
247 case kNoRewindNecessary_RewindState: 207 case kNoRewindNecessary_RewindState:
248 return true; 208 return true;
249 default: 209 default:
250 SkASSERT(false); 210 SkASSERT(false);
251 return false; 211 return false;
252 } 212 }
253 } 213 }
254 214
255 /* 215 /*
216 * Checks if the conversion between the input image and the requested output
217 * image has been implemented
218 * Sets the output color space
219 */
220 bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dst) {
221 const SkImageInfo& src = this->getInfo();
222
223 // Ensure that the profile type is unchanged
224 if (dst.profileType() != src.profileType()) {
225 return false;
226 }
227
228 // Ensure that the alpha type is opaque
229 if (kOpaque_SkAlphaType != dst.alphaType()) {
230 return false;
231 }
232
233 // Check if we will decode to CMYK because a conversion to RGBA is not suppo rted
234 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->jpeg_color_space;
235 bool isCMYK = JCS_CMYK == colorSpace || JCS_YCCK == colorSpace;
236
237 // Check the byte ordering of the RGBA color space for the current platform
238 #if defined(SK_PMCOLOR_IS_RGBA)
239 J_COLOR_SPACE outRGBA = JCS_EXT_RGBA;
scroggo 2015/06/29 14:05:50 It seems like this is only needed if the dst is kN
msarett 2015/06/29 16:52:04 You're right. I will move this to that specific c
240 #else
241 J_COLOR_SPACE outRGBA = JCS_EXT_BGRA;
242 #endif
243
244 // Check for valid color types and set the output color space
245 switch (dst.colorType()) {
246 case kN32_SkColorType:
247 if (isCMYK) {
248 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
249 } else {
250 fDecoderMgr->dinfo()->out_color_space = outRGBA;
251 }
252 return true;
253 case kRGB_565_SkColorType:
254 if (isCMYK) {
255 return false;
256 } else {
257 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565;
258 }
259 return true;
260 case kGray_8_SkColorType:
261 if (isCMYK) {
262 return false;
263 } else {
264 // We will enable decodes to gray even if the image is color bec ause this is
265 // much faster than decoding to color and then converting
266 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE;
267 }
268 return true;
269 default:
270 return false;
271 }
272 }
273
274 /*
256 * Checks if we can scale to the requested dimensions and scales the dimensions 275 * Checks if we can scale to the requested dimensions and scales the dimensions
257 * if possible 276 * if possible
258 */ 277 */
259 bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) { 278 bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) {
260 // libjpeg can scale to 1/1, 1/2, 1/4, and 1/8 279 // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1
261 SkASSERT(1 == fDecoderMgr->dinfo()->scale_num); 280 fDecoderMgr->dinfo()->scale_denom = 8;
262 SkASSERT(1 == fDecoderMgr->dinfo()->scale_denom); 281 fDecoderMgr->dinfo()->scale_num = 8;
263 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); 282 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
264 while (fDecoderMgr->dinfo()->output_width != dstWidth || 283 while (fDecoderMgr->dinfo()->output_width != dstWidth ||
265 fDecoderMgr->dinfo()->output_height != dstHeight) { 284 fDecoderMgr->dinfo()->output_height != dstHeight) {
266 285
267 // Return a failure if we have tried all of the possible scales 286 // Return a failure if we have tried all of the possible scales
268 if (8 == fDecoderMgr->dinfo()->scale_denom || 287 if (1 == fDecoderMgr->dinfo()->scale_num ||
269 dstWidth > fDecoderMgr->dinfo()->output_width || 288 dstWidth > fDecoderMgr->dinfo()->output_width ||
270 dstHeight > fDecoderMgr->dinfo()->output_height) { 289 dstHeight > fDecoderMgr->dinfo()->output_height) {
271 return fDecoderMgr->returnFalse("could not scale to requested dimens ions"); 290 return fDecoderMgr->returnFalse("could not scale to requested dimens ions");
272 } 291 }
273 292
274 // Try the next scale 293 // Try the next scale
275 fDecoderMgr->dinfo()->scale_denom *= 2; 294 fDecoderMgr->dinfo()->scale_num -= 1;
276 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); 295 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
277 } 296 }
278 return true; 297 return true;
279 } 298 }
280 299
281 /* 300 /*
282 * Create the swizzler based on the encoded format
283 */
284 void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo,
285 void* dst, size_t dstRowBytes,
286 const Options& options) {
287 SkSwizzler::SrcConfig srcConfig = get_src_config(*fDecoderMgr->dinfo());
288 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, NULL, dstInfo, dst, ds tRowBytes,
289 options.fZeroInitialized));
290 fSrcRowBytes = SkSwizzler::BytesPerPixel(srcConfig) * dstInfo.width();
291 }
292
293 /*
294 * Performs the jpeg decode 301 * Performs the jpeg decode
295 */ 302 */
296 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, 303 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
297 void* dst, size_t dstRowBytes, 304 void* dst, size_t dstRowBytes,
298 const Options& options, SkPMColor*, int *) { 305 const Options& options, SkPMColor*, int *) {
299 306
300 // Rewind the stream if needed 307 // Rewind the stream if needed
301 if (!this->handleRewind()) { 308 if (!this->handleRewind()) {
302 fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind); 309 fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind);
303 } 310 }
304 311
305 // Get a pointer to the decompress info since we will use it quite frequentl y 312 // Get a pointer to the decompress info since we will use it quite frequentl y
306 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); 313 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
307 314
308 // Set the jump location for libjpeg errors 315 // Set the jump location for libjpeg errors
309 if (setjmp(fDecoderMgr->getJmpBuf())) { 316 if (setjmp(fDecoderMgr->getJmpBuf())) {
310 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); 317 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
311 } 318 }
312 319
313 // Check if we can decode to the requested destination 320 // Check if we can decode to the requested destination and set the output co lor space
314 if (!conversion_possible(dstInfo, this->getInfo())) { 321 if (!this->setOutputColorSpace(dstInfo)) {
315 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers ion); 322 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers ion);
316 } 323 }
317 324
318 // Perform the necessary scaling 325 // Perform the necessary scaling
319 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { 326 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
320 fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidSca le); 327 return fDecoderMgr->returnFailure("cannot scale to requested dims", kInv alidScale);
321 } 328 }
322 329
323 // Now, given valid output dimensions, we can start the decompress 330 // Now, given valid output dimensions, we can start the decompress
324 if (!jpeg_start_decompress(dinfo)) { 331 if (!turbo_jpeg_start_decompress(dinfo)) {
325 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); 332 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
326 } 333 }
327 334
328 // Create the swizzler 335 // The recommended output buffer height should always be 1 in high quality m odes.
329 this->initializeSwizzler(dstInfo, dst, dstRowBytes, options); 336 // If it's not, we want to know because it means our strategy is not optimal .
330 if (NULL == fSwizzler) { 337 SkASSERT(1 == dinfo->rec_outbuf_height);
331 return fDecoderMgr->returnFailure("getSwizzler", kUnimplemented);
332 }
333 338
334 // This is usually 1, but can also be 2 or 4. 339 // Perform the decode a single row at a time
335 // If we wanted to always read one row at a time, we could, but we will save space and time
336 // by using the recommendation from libjpeg.
337 const uint32_t rowsPerDecode = dinfo->rec_outbuf_height;
338 SkASSERT(rowsPerDecode <= 4);
339
340 // Create a buffer to contain decoded rows (libjpeg requires a 2D array)
341 SkASSERT(0 != fSrcRowBytes);
342 SkAutoTDeleteArray<uint8_t> srcBuffer(SkNEW_ARRAY(uint8_t, fSrcRowBytes * ro wsPerDecode));
343 JSAMPLE* srcRows[4];
344 uint8_t* srcPtr = srcBuffer.get();
345 for (uint8_t i = 0; i < rowsPerDecode; i++) {
346 srcRows[i] = (JSAMPLE*) srcPtr;
347 srcPtr += fSrcRowBytes;
348 }
349
350 // Ensure that we loop enough times to decode all of the rows
351 // libjpeg will prevent us from reading past the bottom of the image
352 uint32_t dstHeight = dstInfo.height(); 340 uint32_t dstHeight = dstInfo.height();
353 for (uint32_t y = 0; y < dstHeight + rowsPerDecode - 1; y += rowsPerDecode) { 341 JSAMPLE* dstRow = (JSAMPLE*) dst;
342 for (uint32_t y = 0; y < dstHeight; y++) {
354 // Read rows of the image 343 // Read rows of the image
355 uint32_t rowsDecoded = jpeg_read_scanlines(dinfo, srcRows, rowsPerDecode ); 344 uint32_t rowsDecoded = turbo_jpeg_read_scanlines(dinfo, &dstRow, 1);
356
357 // Convert to RGB if necessary
358 if (JCS_CMYK == dinfo->out_color_space) {
359 convert_CMYK_to_RGB(srcRows[0], dstInfo.width() * rowsDecoded);
360 }
361
362 // Swizzle to output destination
363 for (uint32_t i = 0; i < rowsDecoded; i++) {
364 fSwizzler->next(srcRows[i]);
365 }
366 345
367 // If we cannot read enough rows, assume the input is incomplete 346 // If we cannot read enough rows, assume the input is incomplete
368 if (rowsDecoded < rowsPerDecode && y + rowsDecoded < dstHeight) { 347 if (rowsDecoded != 1) {
369 // Fill the remainder of the image with black. This error handling 348 // Fill the remainder of the image with black. This error handling
370 // behavior is unspecified but SkCodec consistently uses black as 349 // behavior is unspecified but SkCodec consistently uses black as
371 // the fill color for opaque images. If the destination is kGray, 350 // the fill color for opaque images. If the destination is kGray,
372 // the low 8 bits of SK_ColorBLACK will be used. Conveniently, 351 // the low 8 bits of SK_ColorBLACK will be used. Conveniently,
373 // these are zeros, which is the representation for black in kGray. 352 // these are zeros, which is the representation for black in kGray.
374 SkSwizzler::Fill(fSwizzler->getDstRow(), dstInfo, dstRowBytes, 353 SkSwizzler::Fill(dstRow, dstInfo, dstRowBytes, dstHeight - y, SK_Col orBLACK, NULL);
375 dstHeight - y - rowsDecoded, SK_ColorBLACK, NULL);
376 354
377 // Prevent libjpeg from failing on incomplete decode 355 // Prevent libjpeg from failing on incomplete decode
378 dinfo->output_scanline = dstHeight; 356 dinfo->output_scanline = dstHeight;
379 357
380 // Finish the decode and indicate that the input was incomplete. 358 // Finish the decode and indicate that the input was incomplete.
381 jpeg_finish_decompress(dinfo); 359 turbo_jpeg_finish_decompress(dinfo);
382 return fDecoderMgr->returnFailure("Incomplete image data", kIncomple teInput); 360 return fDecoderMgr->returnFailure("Incomplete image data", kIncomple teInput);
383 } 361 }
362
363 // Convert to RGBA if necessary
364 if (JCS_CMYK == dinfo->out_color_space) {
365 convert_CMYK_to_RGBA(dstRow, dstInfo.width());
366 }
367
368 // Move to the next row
369 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes);
384 } 370 }
385 jpeg_finish_decompress(dinfo); 371 turbo_jpeg_finish_decompress(dinfo);
386 372
387 return kSuccess; 373 return kSuccess;
388 } 374 }
389 375
390 /* 376 /*
391 * Enable scanline decoding for jpegs 377 * Enable scanline decoding for jpegs
392 */ 378 */
393 class SkJpegScanlineDecoder : public SkScanlineDecoder { 379 class SkJpegScanlineDecoder : public SkScanlineDecoder {
394 public: 380 public:
395 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec) 381 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec)
396 : INHERITED(dstInfo) 382 : INHERITED(dstInfo)
397 , fCodec(codec) 383 , fCodec(codec)
398 { 384 {}
399 fStorage.reset(fCodec->fSrcRowBytes);
400 fSrcRow = static_cast<uint8_t*>(fStorage.get());
401 }
402 385
403 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowByte s) override { 386 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowByte s) override {
404 // Set the jump location for libjpeg errors 387 // Set the jump location for libjpeg errors
405 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 388 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
406 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator ::kInvalidInput); 389 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator ::kInvalidInput);
407 } 390 }
408 391
409 // Read rows one at a time 392 // Read rows one at a time
393 JSAMPLE* dstRow = (JSAMPLE*) dst;
410 for (int y = 0; y < count; y++) { 394 for (int y = 0; y < count; y++) {
411 // Read row of the image 395 // Read row of the image
412 uint32_t rowsDecoded = jpeg_read_scanlines(fCodec->fDecoderMgr->dinf o(), &fSrcRow, 1); 396 uint32_t rowsDecoded =
397 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dst Row, 1);
413 if (rowsDecoded != 1) { 398 if (rowsDecoded != 1) {
414 SkSwizzler::Fill(dst, this->dstInfo(), rowBytes, count - y, SK_C olorBLACK, NULL); 399 SkSwizzler::Fill(
400 dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBL ACK, NULL);
401 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo(). height();
402 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo());
415 return SkImageGenerator::kIncompleteInput; 403 return SkImageGenerator::kIncompleteInput;
416 } 404 }
417 405
418 // Convert to RGB if necessary 406 // Convert to RGBA if necessary
419 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { 407 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) {
420 convert_CMYK_to_RGB(fSrcRow, dstInfo().width()); 408 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width());
421 } 409 }
422 410
423 // Swizzle to output destination 411 // Move to the next row
424 fCodec->fSwizzler->setDstRow(dst); 412 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes);
425 fCodec->fSwizzler->next(fSrcRow);
426 dst = SkTAddOffset<void>(dst, rowBytes);
427 } 413 }
428 414
429 return SkImageGenerator::kSuccess; 415 return SkImageGenerator::kSuccess;
430 } 416 }
431 417
418 // TODO (msarett): skbug.com/3972
419 // Remove this macro after all platforms have an up to date version of libjp eg-turbo
scroggo 2015/06/29 14:05:50 I think we should remove this comment. After think
msarett 2015/06/29 16:52:04 Agreed. Removing.
420 #ifndef TURBO_HAS_SKIP
421 #define turbo_jpeg_skip_scanlines(dinfo, count) \
422 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \
423 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \
424 for (int y = 0; y < count; y++) { \
425 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \
426 }
427 #endif
428
432 SkImageGenerator::Result onSkipScanlines(int count) override { 429 SkImageGenerator::Result onSkipScanlines(int count) override {
433 // Set the jump location for libjpeg errors 430 // Set the jump location for libjpeg errors
434 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 431 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
435 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator ::kInvalidInput); 432 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator ::kInvalidInput);
436 } 433 }
437 434
438 // Read rows but ignore the output 435 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count);
439 for (int y = 0; y < count; y++) {
440 jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &fSrcRow, 1);
441 }
442 436
443 return SkImageGenerator::kSuccess; 437 return SkImageGenerator::kSuccess;
444 } 438 }
445 439
446 void onFinish() override { 440 void onFinish() override {
447 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 441 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
448 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); 442 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n");
449 return; 443 return;
450 } 444 }
451 445
452 jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); 446 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo());
453 } 447 }
454 448
455 private: 449 private:
456 SkJpegCodec* fCodec; // unowned 450 SkJpegCodec* fCodec; // unowned
457 SkAutoMalloc fStorage;
458 uint8_t* fSrcRow; // ptr into fStorage
459 451
460 typedef SkScanlineDecoder INHERITED; 452 typedef SkScanlineDecoder INHERITED;
461 }; 453 };
462 454
463 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, 455 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
464 const Options& options, SkPMColor ctable[], int* ctableCount) { 456 const Options& options, SkPMColor ctable[], int* ctableCount) {
465 457
466 // Rewind the stream if needed 458 // Rewind the stream if needed
467 if (!this->handleRewind()) { 459 if (!this->handleRewind()) {
468 SkCodecPrintf("Could not rewind\n"); 460 SkCodecPrintf("Could not rewind\n");
469 return NULL; 461 return NULL;
470 } 462 }
471 463
472 // Set the jump location for libjpeg errors 464 // Set the jump location for libjpeg errors
473 if (setjmp(fDecoderMgr->getJmpBuf())) { 465 if (setjmp(fDecoderMgr->getJmpBuf())) {
474 SkCodecPrintf("setjmp: Error from libjpeg\n"); 466 SkCodecPrintf("setjmp: Error from libjpeg\n");
475 return NULL; 467 return NULL;
476 } 468 }
477 469
478 // Check if we can decode to the requested destination 470 // Check if we can decode to the requested destination and set the output co lor space
479 if (!conversion_possible(dstInfo, this->getInfo())) { 471 if (!this->setOutputColorSpace(dstInfo)) {
480 SkCodecPrintf("Cannot convert to output type\n"); 472 SkCodecPrintf("Cannot convert to output type\n");
481 return NULL; 473 return NULL;
482 } 474 }
483 475
484 // Perform the necessary scaling 476 // Perform the necessary scaling
485 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { 477 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
486 SkCodecPrintf("Cannot scale ot output dimensions\n"); 478 SkCodecPrintf("Cannot scale to output dimensions\n");
487 return NULL; 479 return NULL;
488 } 480 }
489 481
490 // Now, given valid output dimensions, we can start the decompress 482 // Now, given valid output dimensions, we can start the decompress
491 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { 483 if (!turbo_jpeg_start_decompress(fDecoderMgr->dinfo())) {
492 SkCodecPrintf("start decompress failed\n"); 484 SkCodecPrintf("start decompress failed\n");
493 return NULL; 485 return NULL;
494 } 486 }
495 487
496 // Create the swizzler
497 this->initializeSwizzler(dstInfo, NULL, dstInfo.minRowBytes(), options);
498 if (NULL == fSwizzler) {
499 SkCodecPrintf("Could not create swizzler\n");
500 return NULL;
501 }
502
503 // Return the new scanline decoder 488 // Return the new scanline decoder
504 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this)); 489 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this));
505 } 490 }
OLDNEW
« gyp/codec.gyp ('K') | « src/codec/SkJpegCodec.h ('k') | src/codec/SkJpegDecoderMgr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698