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