blob: 83656da42dc8d110f106ffc3693b621ccdad388c [file] [log] [blame]
msarette16b04a2015-04-15 07:32:19 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkCodec.h"
9#include "SkJpegCodec.h"
10#include "SkJpegDecoderMgr.h"
mtklein525e90a2015-06-18 09:58:57 -070011#include "SkJpegUtility_codec.h"
msarette16b04a2015-04-15 07:32:19 -070012#include "SkCodecPriv.h"
13#include "SkColorPriv.h"
scroggoeb602a52015-07-09 08:16:03 -070014#include "SkScanlineDecoder.h"
msarette16b04a2015-04-15 07:32:19 -070015#include "SkStream.h"
16#include "SkTemplates.h"
17#include "SkTypes.h"
18
msarett1c8a5872015-07-07 08:50:01 -070019// stdio is needed for libjpeg-turbo
msarette16b04a2015-04-15 07:32:19 -070020#include <stdio.h>
21
22extern "C" {
msarett1c8a5872015-07-07 08:50:01 -070023 #include "jpeglibmangler.h"
msarette16b04a2015-04-15 07:32:19 -070024 #include "jerror.h"
msarette16b04a2015-04-15 07:32:19 -070025 #include "jpegint.h"
26 #include "jpeglib.h"
27}
28
msarette16b04a2015-04-15 07:32:19 -070029/*
msarett1c8a5872015-07-07 08:50:01 -070030 * Convert a row of CMYK samples to RGBA in place.
msarette16b04a2015-04-15 07:32:19 -070031 * Note that this method moves the row pointer.
32 * @param width the number of pixels in the row that is being converted
33 * CMYK is stored as four bytes per pixel
34 */
msarett1c8a5872015-07-07 08:50:01 -070035static void convert_CMYK_to_RGBA(uint8_t* row, uint32_t width) {
msarette16b04a2015-04-15 07:32:19 -070036 // We will implement a crude conversion from CMYK -> RGB using formulas
37 // from easyrgb.com.
38 //
39 // CMYK -> CMY
40 // C = C * (1 - K) + K
41 // M = M * (1 - K) + K
42 // Y = Y * (1 - K) + K
43 //
44 // libjpeg actually gives us inverted CMYK, so we must subtract the
45 // original terms from 1.
46 // CMYK -> CMY
47 // C = (1 - C) * (1 - (1 - K)) + (1 - K)
48 // M = (1 - M) * (1 - (1 - K)) + (1 - K)
49 // Y = (1 - Y) * (1 - (1 - K)) + (1 - K)
50 //
51 // Simplifying the above expression.
52 // CMYK -> CMY
53 // C = 1 - CK
54 // M = 1 - MK
55 // Y = 1 - YK
56 //
57 // CMY -> RGB
58 // R = (1 - C) * 255
59 // G = (1 - M) * 255
60 // B = (1 - Y) * 255
61 //
62 // Therefore the full conversion is below. This can be verified at
63 // www.rapidtables.com (assuming inverted CMYK).
64 // CMYK -> RGB
65 // R = C * K * 255
66 // G = M * K * 255
67 // B = Y * K * 255
68 //
69 // As a final note, we have treated the CMYK values as if they were on
70 // a scale from 0-1, when in fact they are 8-bit ints scaling from 0-255.
71 // We must divide each CMYK component by 255 to obtain the true conversion
72 // we should perform.
73 // CMYK -> RGB
74 // R = C * K / 255
75 // G = M * K / 255
76 // B = Y * K / 255
77 for (uint32_t x = 0; x < width; x++, row += 4) {
msarett1c8a5872015-07-07 08:50:01 -070078#if defined(SK_PMCOLOR_IS_RGBA)
msarette16b04a2015-04-15 07:32:19 -070079 row[0] = SkMulDiv255Round(row[0], row[3]);
80 row[1] = SkMulDiv255Round(row[1], row[3]);
81 row[2] = SkMulDiv255Round(row[2], row[3]);
msarett1c8a5872015-07-07 08:50:01 -070082#else
83 uint8_t tmp = row[0];
84 row[0] = SkMulDiv255Round(row[2], row[3]);
85 row[1] = SkMulDiv255Round(row[1], row[3]);
86 row[2] = SkMulDiv255Round(tmp, row[3]);
87#endif
msarette16b04a2015-04-15 07:32:19 -070088 row[3] = 0xFF;
89 }
90}
91
92bool SkJpegCodec::IsJpeg(SkStream* stream) {
93 static const uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF };
94 char buffer[sizeof(jpegSig)];
95 return stream->read(buffer, sizeof(jpegSig)) == sizeof(jpegSig) &&
96 !memcmp(buffer, jpegSig, sizeof(jpegSig));
97}
98
99bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut,
100 JpegDecoderMgr** decoderMgrOut) {
101
102 // Create a JpegDecoderMgr to own all of the decompress information
103 SkAutoTDelete<JpegDecoderMgr> decoderMgr(SkNEW_ARGS(JpegDecoderMgr, (stream)));
104
105 // libjpeg errors will be caught and reported here
106 if (setjmp(decoderMgr->getJmpBuf())) {
107 return decoderMgr->returnFalse("setjmp");
108 }
109
110 // Initialize the decompress info and the source manager
111 decoderMgr->init();
112
113 // Read the jpeg header
msarettfcaaade2015-08-11 13:32:54 -0700114 if (JPEG_HEADER_OK != chromium_jpeg_read_header(decoderMgr->dinfo(), true)) {
msarette16b04a2015-04-15 07:32:19 -0700115 return decoderMgr->returnFalse("read_header");
116 }
117
118 if (NULL != codecOut) {
119 // Recommend the color type to decode to
120 const SkColorType colorType = decoderMgr->getColorType();
121
122 // Create image info object and the codec
123 const SkImageInfo& imageInfo = SkImageInfo::Make(decoderMgr->dinfo()->image_width,
124 decoderMgr->dinfo()->image_height, colorType, kOpaque_SkAlphaType);
125 *codecOut = SkNEW_ARGS(SkJpegCodec, (imageInfo, stream, decoderMgr.detach()));
126 } else {
127 SkASSERT(NULL != decoderMgrOut);
128 *decoderMgrOut = decoderMgr.detach();
129 }
130 return true;
131}
132
133SkCodec* SkJpegCodec::NewFromStream(SkStream* stream) {
134 SkAutoTDelete<SkStream> streamDeleter(stream);
135 SkCodec* codec = NULL;
136 if (ReadHeader(stream, &codec, NULL)) {
137 // Codec has taken ownership of the stream, we do not need to delete it
138 SkASSERT(codec);
139 streamDeleter.detach();
140 return codec;
141 }
142 return NULL;
143}
144
145SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream,
146 JpegDecoderMgr* decoderMgr)
147 : INHERITED(srcInfo, stream)
148 , fDecoderMgr(decoderMgr)
149{}
150
151/*
152 * Return a valid set of output dimensions for this decoder, given an input scale
153 */
154SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const {
msarett1c8a5872015-07-07 08:50:01 -0700155 // 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
156 // support these as well
157 long num;
158 long denom = 8;
159 if (desiredScale > 0.875f) {
160 num = 8;
161 } else if (desiredScale > 0.75f) {
162 num = 7;
163 } else if (desiredScale > 0.625f) {
164 num = 6;
165 } else if (desiredScale > 0.5f) {
166 num = 5;
msarette16b04a2015-04-15 07:32:19 -0700167 } else if (desiredScale > 0.375f) {
msarett1c8a5872015-07-07 08:50:01 -0700168 num = 4;
169 } else if (desiredScale > 0.25f) {
170 num = 3;
171 } else if (desiredScale > 0.125f) {
172 num = 2;
msarette16b04a2015-04-15 07:32:19 -0700173 } else {
msarett1c8a5872015-07-07 08:50:01 -0700174 num = 1;
msarette16b04a2015-04-15 07:32:19 -0700175 }
176
177 // Set up a fake decompress struct in order to use libjpeg to calculate output dimensions
178 jpeg_decompress_struct dinfo;
mtkleinf7aaadb2015-04-16 06:09:27 -0700179 sk_bzero(&dinfo, sizeof(dinfo));
msarette16b04a2015-04-15 07:32:19 -0700180 dinfo.image_width = this->getInfo().width();
181 dinfo.image_height = this->getInfo().height();
182 dinfo.global_state = DSTATE_READY;
183 dinfo.num_components = 0;
msarett1c8a5872015-07-07 08:50:01 -0700184 dinfo.scale_num = num;
185 dinfo.scale_denom = denom;
msarettfcaaade2015-08-11 13:32:54 -0700186 chromium_jpeg_calc_output_dimensions(&dinfo);
msarette16b04a2015-04-15 07:32:19 -0700187
188 // Return the calculated output dimensions for the given scale
189 return SkISize::Make(dinfo.output_width, dinfo.output_height);
190}
191
scroggob427db12015-08-12 07:24:13 -0700192bool SkJpegCodec::onRewind() {
193 JpegDecoderMgr* decoderMgr = NULL;
194 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) {
195 return fDecoderMgr->returnFalse("could not rewind");
msarett97fdea62015-04-29 08:17:15 -0700196 }
scroggob427db12015-08-12 07:24:13 -0700197 SkASSERT(NULL != decoderMgr);
198 fDecoderMgr.reset(decoderMgr);
199 return true;
msarett97fdea62015-04-29 08:17:15 -0700200}
201
202/*
msarett1c8a5872015-07-07 08:50:01 -0700203 * Checks if the conversion between the input image and the requested output
204 * image has been implemented
205 * Sets the output color space
206 */
207bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dst) {
208 const SkImageInfo& src = this->getInfo();
209
210 // Ensure that the profile type is unchanged
211 if (dst.profileType() != src.profileType()) {
212 return false;
213 }
214
215 // Ensure that the alpha type is opaque
216 if (kOpaque_SkAlphaType != dst.alphaType()) {
217 return false;
218 }
219
220 // Check if we will decode to CMYK because a conversion to RGBA is not supported
221 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->jpeg_color_space;
222 bool isCMYK = JCS_CMYK == colorSpace || JCS_YCCK == colorSpace;
223
224 // Check for valid color types and set the output color space
225 switch (dst.colorType()) {
226 case kN32_SkColorType:
227 if (isCMYK) {
228 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
229 } else {
230 // Check the byte ordering of the RGBA color space for the
231 // current platform
232#if defined(SK_PMCOLOR_IS_RGBA)
233 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
234#else
235 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA;
236#endif
237 }
238 return true;
239 case kRGB_565_SkColorType:
240 if (isCMYK) {
241 return false;
242 } else {
243 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565;
244 }
245 return true;
246 case kGray_8_SkColorType:
247 if (isCMYK) {
248 return false;
249 } else {
250 // We will enable decodes to gray even if the image is color because this is
251 // much faster than decoding to color and then converting
252 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE;
253 }
254 return true;
255 default:
256 return false;
257 }
258}
259
260/*
egdanielb0a32cc2015-08-14 06:37:37 -0700261 * Checks if we can scale to the requested dimensions and scales the dimensions
262 * if possible
msarett97fdea62015-04-29 08:17:15 -0700263 */
egdanielb0a32cc2015-08-14 06:37:37 -0700264bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) {
msarett1c8a5872015-07-07 08:50:01 -0700265 // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1
266 fDecoderMgr->dinfo()->scale_denom = 8;
267 fDecoderMgr->dinfo()->scale_num = 8;
msarettfcaaade2015-08-11 13:32:54 -0700268 chromium_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
msarett97fdea62015-04-29 08:17:15 -0700269 while (fDecoderMgr->dinfo()->output_width != dstWidth ||
270 fDecoderMgr->dinfo()->output_height != dstHeight) {
271
272 // Return a failure if we have tried all of the possible scales
msarett1c8a5872015-07-07 08:50:01 -0700273 if (1 == fDecoderMgr->dinfo()->scale_num ||
msarett97fdea62015-04-29 08:17:15 -0700274 dstWidth > fDecoderMgr->dinfo()->output_width ||
275 dstHeight > fDecoderMgr->dinfo()->output_height) {
egdanielb0a32cc2015-08-14 06:37:37 -0700276 return fDecoderMgr->returnFalse("could not scale to requested dimensions");
msarett97fdea62015-04-29 08:17:15 -0700277 }
278
279 // Try the next scale
msarett1c8a5872015-07-07 08:50:01 -0700280 fDecoderMgr->dinfo()->scale_num -= 1;
msarettfcaaade2015-08-11 13:32:54 -0700281 chromium_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
msarett97fdea62015-04-29 08:17:15 -0700282 }
283 return true;
284}
285
286/*
msarette16b04a2015-04-15 07:32:19 -0700287 * Performs the jpeg decode
288 */
289SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
290 void* dst, size_t dstRowBytes,
291 const Options& options, SkPMColor*, int*) {
292 // Rewind the stream if needed
scroggob427db12015-08-12 07:24:13 -0700293 if (!this->rewindIfNeeded()) {
msarettc0e80c12015-07-01 06:50:35 -0700294 return fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind);
msarette16b04a2015-04-15 07:32:19 -0700295 }
296
scroggob636b452015-07-22 07:16:20 -0700297 if (options.fSubset) {
298 // Subsets are not supported.
299 return kUnimplemented;
300 }
301
msarette16b04a2015-04-15 07:32:19 -0700302 // Get a pointer to the decompress info since we will use it quite frequently
303 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
304
305 // Set the jump location for libjpeg errors
306 if (setjmp(fDecoderMgr->getJmpBuf())) {
307 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
308 }
309
msarett1c8a5872015-07-07 08:50:01 -0700310 // Check if we can decode to the requested destination and set the output color space
311 if (!this->setOutputColorSpace(dstInfo)) {
msarette16b04a2015-04-15 07:32:19 -0700312 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConversion);
313 }
msarette16b04a2015-04-15 07:32:19 -0700314
msarett97fdea62015-04-29 08:17:15 -0700315 // Perform the necessary scaling
egdanielb0a32cc2015-08-14 06:37:37 -0700316 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
msarett1c8a5872015-07-07 08:50:01 -0700317 return fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidScale);
msarette16b04a2015-04-15 07:32:19 -0700318 }
319
320 // Now, given valid output dimensions, we can start the decompress
msarettfcaaade2015-08-11 13:32:54 -0700321 if (!chromium_jpeg_start_decompress(dinfo)) {
msarette16b04a2015-04-15 07:32:19 -0700322 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
323 }
324
msarett1c8a5872015-07-07 08:50:01 -0700325 // The recommended output buffer height should always be 1 in high quality modes.
326 // If it's not, we want to know because it means our strategy is not optimal.
327 SkASSERT(1 == dinfo->rec_outbuf_height);
msarette16b04a2015-04-15 07:32:19 -0700328
msarett1c8a5872015-07-07 08:50:01 -0700329 // Perform the decode a single row at a time
msarett97fdea62015-04-29 08:17:15 -0700330 uint32_t dstHeight = dstInfo.height();
msarett1c8a5872015-07-07 08:50:01 -0700331 JSAMPLE* dstRow = (JSAMPLE*) dst;
332 for (uint32_t y = 0; y < dstHeight; y++) {
msarette16b04a2015-04-15 07:32:19 -0700333 // Read rows of the image
msarettfcaaade2015-08-11 13:32:54 -0700334 uint32_t rowsDecoded = chromium_jpeg_read_scanlines(dinfo, &dstRow, 1);
msarette16b04a2015-04-15 07:32:19 -0700335
336 // If we cannot read enough rows, assume the input is incomplete
msarett1c8a5872015-07-07 08:50:01 -0700337 if (rowsDecoded != 1) {
msarette16b04a2015-04-15 07:32:19 -0700338 // Fill the remainder of the image with black. This error handling
339 // behavior is unspecified but SkCodec consistently uses black as
340 // the fill color for opaque images. If the destination is kGray,
341 // the low 8 bits of SK_ColorBLACK will be used. Conveniently,
342 // these are zeros, which is the representation for black in kGray.
msarettfdb788c2015-07-29 10:37:29 -0700343 // If the destination is kRGB_565, the low 16 bits of SK_ColorBLACK
344 // will be used. Conveniently, these are zeros, which is the
345 // representation for black in kRGB_565.
346 if (kNo_ZeroInitialized == options.fZeroInitialized ||
347 kN32_SkColorType == dstInfo.colorType()) {
348 SkSwizzler::Fill(dstRow, dstInfo, dstRowBytes, dstHeight - y,
349 SK_ColorBLACK, NULL);
350 }
msarette16b04a2015-04-15 07:32:19 -0700351
352 // Prevent libjpeg from failing on incomplete decode
353 dinfo->output_scanline = dstHeight;
354
355 // Finish the decode and indicate that the input was incomplete.
msarettfcaaade2015-08-11 13:32:54 -0700356 chromium_jpeg_finish_decompress(dinfo);
msarette16b04a2015-04-15 07:32:19 -0700357 return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
358 }
msarett1c8a5872015-07-07 08:50:01 -0700359
360 // Convert to RGBA if necessary
361 if (JCS_CMYK == dinfo->out_color_space) {
362 convert_CMYK_to_RGBA(dstRow, dstInfo.width());
363 }
364
365 // Move to the next row
366 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes);
msarette16b04a2015-04-15 07:32:19 -0700367 }
msarettfcaaade2015-08-11 13:32:54 -0700368 chromium_jpeg_finish_decompress(dinfo);
msarette16b04a2015-04-15 07:32:19 -0700369
370 return kSuccess;
371}
msarett97fdea62015-04-29 08:17:15 -0700372
373/*
374 * Enable scanline decoding for jpegs
375 */
376class SkJpegScanlineDecoder : public SkScanlineDecoder {
377public:
scroggo1c005e42015-08-04 09:24:45 -0700378 SkJpegScanlineDecoder(const SkImageInfo& srcInfo, SkJpegCodec* codec)
379 : INHERITED(srcInfo)
msarett97fdea62015-04-29 08:17:15 -0700380 , fCodec(codec)
scroggo1c005e42015-08-04 09:24:45 -0700381 , fOpts()
msarett1c8a5872015-07-07 08:50:01 -0700382 {}
msarett97fdea62015-04-29 08:17:15 -0700383
scroggo1c005e42015-08-04 09:24:45 -0700384 SkCodec::Result onStart(const SkImageInfo& dstInfo, const SkCodec::Options& options,
385 SkPMColor ctable[], int* ctableCount) override {
386
387 // Rewind the stream if needed
scroggob427db12015-08-12 07:24:13 -0700388 if (!fCodec->rewindIfNeeded()) {
scroggo1c005e42015-08-04 09:24:45 -0700389 return SkCodec::kCouldNotRewind;
390 }
391
392 // Set the jump location for libjpeg errors
393 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
394 SkCodecPrintf("setjmp: Error from libjpeg\n");
395 return SkCodec::kInvalidInput;
396 }
397
398 // Check if we can decode to the requested destination and set the output color space
399 if (!fCodec->setOutputColorSpace(dstInfo)) {
400 return SkCodec::kInvalidConversion;
401 }
402
403 // Perform the necessary scaling
egdanielb0a32cc2015-08-14 06:37:37 -0700404 if (!fCodec->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
405 return SkCodec::kInvalidScale;
scroggo1c005e42015-08-04 09:24:45 -0700406 }
407
408 // Now, given valid output dimensions, we can start the decompress
msarettfcaaade2015-08-11 13:32:54 -0700409 if (!chromium_jpeg_start_decompress(fCodec->fDecoderMgr->dinfo())) {
scroggo1c005e42015-08-04 09:24:45 -0700410 SkCodecPrintf("start decompress failed\n");
411 return SkCodec::kInvalidInput;
412 }
413
414 fOpts = options;
415
416 return SkCodec::kSuccess;
417 }
418
scroggo9b2cdbf42015-07-10 12:07:02 -0700419 virtual ~SkJpegScanlineDecoder() {
420 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
421 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n");
422 return;
423 }
424
425 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a
426 // partial decode.
427 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height();
msarettfcaaade2015-08-11 13:32:54 -0700428 chromium_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo());
scroggo9b2cdbf42015-07-10 12:07:02 -0700429 }
430
scroggoeb602a52015-07-09 08:16:03 -0700431 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) override {
msarett97fdea62015-04-29 08:17:15 -0700432 // Set the jump location for libjpeg errors
433 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
scroggoeb602a52015-07-09 08:16:03 -0700434 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvalidInput);
msarett97fdea62015-04-29 08:17:15 -0700435 }
emmaleerb1579172015-08-14 05:46:08 -0700436
egdanielb0a32cc2015-08-14 06:37:37 -0700437 // Read rows one at a time
438 JSAMPLE* dstRow = (JSAMPLE*) dst;
msarett97fdea62015-04-29 08:17:15 -0700439 for (int y = 0; y < count; y++) {
440 // Read row of the image
msarett1c8a5872015-07-07 08:50:01 -0700441 uint32_t rowsDecoded =
msarettfcaaade2015-08-11 13:32:54 -0700442 chromium_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dstRow, 1);
msarett97fdea62015-04-29 08:17:15 -0700443 if (rowsDecoded != 1) {
msarettfdb788c2015-07-29 10:37:29 -0700444 if (SkCodec::kNo_ZeroInitialized == fOpts.fZeroInitialized ||
445 kN32_SkColorType == this->dstInfo().colorType()) {
446 SkSwizzler::Fill(dstRow, this->dstInfo(), rowBytes,
447 count - y, SK_ColorBLACK, NULL);
448 }
msarett1c8a5872015-07-07 08:50:01 -0700449 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height();
scroggoeb602a52015-07-09 08:16:03 -0700450 return SkCodec::kIncompleteInput;
msarett97fdea62015-04-29 08:17:15 -0700451 }
452
msarett1c8a5872015-07-07 08:50:01 -0700453 // Convert to RGBA if necessary
msarett97fdea62015-04-29 08:17:15 -0700454 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) {
egdanielb0a32cc2015-08-14 06:37:37 -0700455 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width());
msarett97fdea62015-04-29 08:17:15 -0700456 }
457
egdanielb0a32cc2015-08-14 06:37:37 -0700458 // Move to the next row
459 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes);
msarett97fdea62015-04-29 08:17:15 -0700460 }
egdanielb0a32cc2015-08-14 06:37:37 -0700461
scroggoeb602a52015-07-09 08:16:03 -0700462 return SkCodec::kSuccess;
msarett97fdea62015-04-29 08:17:15 -0700463 }
464
msarett1c8a5872015-07-07 08:50:01 -0700465#ifndef TURBO_HAS_SKIP
msarettfcaaade2015-08-11 13:32:54 -0700466// TODO (msarett): Make this a member function and avoid reallocating the
467// memory buffer on each call to skip.
468#define chromium_jpeg_skip_scanlines(dinfo, count) \
egdanielb0a32cc2015-08-14 06:37:37 -0700469 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \
msarett1c8a5872015-07-07 08:50:01 -0700470 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \
471 for (int y = 0; y < count; y++) { \
msarettfcaaade2015-08-11 13:32:54 -0700472 chromium_jpeg_read_scanlines(dinfo, &storagePtr, 1); \
msarett1c8a5872015-07-07 08:50:01 -0700473 }
474#endif
475
scroggoeb602a52015-07-09 08:16:03 -0700476 SkCodec::Result onSkipScanlines(int count) override {
msarett97fdea62015-04-29 08:17:15 -0700477 // Set the jump location for libjpeg errors
478 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
scroggoeb602a52015-07-09 08:16:03 -0700479 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvalidInput);
msarett97fdea62015-04-29 08:17:15 -0700480 }
481
msarettfcaaade2015-08-11 13:32:54 -0700482 chromium_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count);
msarett97fdea62015-04-29 08:17:15 -0700483
scroggoeb602a52015-07-09 08:16:03 -0700484 return SkCodec::kSuccess;
msarett97fdea62015-04-29 08:17:15 -0700485 }
486
egdanielb0a32cc2015-08-14 06:37:37 -0700487#ifndef TURBO_HAS_SKIP
488#undef chromium_jpeg_skip_scanlines
489#endif
msarettfcaaade2015-08-11 13:32:54 -0700490
msarett97fdea62015-04-29 08:17:15 -0700491private:
scroggo9b2cdbf42015-07-10 12:07:02 -0700492 SkAutoTDelete<SkJpegCodec> fCodec;
scroggo1c005e42015-08-04 09:24:45 -0700493 SkCodec::Options fOpts;
msarett97fdea62015-04-29 08:17:15 -0700494
495 typedef SkScanlineDecoder INHERITED;
496};
497
scroggo1c005e42015-08-04 09:24:45 -0700498SkScanlineDecoder* SkJpegCodec::NewSDFromStream(SkStream* stream) {
scroggo9b2cdbf42015-07-10 12:07:02 -0700499 SkAutoTDelete<SkJpegCodec> codec(static_cast<SkJpegCodec*>(SkJpegCodec::NewFromStream(stream)));
500 if (!codec) {
501 return NULL;
502 }
503
scroggo1c005e42015-08-04 09:24:45 -0700504 const SkImageInfo& srcInfo = codec->getInfo();
msarett97fdea62015-04-29 08:17:15 -0700505 // Return the new scanline decoder
scroggo1c005e42015-08-04 09:24:45 -0700506 return SkNEW_ARGS(SkJpegScanlineDecoder, (srcInfo, codec.detach()));
msarett97fdea62015-04-29 08:17:15 -0700507}