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