blob: 5e24b17848339fedfb90b3a0d3c1198bc391849b [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"
14#include "SkStream.h"
15#include "SkTemplates.h"
16#include "SkTypes.h"
17
msarette9e3ee32015-07-01 12:36:18 -070018// stdio is needed for libjpeg-turbo
msarette16b04a2015-04-15 07:32:19 -070019#include <stdio.h>
20
21extern "C" {
msarette9e3ee32015-07-01 12:36:18 -070022 #include "jpeglibmangler.h"
msarette16b04a2015-04-15 07:32:19 -070023 #include "jerror.h"
msarette16b04a2015-04-15 07:32:19 -070024 #include "jpegint.h"
25 #include "jpeglib.h"
26}
27
msarette16b04a2015-04-15 07:32:19 -070028/*
msarette9e3ee32015-07-01 12:36:18 -070029 * Convert a row of CMYK samples to RGBA in place.
msarette16b04a2015-04-15 07:32:19 -070030 * Note that this method moves the row pointer.
31 * @param width the number of pixels in the row that is being converted
32 * CMYK is stored as four bytes per pixel
33 */
msarette9e3ee32015-07-01 12:36:18 -070034static void convert_CMYK_to_RGBA(uint8_t* row, uint32_t width) {
msarette16b04a2015-04-15 07:32:19 -070035 // We will implement a crude conversion from CMYK -> RGB using formulas
36 // from easyrgb.com.
37 //
38 // CMYK -> CMY
39 // C = C * (1 - K) + K
40 // M = M * (1 - K) + K
41 // Y = Y * (1 - K) + K
42 //
43 // libjpeg actually gives us inverted CMYK, so we must subtract the
44 // original terms from 1.
45 // CMYK -> CMY
46 // C = (1 - C) * (1 - (1 - K)) + (1 - K)
47 // M = (1 - M) * (1 - (1 - K)) + (1 - K)
48 // Y = (1 - Y) * (1 - (1 - K)) + (1 - K)
49 //
50 // Simplifying the above expression.
51 // CMYK -> CMY
52 // C = 1 - CK
53 // M = 1 - MK
54 // Y = 1 - YK
55 //
56 // CMY -> RGB
57 // R = (1 - C) * 255
58 // G = (1 - M) * 255
59 // B = (1 - Y) * 255
60 //
61 // Therefore the full conversion is below. This can be verified at
62 // www.rapidtables.com (assuming inverted CMYK).
63 // CMYK -> RGB
64 // R = C * K * 255
65 // G = M * K * 255
66 // B = Y * K * 255
67 //
68 // As a final note, we have treated the CMYK values as if they were on
69 // a scale from 0-1, when in fact they are 8-bit ints scaling from 0-255.
70 // We must divide each CMYK component by 255 to obtain the true conversion
71 // we should perform.
72 // CMYK -> RGB
73 // R = C * K / 255
74 // G = M * K / 255
75 // B = Y * K / 255
76 for (uint32_t x = 0; x < width; x++, row += 4) {
msarette9e3ee32015-07-01 12:36:18 -070077#if defined(SK_PMCOLOR_IS_RGBA)
msarette16b04a2015-04-15 07:32:19 -070078 row[0] = SkMulDiv255Round(row[0], row[3]);
79 row[1] = SkMulDiv255Round(row[1], row[3]);
80 row[2] = SkMulDiv255Round(row[2], row[3]);
msarette9e3ee32015-07-01 12:36:18 -070081#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
msarette16b04a2015-04-15 07:32:19 -070087 row[3] = 0xFF;
88 }
89}
90
91bool SkJpegCodec::IsJpeg(SkStream* stream) {
92 static const uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF };
93 char buffer[sizeof(jpegSig)];
94 return stream->read(buffer, sizeof(jpegSig)) == sizeof(jpegSig) &&
95 !memcmp(buffer, jpegSig, sizeof(jpegSig));
96}
97
98bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut,
99 JpegDecoderMgr** decoderMgrOut) {
100
101 // Create a JpegDecoderMgr to own all of the decompress information
102 SkAutoTDelete<JpegDecoderMgr> decoderMgr(SkNEW_ARGS(JpegDecoderMgr, (stream)));
103
104 // libjpeg errors will be caught and reported here
105 if (setjmp(decoderMgr->getJmpBuf())) {
106 return decoderMgr->returnFalse("setjmp");
107 }
108
109 // Initialize the decompress info and the source manager
110 decoderMgr->init();
111
112 // Read the jpeg header
msarette9e3ee32015-07-01 12:36:18 -0700113 if (JPEG_HEADER_OK != turbo_jpeg_read_header(decoderMgr->dinfo(), true)) {
msarette16b04a2015-04-15 07:32:19 -0700114 return decoderMgr->returnFalse("read_header");
115 }
116
117 if (NULL != codecOut) {
118 // Recommend the color type to decode to
119 const SkColorType colorType = decoderMgr->getColorType();
120
121 // Create image info object and the codec
122 const SkImageInfo& imageInfo = SkImageInfo::Make(decoderMgr->dinfo()->image_width,
123 decoderMgr->dinfo()->image_height, colorType, kOpaque_SkAlphaType);
124 *codecOut = SkNEW_ARGS(SkJpegCodec, (imageInfo, stream, decoderMgr.detach()));
125 } else {
126 SkASSERT(NULL != decoderMgrOut);
127 *decoderMgrOut = decoderMgr.detach();
128 }
129 return true;
130}
131
132SkCodec* SkJpegCodec::NewFromStream(SkStream* stream) {
133 SkAutoTDelete<SkStream> streamDeleter(stream);
134 SkCodec* codec = NULL;
135 if (ReadHeader(stream, &codec, NULL)) {
136 // Codec has taken ownership of the stream, we do not need to delete it
137 SkASSERT(codec);
138 streamDeleter.detach();
139 return codec;
140 }
141 return NULL;
142}
143
144SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream,
145 JpegDecoderMgr* decoderMgr)
146 : INHERITED(srcInfo, stream)
147 , fDecoderMgr(decoderMgr)
148{}
149
150/*
151 * Return a valid set of output dimensions for this decoder, given an input scale
152 */
153SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const {
msarette9e3ee32015-07-01 12:36:18 -0700154 // 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
155 // support these as well
156 long num;
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;
msarette16b04a2015-04-15 07:32:19 -0700166 } else if (desiredScale > 0.375f) {
msarette9e3ee32015-07-01 12:36:18 -0700167 num = 4;
168 } else if (desiredScale > 0.25f) {
169 num = 3;
170 } else if (desiredScale > 0.125f) {
171 num = 2;
msarette16b04a2015-04-15 07:32:19 -0700172 } else {
msarette9e3ee32015-07-01 12:36:18 -0700173 num = 1;
msarette16b04a2015-04-15 07:32:19 -0700174 }
175
176 // Set up a fake decompress struct in order to use libjpeg to calculate output dimensions
177 jpeg_decompress_struct dinfo;
mtkleinf7aaadb2015-04-16 06:09:27 -0700178 sk_bzero(&dinfo, sizeof(dinfo));
msarette16b04a2015-04-15 07:32:19 -0700179 dinfo.image_width = this->getInfo().width();
180 dinfo.image_height = this->getInfo().height();
181 dinfo.global_state = DSTATE_READY;
182 dinfo.num_components = 0;
msarette9e3ee32015-07-01 12:36:18 -0700183 dinfo.scale_num = num;
184 dinfo.scale_denom = denom;
185 turbo_jpeg_calc_output_dimensions(&dinfo);
msarette16b04a2015-04-15 07:32:19 -0700186
187 // Return the calculated output dimensions for the given scale
188 return SkISize::Make(dinfo.output_width, dinfo.output_height);
189}
190
191/*
msarett97fdea62015-04-29 08:17:15 -0700192 * Handles rewinding the input stream if it is necessary
193 */
194bool SkJpegCodec::handleRewind() {
195 switch(this->rewindIfNeeded()) {
196 case kCouldNotRewind_RewindState:
197 return fDecoderMgr->returnFalse("could not rewind");
198 case kRewound_RewindState: {
199 JpegDecoderMgr* decoderMgr = NULL;
200 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) {
201 return fDecoderMgr->returnFalse("could not rewind");
202 }
203 SkASSERT(NULL != decoderMgr);
204 fDecoderMgr.reset(decoderMgr);
205 return true;
206 }
207 case kNoRewindNecessary_RewindState:
208 return true;
209 default:
210 SkASSERT(false);
211 return false;
212 }
213}
214
215/*
msarette9e3ee32015-07-01 12:36:18 -0700216 * Checks if the conversion between the input image and the requested output
217 * image has been implemented
218 * Sets the output color space
219 */
220bool 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 supported
234 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->jpeg_color_space;
235 bool isCMYK = JCS_CMYK == colorSpace || JCS_YCCK == colorSpace;
236
237 // Check for valid color types and set the output color space
238 switch (dst.colorType()) {
239 case kN32_SkColorType:
240 if (isCMYK) {
241 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
242 } else {
243 // Check the byte ordering of the RGBA color space for the
244 // current platform
245#if defined(SK_PMCOLOR_IS_RGBA)
246 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
247#else
248 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA;
249#endif
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 because 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/*
msarett97fdea62015-04-29 08:17:15 -0700274 * Checks if we can scale to the requested dimensions and scales the dimensions
275 * if possible
276 */
277bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) {
msarette9e3ee32015-07-01 12:36:18 -0700278 // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1
279 fDecoderMgr->dinfo()->scale_denom = 8;
280 fDecoderMgr->dinfo()->scale_num = 8;
281 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
msarett97fdea62015-04-29 08:17:15 -0700282 while (fDecoderMgr->dinfo()->output_width != dstWidth ||
283 fDecoderMgr->dinfo()->output_height != dstHeight) {
284
285 // Return a failure if we have tried all of the possible scales
msarette9e3ee32015-07-01 12:36:18 -0700286 if (1 == fDecoderMgr->dinfo()->scale_num ||
msarett97fdea62015-04-29 08:17:15 -0700287 dstWidth > fDecoderMgr->dinfo()->output_width ||
288 dstHeight > fDecoderMgr->dinfo()->output_height) {
289 return fDecoderMgr->returnFalse("could not scale to requested dimensions");
290 }
291
292 // Try the next scale
msarette9e3ee32015-07-01 12:36:18 -0700293 fDecoderMgr->dinfo()->scale_num -= 1;
294 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
msarett97fdea62015-04-29 08:17:15 -0700295 }
296 return true;
297}
298
299/*
msarette16b04a2015-04-15 07:32:19 -0700300 * Performs the jpeg decode
301 */
302SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
303 void* dst, size_t dstRowBytes,
304 const Options& options, SkPMColor*, int*) {
msarett97fdea62015-04-29 08:17:15 -0700305
msarettc0e80c12015-07-01 06:50:35 -0700306 // Do not allow a regular decode if the caller has asked for a scanline decoder
307 if (NULL != this->scanlineDecoder()) {
308 return fDecoderMgr->returnFailure("cannot getPixels() if a scanline decoder has been"
309 "created", kInvalidParameters);
310 }
311
msarette16b04a2015-04-15 07:32:19 -0700312 // Rewind the stream if needed
msarett97fdea62015-04-29 08:17:15 -0700313 if (!this->handleRewind()) {
msarettc0e80c12015-07-01 06:50:35 -0700314 return fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind);
msarette16b04a2015-04-15 07:32:19 -0700315 }
316
317 // Get a pointer to the decompress info since we will use it quite frequently
318 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
319
320 // Set the jump location for libjpeg errors
321 if (setjmp(fDecoderMgr->getJmpBuf())) {
322 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
323 }
324
msarette9e3ee32015-07-01 12:36:18 -0700325 // Check if we can decode to the requested destination and set the output color space
326 if (!this->setOutputColorSpace(dstInfo)) {
msarette16b04a2015-04-15 07:32:19 -0700327 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConversion);
328 }
msarette16b04a2015-04-15 07:32:19 -0700329
msarett97fdea62015-04-29 08:17:15 -0700330 // Perform the necessary scaling
331 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
msarette9e3ee32015-07-01 12:36:18 -0700332 return fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidScale);
msarette16b04a2015-04-15 07:32:19 -0700333 }
334
335 // Now, given valid output dimensions, we can start the decompress
msarette9e3ee32015-07-01 12:36:18 -0700336 if (!turbo_jpeg_start_decompress(dinfo)) {
msarette16b04a2015-04-15 07:32:19 -0700337 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
338 }
339
msarette9e3ee32015-07-01 12:36:18 -0700340 // The recommended output buffer height should always be 1 in high quality modes.
341 // If it's not, we want to know because it means our strategy is not optimal.
342 SkASSERT(1 == dinfo->rec_outbuf_height);
msarette16b04a2015-04-15 07:32:19 -0700343
msarette9e3ee32015-07-01 12:36:18 -0700344 // Perform the decode a single row at a time
msarett97fdea62015-04-29 08:17:15 -0700345 uint32_t dstHeight = dstInfo.height();
msarette9e3ee32015-07-01 12:36:18 -0700346 JSAMPLE* dstRow = (JSAMPLE*) dst;
347 for (uint32_t y = 0; y < dstHeight; y++) {
msarette16b04a2015-04-15 07:32:19 -0700348 // Read rows of the image
msarette9e3ee32015-07-01 12:36:18 -0700349 uint32_t rowsDecoded = turbo_jpeg_read_scanlines(dinfo, &dstRow, 1);
msarette16b04a2015-04-15 07:32:19 -0700350
351 // If we cannot read enough rows, assume the input is incomplete
msarette9e3ee32015-07-01 12:36:18 -0700352 if (rowsDecoded != 1) {
msarette16b04a2015-04-15 07:32:19 -0700353 // Fill the remainder of the image with black. This error handling
354 // behavior is unspecified but SkCodec consistently uses black as
355 // the fill color for opaque images. If the destination is kGray,
356 // the low 8 bits of SK_ColorBLACK will be used. Conveniently,
357 // these are zeros, which is the representation for black in kGray.
msarette9e3ee32015-07-01 12:36:18 -0700358 SkSwizzler::Fill(dstRow, dstInfo, dstRowBytes, dstHeight - y, SK_ColorBLACK, NULL);
msarette16b04a2015-04-15 07:32:19 -0700359
360 // Prevent libjpeg from failing on incomplete decode
361 dinfo->output_scanline = dstHeight;
362
363 // Finish the decode and indicate that the input was incomplete.
msarette9e3ee32015-07-01 12:36:18 -0700364 turbo_jpeg_finish_decompress(dinfo);
msarette16b04a2015-04-15 07:32:19 -0700365 return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
366 }
msarette9e3ee32015-07-01 12:36:18 -0700367
368 // Convert to RGBA if necessary
369 if (JCS_CMYK == dinfo->out_color_space) {
370 convert_CMYK_to_RGBA(dstRow, dstInfo.width());
371 }
372
373 // Move to the next row
374 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes);
msarette16b04a2015-04-15 07:32:19 -0700375 }
msarette9e3ee32015-07-01 12:36:18 -0700376 turbo_jpeg_finish_decompress(dinfo);
msarette16b04a2015-04-15 07:32:19 -0700377
378 return kSuccess;
379}
msarett97fdea62015-04-29 08:17:15 -0700380
381/*
msarettc0e80c12015-07-01 06:50:35 -0700382 * We override the destructor to ensure that the scanline decoder is left in a
383 * finished state before destroying the decode manager.
384 */
385SkJpegCodec::~SkJpegCodec() {
386 SkAutoTDelete<SkScanlineDecoder> decoder(this->detachScanlineDecoder());
387 if (NULL != decoder) {
388 if (setjmp(fDecoderMgr->getJmpBuf())) {
389 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n");
390 return;
391 }
392
393 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a
394 // partial decode.
395 fDecoderMgr->dinfo()->output_scanline = this->getInfo().height();
msarette9e3ee32015-07-01 12:36:18 -0700396 turbo_jpeg_finish_decompress(fDecoderMgr->dinfo());
msarettc0e80c12015-07-01 06:50:35 -0700397 }
398}
399
400/*
msarett97fdea62015-04-29 08:17:15 -0700401 * Enable scanline decoding for jpegs
402 */
403class SkJpegScanlineDecoder : public SkScanlineDecoder {
404public:
405 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec)
406 : INHERITED(dstInfo)
407 , fCodec(codec)
msarette9e3ee32015-07-01 12:36:18 -0700408 {}
msarett97fdea62015-04-29 08:17:15 -0700409
410 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowBytes) override {
411 // Set the jump location for libjpeg errors
412 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
413 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator::kInvalidInput);
414 }
415
416 // Read rows one at a time
msarette9e3ee32015-07-01 12:36:18 -0700417 JSAMPLE* dstRow = (JSAMPLE*) dst;
msarett97fdea62015-04-29 08:17:15 -0700418 for (int y = 0; y < count; y++) {
419 // Read row of the image
msarette9e3ee32015-07-01 12:36:18 -0700420 uint32_t rowsDecoded =
421 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dstRow, 1);
msarett97fdea62015-04-29 08:17:15 -0700422 if (rowsDecoded != 1) {
msarette9e3ee32015-07-01 12:36:18 -0700423 SkSwizzler::Fill(
424 dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBLACK, NULL);
425 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height();
msarett97fdea62015-04-29 08:17:15 -0700426 return SkImageGenerator::kIncompleteInput;
427 }
428
msarette9e3ee32015-07-01 12:36:18 -0700429 // Convert to RGBA if necessary
msarett97fdea62015-04-29 08:17:15 -0700430 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) {
msarette9e3ee32015-07-01 12:36:18 -0700431 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width());
msarett97fdea62015-04-29 08:17:15 -0700432 }
433
msarette9e3ee32015-07-01 12:36:18 -0700434 // Move to the next row
435 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes);
msarett97fdea62015-04-29 08:17:15 -0700436 }
437
438 return SkImageGenerator::kSuccess;
439 }
440
msarette9e3ee32015-07-01 12:36:18 -0700441#ifndef TURBO_HAS_SKIP
442#define turbo_jpeg_skip_scanlines(dinfo, count) \
443 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \
444 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \
445 for (int y = 0; y < count; y++) { \
446 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \
447 }
448#endif
449
msarett97fdea62015-04-29 08:17:15 -0700450 SkImageGenerator::Result onSkipScanlines(int count) override {
451 // Set the jump location for libjpeg errors
452 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
453 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator::kInvalidInput);
454 }
455
msarette9e3ee32015-07-01 12:36:18 -0700456 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count);
msarett97fdea62015-04-29 08:17:15 -0700457
458 return SkImageGenerator::kSuccess;
459 }
460
msarett97fdea62015-04-29 08:17:15 -0700461private:
msarette9e3ee32015-07-01 12:36:18 -0700462 SkJpegCodec* fCodec; // unowned
msarett97fdea62015-04-29 08:17:15 -0700463
464 typedef SkScanlineDecoder INHERITED;
465};
466
467SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
468 const Options& options, SkPMColor ctable[], int* ctableCount) {
469
470 // Rewind the stream if needed
471 if (!this->handleRewind()) {
472 SkCodecPrintf("Could not rewind\n");
473 return NULL;
474 }
475
476 // Set the jump location for libjpeg errors
477 if (setjmp(fDecoderMgr->getJmpBuf())) {
478 SkCodecPrintf("setjmp: Error from libjpeg\n");
479 return NULL;
480 }
481
msarette9e3ee32015-07-01 12:36:18 -0700482 // Check if we can decode to the requested destination and set the output color space
483 if (!this->setOutputColorSpace(dstInfo)) {
msarett97fdea62015-04-29 08:17:15 -0700484 SkCodecPrintf("Cannot convert to output type\n");
485 return NULL;
486 }
487
488 // Perform the necessary scaling
489 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
msarette9e3ee32015-07-01 12:36:18 -0700490 SkCodecPrintf("Cannot scale to output dimensions\n");
msarett97fdea62015-04-29 08:17:15 -0700491 return NULL;
492 }
493
494 // Now, given valid output dimensions, we can start the decompress
msarette9e3ee32015-07-01 12:36:18 -0700495 if (!turbo_jpeg_start_decompress(fDecoderMgr->dinfo())) {
msarett97fdea62015-04-29 08:17:15 -0700496 SkCodecPrintf("start decompress failed\n");
497 return NULL;
498 }
499
msarett97fdea62015-04-29 08:17:15 -0700500 // Return the new scanline decoder
501 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this));
502}