• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "src/codec/SkJpegCodec.h"
9 
10 #include "include/codec/SkCodec.h"
11 #include "include/codec/SkJpegDecoder.h"
12 #include "include/core/SkAlphaType.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkData.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPixmap.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkStream.h"
19 #include "include/core/SkTypes.h"
20 #include "include/core/SkYUVAInfo.h"
21 #include "include/private/base/SkAlign.h"
22 #include "include/private/base/SkTemplates.h"
23 #include "modules/skcms/skcms.h"
24 #include "src/codec/SkCodecPriv.h"
25 #include "src/codec/SkJpegConstants.h"
26 #include "src/codec/SkJpegDecoderMgr.h"
27 #include "src/codec/SkJpegMetadataDecoderImpl.h"
28 #include "src/codec/SkJpegPriv.h"
29 #include "src/codec/SkParseEncodedOrigin.h"
30 #include "src/codec/SkSwizzler.h"
31 
32 #ifdef SK_CODEC_DECODES_JPEG_GAINMAPS
33 #include "include/private/SkGainmapInfo.h"
34 #endif  // SK_CODEC_DECODES_JPEG_GAINMAPS
35 
36 #include <array>
37 #include <csetjmp>
38 #include <cstring>
39 #include <utility>
40 
41 using namespace skia_private;
42 
43 class SkSampler;
44 struct SkGainmapInfo;
45 
46 // This warning triggers false postives way too often in here.
47 #if defined(__GNUC__) && !defined(__clang__)
48     #pragma GCC diagnostic ignored "-Wclobbered"
49 #endif
50 
51 extern "C" {
52     #include "jpeglib.h"  // NO_G3_REWRITE
53 }
54 
IsJpeg(const void * buffer,size_t bytesRead)55 bool SkJpegCodec::IsJpeg(const void* buffer, size_t bytesRead) {
56     return bytesRead >= sizeof(kJpegSig) && !memcmp(buffer, kJpegSig, sizeof(kJpegSig));
57 }
58 
get_sk_marker_list(jpeg_decompress_struct * dinfo)59 SkJpegMarkerList get_sk_marker_list(jpeg_decompress_struct* dinfo) {
60     SkJpegMarkerList markerList;
61     for (auto* marker = dinfo->marker_list; marker; marker = marker->next) {
62         markerList.emplace_back(marker->marker,
63                                 SkData::MakeWithoutCopy(marker->data, marker->data_length));
64     }
65     return markerList;
66 }
67 
get_exif_orientation(sk_sp<SkData> exifData)68 static SkEncodedOrigin get_exif_orientation(sk_sp<SkData> exifData) {
69     SkEncodedOrigin origin = kDefault_SkEncodedOrigin;
70     if (exifData && SkParseEncodedOrigin(exifData->bytes(), exifData->size(), &origin)) {
71         return origin;
72     }
73     return kDefault_SkEncodedOrigin;
74 }
75 
ReadHeader(SkStream * stream,SkCodec ** codecOut,JpegDecoderMgr ** decoderMgrOut,std::unique_ptr<SkEncodedInfo::ICCProfile> defaultColorProfile)76 SkCodec::Result SkJpegCodec::ReadHeader(
77         SkStream* stream,
78         SkCodec** codecOut,
79         JpegDecoderMgr** decoderMgrOut,
80         std::unique_ptr<SkEncodedInfo::ICCProfile> defaultColorProfile) {
81     // Create a JpegDecoderMgr to own all of the decompress information
82     std::unique_ptr<JpegDecoderMgr> decoderMgr(new JpegDecoderMgr(stream));
83 
84     // libjpeg errors will be caught and reported here
85     skjpeg_error_mgr::AutoPushJmpBuf jmp(decoderMgr->errorMgr());
86     if (setjmp(jmp)) {
87         return decoderMgr->returnFailure("ReadHeader", kInvalidInput);
88     }
89 
90     // Initialize the decompress info and the source manager
91     decoderMgr->init();
92     auto* dinfo = decoderMgr->dinfo();
93 
94     // Instruct jpeg library to save the markers that we care about.  Since
95     // the orientation and color profile will not change, we can skip this
96     // step on rewinds.
97     if (codecOut) {
98         jpeg_save_markers(dinfo, kExifMarker, 0xFFFF);
99         jpeg_save_markers(dinfo, kICCMarker, 0xFFFF);
100         jpeg_save_markers(dinfo, kMpfMarker, 0xFFFF);
101         jpeg_save_markers(dinfo, kJpegMarkerAPP0, 0xFFFF);
102         jpeg_save_markers(dinfo, kApp3Marker, 0xFFFF);
103         jpeg_save_markers(dinfo, kApp4Marker, 0xFFFF);
104         jpeg_save_markers(dinfo, kApp5Marker, 0xFFFF);
105         jpeg_save_markers(dinfo, kApp6Marker, 0xFFFF);
106         jpeg_save_markers(dinfo, kApp7Marker, 0xFFFF);
107         jpeg_save_markers(dinfo, kApp8Marker, 0xFFFF);
108         jpeg_save_markers(dinfo, kApp9Marker, 0xFFFF);
109         jpeg_save_markers(dinfo, kApp10Marker, 0xFFFF);
110         jpeg_save_markers(dinfo, kApp11Marker, 0xFFFF);
111         jpeg_save_markers(dinfo, kApp12Marker, 0xFFFF);
112         jpeg_save_markers(dinfo, kApp13Marker, 0xFFFF);
113         jpeg_save_markers(dinfo, kApp14Marker, 0xFFFF);
114         jpeg_save_markers(dinfo, kApp15Marker, 0xFFFF);
115     }
116 
117     // Read the jpeg header
118     switch (jpeg_read_header(dinfo, TRUE)) {
119         case JPEG_HEADER_OK:
120             break;
121         case JPEG_SUSPENDED:
122             return decoderMgr->returnFailure("ReadHeader", kIncompleteInput);
123         default:
124             return decoderMgr->returnFailure("ReadHeader", kInvalidInput);
125     }
126 
127     if (codecOut) {
128         // Get the encoded color type
129         SkEncodedInfo::Color color;
130         if (!decoderMgr->getEncodedColor(&color)) {
131             return kInvalidInput;
132         }
133 
134         auto metadataDecoder =
135                 std::make_unique<SkJpegMetadataDecoderImpl>(get_sk_marker_list(dinfo));
136 
137         SkEncodedOrigin orientation =
138                 get_exif_orientation(metadataDecoder->getExifMetadata(/*copyData=*/false));
139 
140         std::unique_ptr<SkEncodedInfo::ICCProfile> profile;
141         if (auto iccProfileData = metadataDecoder->getICCProfileData(/*copyData=*/true)) {
142             profile = SkEncodedInfo::ICCProfile::Make(std::move(iccProfileData));
143         }
144         if (profile) {
145             auto type = profile->profile()->data_color_space;
146             switch (decoderMgr->dinfo()->jpeg_color_space) {
147                 case JCS_CMYK:
148                 case JCS_YCCK:
149                     if (type != skcms_Signature_CMYK) {
150                         profile = nullptr;
151                     }
152                     break;
153                 case JCS_GRAYSCALE:
154                     if (type != skcms_Signature_Gray &&
155                         type != skcms_Signature_RGB)
156                     {
157                         profile = nullptr;
158                     }
159                     break;
160                 default:
161                     if (type != skcms_Signature_RGB) {
162                         profile = nullptr;
163                     }
164                     break;
165             }
166         }
167         if (!profile) {
168             profile = std::move(defaultColorProfile);
169         }
170 
171         SkEncodedInfo info = SkEncodedInfo::Make(dinfo->image_width, dinfo->image_height,
172                                                  color, SkEncodedInfo::kOpaque_Alpha, 8,
173                                                  std::move(profile));
174 
175         SkJpegCodec* codec = new SkJpegCodec(std::move(info),
176                                              std::unique_ptr<SkStream>(stream),
177                                              decoderMgr.release(),
178                                              orientation);
179         *codecOut = codec;
180     } else {
181         SkASSERT(nullptr != decoderMgrOut);
182         *decoderMgrOut = decoderMgr.release();
183     }
184     return kSuccess;
185 }
186 
MakeFromStream(std::unique_ptr<SkStream> stream,Result * result)187 std::unique_ptr<SkCodec> SkJpegCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
188                                                      Result* result) {
189     return SkJpegCodec::MakeFromStream(std::move(stream), result, nullptr);
190 }
191 
MakeFromStream(std::unique_ptr<SkStream> stream,Result * result,std::unique_ptr<SkEncodedInfo::ICCProfile> defaultColorProfile)192 std::unique_ptr<SkCodec> SkJpegCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
193         Result* result, std::unique_ptr<SkEncodedInfo::ICCProfile> defaultColorProfile) {
194     SkASSERT(result);
195     if (!stream) {
196         *result = SkCodec::kInvalidInput;
197         return nullptr;
198     }
199     SkCodec* codec = nullptr;
200     *result = ReadHeader(stream.get(), &codec, nullptr, std::move(defaultColorProfile));
201     if (kSuccess == *result) {
202         // Codec has taken ownership of the stream, we do not need to delete it
203         SkASSERT(codec);
204         stream.release();
205         return std::unique_ptr<SkCodec>(codec);
206     }
207     return nullptr;
208 }
209 
SkJpegCodec(SkEncodedInfo && info,std::unique_ptr<SkStream> stream,JpegDecoderMgr * decoderMgr,SkEncodedOrigin origin)210 SkJpegCodec::SkJpegCodec(SkEncodedInfo&& info,
211                          std::unique_ptr<SkStream> stream,
212                          JpegDecoderMgr* decoderMgr,
213                          SkEncodedOrigin origin)
214         : INHERITED(std::move(info), skcms_PixelFormat_RGBA_8888, std::move(stream), origin)
215         , fDecoderMgr(decoderMgr)
216         , fReadyState(decoderMgr->dinfo()->global_state) {}
217 SkJpegCodec::~SkJpegCodec() = default;
218 
219 /*
220  * Return the row bytes of a particular image type and width
221  */
get_row_bytes(const j_decompress_ptr dinfo)222 static size_t get_row_bytes(const j_decompress_ptr dinfo) {
223     const size_t colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 :
224             dinfo->out_color_components;
225     return dinfo->output_width * colorBytes;
226 
227 }
228 
229 /*
230  *  Calculate output dimensions based on the provided factors.
231  *
232  *  Not to be used on the actual jpeg_decompress_struct used for decoding, since it will
233  *  incorrectly modify num_components.
234  */
calc_output_dimensions(jpeg_decompress_struct * dinfo,unsigned int num,unsigned int denom)235 void calc_output_dimensions(jpeg_decompress_struct* dinfo, unsigned int num, unsigned int denom) {
236     dinfo->num_components = 0;
237     dinfo->scale_num = num;
238     dinfo->scale_denom = denom;
239     jpeg_calc_output_dimensions(dinfo);
240 }
241 
242 /*
243  * Return a valid set of output dimensions for this decoder, given an input scale
244  */
onGetScaledDimensions(float desiredScale) const245 SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const {
246     // 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
247     // support these as well
248     unsigned int num;
249     unsigned int denom = 8;
250     if (desiredScale >= 0.9375) {
251         num = 8;
252     } else if (desiredScale >= 0.8125) {
253         num = 7;
254     } else if (desiredScale >= 0.6875f) {
255         num = 6;
256     } else if (desiredScale >= 0.5625f) {
257         num = 5;
258     } else if (desiredScale >= 0.4375f) {
259         num = 4;
260     } else if (desiredScale >= 0.3125f) {
261         num = 3;
262     } else if (desiredScale >= 0.1875f) {
263         num = 2;
264     } else {
265         num = 1;
266     }
267 
268     // Set up a fake decompress struct in order to use libjpeg to calculate output dimensions.
269     // This isn't conventional use of libjpeg-turbo but initializing the decompress struct with
270     // jpeg_create_decompress allows for less violation of the API regardless of the version.
271     jpeg_decompress_struct dinfo;
272     jpeg_create_decompress(&dinfo);
273     dinfo.image_width = this->dimensions().width();
274     dinfo.image_height = this->dimensions().height();
275     dinfo.global_state = fReadyState;
276     calc_output_dimensions(&dinfo, num, denom);
277     SkISize outputDimensions = SkISize::Make(dinfo.output_width, dinfo.output_height);
278     jpeg_destroy_decompress(&dinfo);
279 
280     return outputDimensions;
281 }
282 
onRewind()283 bool SkJpegCodec::onRewind() {
284     JpegDecoderMgr* decoderMgr = nullptr;
285     if (kSuccess != ReadHeader(this->stream(), nullptr, &decoderMgr, nullptr)) {
286         return fDecoderMgr->returnFalse("onRewind");
287     }
288     SkASSERT(nullptr != decoderMgr);
289     fDecoderMgr.reset(decoderMgr);
290 
291     fSwizzler.reset(nullptr);
292     fSwizzleSrcRow = nullptr;
293     fColorXformSrcRow = nullptr;
294     fStorage.reset();
295 
296     return true;
297 }
298 
conversionSupported(const SkImageInfo & dstInfo,bool srcIsOpaque,bool needsColorXform)299 bool SkJpegCodec::conversionSupported(const SkImageInfo& dstInfo, bool srcIsOpaque,
300                                       bool needsColorXform) {
301     SkASSERT(srcIsOpaque);
302 
303     if (kUnknown_SkAlphaType == dstInfo.alphaType()) {
304         return false;
305     }
306 
307     if (kOpaque_SkAlphaType != dstInfo.alphaType()) {
308         SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
309                       "- it is being decoded as non-opaque, which will draw slower\n");
310     }
311 
312     J_COLOR_SPACE encodedColorType = fDecoderMgr->dinfo()->jpeg_color_space;
313 
314     // Check for valid color types and set the output color space
315     switch (dstInfo.colorType()) {
316         case kRGBA_8888_SkColorType:
317             fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
318             break;
319         case kBGRA_8888_SkColorType:
320             if (needsColorXform) {
321                 // Always using RGBA as the input format for color xforms makes the
322                 // implementation a little simpler.
323                 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
324             } else {
325                 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA;
326             }
327             break;
328         case kRGB_565_SkColorType:
329             if (needsColorXform) {
330                 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
331             } else {
332                 fDecoderMgr->dinfo()->dither_mode = JDITHER_NONE;
333                 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565;
334             }
335             break;
336         case kGray_8_SkColorType:
337             if (JCS_GRAYSCALE != encodedColorType) {
338                 return false;
339             }
340 
341             if (needsColorXform) {
342                 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
343             } else {
344                 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE;
345             }
346             break;
347         case kBGRA_10101010_XR_SkColorType:
348         case kBGR_101010x_XR_SkColorType:
349         case kRGBA_F16_SkColorType:
350             SkASSERT(needsColorXform);
351             fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
352             break;
353         default:
354             return false;
355     }
356 
357     // Check if we will decode to CMYK.  libjpeg-turbo does not convert CMYK to RGBA, so
358     // we must do it ourselves.
359     if (JCS_CMYK == encodedColorType || JCS_YCCK == encodedColorType) {
360         fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
361     }
362 
363     return true;
364 }
365 
366 /*
367  * Checks if we can natively scale to the requested dimensions and natively scales the
368  * dimensions if possible
369  */
onDimensionsSupported(const SkISize & size)370 bool SkJpegCodec::onDimensionsSupported(const SkISize& size) {
371     skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
372     if (setjmp(jmp)) {
373         return fDecoderMgr->returnFalse("onDimensionsSupported");
374     }
375 
376     const unsigned int dstWidth = size.width();
377     const unsigned int dstHeight = size.height();
378 
379     // Set up a fake decompress struct in order to use libjpeg to calculate output dimensions
380     // This isn't conventional use of libjpeg-turbo but initializing the decompress struct with
381     // jpeg_create_decompress allows for less violation of the API regardless of the version.
382     // FIXME: Why is this necessary?
383     jpeg_decompress_struct dinfo;
384     jpeg_create_decompress(&dinfo);
385     dinfo.image_width = this->dimensions().width();
386     dinfo.image_height = this->dimensions().height();
387     dinfo.global_state = fReadyState;
388 
389     // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1
390     unsigned int num = 8;
391     const unsigned int denom = 8;
392     calc_output_dimensions(&dinfo, num, denom);
393     while (dinfo.output_width != dstWidth || dinfo.output_height != dstHeight) {
394 
395         // Return a failure if we have tried all of the possible scales
396         if (1 == num || dstWidth > dinfo.output_width || dstHeight > dinfo.output_height) {
397             jpeg_destroy_decompress(&dinfo);
398             return false;
399         }
400 
401         // Try the next scale
402         num -= 1;
403         calc_output_dimensions(&dinfo, num, denom);
404     }
405     jpeg_destroy_decompress(&dinfo);
406 
407     fDecoderMgr->dinfo()->scale_num = num;
408     fDecoderMgr->dinfo()->scale_denom = denom;
409     return true;
410 }
411 
readRows(const SkImageInfo & dstInfo,void * dst,size_t rowBytes,int count,const Options & opts,int * rowsDecoded)412 SkCodec::Result SkJpegCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count,
413                           const Options& opts, int* rowsDecoded) {
414     // Set the jump location for libjpeg-turbo errors
415     skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
416     if (setjmp(jmp)) {
417         *rowsDecoded = 0;
418         return kInvalidInput;
419     }
420 
421     // When fSwizzleSrcRow is non-null, it means that we need to swizzle.  In this case,
422     // we will always decode into fSwizzlerSrcRow before swizzling into the next buffer.
423     // We can never swizzle "in place" because the swizzler may perform sampling and/or
424     // subsetting.
425     // When fColorXformSrcRow is non-null, it means that we need to color xform and that
426     // we cannot color xform "in place" (many times we can, but not when the src and dst
427     // are different sizes).
428     // In this case, we will color xform from fColorXformSrcRow into the dst.
429     JSAMPLE* decodeDst = (JSAMPLE*) dst;
430     uint32_t* swizzleDst = (uint32_t*) dst;
431     size_t decodeDstRowBytes = rowBytes;
432     size_t swizzleDstRowBytes = rowBytes;
433     int dstWidth = opts.fSubset ? opts.fSubset->width() : dstInfo.width();
434     if (fSwizzleSrcRow && fColorXformSrcRow) {
435         decodeDst = (JSAMPLE*) fSwizzleSrcRow;
436         swizzleDst = fColorXformSrcRow;
437         decodeDstRowBytes = 0;
438         swizzleDstRowBytes = 0;
439         dstWidth = fSwizzler->swizzleWidth();
440     } else if (fColorXformSrcRow) {
441         decodeDst = (JSAMPLE*) fColorXformSrcRow;
442         swizzleDst = fColorXformSrcRow;
443         decodeDstRowBytes = 0;
444         swizzleDstRowBytes = 0;
445     } else if (fSwizzleSrcRow) {
446         decodeDst = (JSAMPLE*) fSwizzleSrcRow;
447         decodeDstRowBytes = 0;
448         dstWidth = fSwizzler->swizzleWidth();
449     }
450 
451     for (int y = 0; y < count; y++) {
452         uint32_t lines = jpeg_read_scanlines(fDecoderMgr->dinfo(), &decodeDst, 1);
453         if (0 == lines) {
454             *rowsDecoded = y;
455             return kSuccess;
456         }
457 
458         if (fSwizzler) {
459             fSwizzler->swizzle(swizzleDst, decodeDst);
460         }
461 
462         if (this->colorXform()) {
463             this->applyColorXform(dst, swizzleDst, dstWidth);
464             dst = SkTAddOffset<void>(dst, rowBytes);
465         }
466 
467         decodeDst = SkTAddOffset<JSAMPLE>(decodeDst, decodeDstRowBytes);
468         swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes);
469     }
470 
471     *rowsDecoded = count;
472     return kSuccess;
473 }
474 
475 /*
476  * This is a bit tricky.  We only need the swizzler to do format conversion if the jpeg is
477  * encoded as CMYK.
478  * And even then we still may not need it.  If the jpeg has a CMYK color profile and a color
479  * xform, the color xform will handle the CMYK->RGB conversion.
480  */
needs_swizzler_to_convert_from_cmyk(J_COLOR_SPACE jpegColorType,const skcms_ICCProfile * srcProfile,bool hasColorSpaceXform)481 static inline bool needs_swizzler_to_convert_from_cmyk(J_COLOR_SPACE jpegColorType,
482                                                        const skcms_ICCProfile* srcProfile,
483                                                        bool hasColorSpaceXform) {
484     if (JCS_CMYK != jpegColorType) {
485         return false;
486     }
487 
488     bool hasCMYKColorSpace = srcProfile && srcProfile->data_color_space == skcms_Signature_CMYK;
489     return !hasCMYKColorSpace || !hasColorSpaceXform;
490 }
491 
492 /*
493  * Performs the jpeg decode
494  */
onGetPixels(const SkImageInfo & dstInfo,void * dst,size_t dstRowBytes,const Options & options,int * rowsDecoded)495 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
496                                          void* dst, size_t dstRowBytes,
497                                          const Options& options,
498                                          int* rowsDecoded) {
499     if (options.fSubset) {
500         // Subsets are not supported.
501         return kUnimplemented;
502     }
503 
504     // Get a pointer to the decompress info since we will use it quite frequently
505     jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
506 
507     // Set the jump location for libjpeg errors
508     skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
509     if (setjmp(jmp)) {
510         return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
511     }
512 
513     const bool isProgressive = dinfo->progressive_mode;
514     if (isProgressive) {
515        dinfo->buffered_image = TRUE;
516        jpeg_start_decompress(dinfo);
517     } else if (!jpeg_start_decompress(dinfo)) {
518         return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
519     }
520 
521     // The recommended output buffer height should always be 1 in high quality modes.
522     // If it's not, we want to know because it means our strategy is not optimal.
523     SkASSERT(1 == dinfo->rec_outbuf_height);
524 
525     if (needs_swizzler_to_convert_from_cmyk(dinfo->out_color_space,
526                                             this->getEncodedInfo().profile(), this->colorXform())) {
527         this->initializeSwizzler(dstInfo, options, true);
528     }
529 
530     if (!this->allocateStorage(dstInfo)) {
531         return kInternalError;
532     }
533 
534     if (isProgressive) {
535       // Keep consuming input until we can't anymore, and only output/read scanlines
536       // if there is at least one valid output.
537       int last_scan_completed = 0;
538       while (!jpeg_input_complete(dinfo)) {
539         // Call the progress monitor hook if present, to prevent decoder from hanging.
540         if (dinfo->progress) {
541            dinfo->progress->progress_monitor((j_common_ptr)dinfo);
542         }
543         const int res = jpeg_consume_input(dinfo);
544         if (res == JPEG_SUSPENDED) {
545            break;
546         }
547         if (res == JPEG_SCAN_COMPLETED) {
548            last_scan_completed = dinfo->input_scan_number;
549         }
550       }
551       if (last_scan_completed >  0) {
552         jpeg_start_output(dinfo, last_scan_completed);
553         int rows = 0;
554          SkCodec::Result readResult = this->readRows(dstInfo, dst, dstRowBytes,
555                                                      dstInfo.height(), options, &rows);
556          // Checks if scan was called too many times to not stall the decoder.
557          jpeg_finish_output(dinfo);
558          if (readResult != kSuccess) {
559             return fDecoderMgr->returnFailure("readRows", readResult);
560          }
561          if (rows < dstInfo.height()) {
562              *rowsDecoded = rows;
563              return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
564          }
565       } else {
566            return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
567       }
568     } else {
569       // Baseline image
570       int rows = 0;
571       this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height(), options, &rows);
572       if (rows < dstInfo.height()) {
573           *rowsDecoded = rows;
574           return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
575       }
576   }
577 
578     return kSuccess;
579 }
580 
allocateStorage(const SkImageInfo & dstInfo)581 bool SkJpegCodec::allocateStorage(const SkImageInfo& dstInfo) {
582     int dstWidth = dstInfo.width();
583 
584     size_t swizzleBytes = 0;
585     if (fSwizzler) {
586         swizzleBytes = get_row_bytes(fDecoderMgr->dinfo());
587         dstWidth = fSwizzler->swizzleWidth();
588         SkASSERT(!this->colorXform() || SkIsAlign4(swizzleBytes));
589     }
590 
591     size_t xformBytes = 0;
592 
593     if (this->colorXform() && sizeof(uint32_t) != dstInfo.bytesPerPixel()) {
594         xformBytes = dstWidth * sizeof(uint32_t);
595     }
596 
597     size_t totalBytes = swizzleBytes + xformBytes;
598     if (totalBytes > 0) {
599         if (!fStorage.reset(totalBytes)) {
600             return false;
601         }
602         fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr;
603         fColorXformSrcRow = (xformBytes > 0) ?
604                 SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr;
605     }
606     return true;
607 }
608 
initializeSwizzler(const SkImageInfo & dstInfo,const Options & options,bool needsCMYKToRGB)609 void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& options,
610         bool needsCMYKToRGB) {
611     Options swizzlerOptions = options;
612     if (options.fSubset) {
613         // Use fSwizzlerSubset if this is a subset decode.  This is necessary in the case
614         // where libjpeg-turbo provides a subset and then we need to subset it further.
615         // Also, verify that fSwizzlerSubset is initialized and valid.
616         SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fSubset->x() &&
617                 fSwizzlerSubset.width() == options.fSubset->width());
618         swizzlerOptions.fSubset = &fSwizzlerSubset;
619     }
620 
621     SkImageInfo swizzlerDstInfo = dstInfo;
622     if (this->colorXform()) {
623         // The color xform will be expecting RGBA 8888 input.
624         swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_8888_SkColorType);
625     }
626 
627     if (needsCMYKToRGB) {
628         // The swizzler is used to convert to from CMYK.
629         // The swizzler does not use the width or height on SkEncodedInfo.
630         auto swizzlerInfo = SkEncodedInfo::Make(0, 0, SkEncodedInfo::kInvertedCMYK_Color,
631                                                 SkEncodedInfo::kOpaque_Alpha, 8);
632         fSwizzler = SkSwizzler::Make(swizzlerInfo, nullptr, swizzlerDstInfo, swizzlerOptions);
633     } else {
634         int srcBPP = 0;
635         switch (fDecoderMgr->dinfo()->out_color_space) {
636             case JCS_EXT_RGBA:
637             case JCS_EXT_BGRA:
638             case JCS_CMYK:
639                 srcBPP = 4;
640                 break;
641             case JCS_RGB565:
642                 srcBPP = 2;
643                 break;
644             case JCS_GRAYSCALE:
645                 srcBPP = 1;
646                 break;
647             default:
648                 SkASSERT(false);
649                 break;
650         }
651         fSwizzler = SkSwizzler::MakeSimple(srcBPP, swizzlerDstInfo, swizzlerOptions);
652     }
653     SkASSERT(fSwizzler);
654 }
655 
getSampler(bool createIfNecessary)656 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) {
657     if (!createIfNecessary || fSwizzler) {
658         SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcRow));
659         return fSwizzler.get();
660     }
661 
662     bool needsCMYKToRGB = needs_swizzler_to_convert_from_cmyk(
663             fDecoderMgr->dinfo()->out_color_space, this->getEncodedInfo().profile(),
664             this->colorXform());
665     this->initializeSwizzler(this->dstInfo(), this->options(), needsCMYKToRGB);
666     if (!this->allocateStorage(this->dstInfo())) {
667         return nullptr;
668     }
669     return fSwizzler.get();
670 }
671 
onStartScanlineDecode(const SkImageInfo & dstInfo,const Options & options)672 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
673         const Options& options) {
674     // Set the jump location for libjpeg errors
675     skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
676     if (setjmp(jmp)) {
677         SkCodecPrintf("setjmp: Error from libjpeg\n");
678         return kInvalidInput;
679     }
680 
681     if (!jpeg_start_decompress(fDecoderMgr->dinfo())) {
682         SkCodecPrintf("start decompress failed\n");
683         return kInvalidInput;
684     }
685 
686     bool needsCMYKToRGB = needs_swizzler_to_convert_from_cmyk(
687             fDecoderMgr->dinfo()->out_color_space, this->getEncodedInfo().profile(),
688             this->colorXform());
689     if (options.fSubset) {
690         uint32_t startX = options.fSubset->x();
691         uint32_t width = options.fSubset->width();
692 
693         // libjpeg-turbo may need to align startX to a multiple of the IDCT
694         // block size.  If this is the case, it will decrease the value of
695         // startX to the appropriate alignment and also increase the value
696         // of width so that the right edge of the requested subset remains
697         // the same.
698         jpeg_crop_scanline(fDecoderMgr->dinfo(), &startX, &width);
699 
700         SkASSERT(startX <= (uint32_t) options.fSubset->x());
701         SkASSERT(width >= (uint32_t) options.fSubset->width());
702         SkASSERT(startX + width >= (uint32_t) options.fSubset->right());
703 
704         // Instruct the swizzler (if it is necessary) to further subset the
705         // output provided by libjpeg-turbo.
706         //
707         // We set this here (rather than in the if statement below), so that
708         // if (1) we don't need a swizzler for the subset, and (2) we need a
709         // swizzler for CMYK, the swizzler will still use the proper subset
710         // dimensions.
711         //
712         // Note that the swizzler will ignore the y and height parameters of
713         // the subset.  Since the scanline decoder (and the swizzler) handle
714         // one row at a time, only the subsetting in the x-dimension matters.
715         fSwizzlerSubset.setXYWH(options.fSubset->x() - startX, 0,
716                 options.fSubset->width(), options.fSubset->height());
717 
718         // We will need a swizzler if libjpeg-turbo cannot provide the exact
719         // subset that we request.
720         if (startX != (uint32_t) options.fSubset->x() ||
721                 width != (uint32_t) options.fSubset->width()) {
722             this->initializeSwizzler(dstInfo, options, needsCMYKToRGB);
723         }
724     }
725 
726     // Make sure we have a swizzler if we are converting from CMYK.
727     if (!fSwizzler && needsCMYKToRGB) {
728         this->initializeSwizzler(dstInfo, options, true);
729     }
730 
731     if (!this->allocateStorage(dstInfo)) {
732         return kInternalError;
733     }
734 
735     return kSuccess;
736 }
737 
onGetScanlines(void * dst,int count,size_t dstRowBytes)738 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
739     int rows = 0;
740     this->readRows(this->dstInfo(), dst, dstRowBytes, count, this->options(), &rows);
741     if (rows < count) {
742         // This allows us to skip calling jpeg_finish_decompress().
743         fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height();
744     }
745 
746     return rows;
747 }
748 
onSkipScanlines(int count)749 bool SkJpegCodec::onSkipScanlines(int count) {
750     // Set the jump location for libjpeg errors
751     skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
752     if (setjmp(jmp)) {
753         return fDecoderMgr->returnFalse("onSkipScanlines");
754     }
755 
756     return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count);
757 }
758 
is_yuv_supported(const jpeg_decompress_struct * dinfo,const SkJpegCodec & codec,const SkYUVAPixmapInfo::SupportedDataTypes * supportedDataTypes,SkYUVAPixmapInfo * yuvaPixmapInfo)759 static bool is_yuv_supported(const jpeg_decompress_struct* dinfo,
760                              const SkJpegCodec& codec,
761                              const SkYUVAPixmapInfo::SupportedDataTypes* supportedDataTypes,
762                              SkYUVAPixmapInfo* yuvaPixmapInfo) {
763     // Scaling is not supported in raw data mode.
764     SkASSERT(dinfo->scale_num == dinfo->scale_denom);
765 
766     // I can't imagine that this would ever change, but we do depend on it.
767     static_assert(8 == DCTSIZE, "DCTSIZE (defined in jpeg library) should always be 8.");
768 
769     if (JCS_YCbCr != dinfo->jpeg_color_space) {
770         return false;
771     }
772 
773     SkASSERT(3 == dinfo->num_components);
774     SkASSERT(dinfo->comp_info);
775 
776     // It is possible to perform a YUV decode for any combination of
777     // horizontal and vertical sampling that is supported by
778     // libjpeg/libjpeg-turbo.  However, we will start by supporting only the
779     // common cases (where U and V have samp_factors of one).
780     //
781     // The definition of samp_factor is kind of the opposite of what SkCodec
782     // thinks of as a sampling factor.  samp_factor is essentially a
783     // multiplier, and the larger the samp_factor is, the more samples that
784     // there will be.  Ex:
785     //     U_plane_width = image_width * (U_h_samp_factor / max_h_samp_factor)
786     //
787     // Supporting cases where the samp_factors for U or V were larger than
788     // that of Y would be an extremely difficult change, given that clients
789     // allocate memory as if the size of the Y plane is always the size of the
790     // image.  However, this case is very, very rare.
791     if  ((1 != dinfo->comp_info[1].h_samp_factor) ||
792          (1 != dinfo->comp_info[1].v_samp_factor) ||
793          (1 != dinfo->comp_info[2].h_samp_factor) ||
794          (1 != dinfo->comp_info[2].v_samp_factor))
795     {
796         return false;
797     }
798 
799     // Support all common cases of Y samp_factors.
800     // TODO (msarett): As mentioned above, it would be possible to support
801     //                 more combinations of samp_factors.  The issues are:
802     //                 (1) Are there actually any images that are not covered
803     //                     by these cases?
804     //                 (2) How much complexity would be added to the
805     //                     implementation in order to support these rare
806     //                     cases?
807     int hSampY = dinfo->comp_info[0].h_samp_factor;
808     int vSampY = dinfo->comp_info[0].v_samp_factor;
809     SkASSERT(hSampY == dinfo->max_h_samp_factor);
810     SkASSERT(vSampY == dinfo->max_v_samp_factor);
811 
812     SkYUVAInfo::Subsampling tempSubsampling;
813     if        (1 == hSampY && 1 == vSampY) {
814         tempSubsampling = SkYUVAInfo::Subsampling::k444;
815     } else if (2 == hSampY && 1 == vSampY) {
816         tempSubsampling = SkYUVAInfo::Subsampling::k422;
817     } else if (2 == hSampY && 2 == vSampY) {
818         tempSubsampling = SkYUVAInfo::Subsampling::k420;
819     } else if (1 == hSampY && 2 == vSampY) {
820         tempSubsampling = SkYUVAInfo::Subsampling::k440;
821     } else if (4 == hSampY && 1 == vSampY) {
822         tempSubsampling = SkYUVAInfo::Subsampling::k411;
823     } else if (4 == hSampY && 2 == vSampY) {
824         tempSubsampling = SkYUVAInfo::Subsampling::k410;
825     } else {
826         return false;
827     }
828     if (supportedDataTypes &&
829         !supportedDataTypes->supported(SkYUVAInfo::PlaneConfig::kY_U_V,
830                                        SkYUVAPixmapInfo::DataType::kUnorm8)) {
831         return false;
832     }
833     if (yuvaPixmapInfo) {
834         SkColorType colorTypes[SkYUVAPixmapInfo::kMaxPlanes];
835         size_t rowBytes[SkYUVAPixmapInfo::kMaxPlanes];
836         for (int i = 0; i < 3; ++i) {
837             colorTypes[i] = kAlpha_8_SkColorType;
838             rowBytes[i] = dinfo->comp_info[i].width_in_blocks * DCTSIZE;
839         }
840         SkYUVAInfo yuvaInfo(codec.dimensions(),
841                             SkYUVAInfo::PlaneConfig::kY_U_V,
842                             tempSubsampling,
843                             kJPEG_Full_SkYUVColorSpace,
844                             codec.getOrigin(),
845                             SkYUVAInfo::Siting::kCentered,
846                             SkYUVAInfo::Siting::kCentered);
847         *yuvaPixmapInfo = SkYUVAPixmapInfo(yuvaInfo, colorTypes, rowBytes);
848     }
849     return true;
850 }
851 
onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes & supportedDataTypes,SkYUVAPixmapInfo * yuvaPixmapInfo) const852 bool SkJpegCodec::onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
853                                   SkYUVAPixmapInfo* yuvaPixmapInfo) const {
854     jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
855     return is_yuv_supported(dinfo, *this, &supportedDataTypes, yuvaPixmapInfo);
856 }
857 
onGetYUVAPlanes(const SkYUVAPixmaps & yuvaPixmaps)858 SkCodec::Result SkJpegCodec::onGetYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps) {
859     // Get a pointer to the decompress info since we will use it quite frequently
860     jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
861     if (!is_yuv_supported(dinfo, *this, nullptr, nullptr)) {
862         return fDecoderMgr->returnFailure("onGetYUVAPlanes", kInvalidInput);
863     }
864     // Set the jump location for libjpeg errors
865     skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
866     if (setjmp(jmp)) {
867         return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
868     }
869 
870     dinfo->raw_data_out = TRUE;
871     if (!jpeg_start_decompress(dinfo)) {
872         return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
873     }
874 
875     const std::array<SkPixmap, SkYUVAPixmaps::kMaxPlanes>& planes = yuvaPixmaps.planes();
876 
877 #ifdef SK_DEBUG
878     {
879         // A previous implementation claims that the return value of is_yuv_supported()
880         // may change after calling jpeg_start_decompress().  It looks to me like this
881         // was caused by a bug in the old code, but we'll be safe and check here.
882         // Also check that pixmap properties agree with expectations.
883         SkYUVAPixmapInfo info;
884         SkASSERT(is_yuv_supported(dinfo, *this, nullptr, &info));
885         SkASSERT(info.yuvaInfo() == yuvaPixmaps.yuvaInfo());
886         for (int i = 0; i < info.numPlanes(); ++i) {
887             SkASSERT(planes[i].colorType() == kAlpha_8_SkColorType);
888             SkASSERT(info.planeInfo(i) == planes[i].info());
889         }
890     }
891 #endif
892 
893     // Build a JSAMPIMAGE to handle output from libjpeg-turbo.  A JSAMPIMAGE has
894     // a 2-D array of pixels for each of the components (Y, U, V) in the image.
895     // Cheat Sheet:
896     //     JSAMPIMAGE == JSAMPLEARRAY* == JSAMPROW** == JSAMPLE***
897     JSAMPARRAY yuv[3];
898 
899     // Set aside enough space for pointers to rows of Y, U, and V.
900     JSAMPROW rowptrs[2 * DCTSIZE + DCTSIZE + DCTSIZE];
901     yuv[0] = &rowptrs[0];            // Y rows (DCTSIZE or 2 * DCTSIZE)
902     yuv[1] = &rowptrs[2 * DCTSIZE];  // U rows (DCTSIZE)
903     yuv[2] = &rowptrs[3 * DCTSIZE];  // V rows (DCTSIZE)
904 
905     // Initialize rowptrs.
906     int numYRowsPerBlock = DCTSIZE * dinfo->comp_info[0].v_samp_factor;
907     static_assert(sizeof(JSAMPLE) == 1);
908     for (int i = 0; i < numYRowsPerBlock; i++) {
909         rowptrs[i] = static_cast<JSAMPLE*>(planes[0].writable_addr()) + i* planes[0].rowBytes();
910     }
911     for (int i = 0; i < DCTSIZE; i++) {
912         rowptrs[i + 2 * DCTSIZE] =
913                 static_cast<JSAMPLE*>(planes[1].writable_addr()) + i* planes[1].rowBytes();
914         rowptrs[i + 3 * DCTSIZE] =
915                 static_cast<JSAMPLE*>(planes[2].writable_addr()) + i* planes[2].rowBytes();
916     }
917 
918     // After each loop iteration, we will increment pointers to Y, U, and V.
919     size_t blockIncrementY = numYRowsPerBlock * planes[0].rowBytes();
920     size_t blockIncrementU = DCTSIZE * planes[1].rowBytes();
921     size_t blockIncrementV = DCTSIZE * planes[2].rowBytes();
922 
923     uint32_t numRowsPerBlock = numYRowsPerBlock;
924 
925     // We intentionally round down here, as this first loop will only handle
926     // full block rows.  As a special case at the end, we will handle any
927     // remaining rows that do not make up a full block.
928     const int numIters = dinfo->output_height / numRowsPerBlock;
929     for (int i = 0; i < numIters; i++) {
930         JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
931         if (linesRead < numRowsPerBlock) {
932             // FIXME: Handle incomplete YUV decodes without signalling an error.
933             return kInvalidInput;
934         }
935 
936         // Update rowptrs.
937         for (int j = 0; j < numYRowsPerBlock; j++) {
938             rowptrs[j] += blockIncrementY;
939         }
940         for (int j = 0; j < DCTSIZE; j++) {
941             rowptrs[j + 2 * DCTSIZE] += blockIncrementU;
942             rowptrs[j + 3 * DCTSIZE] += blockIncrementV;
943         }
944     }
945 
946     uint32_t remainingRows = dinfo->output_height - dinfo->output_scanline;
947     SkASSERT(remainingRows == dinfo->output_height % numRowsPerBlock);
948     SkASSERT(dinfo->output_scanline == numIters * numRowsPerBlock);
949     if (remainingRows > 0) {
950         // libjpeg-turbo needs memory to be padded by the block sizes.  We will fulfill
951         // this requirement using an extra row buffer.
952         // FIXME: Should SkCodec have an extra memory buffer that can be shared among
953         //        all of the implementations that use temporary/garbage memory?
954         AutoTMalloc<JSAMPLE> extraRow(planes[0].rowBytes());
955         for (int i = remainingRows; i < numYRowsPerBlock; i++) {
956             rowptrs[i] = extraRow.get();
957         }
958         int remainingUVRows = dinfo->comp_info[1].downsampled_height - DCTSIZE * numIters;
959         for (int i = remainingUVRows; i < DCTSIZE; i++) {
960             rowptrs[i + 2 * DCTSIZE] = extraRow.get();
961             rowptrs[i + 3 * DCTSIZE] = extraRow.get();
962         }
963 
964         JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
965         if (linesRead < remainingRows) {
966             // FIXME: Handle incomplete YUV decodes without signalling an error.
967             return kInvalidInput;
968         }
969     }
970 
971     return kSuccess;
972 }
973 
onGetGainmapCodec(SkGainmapInfo * info,std::unique_ptr<SkCodec> * gainmapCodec)974 bool SkJpegCodec::onGetGainmapCodec(SkGainmapInfo* info, std::unique_ptr<SkCodec>* gainmapCodec) {
975     std::unique_ptr<SkStream> stream;
976     if (!this->onGetGainmapInfo(info, &stream)) {
977         return false;
978     }
979     if (gainmapCodec) {
980         Result result;
981         *gainmapCodec = MakeFromStream(std::move(stream), &result);
982         if (!*gainmapCodec) {
983             return false;
984         }
985     }
986     return true;
987 }
988 
onGetGainmapInfo(SkGainmapInfo * info,std::unique_ptr<SkStream> * gainmapImageStream)989 bool SkJpegCodec::onGetGainmapInfo(SkGainmapInfo* info,
990                                    std::unique_ptr<SkStream>* gainmapImageStream) {
991 #ifdef SK_CODEC_DECODES_JPEG_GAINMAPS
992     sk_sp<SkData> gainmap_data;
993     SkGainmapInfo gainmap_info;
994 
995     auto metadataDecoder =
996             std::make_unique<SkJpegMetadataDecoderImpl>(get_sk_marker_list(fDecoderMgr->dinfo()));
997     if (!metadataDecoder->findGainmapImage(
998                 fDecoderMgr->getSourceMgr(), gainmap_data, gainmap_info)) {
999         return false;
1000     }
1001 
1002     *info = gainmap_info;
1003     *gainmapImageStream = SkMemoryStream::Make(gainmap_data);
1004     return true;
1005 #else
1006     return false;
1007 #endif  // SK_CODEC_DECODES_JPEG_GAINMAPS
1008 }
1009 
1010 namespace SkJpegDecoder {
IsJpeg(const void * data,size_t len)1011 bool IsJpeg(const void* data, size_t len) {
1012     return SkJpegCodec::IsJpeg(data, len);
1013 }
1014 
Decode(std::unique_ptr<SkStream> stream,SkCodec::Result * outResult,SkCodecs::DecodeContext)1015 std::unique_ptr<SkCodec> Decode(std::unique_ptr<SkStream> stream,
1016                                 SkCodec::Result* outResult,
1017                                 SkCodecs::DecodeContext) {
1018     SkCodec::Result resultStorage;
1019     if (!outResult) {
1020         outResult = &resultStorage;
1021     }
1022     return SkJpegCodec::MakeFromStream(std::move(stream), outResult);
1023 }
1024 
Decode(sk_sp<SkData> data,SkCodec::Result * outResult,SkCodecs::DecodeContext)1025 std::unique_ptr<SkCodec> Decode(sk_sp<SkData> data,
1026                                 SkCodec::Result* outResult,
1027                                 SkCodecs::DecodeContext) {
1028     if (!data) {
1029         if (outResult) {
1030             *outResult = SkCodec::kInvalidInput;
1031         }
1032         return nullptr;
1033     }
1034     return Decode(SkMemoryStream::Make(std::move(data)), outResult, nullptr);
1035 }
1036 
1037 }  // namespace SkJpegDecoder
1038