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