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