blob: 9f9f85c18a9f0b509805eda67a5f331a356072f4 [file] [log] [blame]
msarette16b04a2015-04-15 07:32:19 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkCodec.h"
mtkleine721a8e2016-02-06 19:12:23 -08009#include "SkMSAN.h"
msarette16b04a2015-04-15 07:32:19 -070010#include "SkJpegCodec.h"
11#include "SkJpegDecoderMgr.h"
msarette16b04a2015-04-15 07:32:19 -070012#include "SkCodecPriv.h"
13#include "SkColorPriv.h"
14#include "SkStream.h"
15#include "SkTemplates.h"
16#include "SkTypes.h"
17
msarett1c8a5872015-07-07 08:50:01 -070018// stdio is needed for libjpeg-turbo
msarette16b04a2015-04-15 07:32:19 -070019#include <stdio.h>
msarettc1d03122016-03-25 08:58:55 -070020#include "SkJpegUtility.h"
msarette16b04a2015-04-15 07:32:19 -070021
mtkleindc90b532016-07-28 14:45:28 -070022// This warning triggers false postives way too often in here.
23#if defined(__GNUC__) && !defined(__clang__)
24 #pragma GCC diagnostic ignored "-Wclobbered"
25#endif
26
msarette16b04a2015-04-15 07:32:19 -070027extern "C" {
28 #include "jerror.h"
msarette16b04a2015-04-15 07:32:19 -070029 #include "jpeglib.h"
30}
31
scroggodb30be22015-12-08 18:54:13 -080032bool SkJpegCodec::IsJpeg(const void* buffer, size_t bytesRead) {
msarette16b04a2015-04-15 07:32:19 -070033 static const uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF };
scroggodb30be22015-12-08 18:54:13 -080034 return bytesRead >= 3 && !memcmp(buffer, jpegSig, sizeof(jpegSig));
msarette16b04a2015-04-15 07:32:19 -070035}
36
msarett0e6274f2016-03-21 08:04:40 -070037static uint32_t get_endian_int(const uint8_t* data, bool littleEndian) {
38 if (littleEndian) {
39 return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | (data[0]);
40 }
41
42 return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]);
43}
44
45const uint32_t kExifHeaderSize = 14;
46const uint32_t kICCHeaderSize = 14;
47const uint32_t kExifMarker = JPEG_APP0 + 1;
48const uint32_t kICCMarker = JPEG_APP0 + 2;
49
50static bool is_orientation_marker(jpeg_marker_struct* marker, SkCodec::Origin* orientation) {
51 if (kExifMarker != marker->marker || marker->data_length < kExifHeaderSize) {
52 return false;
53 }
54
55 const uint8_t* data = marker->data;
56 static const uint8_t kExifSig[] { 'E', 'x', 'i', 'f', '\0' };
57 if (memcmp(data, kExifSig, sizeof(kExifSig))) {
58 return false;
59 }
60
61 bool littleEndian;
62 if (!is_valid_endian_marker(data + 6, &littleEndian)) {
63 return false;
64 }
65
66 // Get the offset from the start of the marker.
67 // Account for 'E', 'x', 'i', 'f', '\0', '<fill byte>'.
68 uint32_t offset = get_endian_int(data + 10, littleEndian);
69 offset += sizeof(kExifSig) + 1;
70
71 // Require that the marker is at least large enough to contain the number of entries.
72 if (marker->data_length < offset + 2) {
73 return false;
74 }
75 uint32_t numEntries = get_endian_short(data + offset, littleEndian);
76
77 // Tag (2 bytes), Datatype (2 bytes), Number of elements (4 bytes), Data (4 bytes)
78 const uint32_t kEntrySize = 12;
79 numEntries = SkTMin(numEntries, (marker->data_length - offset - 2) / kEntrySize);
80
81 // Advance the data to the start of the entries.
82 data += offset + 2;
83
84 const uint16_t kOriginTag = 0x112;
85 const uint16_t kOriginType = 3;
86 for (uint32_t i = 0; i < numEntries; i++, data += kEntrySize) {
87 uint16_t tag = get_endian_short(data, littleEndian);
88 uint16_t type = get_endian_short(data + 2, littleEndian);
89 uint32_t count = get_endian_int(data + 4, littleEndian);
90 if (kOriginTag == tag && kOriginType == type && 1 == count) {
91 uint16_t val = get_endian_short(data + 8, littleEndian);
92 if (0 < val && val <= SkCodec::kLast_Origin) {
93 *orientation = (SkCodec::Origin) val;
94 return true;
95 }
96 }
97 }
98
99 return false;
100}
101
102static SkCodec::Origin get_exif_orientation(jpeg_decompress_struct* dinfo) {
103 SkCodec::Origin orientation;
104 for (jpeg_marker_struct* marker = dinfo->marker_list; marker; marker = marker->next) {
105 if (is_orientation_marker(marker, &orientation)) {
106 return orientation;
107 }
108 }
109
110 return SkCodec::kDefault_Origin;
111}
112
113static bool is_icc_marker(jpeg_marker_struct* marker) {
114 if (kICCMarker != marker->marker || marker->data_length < kICCHeaderSize) {
115 return false;
116 }
117
118 static const uint8_t kICCSig[] { 'I', 'C', 'C', '_', 'P', 'R', 'O', 'F', 'I', 'L', 'E', '\0' };
119 return !memcmp(marker->data, kICCSig, sizeof(kICCSig));
120}
121
122/*
123 * ICC profiles may be stored using a sequence of multiple markers. We obtain the ICC profile
124 * in two steps:
125 * (1) Discover all ICC profile markers and verify that they are numbered properly.
126 * (2) Copy the data from each marker into a contiguous ICC profile.
127 */
msarett9876ac52016-06-01 14:47:18 -0700128static sk_sp<SkData> get_icc_profile(jpeg_decompress_struct* dinfo) {
msarett0e6274f2016-03-21 08:04:40 -0700129 // Note that 256 will be enough storage space since each markerIndex is stored in 8-bits.
130 jpeg_marker_struct* markerSequence[256];
131 memset(markerSequence, 0, sizeof(markerSequence));
132 uint8_t numMarkers = 0;
133 size_t totalBytes = 0;
134
135 // Discover any ICC markers and verify that they are numbered properly.
136 for (jpeg_marker_struct* marker = dinfo->marker_list; marker; marker = marker->next) {
137 if (is_icc_marker(marker)) {
138 // Verify that numMarkers is valid and consistent.
139 if (0 == numMarkers) {
140 numMarkers = marker->data[13];
141 if (0 == numMarkers) {
142 SkCodecPrintf("ICC Profile Error: numMarkers must be greater than zero.\n");
143 return nullptr;
144 }
145 } else if (numMarkers != marker->data[13]) {
146 SkCodecPrintf("ICC Profile Error: numMarkers must be consistent.\n");
147 return nullptr;
148 }
149
150 // Verify that the markerIndex is valid and unique. Note that zero is not
151 // a valid index.
152 uint8_t markerIndex = marker->data[12];
153 if (markerIndex == 0 || markerIndex > numMarkers) {
154 SkCodecPrintf("ICC Profile Error: markerIndex is invalid.\n");
155 return nullptr;
156 }
157 if (markerSequence[markerIndex]) {
158 SkCodecPrintf("ICC Profile Error: Duplicate value of markerIndex.\n");
159 return nullptr;
160 }
161 markerSequence[markerIndex] = marker;
162 SkASSERT(marker->data_length >= kICCHeaderSize);
163 totalBytes += marker->data_length - kICCHeaderSize;
164 }
165 }
166
167 if (0 == totalBytes) {
168 // No non-empty ICC profile markers were found.
169 return nullptr;
170 }
171
172 // Combine the ICC marker data into a contiguous profile.
msarett9876ac52016-06-01 14:47:18 -0700173 sk_sp<SkData> iccData = SkData::MakeUninitialized(totalBytes);
174 void* dst = iccData->writable_data();
msarett0e6274f2016-03-21 08:04:40 -0700175 for (uint32_t i = 1; i <= numMarkers; i++) {
176 jpeg_marker_struct* marker = markerSequence[i];
177 if (!marker) {
178 SkCodecPrintf("ICC Profile Error: Missing marker %d of %d.\n", i, numMarkers);
179 return nullptr;
180 }
181
182 void* src = SkTAddOffset<void>(marker->data, kICCHeaderSize);
183 size_t bytes = marker->data_length - kICCHeaderSize;
184 memcpy(dst, src, bytes);
185 dst = SkTAddOffset<void>(dst, bytes);
186 }
187
msarett9876ac52016-06-01 14:47:18 -0700188 return iccData;
msarett0e6274f2016-03-21 08:04:40 -0700189}
190
msarette16b04a2015-04-15 07:32:19 -0700191bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut,
192 JpegDecoderMgr** decoderMgrOut) {
193
194 // Create a JpegDecoderMgr to own all of the decompress information
halcanary385fe4d2015-08-26 13:07:48 -0700195 SkAutoTDelete<JpegDecoderMgr> decoderMgr(new JpegDecoderMgr(stream));
msarette16b04a2015-04-15 07:32:19 -0700196
197 // libjpeg errors will be caught and reported here
mtklein2f428962016-07-28 13:59:59 -0700198 if (setjmp(decoderMgr->getJmpBuf())) {
msarett50ce1f22016-07-29 06:23:33 -0700199 return decoderMgr->returnFalse("ReadHeader");
msarette16b04a2015-04-15 07:32:19 -0700200 }
201
202 // Initialize the decompress info and the source manager
203 decoderMgr->init();
204
msarett0e6274f2016-03-21 08:04:40 -0700205 // Instruct jpeg library to save the markers that we care about. Since
206 // the orientation and color profile will not change, we can skip this
207 // step on rewinds.
208 if (codecOut) {
209 jpeg_save_markers(decoderMgr->dinfo(), kExifMarker, 0xFFFF);
210 jpeg_save_markers(decoderMgr->dinfo(), kICCMarker, 0xFFFF);
211 }
212
msarette16b04a2015-04-15 07:32:19 -0700213 // Read the jpeg header
msarettfbccb592015-09-01 06:43:41 -0700214 if (JPEG_HEADER_OK != jpeg_read_header(decoderMgr->dinfo(), true)) {
msarett50ce1f22016-07-29 06:23:33 -0700215 return decoderMgr->returnFalse("ReadHeader");
msarette16b04a2015-04-15 07:32:19 -0700216 }
217
msarett0e6274f2016-03-21 08:04:40 -0700218 if (codecOut) {
msarettc30c4182016-04-20 11:53:35 -0700219 // Get the encoded color type
msarettac6c7502016-04-25 09:30:24 -0700220 SkEncodedInfo::Color color;
221 if (!decoderMgr->getEncodedColor(&color)) {
msarettc30c4182016-04-20 11:53:35 -0700222 return false;
223 }
msarette16b04a2015-04-15 07:32:19 -0700224
225 // Create image info object and the codec
msarettc30c4182016-04-20 11:53:35 -0700226 SkEncodedInfo info = SkEncodedInfo::Make(color, SkEncodedInfo::kOpaque_Alpha, 8);
msarett0e6274f2016-03-21 08:04:40 -0700227
228 Origin orientation = get_exif_orientation(decoderMgr->dinfo());
msarett9876ac52016-06-01 14:47:18 -0700229 sk_sp<SkData> iccData = get_icc_profile(decoderMgr->dinfo());
230 sk_sp<SkColorSpace> colorSpace = nullptr;
231 if (iccData) {
Brian Osman526972e2016-10-24 09:24:02 -0400232 colorSpace = SkColorSpace::MakeICC(iccData->data(), iccData->size());
msarett9876ac52016-06-01 14:47:18 -0700233 if (!colorSpace) {
234 SkCodecPrintf("Could not create SkColorSpace from ICC data.\n");
235 }
236 }
msarettf34cd632016-05-25 10:13:53 -0700237 if (!colorSpace) {
238 // Treat unmarked jpegs as sRGB.
Brian Osman526972e2016-10-24 09:24:02 -0400239 colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
msarettf34cd632016-05-25 10:13:53 -0700240 }
msarett0e6274f2016-03-21 08:04:40 -0700241
msarettc30c4182016-04-20 11:53:35 -0700242 const int width = decoderMgr->dinfo()->image_width;
243 const int height = decoderMgr->dinfo()->image_height;
msarettf34cd632016-05-25 10:13:53 -0700244 *codecOut = new SkJpegCodec(width, height, info, stream, decoderMgr.release(),
msarett9876ac52016-06-01 14:47:18 -0700245 std::move(colorSpace), orientation, std::move(iccData));
msarette16b04a2015-04-15 07:32:19 -0700246 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700247 SkASSERT(nullptr != decoderMgrOut);
mtklein18300a32016-03-16 13:53:35 -0700248 *decoderMgrOut = decoderMgr.release();
msarette16b04a2015-04-15 07:32:19 -0700249 }
250 return true;
251}
252
253SkCodec* SkJpegCodec::NewFromStream(SkStream* stream) {
254 SkAutoTDelete<SkStream> streamDeleter(stream);
halcanary96fcdcc2015-08-27 07:41:13 -0700255 SkCodec* codec = nullptr;
256 if (ReadHeader(stream, &codec, nullptr)) {
msarette16b04a2015-04-15 07:32:19 -0700257 // Codec has taken ownership of the stream, we do not need to delete it
258 SkASSERT(codec);
mtklein18300a32016-03-16 13:53:35 -0700259 streamDeleter.release();
msarette16b04a2015-04-15 07:32:19 -0700260 return codec;
261 }
halcanary96fcdcc2015-08-27 07:41:13 -0700262 return nullptr;
msarette16b04a2015-04-15 07:32:19 -0700263}
264
msarettc30c4182016-04-20 11:53:35 -0700265SkJpegCodec::SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
msarett9876ac52016-06-01 14:47:18 -0700266 JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origin,
267 sk_sp<SkData> iccData)
msarettf34cd632016-05-25 10:13:53 -0700268 : INHERITED(width, height, info, stream, std::move(colorSpace), origin)
msarette16b04a2015-04-15 07:32:19 -0700269 , fDecoderMgr(decoderMgr)
msarettfbccb592015-09-01 06:43:41 -0700270 , fReadyState(decoderMgr->dinfo()->global_state)
msarett50ce1f22016-07-29 06:23:33 -0700271 , fSwizzleSrcRow(nullptr)
272 , fColorXformSrcRow(nullptr)
msarett91c22b22016-02-22 12:27:46 -0800273 , fSwizzlerSubset(SkIRect::MakeEmpty())
msarett9876ac52016-06-01 14:47:18 -0700274 , fICCData(std::move(iccData))
msarette16b04a2015-04-15 07:32:19 -0700275{}
276
277/*
emmaleer8f4ba762015-08-14 07:44:46 -0700278 * Return the row bytes of a particular image type and width
279 */
msarett23e78d32016-02-06 15:58:50 -0800280static size_t get_row_bytes(const j_decompress_ptr dinfo) {
msarett70e418b2016-02-12 12:35:48 -0800281 const size_t colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 :
282 dinfo->out_color_components;
emmaleer8f4ba762015-08-14 07:44:46 -0700283 return dinfo->output_width * colorBytes;
284
285}
scroggoe7fc14b2015-10-02 13:14:46 -0700286
287/*
288 * Calculate output dimensions based on the provided factors.
289 *
290 * Not to be used on the actual jpeg_decompress_struct used for decoding, since it will
291 * incorrectly modify num_components.
292 */
293void calc_output_dimensions(jpeg_decompress_struct* dinfo, unsigned int num, unsigned int denom) {
294 dinfo->num_components = 0;
295 dinfo->scale_num = num;
296 dinfo->scale_denom = denom;
297 jpeg_calc_output_dimensions(dinfo);
298}
299
emmaleer8f4ba762015-08-14 07:44:46 -0700300/*
msarette16b04a2015-04-15 07:32:19 -0700301 * Return a valid set of output dimensions for this decoder, given an input scale
302 */
303SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const {
msarett1c8a5872015-07-07 08:50:01 -0700304 // 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
305 // support these as well
scroggoe7fc14b2015-10-02 13:14:46 -0700306 unsigned int num;
307 unsigned int denom = 8;
msarettfdb47572015-10-13 12:50:14 -0700308 if (desiredScale >= 0.9375) {
msarett1c8a5872015-07-07 08:50:01 -0700309 num = 8;
msarettfdb47572015-10-13 12:50:14 -0700310 } else if (desiredScale >= 0.8125) {
msarett1c8a5872015-07-07 08:50:01 -0700311 num = 7;
msarettfdb47572015-10-13 12:50:14 -0700312 } else if (desiredScale >= 0.6875f) {
msarett1c8a5872015-07-07 08:50:01 -0700313 num = 6;
msarettfdb47572015-10-13 12:50:14 -0700314 } else if (desiredScale >= 0.5625f) {
msarett1c8a5872015-07-07 08:50:01 -0700315 num = 5;
msarettfdb47572015-10-13 12:50:14 -0700316 } else if (desiredScale >= 0.4375f) {
msarett1c8a5872015-07-07 08:50:01 -0700317 num = 4;
msarettfdb47572015-10-13 12:50:14 -0700318 } else if (desiredScale >= 0.3125f) {
msarett1c8a5872015-07-07 08:50:01 -0700319 num = 3;
msarettfdb47572015-10-13 12:50:14 -0700320 } else if (desiredScale >= 0.1875f) {
msarett1c8a5872015-07-07 08:50:01 -0700321 num = 2;
msarette16b04a2015-04-15 07:32:19 -0700322 } else {
msarett1c8a5872015-07-07 08:50:01 -0700323 num = 1;
msarette16b04a2015-04-15 07:32:19 -0700324 }
325
326 // Set up a fake decompress struct in order to use libjpeg to calculate output dimensions
327 jpeg_decompress_struct dinfo;
mtkleinf7aaadb2015-04-16 06:09:27 -0700328 sk_bzero(&dinfo, sizeof(dinfo));
msarette16b04a2015-04-15 07:32:19 -0700329 dinfo.image_width = this->getInfo().width();
330 dinfo.image_height = this->getInfo().height();
msarettfbccb592015-09-01 06:43:41 -0700331 dinfo.global_state = fReadyState;
scroggoe7fc14b2015-10-02 13:14:46 -0700332 calc_output_dimensions(&dinfo, num, denom);
msarette16b04a2015-04-15 07:32:19 -0700333
334 // Return the calculated output dimensions for the given scale
335 return SkISize::Make(dinfo.output_width, dinfo.output_height);
336}
337
scroggob427db12015-08-12 07:24:13 -0700338bool SkJpegCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -0700339 JpegDecoderMgr* decoderMgr = nullptr;
340 if (!ReadHeader(this->stream(), nullptr, &decoderMgr)) {
msarett50ce1f22016-07-29 06:23:33 -0700341 return fDecoderMgr->returnFalse("onRewind");
msarett97fdea62015-04-29 08:17:15 -0700342 }
halcanary96fcdcc2015-08-27 07:41:13 -0700343 SkASSERT(nullptr != decoderMgr);
scroggob427db12015-08-12 07:24:13 -0700344 fDecoderMgr.reset(decoderMgr);
msarett2812f032016-07-18 15:56:08 -0700345
346 fSwizzler.reset(nullptr);
msarett50ce1f22016-07-29 06:23:33 -0700347 fSwizzleSrcRow = nullptr;
348 fColorXformSrcRow = nullptr;
msarett2812f032016-07-18 15:56:08 -0700349 fStorage.reset();
350
scroggob427db12015-08-12 07:24:13 -0700351 return true;
msarett97fdea62015-04-29 08:17:15 -0700352}
353
354/*
msarett1c8a5872015-07-07 08:50:01 -0700355 * Checks if the conversion between the input image and the requested output
356 * image has been implemented
357 * Sets the output color space
358 */
msarett2ecc35f2016-09-08 11:55:16 -0700359bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dstInfo) {
msarett50ce1f22016-07-29 06:23:33 -0700360 if (kUnknown_SkAlphaType == dstInfo.alphaType()) {
msarett1c8a5872015-07-07 08:50:01 -0700361 return false;
362 }
363
msarett50ce1f22016-07-29 06:23:33 -0700364 if (kOpaque_SkAlphaType != dstInfo.alphaType()) {
scroggoc5560be2016-02-03 09:42:42 -0800365 SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
366 "- it is being decoded as non-opaque, which will draw slower\n");
367 }
368
msarett50ce1f22016-07-29 06:23:33 -0700369 // Check if we will decode to CMYK. libjpeg-turbo does not convert CMYK to RGBA, so
370 // we must do it ourselves.
371 J_COLOR_SPACE encodedColorType = fDecoderMgr->dinfo()->jpeg_color_space;
372 bool isCMYK = (JCS_CMYK == encodedColorType || JCS_YCCK == encodedColorType);
msarett1c8a5872015-07-07 08:50:01 -0700373
374 // Check for valid color types and set the output color space
msarett50ce1f22016-07-29 06:23:33 -0700375 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -0700376 case kRGBA_8888_SkColorType:
msarett1c8a5872015-07-07 08:50:01 -0700377 if (isCMYK) {
378 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
379 } else {
msarettf25bff92016-07-21 12:00:24 -0700380 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
msarett34e0ec42016-04-22 16:27:24 -0700381 }
382 return true;
383 case kBGRA_8888_SkColorType:
384 if (isCMYK) {
385 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
Matt Sarett313c4632016-10-20 12:35:23 -0400386 } else if (this->colorXform()) {
msarett50ce1f22016-07-29 06:23:33 -0700387 // Our color transformation code requires RGBA order inputs, but it'll swizzle
388 // to BGRA for us.
389 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
msarett34e0ec42016-04-22 16:27:24 -0700390 } else {
msarettf25bff92016-07-21 12:00:24 -0700391 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA;
msarett1c8a5872015-07-07 08:50:01 -0700392 }
393 return true;
394 case kRGB_565_SkColorType:
Matt Sarett313c4632016-10-20 12:35:23 -0400395 if (this->colorXform()) {
msarett50ce1f22016-07-29 06:23:33 -0700396 return false;
397 }
398
msarett1c8a5872015-07-07 08:50:01 -0700399 if (isCMYK) {
scroggoef27d892015-10-23 09:29:22 -0700400 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
msarett1c8a5872015-07-07 08:50:01 -0700401 } else {
msarett8ff6ca62015-09-18 12:06:04 -0700402 fDecoderMgr->dinfo()->dither_mode = JDITHER_NONE;
msarett1c8a5872015-07-07 08:50:01 -0700403 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565;
404 }
405 return true;
406 case kGray_8_SkColorType:
Matt Sarett313c4632016-10-20 12:35:23 -0400407 if (this->colorXform() || JCS_GRAYSCALE != encodedColorType) {
msarett39979d82016-07-28 17:11:18 -0700408 return false;
msarett50ce1f22016-07-29 06:23:33 -0700409 }
410
411 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE;
412 return true;
413 case kRGBA_F16_SkColorType:
Matt Sarett313c4632016-10-20 12:35:23 -0400414 SkASSERT(this->colorXform());
415
msarett2ecc35f2016-09-08 11:55:16 -0700416 if (!dstInfo.colorSpace()->gammaIsLinear()) {
417 return false;
418 }
msarett50ce1f22016-07-29 06:23:33 -0700419
420 if (isCMYK) {
421 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
msarett1c8a5872015-07-07 08:50:01 -0700422 } else {
msarett50ce1f22016-07-29 06:23:33 -0700423 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
msarett1c8a5872015-07-07 08:50:01 -0700424 }
425 return true;
426 default:
427 return false;
428 }
429}
430
431/*
mtkleine721a8e2016-02-06 19:12:23 -0800432 * Checks if we can natively scale to the requested dimensions and natively scales the
emmaleer8f4ba762015-08-14 07:44:46 -0700433 * dimensions if possible
msarett97fdea62015-04-29 08:17:15 -0700434 */
scroggoe7fc14b2015-10-02 13:14:46 -0700435bool SkJpegCodec::onDimensionsSupported(const SkISize& size) {
mtklein2f428962016-07-28 13:59:59 -0700436 if (setjmp(fDecoderMgr->getJmpBuf())) {
msarett50ce1f22016-07-29 06:23:33 -0700437 return fDecoderMgr->returnFalse("onDimensionsSupported");
scroggoe7fc14b2015-10-02 13:14:46 -0700438 }
439
440 const unsigned int dstWidth = size.width();
441 const unsigned int dstHeight = size.height();
442
443 // Set up a fake decompress struct in order to use libjpeg to calculate output dimensions
444 // FIXME: Why is this necessary?
445 jpeg_decompress_struct dinfo;
446 sk_bzero(&dinfo, sizeof(dinfo));
447 dinfo.image_width = this->getInfo().width();
448 dinfo.image_height = this->getInfo().height();
449 dinfo.global_state = fReadyState;
450
msarett1c8a5872015-07-07 08:50:01 -0700451 // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1
scroggoe7fc14b2015-10-02 13:14:46 -0700452 unsigned int num = 8;
453 const unsigned int denom = 8;
454 calc_output_dimensions(&dinfo, num, denom);
455 while (dinfo.output_width != dstWidth || dinfo.output_height != dstHeight) {
msarett97fdea62015-04-29 08:17:15 -0700456
457 // Return a failure if we have tried all of the possible scales
scroggoe7fc14b2015-10-02 13:14:46 -0700458 if (1 == num || dstWidth > dinfo.output_width || dstHeight > dinfo.output_height) {
emmaleer8f4ba762015-08-14 07:44:46 -0700459 return false;
msarett97fdea62015-04-29 08:17:15 -0700460 }
461
462 // Try the next scale
scroggoe7fc14b2015-10-02 13:14:46 -0700463 num -= 1;
464 calc_output_dimensions(&dinfo, num, denom);
msarett97fdea62015-04-29 08:17:15 -0700465 }
scroggoe7fc14b2015-10-02 13:14:46 -0700466
467 fDecoderMgr->dinfo()->scale_num = num;
468 fDecoderMgr->dinfo()->scale_denom = denom;
msarett97fdea62015-04-29 08:17:15 -0700469 return true;
470}
471
msarett50ce1f22016-07-29 06:23:33 -0700472int SkJpegCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count) {
473 // Set the jump location for libjpeg-turbo errors
474 if (setjmp(fDecoderMgr->getJmpBuf())) {
475 return 0;
476 }
477
478 // When fSwizzleSrcRow is non-null, it means that we need to swizzle. In this case,
479 // we will always decode into fSwizzlerSrcRow before swizzling into the next buffer.
480 // We can never swizzle "in place" because the swizzler may perform sampling and/or
481 // subsetting.
482 // When fColorXformSrcRow is non-null, it means that we need to color xform and that
483 // we cannot color xform "in place" (many times we can, but not when the dst is F16).
Matt Sarett313c4632016-10-20 12:35:23 -0400484 // In this case, we will color xform from fColorXformSrcRow into the dst.
msarett50ce1f22016-07-29 06:23:33 -0700485 JSAMPLE* decodeDst = (JSAMPLE*) dst;
486 uint32_t* swizzleDst = (uint32_t*) dst;
487 size_t decodeDstRowBytes = rowBytes;
488 size_t swizzleDstRowBytes = rowBytes;
msarett35bb74b2016-08-22 07:41:28 -0700489 int dstWidth = dstInfo.width();
msarett50ce1f22016-07-29 06:23:33 -0700490 if (fSwizzleSrcRow && fColorXformSrcRow) {
491 decodeDst = (JSAMPLE*) fSwizzleSrcRow;
492 swizzleDst = fColorXformSrcRow;
493 decodeDstRowBytes = 0;
494 swizzleDstRowBytes = 0;
msarett35bb74b2016-08-22 07:41:28 -0700495 dstWidth = fSwizzler->swizzleWidth();
msarett50ce1f22016-07-29 06:23:33 -0700496 } else if (fColorXformSrcRow) {
497 decodeDst = (JSAMPLE*) fColorXformSrcRow;
498 swizzleDst = fColorXformSrcRow;
499 decodeDstRowBytes = 0;
500 swizzleDstRowBytes = 0;
501 } else if (fSwizzleSrcRow) {
502 decodeDst = (JSAMPLE*) fSwizzleSrcRow;
503 decodeDstRowBytes = 0;
msarett35bb74b2016-08-22 07:41:28 -0700504 dstWidth = fSwizzler->swizzleWidth();
msarett50ce1f22016-07-29 06:23:33 -0700505 }
506
507 for (int y = 0; y < count; y++) {
508 uint32_t lines = jpeg_read_scanlines(fDecoderMgr->dinfo(), &decodeDst, 1);
509 size_t srcRowBytes = get_row_bytes(fDecoderMgr->dinfo());
510 sk_msan_mark_initialized(decodeDst, decodeDst + srcRowBytes, "skbug.com/4550");
511 if (0 == lines) {
512 return y;
513 }
514
515 if (fSwizzler) {
516 fSwizzler->swizzle(swizzleDst, decodeDst);
517 }
518
Matt Sarett313c4632016-10-20 12:35:23 -0400519 if (this->colorXform()) {
520 SkAssertResult(this->colorXform()->apply(select_xform_format(dstInfo.colorType()), dst,
521 SkColorSpaceXform::kRGBA_8888_ColorFormat, swizzleDst, dstWidth,
522 kOpaque_SkAlphaType));
msarett50ce1f22016-07-29 06:23:33 -0700523 dst = SkTAddOffset<void>(dst, rowBytes);
524 }
525
526 decodeDst = SkTAddOffset<JSAMPLE>(decodeDst, decodeDstRowBytes);
527 swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes);
528 }
529
530 return count;
531}
532
msarett97fdea62015-04-29 08:17:15 -0700533/*
msarette16b04a2015-04-15 07:32:19 -0700534 * Performs the jpeg decode
535 */
536SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
537 void* dst, size_t dstRowBytes,
msarette6dd0042015-10-09 11:07:34 -0700538 const Options& options, SkPMColor*, int*,
539 int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700540 if (options.fSubset) {
541 // Subsets are not supported.
542 return kUnimplemented;
543 }
544
msarette16b04a2015-04-15 07:32:19 -0700545 // Get a pointer to the decompress info since we will use it quite frequently
546 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
547
548 // Set the jump location for libjpeg errors
mtklein2f428962016-07-28 13:59:59 -0700549 if (setjmp(fDecoderMgr->getJmpBuf())) {
msarette16b04a2015-04-15 07:32:19 -0700550 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
551 }
552
Matt Sarett313c4632016-10-20 12:35:23 -0400553 if (!this->initializeColorXform(dstInfo)) {
554 return kInvalidConversion;
555 }
msarett50ce1f22016-07-29 06:23:33 -0700556
msarett2ecc35f2016-09-08 11:55:16 -0700557 // Check if we can decode to the requested destination and set the output color space
558 if (!this->setOutputColorSpace(dstInfo)) {
559 return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConversion);
msarett85c922a2016-09-08 10:54:34 -0700560 }
561
msarettfbccb592015-09-01 06:43:41 -0700562 if (!jpeg_start_decompress(dinfo)) {
msarette16b04a2015-04-15 07:32:19 -0700563 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
564 }
565
msarett1c8a5872015-07-07 08:50:01 -0700566 // The recommended output buffer height should always be 1 in high quality modes.
567 // If it's not, we want to know because it means our strategy is not optimal.
568 SkASSERT(1 == dinfo->rec_outbuf_height);
msarette16b04a2015-04-15 07:32:19 -0700569
msarett70e418b2016-02-12 12:35:48 -0800570 J_COLOR_SPACE colorSpace = dinfo->out_color_space;
mtkleinda19f6f2016-08-23 11:49:29 -0700571 if (JCS_CMYK == colorSpace) {
scroggoef27d892015-10-23 09:29:22 -0700572 this->initializeSwizzler(dstInfo, options);
573 }
574
msarett50ce1f22016-07-29 06:23:33 -0700575 this->allocateStorage(dstInfo);
scroggoef27d892015-10-23 09:29:22 -0700576
msarett50ce1f22016-07-29 06:23:33 -0700577 int rows = this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height());
578 if (rows < dstInfo.height()) {
579 *rowsDecoded = rows;
580 return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
msarette16b04a2015-04-15 07:32:19 -0700581 }
msarette16b04a2015-04-15 07:32:19 -0700582
583 return kSuccess;
584}
msarett97fdea62015-04-29 08:17:15 -0700585
msarett50ce1f22016-07-29 06:23:33 -0700586void SkJpegCodec::allocateStorage(const SkImageInfo& dstInfo) {
msarett35bb74b2016-08-22 07:41:28 -0700587 int dstWidth = dstInfo.width();
588
msarett50ce1f22016-07-29 06:23:33 -0700589 size_t swizzleBytes = 0;
590 if (fSwizzler) {
591 swizzleBytes = get_row_bytes(fDecoderMgr->dinfo());
msarett35bb74b2016-08-22 07:41:28 -0700592 dstWidth = fSwizzler->swizzleWidth();
Matt Sarett313c4632016-10-20 12:35:23 -0400593 SkASSERT(!this->colorXform() || SkIsAlign4(swizzleBytes));
msarett50ce1f22016-07-29 06:23:33 -0700594 }
595
596 size_t xformBytes = 0;
597 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
Matt Sarett313c4632016-10-20 12:35:23 -0400598 SkASSERT(this->colorXform());
msarett35bb74b2016-08-22 07:41:28 -0700599 xformBytes = dstWidth * sizeof(uint32_t);
msarett50ce1f22016-07-29 06:23:33 -0700600 }
601
602 size_t totalBytes = swizzleBytes + xformBytes;
603 if (totalBytes > 0) {
604 fStorage.reset(totalBytes);
605 fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr;
606 fColorXformSrcRow = (xformBytes > 0) ?
607 SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr;
608 }
609}
610
msarettfdb47572015-10-13 12:50:14 -0700611void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& options) {
msaretta45a6682016-04-22 13:18:37 -0700612 // libjpeg-turbo may have already performed color conversion. We must indicate the
613 // appropriate format to the swizzler.
614 SkEncodedInfo swizzlerInfo = this->getEncodedInfo();
msarett68758ae2016-04-25 11:41:15 -0700615 bool preSwizzled = true;
mtkleinda19f6f2016-08-23 11:49:29 -0700616 if (JCS_CMYK == fDecoderMgr->dinfo()->out_color_space) {
617 preSwizzled = false;
618 swizzlerInfo = SkEncodedInfo::Make(SkEncodedInfo::kInvertedCMYK_Color,
619 swizzlerInfo.alpha(),
620 swizzlerInfo.bitsPerComponent());
msarett70e418b2016-02-12 12:35:48 -0800621 }
622
msarett91c22b22016-02-22 12:27:46 -0800623 Options swizzlerOptions = options;
624 if (options.fSubset) {
625 // Use fSwizzlerSubset if this is a subset decode. This is necessary in the case
626 // where libjpeg-turbo provides a subset and then we need to subset it further.
627 // Also, verify that fSwizzlerSubset is initialized and valid.
628 SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fSubset->x() &&
629 fSwizzlerSubset.width() == options.fSubset->width());
630 swizzlerOptions.fSubset = &fSwizzlerSubset;
631 }
msarett68758ae2016-04-25 11:41:15 -0700632 fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, nullptr, dstInfo, swizzlerOptions,
633 nullptr, preSwizzled));
msarettb30d6982016-02-15 10:18:45 -0800634 SkASSERT(fSwizzler);
msarett50ce1f22016-07-29 06:23:33 -0700635}
636
msarettfdb47572015-10-13 12:50:14 -0700637SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) {
638 if (!createIfNecessary || fSwizzler) {
msarett50ce1f22016-07-29 06:23:33 -0700639 SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcRow));
msarettfdb47572015-10-13 12:50:14 -0700640 return fSwizzler;
641 }
642
643 this->initializeSwizzler(this->dstInfo(), this->options());
msarett50ce1f22016-07-29 06:23:33 -0700644 this->allocateStorage(this->dstInfo());
scroggoe7fc14b2015-10-02 13:14:46 -0700645 return fSwizzler;
scroggo46c57472015-09-30 08:57:13 -0700646}
scroggo1c005e42015-08-04 09:24:45 -0700647
scroggo46c57472015-09-30 08:57:13 -0700648SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
649 const Options& options, SkPMColor ctable[], int* ctableCount) {
scroggo46c57472015-09-30 08:57:13 -0700650 // Set the jump location for libjpeg errors
mtklein2f428962016-07-28 13:59:59 -0700651 if (setjmp(fDecoderMgr->getJmpBuf())) {
scroggo46c57472015-09-30 08:57:13 -0700652 SkCodecPrintf("setjmp: Error from libjpeg\n");
653 return kInvalidInput;
654 }
655
Matt Sarett313c4632016-10-20 12:35:23 -0400656 if (!this->initializeColorXform(dstInfo)) {
657 return kInvalidConversion;
658 }
msarett85c922a2016-09-08 10:54:34 -0700659
msarett2ecc35f2016-09-08 11:55:16 -0700660 // Check if we can decode to the requested destination and set the output color space
661 if (!this->setOutputColorSpace(dstInfo)) {
662 return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConversion);
msarett50ce1f22016-07-29 06:23:33 -0700663 }
664
scroggo46c57472015-09-30 08:57:13 -0700665 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) {
666 SkCodecPrintf("start decompress failed\n");
667 return kInvalidInput;
668 }
669
msarett91c22b22016-02-22 12:27:46 -0800670 if (options.fSubset) {
671 uint32_t startX = options.fSubset->x();
672 uint32_t width = options.fSubset->width();
673
674 // libjpeg-turbo may need to align startX to a multiple of the IDCT
675 // block size. If this is the case, it will decrease the value of
676 // startX to the appropriate alignment and also increase the value
677 // of width so that the right edge of the requested subset remains
678 // the same.
679 jpeg_crop_scanline(fDecoderMgr->dinfo(), &startX, &width);
680
681 SkASSERT(startX <= (uint32_t) options.fSubset->x());
682 SkASSERT(width >= (uint32_t) options.fSubset->width());
683 SkASSERT(startX + width >= (uint32_t) options.fSubset->right());
684
685 // Instruct the swizzler (if it is necessary) to further subset the
686 // output provided by libjpeg-turbo.
687 //
688 // We set this here (rather than in the if statement below), so that
689 // if (1) we don't need a swizzler for the subset, and (2) we need a
690 // swizzler for CMYK, the swizzler will still use the proper subset
691 // dimensions.
692 //
693 // Note that the swizzler will ignore the y and height parameters of
694 // the subset. Since the scanline decoder (and the swizzler) handle
695 // one row at a time, only the subsetting in the x-dimension matters.
696 fSwizzlerSubset.setXYWH(options.fSubset->x() - startX, 0,
697 options.fSubset->width(), options.fSubset->height());
698
699 // We will need a swizzler if libjpeg-turbo cannot provide the exact
700 // subset that we request.
701 if (startX != (uint32_t) options.fSubset->x() ||
702 width != (uint32_t) options.fSubset->width()) {
703 this->initializeSwizzler(dstInfo, options);
704 }
705 }
706
707 // Make sure we have a swizzler if we are converting from CMYK.
708 if (!fSwizzler && JCS_CMYK == fDecoderMgr->dinfo()->out_color_space) {
709 this->initializeSwizzler(dstInfo, options);
710 }
msarettfdb47572015-10-13 12:50:14 -0700711
msarett50ce1f22016-07-29 06:23:33 -0700712 this->allocateStorage(dstInfo);
713
scroggo46c57472015-09-30 08:57:13 -0700714 return kSuccess;
715}
716
mtkleine721a8e2016-02-06 19:12:23 -0800717int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
msarett50ce1f22016-07-29 06:23:33 -0700718 int rows = this->readRows(this->dstInfo(), dst, dstRowBytes, count);
719 if (rows < count) {
720 // This allows us to skip calling jpeg_finish_decompress().
721 fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height();
scroggo46c57472015-09-30 08:57:13 -0700722 }
723
msarett50ce1f22016-07-29 06:23:33 -0700724 return rows;
scroggo46c57472015-09-30 08:57:13 -0700725}
msarett97fdea62015-04-29 08:17:15 -0700726
msarette6dd0042015-10-09 11:07:34 -0700727bool SkJpegCodec::onSkipScanlines(int count) {
scroggo46c57472015-09-30 08:57:13 -0700728 // Set the jump location for libjpeg errors
mtklein2f428962016-07-28 13:59:59 -0700729 if (setjmp(fDecoderMgr->getJmpBuf())) {
msarett50ce1f22016-07-29 06:23:33 -0700730 return fDecoderMgr->returnFalse("onSkipScanlines");
msarett97fdea62015-04-29 08:17:15 -0700731 }
732
msarettf724b992015-10-15 06:41:06 -0700733 return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count);
msarett97fdea62015-04-29 08:17:15 -0700734}
msarettb714fb02016-01-22 14:46:42 -0800735
736static bool is_yuv_supported(jpeg_decompress_struct* dinfo) {
737 // Scaling is not supported in raw data mode.
738 SkASSERT(dinfo->scale_num == dinfo->scale_denom);
739
740 // I can't imagine that this would ever change, but we do depend on it.
741 static_assert(8 == DCTSIZE, "DCTSIZE (defined in jpeg library) should always be 8.");
742
743 if (JCS_YCbCr != dinfo->jpeg_color_space) {
744 return false;
745 }
746
747 SkASSERT(3 == dinfo->num_components);
748 SkASSERT(dinfo->comp_info);
749
750 // It is possible to perform a YUV decode for any combination of
751 // horizontal and vertical sampling that is supported by
752 // libjpeg/libjpeg-turbo. However, we will start by supporting only the
753 // common cases (where U and V have samp_factors of one).
754 //
755 // The definition of samp_factor is kind of the opposite of what SkCodec
756 // thinks of as a sampling factor. samp_factor is essentially a
757 // multiplier, and the larger the samp_factor is, the more samples that
758 // there will be. Ex:
759 // U_plane_width = image_width * (U_h_samp_factor / max_h_samp_factor)
760 //
761 // Supporting cases where the samp_factors for U or V were larger than
762 // that of Y would be an extremely difficult change, given that clients
763 // allocate memory as if the size of the Y plane is always the size of the
764 // image. However, this case is very, very rare.
msarett7c87cf42016-03-04 06:23:20 -0800765 if ((1 != dinfo->comp_info[1].h_samp_factor) ||
766 (1 != dinfo->comp_info[1].v_samp_factor) ||
767 (1 != dinfo->comp_info[2].h_samp_factor) ||
768 (1 != dinfo->comp_info[2].v_samp_factor))
769 {
msarettb714fb02016-01-22 14:46:42 -0800770 return false;
771 }
772
773 // Support all common cases of Y samp_factors.
774 // TODO (msarett): As mentioned above, it would be possible to support
775 // more combinations of samp_factors. The issues are:
776 // (1) Are there actually any images that are not covered
777 // by these cases?
778 // (2) How much complexity would be added to the
779 // implementation in order to support these rare
780 // cases?
781 int hSampY = dinfo->comp_info[0].h_samp_factor;
782 int vSampY = dinfo->comp_info[0].v_samp_factor;
783 return (1 == hSampY && 1 == vSampY) ||
784 (2 == hSampY && 1 == vSampY) ||
785 (2 == hSampY && 2 == vSampY) ||
786 (1 == hSampY && 2 == vSampY) ||
787 (4 == hSampY && 1 == vSampY) ||
788 (4 == hSampY && 2 == vSampY);
789}
790
msarett4984c3c2016-03-10 05:44:43 -0800791bool SkJpegCodec::onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const {
msarettb714fb02016-01-22 14:46:42 -0800792 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
793 if (!is_yuv_supported(dinfo)) {
794 return false;
795 }
796
msarett4984c3c2016-03-10 05:44:43 -0800797 sizeInfo->fSizes[SkYUVSizeInfo::kY].set(dinfo->comp_info[0].downsampled_width,
798 dinfo->comp_info[0].downsampled_height);
799 sizeInfo->fSizes[SkYUVSizeInfo::kU].set(dinfo->comp_info[1].downsampled_width,
800 dinfo->comp_info[1].downsampled_height);
801 sizeInfo->fSizes[SkYUVSizeInfo::kV].set(dinfo->comp_info[2].downsampled_width,
802 dinfo->comp_info[2].downsampled_height);
803 sizeInfo->fWidthBytes[SkYUVSizeInfo::kY] = dinfo->comp_info[0].width_in_blocks * DCTSIZE;
804 sizeInfo->fWidthBytes[SkYUVSizeInfo::kU] = dinfo->comp_info[1].width_in_blocks * DCTSIZE;
805 sizeInfo->fWidthBytes[SkYUVSizeInfo::kV] = dinfo->comp_info[2].width_in_blocks * DCTSIZE;
msarettb714fb02016-01-22 14:46:42 -0800806
807 if (colorSpace) {
808 *colorSpace = kJPEG_SkYUVColorSpace;
809 }
810
811 return true;
812}
813
msarett4984c3c2016-03-10 05:44:43 -0800814SkCodec::Result SkJpegCodec::onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) {
815 SkYUVSizeInfo defaultInfo;
msarettb714fb02016-01-22 14:46:42 -0800816
817 // This will check is_yuv_supported(), so we don't need to here.
818 bool supportsYUV = this->onQueryYUV8(&defaultInfo, nullptr);
msarett4984c3c2016-03-10 05:44:43 -0800819 if (!supportsYUV ||
820 sizeInfo.fSizes[SkYUVSizeInfo::kY] != defaultInfo.fSizes[SkYUVSizeInfo::kY] ||
821 sizeInfo.fSizes[SkYUVSizeInfo::kU] != defaultInfo.fSizes[SkYUVSizeInfo::kU] ||
822 sizeInfo.fSizes[SkYUVSizeInfo::kV] != defaultInfo.fSizes[SkYUVSizeInfo::kV] ||
823 sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] < defaultInfo.fWidthBytes[SkYUVSizeInfo::kY] ||
824 sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] < defaultInfo.fWidthBytes[SkYUVSizeInfo::kU] ||
825 sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] < defaultInfo.fWidthBytes[SkYUVSizeInfo::kV]) {
msarettb714fb02016-01-22 14:46:42 -0800826 return fDecoderMgr->returnFailure("onGetYUV8Planes", kInvalidInput);
827 }
828
829 // Set the jump location for libjpeg errors
mtklein2f428962016-07-28 13:59:59 -0700830 if (setjmp(fDecoderMgr->getJmpBuf())) {
msarettb714fb02016-01-22 14:46:42 -0800831 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
832 }
833
834 // Get a pointer to the decompress info since we will use it quite frequently
835 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
836
837 dinfo->raw_data_out = TRUE;
838 if (!jpeg_start_decompress(dinfo)) {
839 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
840 }
841
842 // A previous implementation claims that the return value of is_yuv_supported()
843 // may change after calling jpeg_start_decompress(). It looks to me like this
844 // was caused by a bug in the old code, but we'll be safe and check here.
845 SkASSERT(is_yuv_supported(dinfo));
846
847 // Currently, we require that the Y plane dimensions match the image dimensions
848 // and that the U and V planes are the same dimensions.
msarett4984c3c2016-03-10 05:44:43 -0800849 SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kU] == sizeInfo.fSizes[SkYUVSizeInfo::kV]);
850 SkASSERT((uint32_t) sizeInfo.fSizes[SkYUVSizeInfo::kY].width() == dinfo->output_width &&
851 (uint32_t) sizeInfo.fSizes[SkYUVSizeInfo::kY].height() == dinfo->output_height);
msarettb714fb02016-01-22 14:46:42 -0800852
853 // Build a JSAMPIMAGE to handle output from libjpeg-turbo. A JSAMPIMAGE has
854 // a 2-D array of pixels for each of the components (Y, U, V) in the image.
855 // Cheat Sheet:
856 // JSAMPIMAGE == JSAMPLEARRAY* == JSAMPROW** == JSAMPLE***
857 JSAMPARRAY yuv[3];
858
859 // Set aside enough space for pointers to rows of Y, U, and V.
860 JSAMPROW rowptrs[2 * DCTSIZE + DCTSIZE + DCTSIZE];
861 yuv[0] = &rowptrs[0]; // Y rows (DCTSIZE or 2 * DCTSIZE)
862 yuv[1] = &rowptrs[2 * DCTSIZE]; // U rows (DCTSIZE)
863 yuv[2] = &rowptrs[3 * DCTSIZE]; // V rows (DCTSIZE)
864
865 // Initialize rowptrs.
866 int numYRowsPerBlock = DCTSIZE * dinfo->comp_info[0].v_samp_factor;
867 for (int i = 0; i < numYRowsPerBlock; i++) {
msarett4984c3c2016-03-10 05:44:43 -0800868 rowptrs[i] = SkTAddOffset<JSAMPLE>(planes[SkYUVSizeInfo::kY],
869 i * sizeInfo.fWidthBytes[SkYUVSizeInfo::kY]);
msarettb714fb02016-01-22 14:46:42 -0800870 }
871 for (int i = 0; i < DCTSIZE; i++) {
msarett4984c3c2016-03-10 05:44:43 -0800872 rowptrs[i + 2 * DCTSIZE] = SkTAddOffset<JSAMPLE>(planes[SkYUVSizeInfo::kU],
873 i * sizeInfo.fWidthBytes[SkYUVSizeInfo::kU]);
874 rowptrs[i + 3 * DCTSIZE] = SkTAddOffset<JSAMPLE>(planes[SkYUVSizeInfo::kV],
875 i * sizeInfo.fWidthBytes[SkYUVSizeInfo::kV]);
msarettb714fb02016-01-22 14:46:42 -0800876 }
877
878 // After each loop iteration, we will increment pointers to Y, U, and V.
msarett4984c3c2016-03-10 05:44:43 -0800879 size_t blockIncrementY = numYRowsPerBlock * sizeInfo.fWidthBytes[SkYUVSizeInfo::kY];
880 size_t blockIncrementU = DCTSIZE * sizeInfo.fWidthBytes[SkYUVSizeInfo::kU];
881 size_t blockIncrementV = DCTSIZE * sizeInfo.fWidthBytes[SkYUVSizeInfo::kV];
msarettb714fb02016-01-22 14:46:42 -0800882
883 uint32_t numRowsPerBlock = numYRowsPerBlock;
884
885 // We intentionally round down here, as this first loop will only handle
886 // full block rows. As a special case at the end, we will handle any
887 // remaining rows that do not make up a full block.
888 const int numIters = dinfo->output_height / numRowsPerBlock;
889 for (int i = 0; i < numIters; i++) {
890 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
891 if (linesRead < numRowsPerBlock) {
892 // FIXME: Handle incomplete YUV decodes without signalling an error.
893 return kInvalidInput;
894 }
895
896 // Update rowptrs.
897 for (int i = 0; i < numYRowsPerBlock; i++) {
898 rowptrs[i] += blockIncrementY;
899 }
900 for (int i = 0; i < DCTSIZE; i++) {
901 rowptrs[i + 2 * DCTSIZE] += blockIncrementU;
902 rowptrs[i + 3 * DCTSIZE] += blockIncrementV;
903 }
904 }
905
906 uint32_t remainingRows = dinfo->output_height - dinfo->output_scanline;
907 SkASSERT(remainingRows == dinfo->output_height % numRowsPerBlock);
908 SkASSERT(dinfo->output_scanline == numIters * numRowsPerBlock);
909 if (remainingRows > 0) {
910 // libjpeg-turbo needs memory to be padded by the block sizes. We will fulfill
911 // this requirement using a dummy row buffer.
912 // FIXME: Should SkCodec have an extra memory buffer that can be shared among
913 // all of the implementations that use temporary/garbage memory?
msarett4984c3c2016-03-10 05:44:43 -0800914 SkAutoTMalloc<JSAMPLE> dummyRow(sizeInfo.fWidthBytes[SkYUVSizeInfo::kY]);
msarettb714fb02016-01-22 14:46:42 -0800915 for (int i = remainingRows; i < numYRowsPerBlock; i++) {
916 rowptrs[i] = dummyRow.get();
917 }
918 int remainingUVRows = dinfo->comp_info[1].downsampled_height - DCTSIZE * numIters;
919 for (int i = remainingUVRows; i < DCTSIZE; i++) {
920 rowptrs[i + 2 * DCTSIZE] = dummyRow.get();
921 rowptrs[i + 3 * DCTSIZE] = dummyRow.get();
922 }
923
924 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
925 if (linesRead < remainingRows) {
926 // FIXME: Handle incomplete YUV decodes without signalling an error.
927 return kInvalidInput;
928 }
929 }
930
931 return kSuccess;
932}