blob: 5acc0b396c54d34abb922f80687104b54f85990a [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
msarett1c8a5872015-07-07 08:50:01 -0700114 if (JPEG_HEADER_OK != turbo_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;
186 turbo_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
192/*
msarett97fdea62015-04-29 08:17:15 -0700193 * Handles rewinding the input stream if it is necessary
194 */
195bool SkJpegCodec::handleRewind() {
196 switch(this->rewindIfNeeded()) {
197 case kCouldNotRewind_RewindState:
198 return fDecoderMgr->returnFalse("could not rewind");
199 case kRewound_RewindState: {
200 JpegDecoderMgr* decoderMgr = NULL;
201 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) {
202 return fDecoderMgr->returnFalse("could not rewind");
203 }
204 SkASSERT(NULL != decoderMgr);
205 fDecoderMgr.reset(decoderMgr);
206 return true;
207 }
208 case kNoRewindNecessary_RewindState:
209 return true;
210 default:
211 SkASSERT(false);
212 return false;
213 }
214}
215
216/*
msarett1c8a5872015-07-07 08:50:01 -0700217 * Checks if the conversion between the input image and the requested output
218 * image has been implemented
219 * Sets the output color space
220 */
221bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dst) {
222 const SkImageInfo& src = this->getInfo();
223
224 // Ensure that the profile type is unchanged
225 if (dst.profileType() != src.profileType()) {
226 return false;
227 }
228
229 // Ensure that the alpha type is opaque
230 if (kOpaque_SkAlphaType != dst.alphaType()) {
231 return false;
232 }
233
234 // Check if we will decode to CMYK because a conversion to RGBA is not supported
235 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->jpeg_color_space;
236 bool isCMYK = JCS_CMYK == colorSpace || JCS_YCCK == colorSpace;
237
238 // Check for valid color types and set the output color space
239 switch (dst.colorType()) {
240 case kN32_SkColorType:
241 if (isCMYK) {
242 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
243 } else {
244 // Check the byte ordering of the RGBA color space for the
245 // current platform
246#if defined(SK_PMCOLOR_IS_RGBA)
247 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
248#else
249 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA;
250#endif
251 }
252 return true;
253 case kRGB_565_SkColorType:
254 if (isCMYK) {
255 return false;
256 } else {
257 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565;
258 }
259 return true;
260 case kGray_8_SkColorType:
261 if (isCMYK) {
262 return false;
263 } else {
264 // We will enable decodes to gray even if the image is color because this is
265 // much faster than decoding to color and then converting
266 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE;
267 }
268 return true;
269 default:
270 return false;
271 }
272}
273
274/*
msarett97fdea62015-04-29 08:17:15 -0700275 * Checks if we can scale to the requested dimensions and scales the dimensions
276 * if possible
277 */
278bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) {
msarett1c8a5872015-07-07 08:50:01 -0700279 // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1
280 fDecoderMgr->dinfo()->scale_denom = 8;
281 fDecoderMgr->dinfo()->scale_num = 8;
282 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
msarett97fdea62015-04-29 08:17:15 -0700283 while (fDecoderMgr->dinfo()->output_width != dstWidth ||
284 fDecoderMgr->dinfo()->output_height != dstHeight) {
285
286 // Return a failure if we have tried all of the possible scales
msarett1c8a5872015-07-07 08:50:01 -0700287 if (1 == fDecoderMgr->dinfo()->scale_num ||
msarett97fdea62015-04-29 08:17:15 -0700288 dstWidth > fDecoderMgr->dinfo()->output_width ||
289 dstHeight > fDecoderMgr->dinfo()->output_height) {
290 return fDecoderMgr->returnFalse("could not scale to requested dimensions");
291 }
292
293 // Try the next scale
msarett1c8a5872015-07-07 08:50:01 -0700294 fDecoderMgr->dinfo()->scale_num -= 1;
295 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
msarett97fdea62015-04-29 08:17:15 -0700296 }
297 return true;
298}
299
300/*
msarette16b04a2015-04-15 07:32:19 -0700301 * Performs the jpeg decode
302 */
303SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
304 void* dst, size_t dstRowBytes,
305 const Options& options, SkPMColor*, int*) {
306 // Rewind the stream if needed
msarett97fdea62015-04-29 08:17:15 -0700307 if (!this->handleRewind()) {
msarettc0e80c12015-07-01 06:50:35 -0700308 return fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind);
msarette16b04a2015-04-15 07:32:19 -0700309 }
310
scroggob636b452015-07-22 07:16:20 -0700311 if (options.fSubset) {
312 // Subsets are not supported.
313 return kUnimplemented;
314 }
315
msarette16b04a2015-04-15 07:32:19 -0700316 // Get a pointer to the decompress info since we will use it quite frequently
317 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
318
319 // Set the jump location for libjpeg errors
320 if (setjmp(fDecoderMgr->getJmpBuf())) {
321 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
322 }
323
msarett1c8a5872015-07-07 08:50:01 -0700324 // Check if we can decode to the requested destination and set the output color space
325 if (!this->setOutputColorSpace(dstInfo)) {
msarette16b04a2015-04-15 07:32:19 -0700326 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConversion);
327 }
msarette16b04a2015-04-15 07:32:19 -0700328
msarett97fdea62015-04-29 08:17:15 -0700329 // Perform the necessary scaling
330 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
msarett1c8a5872015-07-07 08:50:01 -0700331 return fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidScale);
msarette16b04a2015-04-15 07:32:19 -0700332 }
333
334 // Now, given valid output dimensions, we can start the decompress
msarett1c8a5872015-07-07 08:50:01 -0700335 if (!turbo_jpeg_start_decompress(dinfo)) {
msarette16b04a2015-04-15 07:32:19 -0700336 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
337 }
338
msarett1c8a5872015-07-07 08:50:01 -0700339 // The recommended output buffer height should always be 1 in high quality modes.
340 // If it's not, we want to know because it means our strategy is not optimal.
341 SkASSERT(1 == dinfo->rec_outbuf_height);
msarette16b04a2015-04-15 07:32:19 -0700342
msarett1c8a5872015-07-07 08:50:01 -0700343 // Perform the decode a single row at a time
msarett97fdea62015-04-29 08:17:15 -0700344 uint32_t dstHeight = dstInfo.height();
msarett1c8a5872015-07-07 08:50:01 -0700345 JSAMPLE* dstRow = (JSAMPLE*) dst;
346 for (uint32_t y = 0; y < dstHeight; y++) {
msarette16b04a2015-04-15 07:32:19 -0700347 // Read rows of the image
msarett1c8a5872015-07-07 08:50:01 -0700348 uint32_t rowsDecoded = turbo_jpeg_read_scanlines(dinfo, &dstRow, 1);
msarette16b04a2015-04-15 07:32:19 -0700349
350 // If we cannot read enough rows, assume the input is incomplete
msarett1c8a5872015-07-07 08:50:01 -0700351 if (rowsDecoded != 1) {
msarette16b04a2015-04-15 07:32:19 -0700352 // Fill the remainder of the image with black. This error handling
353 // behavior is unspecified but SkCodec consistently uses black as
354 // the fill color for opaque images. If the destination is kGray,
355 // the low 8 bits of SK_ColorBLACK will be used. Conveniently,
356 // these are zeros, which is the representation for black in kGray.
msarett1c8a5872015-07-07 08:50:01 -0700357 SkSwizzler::Fill(dstRow, dstInfo, dstRowBytes, dstHeight - y, SK_ColorBLACK, NULL);
msarette16b04a2015-04-15 07:32:19 -0700358
359 // Prevent libjpeg from failing on incomplete decode
360 dinfo->output_scanline = dstHeight;
361
362 // Finish the decode and indicate that the input was incomplete.
msarett1c8a5872015-07-07 08:50:01 -0700363 turbo_jpeg_finish_decompress(dinfo);
msarette16b04a2015-04-15 07:32:19 -0700364 return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
365 }
msarett1c8a5872015-07-07 08:50:01 -0700366
367 // Convert to RGBA if necessary
368 if (JCS_CMYK == dinfo->out_color_space) {
369 convert_CMYK_to_RGBA(dstRow, dstInfo.width());
370 }
371
372 // Move to the next row
373 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes);
msarette16b04a2015-04-15 07:32:19 -0700374 }
msarett1c8a5872015-07-07 08:50:01 -0700375 turbo_jpeg_finish_decompress(dinfo);
msarette16b04a2015-04-15 07:32:19 -0700376
377 return kSuccess;
378}
msarett97fdea62015-04-29 08:17:15 -0700379
380/*
381 * Enable scanline decoding for jpegs
382 */
383class SkJpegScanlineDecoder : public SkScanlineDecoder {
384public:
385 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec)
386 : INHERITED(dstInfo)
387 , fCodec(codec)
msarett1c8a5872015-07-07 08:50:01 -0700388 {}
msarett97fdea62015-04-29 08:17:15 -0700389
scroggo9b2cdbf42015-07-10 12:07:02 -0700390 virtual ~SkJpegScanlineDecoder() {
391 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
392 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n");
393 return;
394 }
395
396 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a
397 // partial decode.
398 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height();
399 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo());
400 }
401
scroggoeb602a52015-07-09 08:16:03 -0700402 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) override {
msarett97fdea62015-04-29 08:17:15 -0700403 // Set the jump location for libjpeg errors
404 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
scroggoeb602a52015-07-09 08:16:03 -0700405 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvalidInput);
msarett97fdea62015-04-29 08:17:15 -0700406 }
407
408 // Read rows one at a time
msarett1c8a5872015-07-07 08:50:01 -0700409 JSAMPLE* dstRow = (JSAMPLE*) dst;
msarett97fdea62015-04-29 08:17:15 -0700410 for (int y = 0; y < count; y++) {
411 // Read row of the image
msarett1c8a5872015-07-07 08:50:01 -0700412 uint32_t rowsDecoded =
413 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dstRow, 1);
msarett97fdea62015-04-29 08:17:15 -0700414 if (rowsDecoded != 1) {
msarett1c8a5872015-07-07 08:50:01 -0700415 SkSwizzler::Fill(
416 dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBLACK, NULL);
417 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height();
scroggoeb602a52015-07-09 08:16:03 -0700418 return SkCodec::kIncompleteInput;
msarett97fdea62015-04-29 08:17:15 -0700419 }
420
msarett1c8a5872015-07-07 08:50:01 -0700421 // Convert to RGBA if necessary
msarett97fdea62015-04-29 08:17:15 -0700422 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) {
msarett1c8a5872015-07-07 08:50:01 -0700423 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width());
msarett97fdea62015-04-29 08:17:15 -0700424 }
425
msarett1c8a5872015-07-07 08:50:01 -0700426 // Move to the next row
427 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes);
msarett97fdea62015-04-29 08:17:15 -0700428 }
429
scroggoeb602a52015-07-09 08:16:03 -0700430 return SkCodec::kSuccess;
msarett97fdea62015-04-29 08:17:15 -0700431 }
432
msarett1c8a5872015-07-07 08:50:01 -0700433#ifndef TURBO_HAS_SKIP
434#define turbo_jpeg_skip_scanlines(dinfo, count) \
435 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \
436 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \
437 for (int y = 0; y < count; y++) { \
438 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \
439 }
440#endif
441
scroggoeb602a52015-07-09 08:16:03 -0700442 SkCodec::Result onSkipScanlines(int count) override {
msarett97fdea62015-04-29 08:17:15 -0700443 // Set the jump location for libjpeg errors
444 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
scroggoeb602a52015-07-09 08:16:03 -0700445 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvalidInput);
msarett97fdea62015-04-29 08:17:15 -0700446 }
447
msarett1c8a5872015-07-07 08:50:01 -0700448 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count);
msarett97fdea62015-04-29 08:17:15 -0700449
scroggoeb602a52015-07-09 08:16:03 -0700450 return SkCodec::kSuccess;
msarett97fdea62015-04-29 08:17:15 -0700451 }
452
msarett97fdea62015-04-29 08:17:15 -0700453private:
scroggo9b2cdbf42015-07-10 12:07:02 -0700454 SkAutoTDelete<SkJpegCodec> fCodec;
msarett97fdea62015-04-29 08:17:15 -0700455
456 typedef SkScanlineDecoder INHERITED;
457};
458
459SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
460 const Options& options, SkPMColor ctable[], int* ctableCount) {
461
462 // Rewind the stream if needed
463 if (!this->handleRewind()) {
464 SkCodecPrintf("Could not rewind\n");
465 return NULL;
466 }
467
468 // Set the jump location for libjpeg errors
469 if (setjmp(fDecoderMgr->getJmpBuf())) {
470 SkCodecPrintf("setjmp: Error from libjpeg\n");
471 return NULL;
472 }
473
scroggo9b2cdbf42015-07-10 12:07:02 -0700474 SkStream* stream = this->stream()->duplicate();
475 if (!stream) {
476 return NULL;
477 }
478 SkAutoTDelete<SkJpegCodec> codec(static_cast<SkJpegCodec*>(SkJpegCodec::NewFromStream(stream)));
479 if (!codec) {
480 return NULL;
481 }
482
msarett1c8a5872015-07-07 08:50:01 -0700483 // Check if we can decode to the requested destination and set the output color space
scroggo9b2cdbf42015-07-10 12:07:02 -0700484 if (!codec->setOutputColorSpace(dstInfo)) {
msarett97fdea62015-04-29 08:17:15 -0700485 SkCodecPrintf("Cannot convert to output type\n");
486 return NULL;
487 }
488
489 // Perform the necessary scaling
scroggo9b2cdbf42015-07-10 12:07:02 -0700490 if (!codec->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
msarett1c8a5872015-07-07 08:50:01 -0700491 SkCodecPrintf("Cannot scale to output dimensions\n");
msarett97fdea62015-04-29 08:17:15 -0700492 return NULL;
493 }
494
495 // Now, given valid output dimensions, we can start the decompress
scroggo9b2cdbf42015-07-10 12:07:02 -0700496 if (!turbo_jpeg_start_decompress(codec->fDecoderMgr->dinfo())) {
msarett97fdea62015-04-29 08:17:15 -0700497 SkCodecPrintf("start decompress failed\n");
498 return NULL;
499 }
500
msarett97fdea62015-04-29 08:17:15 -0700501 // Return the new scanline decoder
scroggo9b2cdbf42015-07-10 12:07:02 -0700502 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, codec.detach()));
msarett97fdea62015-04-29 08:17:15 -0700503}