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