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,int * rowsDecoded)398 SkCodec::Result SkJpegCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count,
399 const Options& opts, int* rowsDecoded) {
400 // Set the jump location for libjpeg-turbo errors
401 skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
402 if (setjmp(jmp)) {
403 *rowsDecoded = 0;
404 return kInvalidInput;
405 }
406
407 // When fSwizzleSrcRow is non-null, it means that we need to swizzle. In this case,
408 // we will always decode into fSwizzlerSrcRow before swizzling into the next buffer.
409 // We can never swizzle "in place" because the swizzler may perform sampling and/or
410 // subsetting.
411 // When fColorXformSrcRow is non-null, it means that we need to color xform and that
412 // we cannot color xform "in place" (many times we can, but not when the src and dst
413 // are different sizes).
414 // In this case, we will color xform from fColorXformSrcRow into the dst.
415 JSAMPLE* decodeDst = (JSAMPLE*) dst;
416 uint32_t* swizzleDst = (uint32_t*) dst;
417 size_t decodeDstRowBytes = rowBytes;
418 size_t swizzleDstRowBytes = rowBytes;
419 int dstWidth = opts.fSubset ? opts.fSubset->width() : dstInfo.width();
420 if (fSwizzleSrcRow && fColorXformSrcRow) {
421 decodeDst = (JSAMPLE*) fSwizzleSrcRow;
422 swizzleDst = fColorXformSrcRow;
423 decodeDstRowBytes = 0;
424 swizzleDstRowBytes = 0;
425 dstWidth = fSwizzler->swizzleWidth();
426 } else if (fColorXformSrcRow) {
427 decodeDst = (JSAMPLE*) fColorXformSrcRow;
428 swizzleDst = fColorXformSrcRow;
429 decodeDstRowBytes = 0;
430 swizzleDstRowBytes = 0;
431 } else if (fSwizzleSrcRow) {
432 decodeDst = (JSAMPLE*) fSwizzleSrcRow;
433 decodeDstRowBytes = 0;
434 dstWidth = fSwizzler->swizzleWidth();
435 }
436
437 for (int y = 0; y < count; y++) {
438 uint32_t lines = jpeg_read_scanlines(fDecoderMgr->dinfo(), &decodeDst, 1);
439 if (0 == lines) {
440 *rowsDecoded = y;
441 return kSuccess;
442 }
443
444 if (fSwizzler) {
445 fSwizzler->swizzle(swizzleDst, decodeDst);
446 }
447
448 if (this->colorXform()) {
449 this->applyColorXform(dst, swizzleDst, dstWidth);
450 dst = SkTAddOffset<void>(dst, rowBytes);
451 }
452
453 decodeDst = SkTAddOffset<JSAMPLE>(decodeDst, decodeDstRowBytes);
454 swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes);
455 }
456
457 *rowsDecoded = count;
458 return kSuccess;
459 }
460
461 /*
462 * This is a bit tricky. We only need the swizzler to do format conversion if the jpeg is
463 * encoded as CMYK.
464 * And even then we still may not need it. If the jpeg has a CMYK color profile and a color
465 * xform, the color xform will handle the CMYK->RGB conversion.
466 */
needs_swizzler_to_convert_from_cmyk(J_COLOR_SPACE jpegColorType,const skcms_ICCProfile * srcProfile,bool hasColorSpaceXform)467 static inline bool needs_swizzler_to_convert_from_cmyk(J_COLOR_SPACE jpegColorType,
468 const skcms_ICCProfile* srcProfile,
469 bool hasColorSpaceXform) {
470 if (JCS_CMYK != jpegColorType) {
471 return false;
472 }
473
474 bool hasCMYKColorSpace = srcProfile && srcProfile->data_color_space == skcms_Signature_CMYK;
475 return !hasCMYKColorSpace || !hasColorSpaceXform;
476 }
477
478 /*
479 * Performs the jpeg decode
480 */
onGetPixels(const SkImageInfo & dstInfo,void * dst,size_t dstRowBytes,const Options & options,int * rowsDecoded)481 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
482 void* dst, size_t dstRowBytes,
483 const Options& options,
484 int* rowsDecoded) {
485 if (options.fSubset) {
486 // Subsets are not supported.
487 return kUnimplemented;
488 }
489
490 // Get a pointer to the decompress info since we will use it quite frequently
491 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
492
493 // Set the jump location for libjpeg errors
494 skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
495 if (setjmp(jmp)) {
496 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
497 }
498
499 const bool isProgressive = dinfo->progressive_mode;
500 if (isProgressive) {
501 dinfo->buffered_image = TRUE;
502 jpeg_start_decompress(dinfo);
503 } else if (!jpeg_start_decompress(dinfo)) {
504 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
505 }
506
507 // The recommended output buffer height should always be 1 in high quality modes.
508 // If it's not, we want to know because it means our strategy is not optimal.
509 SkASSERT(1 == dinfo->rec_outbuf_height);
510
511 if (needs_swizzler_to_convert_from_cmyk(dinfo->out_color_space,
512 this->getEncodedInfo().profile(), this->colorXform())) {
513 this->initializeSwizzler(dstInfo, options, true);
514 }
515
516 if (!this->allocateStorage(dstInfo)) {
517 return kInternalError;
518 }
519
520 if (isProgressive) {
521 // Keep consuming input until we can't anymore, and only output/read scanlines
522 // if there is at least one valid output.
523 int last_scan_completed = 0;
524 while (!jpeg_input_complete(dinfo)) {
525 // Call the progress monitor hook if present, to prevent decoder from hanging.
526 if (dinfo->progress) {
527 dinfo->progress->progress_monitor((j_common_ptr)dinfo);
528 }
529 const int res = jpeg_consume_input(dinfo);
530 if (res == JPEG_SUSPENDED) {
531 break;
532 }
533 if (res == JPEG_SCAN_COMPLETED) {
534 last_scan_completed = dinfo->input_scan_number;
535 }
536 }
537 if (last_scan_completed > 0) {
538 jpeg_start_output(dinfo, last_scan_completed);
539 int rows = 0;
540 SkCodec::Result readResult = this->readRows(dstInfo, dst, dstRowBytes,
541 dstInfo.height(), options, &rows);
542 // Checks if scan was called too many times to not stall the decoder.
543 jpeg_finish_output(dinfo);
544 if (readResult != kSuccess) {
545 return fDecoderMgr->returnFailure("readRows", readResult);
546 }
547 if (rows < dstInfo.height()) {
548 *rowsDecoded = rows;
549 return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
550 }
551 } else {
552 return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
553 }
554 } else {
555 // Baseline image
556 int rows = 0;
557 this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height(), options, &rows);
558 if (rows < dstInfo.height()) {
559 *rowsDecoded = rows;
560 return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
561 }
562 }
563
564 return kSuccess;
565 }
566
allocateStorage(const SkImageInfo & dstInfo)567 bool SkJpegCodec::allocateStorage(const SkImageInfo& dstInfo) {
568 int dstWidth = dstInfo.width();
569
570 size_t swizzleBytes = 0;
571 if (fSwizzler) {
572 swizzleBytes = get_row_bytes(fDecoderMgr->dinfo());
573 dstWidth = fSwizzler->swizzleWidth();
574 SkASSERT(!this->colorXform() || SkIsAlign4(swizzleBytes));
575 }
576
577 size_t xformBytes = 0;
578
579 if (this->colorXform() && sizeof(uint32_t) != dstInfo.bytesPerPixel()) {
580 xformBytes = dstWidth * sizeof(uint32_t);
581 }
582
583 size_t totalBytes = swizzleBytes + xformBytes;
584 if (totalBytes > 0) {
585 if (!fStorage.reset(totalBytes)) {
586 return false;
587 }
588 fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr;
589 fColorXformSrcRow = (xformBytes > 0) ?
590 SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr;
591 }
592 return true;
593 }
594
initializeSwizzler(const SkImageInfo & dstInfo,const Options & options,bool needsCMYKToRGB)595 void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& options,
596 bool needsCMYKToRGB) {
597 Options swizzlerOptions = options;
598 if (options.fSubset) {
599 // Use fSwizzlerSubset if this is a subset decode. This is necessary in the case
600 // where libjpeg-turbo provides a subset and then we need to subset it further.
601 // Also, verify that fSwizzlerSubset is initialized and valid.
602 SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fSubset->x() &&
603 fSwizzlerSubset.width() == options.fSubset->width());
604 swizzlerOptions.fSubset = &fSwizzlerSubset;
605 }
606
607 SkImageInfo swizzlerDstInfo = dstInfo;
608 if (this->colorXform()) {
609 // The color xform will be expecting RGBA 8888 input.
610 swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_8888_SkColorType);
611 }
612
613 if (needsCMYKToRGB) {
614 // The swizzler is used to convert to from CMYK.
615 // The swizzler does not use the width or height on SkEncodedInfo.
616 auto swizzlerInfo = SkEncodedInfo::Make(0, 0, SkEncodedInfo::kInvertedCMYK_Color,
617 SkEncodedInfo::kOpaque_Alpha, 8);
618 fSwizzler = SkSwizzler::Make(swizzlerInfo, nullptr, swizzlerDstInfo, swizzlerOptions);
619 } else {
620 int srcBPP = 0;
621 switch (fDecoderMgr->dinfo()->out_color_space) {
622 case JCS_EXT_RGBA:
623 case JCS_EXT_BGRA:
624 case JCS_CMYK:
625 srcBPP = 4;
626 break;
627 case JCS_RGB565:
628 srcBPP = 2;
629 break;
630 case JCS_GRAYSCALE:
631 srcBPP = 1;
632 break;
633 default:
634 SkASSERT(false);
635 break;
636 }
637 fSwizzler = SkSwizzler::MakeSimple(srcBPP, swizzlerDstInfo, swizzlerOptions);
638 }
639 SkASSERT(fSwizzler);
640 }
641
getSampler(bool createIfNecessary)642 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) {
643 if (!createIfNecessary || fSwizzler) {
644 SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcRow));
645 return fSwizzler.get();
646 }
647
648 bool needsCMYKToRGB = needs_swizzler_to_convert_from_cmyk(
649 fDecoderMgr->dinfo()->out_color_space, this->getEncodedInfo().profile(),
650 this->colorXform());
651 this->initializeSwizzler(this->dstInfo(), this->options(), needsCMYKToRGB);
652 if (!this->allocateStorage(this->dstInfo())) {
653 return nullptr;
654 }
655 return fSwizzler.get();
656 }
657
onStartScanlineDecode(const SkImageInfo & dstInfo,const Options & options)658 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
659 const Options& options) {
660 // Set the jump location for libjpeg errors
661 skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
662 if (setjmp(jmp)) {
663 SkCodecPrintf("setjmp: Error from libjpeg\n");
664 return kInvalidInput;
665 }
666
667 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) {
668 SkCodecPrintf("start decompress failed\n");
669 return kInvalidInput;
670 }
671
672 bool needsCMYKToRGB = needs_swizzler_to_convert_from_cmyk(
673 fDecoderMgr->dinfo()->out_color_space, this->getEncodedInfo().profile(),
674 this->colorXform());
675 if (options.fSubset) {
676 uint32_t startX = options.fSubset->x();
677 uint32_t width = options.fSubset->width();
678
679 // libjpeg-turbo may need to align startX to a multiple of the IDCT
680 // block size. If this is the case, it will decrease the value of
681 // startX to the appropriate alignment and also increase the value
682 // of width so that the right edge of the requested subset remains
683 // the same.
684 jpeg_crop_scanline(fDecoderMgr->dinfo(), &startX, &width);
685
686 SkASSERT(startX <= (uint32_t) options.fSubset->x());
687 SkASSERT(width >= (uint32_t) options.fSubset->width());
688 SkASSERT(startX + width >= (uint32_t) options.fSubset->right());
689
690 // Instruct the swizzler (if it is necessary) to further subset the
691 // output provided by libjpeg-turbo.
692 //
693 // We set this here (rather than in the if statement below), so that
694 // if (1) we don't need a swizzler for the subset, and (2) we need a
695 // swizzler for CMYK, the swizzler will still use the proper subset
696 // dimensions.
697 //
698 // Note that the swizzler will ignore the y and height parameters of
699 // the subset. Since the scanline decoder (and the swizzler) handle
700 // one row at a time, only the subsetting in the x-dimension matters.
701 fSwizzlerSubset.setXYWH(options.fSubset->x() - startX, 0,
702 options.fSubset->width(), options.fSubset->height());
703
704 // We will need a swizzler if libjpeg-turbo cannot provide the exact
705 // subset that we request.
706 if (startX != (uint32_t) options.fSubset->x() ||
707 width != (uint32_t) options.fSubset->width()) {
708 this->initializeSwizzler(dstInfo, options, needsCMYKToRGB);
709 }
710 }
711
712 // Make sure we have a swizzler if we are converting from CMYK.
713 if (!fSwizzler && needsCMYKToRGB) {
714 this->initializeSwizzler(dstInfo, options, true);
715 }
716
717 if (!this->allocateStorage(dstInfo)) {
718 return kInternalError;
719 }
720
721 return kSuccess;
722 }
723
onGetScanlines(void * dst,int count,size_t dstRowBytes)724 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
725 int rows = 0;
726 this->readRows(this->dstInfo(), dst, dstRowBytes, count, this->options(), &rows);
727 if (rows < count) {
728 // This allows us to skip calling jpeg_finish_decompress().
729 fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height();
730 }
731
732 return rows;
733 }
734
onSkipScanlines(int count)735 bool SkJpegCodec::onSkipScanlines(int count) {
736 // Set the jump location for libjpeg errors
737 skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
738 if (setjmp(jmp)) {
739 return fDecoderMgr->returnFalse("onSkipScanlines");
740 }
741
742 return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count);
743 }
744
is_yuv_supported(const jpeg_decompress_struct * dinfo,const SkJpegCodec & codec,const SkYUVAPixmapInfo::SupportedDataTypes * supportedDataTypes,SkYUVAPixmapInfo * yuvaPixmapInfo)745 static bool is_yuv_supported(const jpeg_decompress_struct* dinfo,
746 const SkJpegCodec& codec,
747 const SkYUVAPixmapInfo::SupportedDataTypes* supportedDataTypes,
748 SkYUVAPixmapInfo* yuvaPixmapInfo) {
749 // Scaling is not supported in raw data mode.
750 SkASSERT(dinfo->scale_num == dinfo->scale_denom);
751
752 // I can't imagine that this would ever change, but we do depend on it.
753 static_assert(8 == DCTSIZE, "DCTSIZE (defined in jpeg library) should always be 8.");
754
755 if (JCS_YCbCr != dinfo->jpeg_color_space) {
756 return false;
757 }
758
759 SkASSERT(3 == dinfo->num_components);
760 SkASSERT(dinfo->comp_info);
761
762 // It is possible to perform a YUV decode for any combination of
763 // horizontal and vertical sampling that is supported by
764 // libjpeg/libjpeg-turbo. However, we will start by supporting only the
765 // common cases (where U and V have samp_factors of one).
766 //
767 // The definition of samp_factor is kind of the opposite of what SkCodec
768 // thinks of as a sampling factor. samp_factor is essentially a
769 // multiplier, and the larger the samp_factor is, the more samples that
770 // there will be. Ex:
771 // U_plane_width = image_width * (U_h_samp_factor / max_h_samp_factor)
772 //
773 // Supporting cases where the samp_factors for U or V were larger than
774 // that of Y would be an extremely difficult change, given that clients
775 // allocate memory as if the size of the Y plane is always the size of the
776 // image. However, this case is very, very rare.
777 if ((1 != dinfo->comp_info[1].h_samp_factor) ||
778 (1 != dinfo->comp_info[1].v_samp_factor) ||
779 (1 != dinfo->comp_info[2].h_samp_factor) ||
780 (1 != dinfo->comp_info[2].v_samp_factor))
781 {
782 return false;
783 }
784
785 // Support all common cases of Y samp_factors.
786 // TODO (msarett): As mentioned above, it would be possible to support
787 // more combinations of samp_factors. The issues are:
788 // (1) Are there actually any images that are not covered
789 // by these cases?
790 // (2) How much complexity would be added to the
791 // implementation in order to support these rare
792 // cases?
793 int hSampY = dinfo->comp_info[0].h_samp_factor;
794 int vSampY = dinfo->comp_info[0].v_samp_factor;
795 SkASSERT(hSampY == dinfo->max_h_samp_factor);
796 SkASSERT(vSampY == dinfo->max_v_samp_factor);
797
798 SkYUVAInfo::Subsampling tempSubsampling;
799 if (1 == hSampY && 1 == vSampY) {
800 tempSubsampling = SkYUVAInfo::Subsampling::k444;
801 } else if (2 == hSampY && 1 == vSampY) {
802 tempSubsampling = SkYUVAInfo::Subsampling::k422;
803 } else if (2 == hSampY && 2 == vSampY) {
804 tempSubsampling = SkYUVAInfo::Subsampling::k420;
805 } else if (1 == hSampY && 2 == vSampY) {
806 tempSubsampling = SkYUVAInfo::Subsampling::k440;
807 } else if (4 == hSampY && 1 == vSampY) {
808 tempSubsampling = SkYUVAInfo::Subsampling::k411;
809 } else if (4 == hSampY && 2 == vSampY) {
810 tempSubsampling = SkYUVAInfo::Subsampling::k410;
811 } else {
812 return false;
813 }
814 if (supportedDataTypes &&
815 !supportedDataTypes->supported(SkYUVAInfo::PlaneConfig::kY_U_V,
816 SkYUVAPixmapInfo::DataType::kUnorm8)) {
817 return false;
818 }
819 if (yuvaPixmapInfo) {
820 SkColorType colorTypes[SkYUVAPixmapInfo::kMaxPlanes];
821 size_t rowBytes[SkYUVAPixmapInfo::kMaxPlanes];
822 for (int i = 0; i < 3; ++i) {
823 colorTypes[i] = kAlpha_8_SkColorType;
824 rowBytes[i] = dinfo->comp_info[i].width_in_blocks * DCTSIZE;
825 }
826 SkYUVAInfo yuvaInfo(codec.dimensions(),
827 SkYUVAInfo::PlaneConfig::kY_U_V,
828 tempSubsampling,
829 kJPEG_Full_SkYUVColorSpace,
830 codec.getOrigin(),
831 SkYUVAInfo::Siting::kCentered,
832 SkYUVAInfo::Siting::kCentered);
833 *yuvaPixmapInfo = SkYUVAPixmapInfo(yuvaInfo, colorTypes, rowBytes);
834 }
835 return true;
836 }
837
onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes & supportedDataTypes,SkYUVAPixmapInfo * yuvaPixmapInfo) const838 bool SkJpegCodec::onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
839 SkYUVAPixmapInfo* yuvaPixmapInfo) const {
840 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
841 return is_yuv_supported(dinfo, *this, &supportedDataTypes, yuvaPixmapInfo);
842 }
843
onGetYUVAPlanes(const SkYUVAPixmaps & yuvaPixmaps)844 SkCodec::Result SkJpegCodec::onGetYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps) {
845 // Get a pointer to the decompress info since we will use it quite frequently
846 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
847 if (!is_yuv_supported(dinfo, *this, nullptr, nullptr)) {
848 return fDecoderMgr->returnFailure("onGetYUVAPlanes", kInvalidInput);
849 }
850 // Set the jump location for libjpeg errors
851 skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
852 if (setjmp(jmp)) {
853 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
854 }
855
856 dinfo->raw_data_out = TRUE;
857 if (!jpeg_start_decompress(dinfo)) {
858 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
859 }
860
861 const std::array<SkPixmap, SkYUVAPixmaps::kMaxPlanes>& planes = yuvaPixmaps.planes();
862
863 #ifdef SK_DEBUG
864 {
865 // A previous implementation claims that the return value of is_yuv_supported()
866 // may change after calling jpeg_start_decompress(). It looks to me like this
867 // was caused by a bug in the old code, but we'll be safe and check here.
868 // Also check that pixmap properties agree with expectations.
869 SkYUVAPixmapInfo info;
870 SkASSERT(is_yuv_supported(dinfo, *this, nullptr, &info));
871 SkASSERT(info.yuvaInfo() == yuvaPixmaps.yuvaInfo());
872 for (int i = 0; i < info.numPlanes(); ++i) {
873 SkASSERT(planes[i].colorType() == kAlpha_8_SkColorType);
874 SkASSERT(info.planeInfo(i) == planes[i].info());
875 }
876 }
877 #endif
878
879 // Build a JSAMPIMAGE to handle output from libjpeg-turbo. A JSAMPIMAGE has
880 // a 2-D array of pixels for each of the components (Y, U, V) in the image.
881 // Cheat Sheet:
882 // JSAMPIMAGE == JSAMPLEARRAY* == JSAMPROW** == JSAMPLE***
883 JSAMPARRAY yuv[3];
884
885 // Set aside enough space for pointers to rows of Y, U, and V.
886 JSAMPROW rowptrs[2 * DCTSIZE + DCTSIZE + DCTSIZE];
887 yuv[0] = &rowptrs[0]; // Y rows (DCTSIZE or 2 * DCTSIZE)
888 yuv[1] = &rowptrs[2 * DCTSIZE]; // U rows (DCTSIZE)
889 yuv[2] = &rowptrs[3 * DCTSIZE]; // V rows (DCTSIZE)
890
891 // Initialize rowptrs.
892 int numYRowsPerBlock = DCTSIZE * dinfo->comp_info[0].v_samp_factor;
893 static_assert(sizeof(JSAMPLE) == 1);
894 for (int i = 0; i < numYRowsPerBlock; i++) {
895 rowptrs[i] = static_cast<JSAMPLE*>(planes[0].writable_addr()) + i* planes[0].rowBytes();
896 }
897 for (int i = 0; i < DCTSIZE; i++) {
898 rowptrs[i + 2 * DCTSIZE] =
899 static_cast<JSAMPLE*>(planes[1].writable_addr()) + i* planes[1].rowBytes();
900 rowptrs[i + 3 * DCTSIZE] =
901 static_cast<JSAMPLE*>(planes[2].writable_addr()) + i* planes[2].rowBytes();
902 }
903
904 // After each loop iteration, we will increment pointers to Y, U, and V.
905 size_t blockIncrementY = numYRowsPerBlock * planes[0].rowBytes();
906 size_t blockIncrementU = DCTSIZE * planes[1].rowBytes();
907 size_t blockIncrementV = DCTSIZE * planes[2].rowBytes();
908
909 uint32_t numRowsPerBlock = numYRowsPerBlock;
910
911 // We intentionally round down here, as this first loop will only handle
912 // full block rows. As a special case at the end, we will handle any
913 // remaining rows that do not make up a full block.
914 const int numIters = dinfo->output_height / numRowsPerBlock;
915 for (int i = 0; i < numIters; i++) {
916 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
917 if (linesRead < numRowsPerBlock) {
918 // FIXME: Handle incomplete YUV decodes without signalling an error.
919 return kInvalidInput;
920 }
921
922 // Update rowptrs.
923 for (int j = 0; j < numYRowsPerBlock; j++) {
924 rowptrs[j] += blockIncrementY;
925 }
926 for (int j = 0; j < DCTSIZE; j++) {
927 rowptrs[j + 2 * DCTSIZE] += blockIncrementU;
928 rowptrs[j + 3 * DCTSIZE] += blockIncrementV;
929 }
930 }
931
932 uint32_t remainingRows = dinfo->output_height - dinfo->output_scanline;
933 SkASSERT(remainingRows == dinfo->output_height % numRowsPerBlock);
934 SkASSERT(dinfo->output_scanline == numIters * numRowsPerBlock);
935 if (remainingRows > 0) {
936 // libjpeg-turbo needs memory to be padded by the block sizes. We will fulfill
937 // this requirement using an extra row buffer.
938 // FIXME: Should SkCodec have an extra memory buffer that can be shared among
939 // all of the implementations that use temporary/garbage memory?
940 AutoTMalloc<JSAMPLE> extraRow(planes[0].rowBytes());
941 for (int i = remainingRows; i < numYRowsPerBlock; i++) {
942 rowptrs[i] = extraRow.get();
943 }
944 int remainingUVRows = dinfo->comp_info[1].downsampled_height - DCTSIZE * numIters;
945 for (int i = remainingUVRows; i < DCTSIZE; i++) {
946 rowptrs[i + 2 * DCTSIZE] = extraRow.get();
947 rowptrs[i + 3 * DCTSIZE] = extraRow.get();
948 }
949
950 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
951 if (linesRead < remainingRows) {
952 // FIXME: Handle incomplete YUV decodes without signalling an error.
953 return kInvalidInput;
954 }
955 }
956
957 return kSuccess;
958 }
959
onGetGainmapCodec(SkGainmapInfo * info,std::unique_ptr<SkCodec> * gainmapCodec)960 bool SkJpegCodec::onGetGainmapCodec(SkGainmapInfo* info, std::unique_ptr<SkCodec>* gainmapCodec) {
961 std::unique_ptr<SkStream> stream;
962 if (!this->onGetGainmapInfo(info, &stream)) {
963 return false;
964 }
965 if (gainmapCodec) {
966 Result result;
967 *gainmapCodec = MakeFromStream(std::move(stream), &result);
968 if (!*gainmapCodec) {
969 return false;
970 }
971 }
972 return true;
973 }
974
onGetGainmapInfo(SkGainmapInfo * info,std::unique_ptr<SkStream> * gainmapImageStream)975 bool SkJpegCodec::onGetGainmapInfo(SkGainmapInfo* info,
976 std::unique_ptr<SkStream>* gainmapImageStream) {
977 #ifdef SK_CODEC_DECODES_JPEG_GAINMAPS
978 sk_sp<SkData> gainmap_data;
979 SkGainmapInfo gainmap_info;
980
981 auto metadataDecoder =
982 std::make_unique<SkJpegMetadataDecoderImpl>(get_sk_marker_list(fDecoderMgr->dinfo()));
983 if (!metadataDecoder->findGainmapImage(
984 fDecoderMgr->getSourceMgr(), gainmap_data, gainmap_info)) {
985 return false;
986 }
987
988 *info = gainmap_info;
989 *gainmapImageStream = SkMemoryStream::Make(gainmap_data);
990 return true;
991 #else
992 return false;
993 #endif // SK_CODEC_DECODES_JPEG_GAINMAPS
994 }
995
996 namespace SkJpegDecoder {
IsJpeg(const void * data,size_t len)997 bool IsJpeg(const void* data, size_t len) {
998 return SkJpegCodec::IsJpeg(data, len);
999 }
1000
Decode(std::unique_ptr<SkStream> stream,SkCodec::Result * outResult,SkCodecs::DecodeContext)1001 std::unique_ptr<SkCodec> Decode(std::unique_ptr<SkStream> stream,
1002 SkCodec::Result* outResult,
1003 SkCodecs::DecodeContext) {
1004 SkCodec::Result resultStorage;
1005 if (!outResult) {
1006 outResult = &resultStorage;
1007 }
1008 return SkJpegCodec::MakeFromStream(std::move(stream), outResult);
1009 }
1010
Decode(sk_sp<SkData> data,SkCodec::Result * outResult,SkCodecs::DecodeContext)1011 std::unique_ptr<SkCodec> Decode(sk_sp<SkData> data,
1012 SkCodec::Result* outResult,
1013 SkCodecs::DecodeContext) {
1014 if (!data) {
1015 if (outResult) {
1016 *outResult = SkCodec::kInvalidInput;
1017 }
1018 return nullptr;
1019 }
1020 return Decode(SkMemoryStream::Make(std::move(data)), outResult, nullptr);
1021 }
1022
1023 } // namespace SkJpegDecoder
1024