blob: 9c9bd77c13515ff17f32e2cf33bede895e7463f7 [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
msarettaa2a7de2015-07-01 13:11:08 -070018// stdio is needed for jpeglib
msarette16b04a2015-04-15 07:32:19 -070019#include <stdio.h>
20
21extern "C" {
22 #include "jerror.h"
msarettaa2a7de2015-07-01 13:11:08 -070023 #include "jmorecfg.h"
msarette16b04a2015-04-15 07:32:19 -070024 #include "jpegint.h"
25 #include "jpeglib.h"
26}
27
msarettaa2a7de2015-07-01 13:11:08 -070028// 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
msarette16b04a2015-04-15 07:32:19 -070032/*
msarettaa2a7de2015-07-01 13:11:08 -070033 * Get the source configuarion for the swizzler
34 */
35SkSwizzler::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_space) {
53 return SkSwizzler::kGray;
54 }
55 return SkSwizzler::kUnknown;
56}
57
58/*
59 * Convert a row of CMYK samples to RGBX in place.
msarette16b04a2015-04-15 07:32:19 -070060 * Note that this method moves the row pointer.
61 * @param width the number of pixels in the row that is being converted
62 * CMYK is stored as four bytes per pixel
63 */
msarettaa2a7de2015-07-01 13:11:08 -070064static void convert_CMYK_to_RGB(uint8_t* row, uint32_t width) {
msarette16b04a2015-04-15 07:32:19 -070065 // We will implement a crude conversion from CMYK -> RGB using formulas
66 // from easyrgb.com.
67 //
68 // CMYK -> CMY
69 // C = C * (1 - K) + K
70 // M = M * (1 - K) + K
71 // Y = Y * (1 - K) + K
72 //
73 // libjpeg actually gives us inverted CMYK, so we must subtract the
74 // original terms from 1.
75 // CMYK -> CMY
76 // C = (1 - C) * (1 - (1 - K)) + (1 - K)
77 // M = (1 - M) * (1 - (1 - K)) + (1 - K)
78 // Y = (1 - Y) * (1 - (1 - K)) + (1 - K)
79 //
80 // Simplifying the above expression.
81 // CMYK -> CMY
82 // C = 1 - CK
83 // M = 1 - MK
84 // Y = 1 - YK
85 //
86 // CMY -> RGB
87 // R = (1 - C) * 255
88 // G = (1 - M) * 255
89 // B = (1 - Y) * 255
90 //
91 // Therefore the full conversion is below. This can be verified at
92 // www.rapidtables.com (assuming inverted CMYK).
93 // CMYK -> RGB
94 // R = C * K * 255
95 // G = M * K * 255
96 // B = Y * K * 255
97 //
98 // 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.
100 // We must divide each CMYK component by 255 to obtain the true conversion
101 // we should perform.
102 // CMYK -> RGB
103 // R = C * K / 255
104 // G = M * K / 255
105 // B = Y * K / 255
106 for (uint32_t x = 0; x < width; x++, row += 4) {
107 row[0] = SkMulDiv255Round(row[0], row[3]);
108 row[1] = SkMulDiv255Round(row[1], row[3]);
109 row[2] = SkMulDiv255Round(row[2], row[3]);
110 row[3] = 0xFF;
111 }
112}
113
114bool SkJpegCodec::IsJpeg(SkStream* stream) {
115 static const uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF };
116 char buffer[sizeof(jpegSig)];
117 return stream->read(buffer, sizeof(jpegSig)) == sizeof(jpegSig) &&
118 !memcmp(buffer, jpegSig, sizeof(jpegSig));
119}
120
121bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut,
122 JpegDecoderMgr** decoderMgrOut) {
123
124 // Create a JpegDecoderMgr to own all of the decompress information
125 SkAutoTDelete<JpegDecoderMgr> decoderMgr(SkNEW_ARGS(JpegDecoderMgr, (stream)));
126
127 // libjpeg errors will be caught and reported here
128 if (setjmp(decoderMgr->getJmpBuf())) {
129 return decoderMgr->returnFalse("setjmp");
130 }
131
132 // Initialize the decompress info and the source manager
133 decoderMgr->init();
134
135 // Read the jpeg header
msarettaa2a7de2015-07-01 13:11:08 -0700136 if (JPEG_HEADER_OK != jpeg_read_header(decoderMgr->dinfo(), true)) {
msarette16b04a2015-04-15 07:32:19 -0700137 return decoderMgr->returnFalse("read_header");
138 }
139
140 if (NULL != codecOut) {
141 // Recommend the color type to decode to
142 const SkColorType colorType = decoderMgr->getColorType();
143
144 // Create image info object and the codec
145 const SkImageInfo& imageInfo = SkImageInfo::Make(decoderMgr->dinfo()->image_width,
146 decoderMgr->dinfo()->image_height, colorType, kOpaque_SkAlphaType);
147 *codecOut = SkNEW_ARGS(SkJpegCodec, (imageInfo, stream, decoderMgr.detach()));
148 } else {
149 SkASSERT(NULL != decoderMgrOut);
150 *decoderMgrOut = decoderMgr.detach();
151 }
152 return true;
153}
154
155SkCodec* SkJpegCodec::NewFromStream(SkStream* stream) {
156 SkAutoTDelete<SkStream> streamDeleter(stream);
157 SkCodec* codec = NULL;
158 if (ReadHeader(stream, &codec, NULL)) {
159 // Codec has taken ownership of the stream, we do not need to delete it
160 SkASSERT(codec);
161 streamDeleter.detach();
162 return codec;
163 }
164 return NULL;
165}
166
167SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream,
168 JpegDecoderMgr* decoderMgr)
169 : INHERITED(srcInfo, stream)
170 , fDecoderMgr(decoderMgr)
msarettaa2a7de2015-07-01 13:11:08 -0700171 , fSwizzler(NULL)
172 , fSrcRowBytes(0)
msarette16b04a2015-04-15 07:32:19 -0700173{}
174
175/*
176 * Return a valid set of output dimensions for this decoder, given an input scale
177 */
178SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const {
msarettaa2a7de2015-07-01 13:11:08 -0700179 // libjpeg supports scaling by 1/1, 1/2, 1/4, and 1/8, so we will support these as well
180 long scale;
181 if (desiredScale > 0.75f) {
182 scale = 1;
msarette16b04a2015-04-15 07:32:19 -0700183 } else if (desiredScale > 0.375f) {
msarettaa2a7de2015-07-01 13:11:08 -0700184 scale = 2;
185 } else if (desiredScale > 0.1875f) {
186 scale = 4;
msarette16b04a2015-04-15 07:32:19 -0700187 } else {
msarettaa2a7de2015-07-01 13:11:08 -0700188 scale = 8;
msarette16b04a2015-04-15 07:32:19 -0700189 }
190
191 // Set up a fake decompress struct in order to use libjpeg to calculate output dimensions
192 jpeg_decompress_struct dinfo;
mtkleinf7aaadb2015-04-16 06:09:27 -0700193 sk_bzero(&dinfo, sizeof(dinfo));
msarette16b04a2015-04-15 07:32:19 -0700194 dinfo.image_width = this->getInfo().width();
195 dinfo.image_height = this->getInfo().height();
196 dinfo.global_state = DSTATE_READY;
197 dinfo.num_components = 0;
msarettaa2a7de2015-07-01 13:11:08 -0700198 dinfo.scale_num = 1;
199 dinfo.scale_denom = scale;
200 jpeg_calc_output_dimensions(&dinfo);
msarette16b04a2015-04-15 07:32:19 -0700201
202 // Return the calculated output dimensions for the given scale
203 return SkISize::Make(dinfo.output_width, dinfo.output_height);
204}
205
206/*
msarettaa2a7de2015-07-01 13:11:08 -0700207 * Checks if the conversion between the input image and the requested output
208 * image has been implemented
209 */
210static 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 recommendation
228 return dst.colorType() == src.colorType();
229}
230
231/*
msarett97fdea62015-04-29 08:17:15 -0700232 * Handles rewinding the input stream if it is necessary
233 */
234bool SkJpegCodec::handleRewind() {
235 switch(this->rewindIfNeeded()) {
236 case kCouldNotRewind_RewindState:
237 return fDecoderMgr->returnFalse("could not rewind");
238 case kRewound_RewindState: {
239 JpegDecoderMgr* decoderMgr = NULL;
240 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) {
241 return fDecoderMgr->returnFalse("could not rewind");
242 }
243 SkASSERT(NULL != decoderMgr);
244 fDecoderMgr.reset(decoderMgr);
245 return true;
246 }
247 case kNoRewindNecessary_RewindState:
248 return true;
249 default:
250 SkASSERT(false);
251 return false;
252 }
253}
254
255/*
256 * Checks if we can scale to the requested dimensions and scales the dimensions
257 * if possible
258 */
259bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) {
msarettaa2a7de2015-07-01 13:11:08 -0700260 // libjpeg can scale to 1/1, 1/2, 1/4, and 1/8
261 SkASSERT(1 == fDecoderMgr->dinfo()->scale_num);
262 SkASSERT(1 == fDecoderMgr->dinfo()->scale_denom);
263 jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
msarett97fdea62015-04-29 08:17:15 -0700264 while (fDecoderMgr->dinfo()->output_width != dstWidth ||
265 fDecoderMgr->dinfo()->output_height != dstHeight) {
266
267 // Return a failure if we have tried all of the possible scales
msarettaa2a7de2015-07-01 13:11:08 -0700268 if (8 == fDecoderMgr->dinfo()->scale_denom ||
msarett97fdea62015-04-29 08:17:15 -0700269 dstWidth > fDecoderMgr->dinfo()->output_width ||
270 dstHeight > fDecoderMgr->dinfo()->output_height) {
271 return fDecoderMgr->returnFalse("could not scale to requested dimensions");
272 }
273
274 // Try the next scale
msarettaa2a7de2015-07-01 13:11:08 -0700275 fDecoderMgr->dinfo()->scale_denom *= 2;
276 jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
msarett97fdea62015-04-29 08:17:15 -0700277 }
278 return true;
279}
280
281/*
msarettaa2a7de2015-07-01 13:11:08 -0700282 * Create the swizzler based on the encoded format
283 */
284void 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, dstRowBytes,
289 options.fZeroInitialized));
290 fSrcRowBytes = SkSwizzler::BytesPerPixel(srcConfig) * dstInfo.width();
291}
292
293/*
msarette16b04a2015-04-15 07:32:19 -0700294 * Performs the jpeg decode
295 */
296SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
297 void* dst, size_t dstRowBytes,
298 const Options& options, SkPMColor*, int*) {
msarett97fdea62015-04-29 08:17:15 -0700299
msarettc0e80c12015-07-01 06:50:35 -0700300 // Do not allow a regular decode if the caller has asked for a scanline decoder
301 if (NULL != this->scanlineDecoder()) {
302 return fDecoderMgr->returnFailure("cannot getPixels() if a scanline decoder has been"
303 "created", kInvalidParameters);
304 }
305
msarette16b04a2015-04-15 07:32:19 -0700306 // 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
311 // Get a pointer to the decompress info since we will use it quite frequently
312 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
313
314 // Set the jump location for libjpeg errors
315 if (setjmp(fDecoderMgr->getJmpBuf())) {
316 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
317 }
318
msarettaa2a7de2015-07-01 13:11:08 -0700319 // Check if we can decode to the requested destination
320 if (!conversion_possible(dstInfo, this->getInfo())) {
msarette16b04a2015-04-15 07:32:19 -0700321 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConversion);
322 }
msarette16b04a2015-04-15 07:32:19 -0700323
msarett97fdea62015-04-29 08:17:15 -0700324 // Perform the necessary scaling
325 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
msarettaa2a7de2015-07-01 13:11:08 -0700326 fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidScale);
msarette16b04a2015-04-15 07:32:19 -0700327 }
328
329 // Now, given valid output dimensions, we can start the decompress
msarettaa2a7de2015-07-01 13:11:08 -0700330 if (!jpeg_start_decompress(dinfo)) {
msarette16b04a2015-04-15 07:32:19 -0700331 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
332 }
333
msarettaa2a7de2015-07-01 13:11:08 -0700334 // Create the swizzler
335 this->initializeSwizzler(dstInfo, dst, dstRowBytes, options);
336 if (NULL == fSwizzler) {
337 return fDecoderMgr->returnFailure("getSwizzler", kUnimplemented);
338 }
msarette16b04a2015-04-15 07:32:19 -0700339
msarettaa2a7de2015-07-01 13:11:08 -0700340 // This is usually 1, but can also be 2 or 4.
341 // If we wanted to always read one row at a time, we could, but we will save space and time
342 // by using the recommendation from libjpeg.
343 const uint32_t rowsPerDecode = dinfo->rec_outbuf_height;
344 SkASSERT(rowsPerDecode <= 4);
345
346 // Create a buffer to contain decoded rows (libjpeg requires a 2D array)
347 SkASSERT(0 != fSrcRowBytes);
348 SkAutoTDeleteArray<uint8_t> srcBuffer(SkNEW_ARRAY(uint8_t, fSrcRowBytes * rowsPerDecode));
349 JSAMPLE* srcRows[4];
350 uint8_t* srcPtr = srcBuffer.get();
351 for (uint8_t i = 0; i < rowsPerDecode; i++) {
352 srcRows[i] = (JSAMPLE*) srcPtr;
353 srcPtr += fSrcRowBytes;
354 }
355
356 // Ensure that we loop enough times to decode all of the rows
357 // libjpeg will prevent us from reading past the bottom of the image
msarett97fdea62015-04-29 08:17:15 -0700358 uint32_t dstHeight = dstInfo.height();
msarettaa2a7de2015-07-01 13:11:08 -0700359 for (uint32_t y = 0; y < dstHeight + rowsPerDecode - 1; y += rowsPerDecode) {
msarette16b04a2015-04-15 07:32:19 -0700360 // Read rows of the image
msarettaa2a7de2015-07-01 13:11:08 -0700361 uint32_t rowsDecoded = jpeg_read_scanlines(dinfo, srcRows, rowsPerDecode);
362
363 // Convert to RGB if necessary
364 if (JCS_CMYK == dinfo->out_color_space) {
365 convert_CMYK_to_RGB(srcRows[0], dstInfo.width() * rowsDecoded);
366 }
367
368 // Swizzle to output destination
369 for (uint32_t i = 0; i < rowsDecoded; i++) {
370 fSwizzler->next(srcRows[i]);
371 }
msarette16b04a2015-04-15 07:32:19 -0700372
373 // If we cannot read enough rows, assume the input is incomplete
msarettaa2a7de2015-07-01 13:11:08 -0700374 if (rowsDecoded < rowsPerDecode && y + rowsDecoded < dstHeight) {
msarette16b04a2015-04-15 07:32:19 -0700375 // Fill the remainder of the image with black. This error handling
376 // behavior is unspecified but SkCodec consistently uses black as
377 // the fill color for opaque images. If the destination is kGray,
378 // the low 8 bits of SK_ColorBLACK will be used. Conveniently,
379 // these are zeros, which is the representation for black in kGray.
msarettaa2a7de2015-07-01 13:11:08 -0700380 SkSwizzler::Fill(fSwizzler->getDstRow(), dstInfo, dstRowBytes,
381 dstHeight - y - rowsDecoded, SK_ColorBLACK, NULL);
msarette16b04a2015-04-15 07:32:19 -0700382
383 // Prevent libjpeg from failing on incomplete decode
384 dinfo->output_scanline = dstHeight;
385
386 // Finish the decode and indicate that the input was incomplete.
msarettaa2a7de2015-07-01 13:11:08 -0700387 jpeg_finish_decompress(dinfo);
msarette16b04a2015-04-15 07:32:19 -0700388 return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
389 }
390 }
msarettaa2a7de2015-07-01 13:11:08 -0700391 jpeg_finish_decompress(dinfo);
msarette16b04a2015-04-15 07:32:19 -0700392
393 return kSuccess;
394}
msarett97fdea62015-04-29 08:17:15 -0700395
396/*
msarettc0e80c12015-07-01 06:50:35 -0700397 * We override the destructor to ensure that the scanline decoder is left in a
398 * finished state before destroying the decode manager.
399 */
400SkJpegCodec::~SkJpegCodec() {
401 SkAutoTDelete<SkScanlineDecoder> decoder(this->detachScanlineDecoder());
402 if (NULL != decoder) {
403 if (setjmp(fDecoderMgr->getJmpBuf())) {
404 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n");
405 return;
406 }
407
408 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a
409 // partial decode.
410 fDecoderMgr->dinfo()->output_scanline = this->getInfo().height();
msarettaa2a7de2015-07-01 13:11:08 -0700411 jpeg_finish_decompress(fDecoderMgr->dinfo());
msarettc0e80c12015-07-01 06:50:35 -0700412 }
413}
414
415/*
msarett97fdea62015-04-29 08:17:15 -0700416 * Enable scanline decoding for jpegs
417 */
418class SkJpegScanlineDecoder : public SkScanlineDecoder {
419public:
420 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec)
421 : INHERITED(dstInfo)
422 , fCodec(codec)
msarettaa2a7de2015-07-01 13:11:08 -0700423 {
424 fStorage.reset(fCodec->fSrcRowBytes);
425 fSrcRow = static_cast<uint8_t*>(fStorage.get());
426 }
msarett97fdea62015-04-29 08:17:15 -0700427
428 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowBytes) override {
429 // Set the jump location for libjpeg errors
430 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
431 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator::kInvalidInput);
432 }
433
434 // Read rows one at a time
435 for (int y = 0; y < count; y++) {
436 // Read row of the image
msarettaa2a7de2015-07-01 13:11:08 -0700437 uint32_t rowsDecoded = jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &fSrcRow, 1);
msarett97fdea62015-04-29 08:17:15 -0700438 if (rowsDecoded != 1) {
msarettaa2a7de2015-07-01 13:11:08 -0700439 SkSwizzler::Fill(dst, this->dstInfo(), rowBytes, count - y, SK_ColorBLACK, NULL);
msarett97fdea62015-04-29 08:17:15 -0700440 return SkImageGenerator::kIncompleteInput;
441 }
442
msarettaa2a7de2015-07-01 13:11:08 -0700443 // Convert to RGB if necessary
msarett97fdea62015-04-29 08:17:15 -0700444 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) {
msarettaa2a7de2015-07-01 13:11:08 -0700445 convert_CMYK_to_RGB(fSrcRow, dstInfo().width());
msarett97fdea62015-04-29 08:17:15 -0700446 }
447
msarettaa2a7de2015-07-01 13:11:08 -0700448 // Swizzle to output destination
449 fCodec->fSwizzler->setDstRow(dst);
450 fCodec->fSwizzler->next(fSrcRow);
451 dst = SkTAddOffset<void>(dst, rowBytes);
msarett97fdea62015-04-29 08:17:15 -0700452 }
453
454 return SkImageGenerator::kSuccess;
455 }
456
457 SkImageGenerator::Result onSkipScanlines(int count) override {
458 // Set the jump location for libjpeg errors
459 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
460 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator::kInvalidInput);
461 }
462
msarettaa2a7de2015-07-01 13:11:08 -0700463 // Read rows but ignore the output
464 for (int y = 0; y < count; y++) {
465 jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &fSrcRow, 1);
466 }
msarett97fdea62015-04-29 08:17:15 -0700467
468 return SkImageGenerator::kSuccess;
469 }
470
msarett97fdea62015-04-29 08:17:15 -0700471private:
msarettaa2a7de2015-07-01 13:11:08 -0700472 SkJpegCodec* fCodec; // unowned
473 SkAutoMalloc fStorage;
474 uint8_t* fSrcRow; // ptr into fStorage
msarett97fdea62015-04-29 08:17:15 -0700475
476 typedef SkScanlineDecoder INHERITED;
477};
478
479SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
480 const Options& options, SkPMColor ctable[], int* ctableCount) {
481
482 // Rewind the stream if needed
483 if (!this->handleRewind()) {
484 SkCodecPrintf("Could not rewind\n");
485 return NULL;
486 }
487
488 // Set the jump location for libjpeg errors
489 if (setjmp(fDecoderMgr->getJmpBuf())) {
490 SkCodecPrintf("setjmp: Error from libjpeg\n");
491 return NULL;
492 }
493
msarettaa2a7de2015-07-01 13:11:08 -0700494 // Check if we can decode to the requested destination
495 if (!conversion_possible(dstInfo, this->getInfo())) {
msarett97fdea62015-04-29 08:17:15 -0700496 SkCodecPrintf("Cannot convert to output type\n");
497 return NULL;
498 }
499
500 // Perform the necessary scaling
501 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
msarettaa2a7de2015-07-01 13:11:08 -0700502 SkCodecPrintf("Cannot scale ot output dimensions\n");
msarett97fdea62015-04-29 08:17:15 -0700503 return NULL;
504 }
505
506 // Now, given valid output dimensions, we can start the decompress
msarettaa2a7de2015-07-01 13:11:08 -0700507 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) {
msarett97fdea62015-04-29 08:17:15 -0700508 SkCodecPrintf("start decompress failed\n");
509 return NULL;
510 }
511
msarettaa2a7de2015-07-01 13:11:08 -0700512 // Create the swizzler
513 this->initializeSwizzler(dstInfo, NULL, dstInfo.minRowBytes(), options);
514 if (NULL == fSwizzler) {
515 SkCodecPrintf("Could not create swizzler\n");
516 return NULL;
517 }
518
msarett97fdea62015-04-29 08:17:15 -0700519 // Return the new scanline decoder
520 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this));
521}