blob: 858e305b067132b78dd25bd03403090eadd6d2e5 [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*) {
msarett97fdea62015-04-29 08:17:15 -0700306
msarettc0e80c12015-07-01 06:50:35 -0700307 // Do not allow a regular decode if the caller has asked for a scanline decoder
308 if (NULL != this->scanlineDecoder()) {
309 return fDecoderMgr->returnFailure("cannot getPixels() if a scanline decoder has been"
310 "created", kInvalidParameters);
311 }
312
msarette16b04a2015-04-15 07:32:19 -0700313 // Rewind the stream if needed
msarett97fdea62015-04-29 08:17:15 -0700314 if (!this->handleRewind()) {
msarettc0e80c12015-07-01 06:50:35 -0700315 return fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind);
msarette16b04a2015-04-15 07:32:19 -0700316 }
317
318 // Get a pointer to the decompress info since we will use it quite frequently
319 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
320
321 // Set the jump location for libjpeg errors
322 if (setjmp(fDecoderMgr->getJmpBuf())) {
323 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
324 }
325
msarett1c8a5872015-07-07 08:50:01 -0700326 // Check if we can decode to the requested destination and set the output color space
327 if (!this->setOutputColorSpace(dstInfo)) {
msarette16b04a2015-04-15 07:32:19 -0700328 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConversion);
329 }
msarette16b04a2015-04-15 07:32:19 -0700330
msarett97fdea62015-04-29 08:17:15 -0700331 // Perform the necessary scaling
332 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
msarett1c8a5872015-07-07 08:50:01 -0700333 return fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidScale);
msarette16b04a2015-04-15 07:32:19 -0700334 }
335
336 // Now, given valid output dimensions, we can start the decompress
msarett1c8a5872015-07-07 08:50:01 -0700337 if (!turbo_jpeg_start_decompress(dinfo)) {
msarette16b04a2015-04-15 07:32:19 -0700338 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
339 }
340
msarett1c8a5872015-07-07 08:50:01 -0700341 // The recommended output buffer height should always be 1 in high quality modes.
342 // If it's not, we want to know because it means our strategy is not optimal.
343 SkASSERT(1 == dinfo->rec_outbuf_height);
msarette16b04a2015-04-15 07:32:19 -0700344
msarett1c8a5872015-07-07 08:50:01 -0700345 // Perform the decode a single row at a time
msarett97fdea62015-04-29 08:17:15 -0700346 uint32_t dstHeight = dstInfo.height();
msarett1c8a5872015-07-07 08:50:01 -0700347 JSAMPLE* dstRow = (JSAMPLE*) dst;
348 for (uint32_t y = 0; y < dstHeight; y++) {
msarette16b04a2015-04-15 07:32:19 -0700349 // Read rows of the image
msarett1c8a5872015-07-07 08:50:01 -0700350 uint32_t rowsDecoded = turbo_jpeg_read_scanlines(dinfo, &dstRow, 1);
msarette16b04a2015-04-15 07:32:19 -0700351
352 // If we cannot read enough rows, assume the input is incomplete
msarett1c8a5872015-07-07 08:50:01 -0700353 if (rowsDecoded != 1) {
msarette16b04a2015-04-15 07:32:19 -0700354 // Fill the remainder of the image with black. This error handling
355 // behavior is unspecified but SkCodec consistently uses black as
356 // the fill color for opaque images. If the destination is kGray,
357 // the low 8 bits of SK_ColorBLACK will be used. Conveniently,
358 // these are zeros, which is the representation for black in kGray.
msarett1c8a5872015-07-07 08:50:01 -0700359 SkSwizzler::Fill(dstRow, dstInfo, dstRowBytes, dstHeight - y, SK_ColorBLACK, NULL);
msarette16b04a2015-04-15 07:32:19 -0700360
361 // Prevent libjpeg from failing on incomplete decode
362 dinfo->output_scanline = dstHeight;
363
364 // Finish the decode and indicate that the input was incomplete.
msarett1c8a5872015-07-07 08:50:01 -0700365 turbo_jpeg_finish_decompress(dinfo);
msarette16b04a2015-04-15 07:32:19 -0700366 return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
367 }
msarett1c8a5872015-07-07 08:50:01 -0700368
369 // Convert to RGBA if necessary
370 if (JCS_CMYK == dinfo->out_color_space) {
371 convert_CMYK_to_RGBA(dstRow, dstInfo.width());
372 }
373
374 // Move to the next row
375 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes);
msarette16b04a2015-04-15 07:32:19 -0700376 }
msarett1c8a5872015-07-07 08:50:01 -0700377 turbo_jpeg_finish_decompress(dinfo);
msarette16b04a2015-04-15 07:32:19 -0700378
379 return kSuccess;
380}
msarett97fdea62015-04-29 08:17:15 -0700381
382/*
msarettc0e80c12015-07-01 06:50:35 -0700383 * We override the destructor to ensure that the scanline decoder is left in a
384 * finished state before destroying the decode manager.
385 */
386SkJpegCodec::~SkJpegCodec() {
387 SkAutoTDelete<SkScanlineDecoder> decoder(this->detachScanlineDecoder());
388 if (NULL != decoder) {
389 if (setjmp(fDecoderMgr->getJmpBuf())) {
390 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n");
391 return;
392 }
393
394 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a
395 // partial decode.
396 fDecoderMgr->dinfo()->output_scanline = this->getInfo().height();
msarett1c8a5872015-07-07 08:50:01 -0700397 turbo_jpeg_finish_decompress(fDecoderMgr->dinfo());
msarettc0e80c12015-07-01 06:50:35 -0700398 }
399}
400
401/*
msarett97fdea62015-04-29 08:17:15 -0700402 * Enable scanline decoding for jpegs
403 */
404class SkJpegScanlineDecoder : public SkScanlineDecoder {
405public:
406 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec)
407 : INHERITED(dstInfo)
408 , fCodec(codec)
msarett1c8a5872015-07-07 08:50:01 -0700409 {}
msarett97fdea62015-04-29 08:17:15 -0700410
scroggoeb602a52015-07-09 08:16:03 -0700411 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) override {
msarett97fdea62015-04-29 08:17:15 -0700412 // Set the jump location for libjpeg errors
413 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
scroggoeb602a52015-07-09 08:16:03 -0700414 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvalidInput);
msarett97fdea62015-04-29 08:17:15 -0700415 }
416
417 // Read rows one at a time
msarett1c8a5872015-07-07 08:50:01 -0700418 JSAMPLE* dstRow = (JSAMPLE*) dst;
msarett97fdea62015-04-29 08:17:15 -0700419 for (int y = 0; y < count; y++) {
420 // Read row of the image
msarett1c8a5872015-07-07 08:50:01 -0700421 uint32_t rowsDecoded =
422 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dstRow, 1);
msarett97fdea62015-04-29 08:17:15 -0700423 if (rowsDecoded != 1) {
msarett1c8a5872015-07-07 08:50:01 -0700424 SkSwizzler::Fill(
425 dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBLACK, NULL);
426 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height();
scroggoeb602a52015-07-09 08:16:03 -0700427 return SkCodec::kIncompleteInput;
msarett97fdea62015-04-29 08:17:15 -0700428 }
429
msarett1c8a5872015-07-07 08:50:01 -0700430 // Convert to RGBA if necessary
msarett97fdea62015-04-29 08:17:15 -0700431 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) {
msarett1c8a5872015-07-07 08:50:01 -0700432 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width());
msarett97fdea62015-04-29 08:17:15 -0700433 }
434
msarett1c8a5872015-07-07 08:50:01 -0700435 // Move to the next row
436 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes);
msarett97fdea62015-04-29 08:17:15 -0700437 }
438
scroggoeb602a52015-07-09 08:16:03 -0700439 return SkCodec::kSuccess;
msarett97fdea62015-04-29 08:17:15 -0700440 }
441
msarett1c8a5872015-07-07 08:50:01 -0700442#ifndef TURBO_HAS_SKIP
443#define turbo_jpeg_skip_scanlines(dinfo, count) \
444 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \
445 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \
446 for (int y = 0; y < count; y++) { \
447 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \
448 }
449#endif
450
scroggoeb602a52015-07-09 08:16:03 -0700451 SkCodec::Result onSkipScanlines(int count) override {
msarett97fdea62015-04-29 08:17:15 -0700452 // Set the jump location for libjpeg errors
453 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
scroggoeb602a52015-07-09 08:16:03 -0700454 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvalidInput);
msarett97fdea62015-04-29 08:17:15 -0700455 }
456
msarett1c8a5872015-07-07 08:50:01 -0700457 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count);
msarett97fdea62015-04-29 08:17:15 -0700458
scroggoeb602a52015-07-09 08:16:03 -0700459 return SkCodec::kSuccess;
msarett97fdea62015-04-29 08:17:15 -0700460 }
461
msarett97fdea62015-04-29 08:17:15 -0700462private:
msarett1c8a5872015-07-07 08:50:01 -0700463 SkJpegCodec* fCodec; // unowned
msarett97fdea62015-04-29 08:17:15 -0700464
465 typedef SkScanlineDecoder INHERITED;
466};
467
468SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
469 const Options& options, SkPMColor ctable[], int* ctableCount) {
470
471 // Rewind the stream if needed
472 if (!this->handleRewind()) {
473 SkCodecPrintf("Could not rewind\n");
474 return NULL;
475 }
476
477 // Set the jump location for libjpeg errors
478 if (setjmp(fDecoderMgr->getJmpBuf())) {
479 SkCodecPrintf("setjmp: Error from libjpeg\n");
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
484 if (!this->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
490 if (!this->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
msarett1c8a5872015-07-07 08:50:01 -0700496 if (!turbo_jpeg_start_decompress(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
502 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this));
503}