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

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: Adding turbo to DEPS 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 "jerror.h" 22 #include "jerror.h"
23 #include "jmorecfg.h"
24 #include "jpegint.h" 23 #include "jpegint.h"
25 #include "jpeglib.h" 24 #include "jpeglib.h"
26 } 25 }
27 26
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 /* 27 /*
33 * Get the source configuarion for the swizzler 28 * 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. 29 * Note that this method moves the row pointer.
61 * @param width the number of pixels in the row that is being converted 30 * @param width the number of pixels in the row that is being converted
62 * CMYK is stored as four bytes per pixel 31 * CMYK is stored as four bytes per pixel
63 */ 32 */
64 static void convert_CMYK_to_RGB(uint8_t* row, uint32_t width) { 33 static void convert_CMYK_to_RGBA(uint8_t* row, uint32_t width) {
65 // We will implement a crude conversion from CMYK -> RGB using formulas 34 // We will implement a crude conversion from CMYK -> RGB using formulas
66 // from easyrgb.com. 35 // from easyrgb.com.
67 // 36 //
68 // CMYK -> CMY 37 // CMYK -> CMY
69 // C = C * (1 - K) + K 38 // C = C * (1 - K) + K
70 // M = M * (1 - K) + K 39 // M = M * (1 - K) + K
71 // Y = Y * (1 - K) + K 40 // Y = Y * (1 - K) + K
72 // 41 //
73 // libjpeg actually gives us inverted CMYK, so we must subtract the 42 // libjpeg actually gives us inverted CMYK, so we must subtract the
74 // original terms from 1. 43 // original terms from 1.
(...skipping 22 matching lines...) Expand all
97 // 66 //
98 // As a final note, we have treated the CMYK values as if they were on 67 // 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. 68 // 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 69 // We must divide each CMYK component by 255 to obtain the true conversion
101 // we should perform. 70 // we should perform.
102 // CMYK -> RGB 71 // CMYK -> RGB
103 // R = C * K / 255 72 // R = C * K / 255
104 // G = M * K / 255 73 // G = M * K / 255
105 // B = Y * K / 255 74 // B = Y * K / 255
106 for (uint32_t x = 0; x < width; x++, row += 4) { 75 for (uint32_t x = 0; x < width; x++, row += 4) {
76 #if defined(SK_PMCOLOR_IS_RGBA)
107 row[0] = SkMulDiv255Round(row[0], row[3]); 77 row[0] = SkMulDiv255Round(row[0], row[3]);
108 row[1] = SkMulDiv255Round(row[1], row[3]); 78 row[1] = SkMulDiv255Round(row[1], row[3]);
109 row[2] = SkMulDiv255Round(row[2], row[3]); 79 row[2] = SkMulDiv255Round(row[2], row[3]);
80 #else
81 uint8_t tmp = row[0];
82 row[0] = SkMulDiv255Round(row[2], row[3]);
83 row[1] = SkMulDiv255Round(row[1], row[3]);
84 row[2] = SkMulDiv255Round(tmp, row[3]);
85 #endif
110 row[3] = 0xFF; 86 row[3] = 0xFF;
111 } 87 }
112 } 88 }
113 89
114 bool SkJpegCodec::IsJpeg(SkStream* stream) { 90 bool SkJpegCodec::IsJpeg(SkStream* stream) {
115 static const uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF }; 91 static const uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF };
116 char buffer[sizeof(jpegSig)]; 92 char buffer[sizeof(jpegSig)];
117 return stream->read(buffer, sizeof(jpegSig)) == sizeof(jpegSig) && 93 return stream->read(buffer, sizeof(jpegSig)) == sizeof(jpegSig) &&
118 !memcmp(buffer, jpegSig, sizeof(jpegSig)); 94 !memcmp(buffer, jpegSig, sizeof(jpegSig));
119 } 95 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 streamDeleter.detach(); 137 streamDeleter.detach();
162 return codec; 138 return codec;
163 } 139 }
164 return NULL; 140 return NULL;
165 } 141 }
166 142
167 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, 143 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream,
168 JpegDecoderMgr* decoderMgr) 144 JpegDecoderMgr* decoderMgr)
169 : INHERITED(srcInfo, stream) 145 : INHERITED(srcInfo, stream)
170 , fDecoderMgr(decoderMgr) 146 , fDecoderMgr(decoderMgr)
171 , fSwizzler(NULL)
172 , fSrcRowBytes(0)
173 {} 147 {}
174 148
175 /* 149 /*
176 * Return a valid set of output dimensions for this decoder, given an input scal e 150 * Return a valid set of output dimensions for this decoder, given an input scal e
177 */ 151 */
178 SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const { 152 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 153 // 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; 154 // support these as well
181 if (desiredScale > 0.75f) { 155 long num;
182 scale = 1; 156 long denom = 8;
157 if (desiredScale > 0.875f) {
158 num = 8;
159 } else if (desiredScale > 0.75f) {
160 num = 7;
161 } else if (desiredScale > 0.625f) {
162 num = 6;
163 } else if (desiredScale > 0.5f) {
164 num = 5;
183 } else if (desiredScale > 0.375f) { 165 } else if (desiredScale > 0.375f) {
184 scale = 2; 166 num = 4;
185 } else if (desiredScale > 0.1875f) { 167 } else if (desiredScale > 0.25f) {
186 scale = 4; 168 num = 3;
169 } else if (desiredScale > 0.125f) {
170 num = 2;
187 } else { 171 } else {
188 scale = 8; 172 num = 1;
189 } 173 }
190 174
191 // Set up a fake decompress struct in order to use libjpeg to calculate outp ut dimensions 175 // Set up a fake decompress struct in order to use libjpeg to calculate outp ut dimensions
192 jpeg_decompress_struct dinfo; 176 jpeg_decompress_struct dinfo;
193 sk_bzero(&dinfo, sizeof(dinfo)); 177 sk_bzero(&dinfo, sizeof(dinfo));
194 dinfo.image_width = this->getInfo().width(); 178 dinfo.image_width = this->getInfo().width();
195 dinfo.image_height = this->getInfo().height(); 179 dinfo.image_height = this->getInfo().height();
196 dinfo.global_state = DSTATE_READY; 180 dinfo.global_state = DSTATE_READY;
197 dinfo.num_components = 0; 181 dinfo.num_components = 0;
198 dinfo.scale_num = 1; 182 dinfo.scale_num = num;
199 dinfo.scale_denom = scale; 183 dinfo.scale_denom = denom;
200 jpeg_calc_output_dimensions(&dinfo); 184 jpeg_calc_output_dimensions(&dinfo);
201 185
202 // Return the calculated output dimensions for the given scale 186 // Return the calculated output dimensions for the given scale
203 return SkISize::Make(dinfo.output_width, dinfo.output_height); 187 return SkISize::Make(dinfo.output_width, dinfo.output_height);
204 } 188 }
205 189
206 /* 190 /*
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 191 * Handles rewinding the input stream if it is necessary
233 */ 192 */
234 bool SkJpegCodec::handleRewind() { 193 bool SkJpegCodec::handleRewind() {
235 switch(this->rewindIfNeeded()) { 194 switch(this->rewindIfNeeded()) {
236 case kCouldNotRewind_RewindState: 195 case kCouldNotRewind_RewindState:
237 return fDecoderMgr->returnFalse("could not rewind"); 196 return fDecoderMgr->returnFalse("could not rewind");
238 case kRewound_RewindState: { 197 case kRewound_RewindState: {
239 JpegDecoderMgr* decoderMgr = NULL; 198 JpegDecoderMgr* decoderMgr = NULL;
240 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) { 199 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) {
241 return fDecoderMgr->returnFalse("could not rewind"); 200 return fDecoderMgr->returnFalse("could not rewind");
242 } 201 }
243 SkASSERT(NULL != decoderMgr); 202 SkASSERT(NULL != decoderMgr);
244 fDecoderMgr.reset(decoderMgr); 203 fDecoderMgr.reset(decoderMgr);
245 return true; 204 return true;
246 } 205 }
247 case kNoRewindNecessary_RewindState: 206 case kNoRewindNecessary_RewindState:
248 return true; 207 return true;
249 default: 208 default:
250 SkASSERT(false); 209 SkASSERT(false);
251 return false; 210 return false;
252 } 211 }
253 } 212 }
254 213
255 /* 214 /*
215 * Checks if the conversion between the input image and the requested output
216 * image has been implemented
217 * Sets the output color space
218 */
219 bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dst) {
220 const SkImageInfo& src = this->getInfo();
221
222 // Ensure that the profile type is unchanged
223 if (dst.profileType() != src.profileType()) {
224 return false;
225 }
226
227 // Ensure that the alpha type is opaque
228 if (kOpaque_SkAlphaType != dst.alphaType()) {
229 return false;
230 }
231
232 // Check if we will decode to CMYK because a conversion to RGBA is not suppo rted
233 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->jpeg_color_space;
234 bool isCMYK = JCS_CMYK == colorSpace || JCS_YCCK == colorSpace;
235
236 // Check the byte ordering of the RGBA color space for the current platform
237 #if defined(SK_PMCOLOR_IS_RGBA)
238 J_COLOR_SPACE outRGBA = JCS_EXT_RGBA;
239 #else
240 J_COLOR_SPACE outRGBA = JCS_EXT_BGRA;
241 #endif
242
243 // Check for valid color types and set the output color space
244 switch (dst.colorType()) {
245 case kN32_SkColorType:
246 if (isCMYK) {
247 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
248 } else {
249 fDecoderMgr->dinfo()->out_color_space = outRGBA;
250 }
251 return true;
252 case kRGB_565_SkColorType:
253 if (isCMYK) {
254 return false;
255 } else {
256 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565;
257 }
258 return true;
259 case kGray_8_SkColorType:
260 if (isCMYK) {
261 return false;
262 } else {
263 // We will enable decodes to gray even if the image is color bec ause this is
264 // much faster than decoding to color and then converting
265 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE;
266 }
267 return true;
268 default:
269 return false;
270 }
271 }
272
273 /*
256 * Checks if we can scale to the requested dimensions and scales the dimensions 274 * Checks if we can scale to the requested dimensions and scales the dimensions
257 * if possible 275 * if possible
258 */ 276 */
259 bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) { 277 bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) {
260 // libjpeg can scale to 1/1, 1/2, 1/4, and 1/8 278 // 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); 279 fDecoderMgr->dinfo()->scale_denom = 8;
262 SkASSERT(1 == fDecoderMgr->dinfo()->scale_denom); 280 fDecoderMgr->dinfo()->scale_num = 8;
263 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); 281 jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
264 while (fDecoderMgr->dinfo()->output_width != dstWidth || 282 while (fDecoderMgr->dinfo()->output_width != dstWidth ||
265 fDecoderMgr->dinfo()->output_height != dstHeight) { 283 fDecoderMgr->dinfo()->output_height != dstHeight) {
266 284
267 // Return a failure if we have tried all of the possible scales 285 // Return a failure if we have tried all of the possible scales
268 if (8 == fDecoderMgr->dinfo()->scale_denom || 286 if (1 == fDecoderMgr->dinfo()->scale_num ||
269 dstWidth > fDecoderMgr->dinfo()->output_width || 287 dstWidth > fDecoderMgr->dinfo()->output_width ||
270 dstHeight > fDecoderMgr->dinfo()->output_height) { 288 dstHeight > fDecoderMgr->dinfo()->output_height) {
271 return fDecoderMgr->returnFalse("could not scale to requested dimens ions"); 289 return fDecoderMgr->returnFalse("could not scale to requested dimens ions");
272 } 290 }
273 291
274 // Try the next scale 292 // Try the next scale
275 fDecoderMgr->dinfo()->scale_denom *= 2; 293 fDecoderMgr->dinfo()->scale_num -= 1;
276 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); 294 jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
277 } 295 }
278 return true; 296 return true;
279 } 297 }
280 298
281 /* 299 /*
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 300 * Performs the jpeg decode
295 */ 301 */
296 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, 302 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
297 void* dst, size_t dstRowBytes, 303 void* dst, size_t dstRowBytes,
298 const Options& options, SkPMColor*, int *) { 304 const Options& options, SkPMColor*, int *) {
299 305
300 // Rewind the stream if needed 306 // Rewind the stream if needed
301 if (!this->handleRewind()) { 307 if (!this->handleRewind()) {
302 fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind); 308 fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind);
303 } 309 }
304 310
305 // Get a pointer to the decompress info since we will use it quite frequentl y 311 // Get a pointer to the decompress info since we will use it quite frequentl y
306 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); 312 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
307 313
308 // Set the jump location for libjpeg errors 314 // Set the jump location for libjpeg errors
309 if (setjmp(fDecoderMgr->getJmpBuf())) { 315 if (setjmp(fDecoderMgr->getJmpBuf())) {
310 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); 316 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
311 } 317 }
312 318
313 // Check if we can decode to the requested destination 319 // Check if we can decode to the requested destination and set the output co lor space
314 if (!conversion_possible(dstInfo, this->getInfo())) { 320 if (!this->setOutputColorSpace(dstInfo)) {
315 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers ion); 321 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers ion);
316 } 322 }
317 323
318 // Perform the necessary scaling 324 // Perform the necessary scaling
319 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { 325 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
320 fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidSca le); 326 return fDecoderMgr->returnFailure("cannot scale to requested dims", kInv alidScale);
321 } 327 }
322 328
323 // Now, given valid output dimensions, we can start the decompress 329 // Now, given valid output dimensions, we can start the decompress
324 if (!jpeg_start_decompress(dinfo)) { 330 if (!jpeg_start_decompress(dinfo)) {
325 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); 331 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
326 } 332 }
327 333
328 // Create the swizzler 334 // The recommended output buffer height should always be 1 in high quality m odes.
329 this->initializeSwizzler(dstInfo, dst, dstRowBytes, options); 335 // If it's not, we want to know because it means our strategy is not optimal .
330 if (NULL == fSwizzler) { 336 SkASSERT(1 == dinfo->rec_outbuf_height);
331 return fDecoderMgr->returnFailure("getSwizzler", kUnimplemented);
332 }
333 337
334 // This is usually 1, but can also be 2 or 4. 338 // 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(); 339 uint32_t dstHeight = dstInfo.height();
353 for (uint32_t y = 0; y < dstHeight + rowsPerDecode - 1; y += rowsPerDecode) { 340 JSAMPLE* dstRow = (JSAMPLE*) dst;
341 for (uint32_t y = 0; y < dstHeight; y++) {
354 // Read rows of the image 342 // Read rows of the image
355 uint32_t rowsDecoded = jpeg_read_scanlines(dinfo, srcRows, rowsPerDecode ); 343 uint32_t rowsDecoded = 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 344
367 // If we cannot read enough rows, assume the input is incomplete 345 // If we cannot read enough rows, assume the input is incomplete
368 if (rowsDecoded < rowsPerDecode && y + rowsDecoded < dstHeight) { 346 if (rowsDecoded != 1) {
369 // Fill the remainder of the image with black. This error handling 347 // Fill the remainder of the image with black. This error handling
370 // behavior is unspecified but SkCodec consistently uses black as 348 // behavior is unspecified but SkCodec consistently uses black as
371 // the fill color for opaque images. If the destination is kGray, 349 // the fill color for opaque images. If the destination is kGray,
372 // the low 8 bits of SK_ColorBLACK will be used. Conveniently, 350 // the low 8 bits of SK_ColorBLACK will be used. Conveniently,
373 // these are zeros, which is the representation for black in kGray. 351 // these are zeros, which is the representation for black in kGray.
374 SkSwizzler::Fill(fSwizzler->getDstRow(), dstInfo, dstRowBytes, 352 SkSwizzler::Fill(dstRow, dstInfo, dstRowBytes, dstHeight - y, SK_Col orBLACK, NULL);
375 dstHeight - y - rowsDecoded, SK_ColorBLACK, NULL);
376 353
377 // Prevent libjpeg from failing on incomplete decode 354 // Prevent libjpeg from failing on incomplete decode
378 dinfo->output_scanline = dstHeight; 355 dinfo->output_scanline = dstHeight;
379 356
380 // Finish the decode and indicate that the input was incomplete. 357 // Finish the decode and indicate that the input was incomplete.
381 jpeg_finish_decompress(dinfo); 358 jpeg_finish_decompress(dinfo);
382 return fDecoderMgr->returnFailure("Incomplete image data", kIncomple teInput); 359 return fDecoderMgr->returnFailure("Incomplete image data", kIncomple teInput);
383 } 360 }
361
362 // Convert to RGBA if necessary
363 if (JCS_CMYK == dinfo->out_color_space) {
364 convert_CMYK_to_RGBA(dstRow, dstInfo.width());
365 }
366
367 // Move to the next row
368 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes);
384 } 369 }
385 jpeg_finish_decompress(dinfo); 370 jpeg_finish_decompress(dinfo);
386 371
387 return kSuccess; 372 return kSuccess;
388 } 373 }
389 374
390 /* 375 /*
391 * Enable scanline decoding for jpegs 376 * Enable scanline decoding for jpegs
392 */ 377 */
393 class SkJpegScanlineDecoder : public SkScanlineDecoder { 378 class SkJpegScanlineDecoder : public SkScanlineDecoder {
394 public: 379 public:
395 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec) 380 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec)
396 : INHERITED(dstInfo) 381 : INHERITED(dstInfo)
397 , fCodec(codec) 382 , fCodec(codec)
398 { 383 {}
399 fStorage.reset(fCodec->fSrcRowBytes);
400 fSrcRow = static_cast<uint8_t*>(fStorage.get());
401 }
402 384
403 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowByte s) override { 385 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowByte s) override {
404 // Set the jump location for libjpeg errors 386 // Set the jump location for libjpeg errors
405 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 387 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
406 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator ::kInvalidInput); 388 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator ::kInvalidInput);
407 } 389 }
408 390
409 // Read rows one at a time 391 // Read rows one at a time
392 JSAMPLE* dstRow = (JSAMPLE*) dst;
410 for (int y = 0; y < count; y++) { 393 for (int y = 0; y < count; y++) {
411 // Read row of the image 394 // Read row of the image
412 uint32_t rowsDecoded = jpeg_read_scanlines(fCodec->fDecoderMgr->dinf o(), &fSrcRow, 1); 395 uint32_t rowsDecoded = jpeg_read_scanlines(fCodec->fDecoderMgr->dinf o(), &dstRow, 1);
413 if (rowsDecoded != 1) { 396 if (rowsDecoded != 1) {
414 SkSwizzler::Fill(dst, this->dstInfo(), rowBytes, count - y, SK_C olorBLACK, NULL); 397 SkSwizzler::Fill(dstRow, this->dstInfo(), rowBytes, count - y, S K_ColorBLACK, NULL);
398 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo(). height();
399 jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo());
415 return SkImageGenerator::kIncompleteInput; 400 return SkImageGenerator::kIncompleteInput;
416 } 401 }
417 402
418 // Convert to RGB if necessary 403 // Convert to RGBA if necessary
419 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { 404 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) {
420 convert_CMYK_to_RGB(fSrcRow, dstInfo().width()); 405 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width());
421 } 406 }
422 407
423 // Swizzle to output destination 408 // Move to the next row
424 fCodec->fSwizzler->setDstRow(dst); 409 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes);
425 fCodec->fSwizzler->next(fSrcRow);
426 dst = SkTAddOffset<void>(dst, rowBytes);
427 } 410 }
428 411
429 return SkImageGenerator::kSuccess; 412 return SkImageGenerator::kSuccess;
430 } 413 }
431 414
415 // This is a temporary function that will be removed after we upstream jpeg_ skip_scanlines()
416 // to libjpeg-turbo.
417 // skbug.com/3972
418 // TODO (msarett): Remove this function when it is no longer necessary.
scroggo 2015/06/23 19:08:35 Alternatively, what do you think about making this
msarett 2015/06/24 20:07:08 Works for me.
419 JDIMENSION jpeg_skip_scanlines(jpeg_decompress_struct* dinfo, JDIMENSION cou nt) {
scroggo 2015/06/23 19:08:35 Shouldn't this return something? Also, it seems l
msarett 2015/06/24 20:07:09 Done.
420 // Create a garbage buffer to read into
421 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components);
422 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get());
423
424 // Read rows but ignore the output
425 for (int y = 0; y < count; y++) {
426 jpeg_read_scanlines(dinfo, &storagePtr, 1);
427 }
428 }
429
432 SkImageGenerator::Result onSkipScanlines(int count) override { 430 SkImageGenerator::Result onSkipScanlines(int count) override {
433 // Set the jump location for libjpeg errors 431 // Set the jump location for libjpeg errors
434 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 432 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
435 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator ::kInvalidInput); 433 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator ::kInvalidInput);
436 } 434 }
437 435
438 // Read rows but ignore the output 436 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 437
443 return SkImageGenerator::kSuccess; 438 return SkImageGenerator::kSuccess;
444 } 439 }
445 440
446 void onFinish() override { 441 void onFinish() override {
447 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 442 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
448 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); 443 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n");
449 return; 444 return;
450 } 445 }
451 446
452 jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); 447 jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo());
453 } 448 }
454 449
455 private: 450 private:
456 SkJpegCodec* fCodec; // unowned 451 SkJpegCodec* fCodec; // unowned
457 SkAutoMalloc fStorage;
458 uint8_t* fSrcRow; // ptr into fStorage
459 452
460 typedef SkScanlineDecoder INHERITED; 453 typedef SkScanlineDecoder INHERITED;
461 }; 454 };
462 455
463 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, 456 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
464 const Options& options, SkPMColor ctable[], int* ctableCount) { 457 const Options& options, SkPMColor ctable[], int* ctableCount) {
465 458
466 // Rewind the stream if needed 459 // Rewind the stream if needed
467 if (!this->handleRewind()) { 460 if (!this->handleRewind()) {
468 SkCodecPrintf("Could not rewind\n"); 461 SkCodecPrintf("Could not rewind\n");
469 return NULL; 462 return NULL;
470 } 463 }
471 464
472 // Set the jump location for libjpeg errors 465 // Set the jump location for libjpeg errors
473 if (setjmp(fDecoderMgr->getJmpBuf())) { 466 if (setjmp(fDecoderMgr->getJmpBuf())) {
474 SkCodecPrintf("setjmp: Error from libjpeg\n"); 467 SkCodecPrintf("setjmp: Error from libjpeg\n");
475 return NULL; 468 return NULL;
476 } 469 }
477 470
478 // Check if we can decode to the requested destination 471 // Check if we can decode to the requested destination and set the output co lor space
479 if (!conversion_possible(dstInfo, this->getInfo())) { 472 if (!this->setOutputColorSpace(dstInfo)) {
480 SkCodecPrintf("Cannot convert to output type\n"); 473 SkCodecPrintf("Cannot convert to output type\n");
481 return NULL; 474 return NULL;
482 } 475 }
483 476
484 // Perform the necessary scaling 477 // Perform the necessary scaling
485 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { 478 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
486 SkCodecPrintf("Cannot scale ot output dimensions\n"); 479 SkCodecPrintf("Cannot scale to output dimensions\n");
487 return NULL; 480 return NULL;
488 } 481 }
489 482
490 // Now, given valid output dimensions, we can start the decompress 483 // Now, given valid output dimensions, we can start the decompress
491 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { 484 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) {
492 SkCodecPrintf("start decompress failed\n"); 485 SkCodecPrintf("start decompress failed\n");
493 return NULL; 486 return NULL;
494 } 487 }
495 488
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 489 // Return the new scanline decoder
504 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this)); 490 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this));
505 } 491 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698