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