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

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

Powered by Google App Engine
This is Rietveld 408576698