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