msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkCodec.h" |
| 9 | #include "SkJpegCodec.h" |
| 10 | #include "SkJpegDecoderMgr.h" |
mtklein | 525e90a | 2015-06-18 09:58:57 -0700 | [diff] [blame] | 11 | #include "SkJpegUtility_codec.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 12 | #include "SkCodecPriv.h" |
| 13 | #include "SkColorPriv.h" |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 14 | #include "SkScanlineDecoder.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 15 | #include "SkStream.h" |
| 16 | #include "SkTemplates.h" |
| 17 | #include "SkTypes.h" |
| 18 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 19 | // stdio is needed for libjpeg-turbo |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | |
| 22 | extern "C" { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 23 | #include "jpeglibmangler.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 24 | #include "jerror.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 25 | #include "jpegint.h" |
| 26 | #include "jpeglib.h" |
| 27 | } |
| 28 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 29 | /* |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 30 | * Convert a row of CMYK samples to RGBA in place. |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 31 | * Note that this method moves the row pointer. |
| 32 | * @param width the number of pixels in the row that is being converted |
| 33 | * CMYK is stored as four bytes per pixel |
| 34 | */ |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 35 | static void convert_CMYK_to_RGBA(uint8_t* row, uint32_t width) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 36 | // We will implement a crude conversion from CMYK -> RGB using formulas |
| 37 | // from easyrgb.com. |
| 38 | // |
| 39 | // CMYK -> CMY |
| 40 | // C = C * (1 - K) + K |
| 41 | // M = M * (1 - K) + K |
| 42 | // Y = Y * (1 - K) + K |
| 43 | // |
| 44 | // libjpeg actually gives us inverted CMYK, so we must subtract the |
| 45 | // original terms from 1. |
| 46 | // CMYK -> CMY |
| 47 | // C = (1 - C) * (1 - (1 - K)) + (1 - K) |
| 48 | // M = (1 - M) * (1 - (1 - K)) + (1 - K) |
| 49 | // Y = (1 - Y) * (1 - (1 - K)) + (1 - K) |
| 50 | // |
| 51 | // Simplifying the above expression. |
| 52 | // CMYK -> CMY |
| 53 | // C = 1 - CK |
| 54 | // M = 1 - MK |
| 55 | // Y = 1 - YK |
| 56 | // |
| 57 | // CMY -> RGB |
| 58 | // R = (1 - C) * 255 |
| 59 | // G = (1 - M) * 255 |
| 60 | // B = (1 - Y) * 255 |
| 61 | // |
| 62 | // Therefore the full conversion is below. This can be verified at |
| 63 | // www.rapidtables.com (assuming inverted CMYK). |
| 64 | // CMYK -> RGB |
| 65 | // R = C * K * 255 |
| 66 | // G = M * K * 255 |
| 67 | // B = Y * K * 255 |
| 68 | // |
| 69 | // As a final note, we have treated the CMYK values as if they were on |
| 70 | // a scale from 0-1, when in fact they are 8-bit ints scaling from 0-255. |
| 71 | // We must divide each CMYK component by 255 to obtain the true conversion |
| 72 | // we should perform. |
| 73 | // CMYK -> RGB |
| 74 | // R = C * K / 255 |
| 75 | // G = M * K / 255 |
| 76 | // B = Y * K / 255 |
| 77 | for (uint32_t x = 0; x < width; x++, row += 4) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 78 | #if defined(SK_PMCOLOR_IS_RGBA) |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 79 | row[0] = SkMulDiv255Round(row[0], row[3]); |
| 80 | row[1] = SkMulDiv255Round(row[1], row[3]); |
| 81 | row[2] = SkMulDiv255Round(row[2], row[3]); |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 82 | #else |
| 83 | uint8_t tmp = row[0]; |
| 84 | row[0] = SkMulDiv255Round(row[2], row[3]); |
| 85 | row[1] = SkMulDiv255Round(row[1], row[3]); |
| 86 | row[2] = SkMulDiv255Round(tmp, row[3]); |
| 87 | #endif |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 88 | row[3] = 0xFF; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | bool SkJpegCodec::IsJpeg(SkStream* stream) { |
| 93 | static const uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF }; |
| 94 | char buffer[sizeof(jpegSig)]; |
| 95 | return stream->read(buffer, sizeof(jpegSig)) == sizeof(jpegSig) && |
| 96 | !memcmp(buffer, jpegSig, sizeof(jpegSig)); |
| 97 | } |
| 98 | |
| 99 | bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut, |
| 100 | JpegDecoderMgr** decoderMgrOut) { |
| 101 | |
| 102 | // Create a JpegDecoderMgr to own all of the decompress information |
| 103 | SkAutoTDelete<JpegDecoderMgr> decoderMgr(SkNEW_ARGS(JpegDecoderMgr, (stream))); |
| 104 | |
| 105 | // libjpeg errors will be caught and reported here |
| 106 | if (setjmp(decoderMgr->getJmpBuf())) { |
| 107 | return decoderMgr->returnFalse("setjmp"); |
| 108 | } |
| 109 | |
| 110 | // Initialize the decompress info and the source manager |
| 111 | decoderMgr->init(); |
| 112 | |
| 113 | // Read the jpeg header |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 114 | if (JPEG_HEADER_OK != turbo_jpeg_read_header(decoderMgr->dinfo(), true)) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 115 | return decoderMgr->returnFalse("read_header"); |
| 116 | } |
| 117 | |
| 118 | if (NULL != codecOut) { |
| 119 | // Recommend the color type to decode to |
| 120 | const SkColorType colorType = decoderMgr->getColorType(); |
| 121 | |
| 122 | // Create image info object and the codec |
| 123 | const SkImageInfo& imageInfo = SkImageInfo::Make(decoderMgr->dinfo()->image_width, |
| 124 | decoderMgr->dinfo()->image_height, colorType, kOpaque_SkAlphaType); |
| 125 | *codecOut = SkNEW_ARGS(SkJpegCodec, (imageInfo, stream, decoderMgr.detach())); |
| 126 | } else { |
| 127 | SkASSERT(NULL != decoderMgrOut); |
| 128 | *decoderMgrOut = decoderMgr.detach(); |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | SkCodec* SkJpegCodec::NewFromStream(SkStream* stream) { |
| 134 | SkAutoTDelete<SkStream> streamDeleter(stream); |
| 135 | SkCodec* codec = NULL; |
| 136 | if (ReadHeader(stream, &codec, NULL)) { |
| 137 | // Codec has taken ownership of the stream, we do not need to delete it |
| 138 | SkASSERT(codec); |
| 139 | streamDeleter.detach(); |
| 140 | return codec; |
| 141 | } |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, |
| 146 | JpegDecoderMgr* decoderMgr) |
| 147 | : INHERITED(srcInfo, stream) |
| 148 | , fDecoderMgr(decoderMgr) |
| 149 | {} |
| 150 | |
| 151 | /* |
| 152 | * Return a valid set of output dimensions for this decoder, given an input scale |
| 153 | */ |
| 154 | SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 155 | // 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 |
| 156 | // support these as well |
| 157 | long num; |
| 158 | long denom = 8; |
| 159 | if (desiredScale > 0.875f) { |
| 160 | num = 8; |
| 161 | } else if (desiredScale > 0.75f) { |
| 162 | num = 7; |
| 163 | } else if (desiredScale > 0.625f) { |
| 164 | num = 6; |
| 165 | } else if (desiredScale > 0.5f) { |
| 166 | num = 5; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 167 | } else if (desiredScale > 0.375f) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 168 | num = 4; |
| 169 | } else if (desiredScale > 0.25f) { |
| 170 | num = 3; |
| 171 | } else if (desiredScale > 0.125f) { |
| 172 | num = 2; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 173 | } else { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 174 | num = 1; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Set up a fake decompress struct in order to use libjpeg to calculate output dimensions |
| 178 | jpeg_decompress_struct dinfo; |
mtklein | f7aaadb | 2015-04-16 06:09:27 -0700 | [diff] [blame] | 179 | sk_bzero(&dinfo, sizeof(dinfo)); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 180 | dinfo.image_width = this->getInfo().width(); |
| 181 | dinfo.image_height = this->getInfo().height(); |
| 182 | dinfo.global_state = DSTATE_READY; |
| 183 | dinfo.num_components = 0; |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 184 | dinfo.scale_num = num; |
| 185 | dinfo.scale_denom = denom; |
| 186 | turbo_jpeg_calc_output_dimensions(&dinfo); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 187 | |
| 188 | // Return the calculated output dimensions for the given scale |
| 189 | return SkISize::Make(dinfo.output_width, dinfo.output_height); |
| 190 | } |
| 191 | |
| 192 | /* |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 193 | * Handles rewinding the input stream if it is necessary |
| 194 | */ |
| 195 | bool SkJpegCodec::handleRewind() { |
| 196 | switch(this->rewindIfNeeded()) { |
| 197 | case kCouldNotRewind_RewindState: |
| 198 | return fDecoderMgr->returnFalse("could not rewind"); |
| 199 | case kRewound_RewindState: { |
| 200 | JpegDecoderMgr* decoderMgr = NULL; |
| 201 | if (!ReadHeader(this->stream(), NULL, &decoderMgr)) { |
| 202 | return fDecoderMgr->returnFalse("could not rewind"); |
| 203 | } |
| 204 | SkASSERT(NULL != decoderMgr); |
| 205 | fDecoderMgr.reset(decoderMgr); |
| 206 | return true; |
| 207 | } |
| 208 | case kNoRewindNecessary_RewindState: |
| 209 | return true; |
| 210 | default: |
| 211 | SkASSERT(false); |
| 212 | return false; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 217 | * Checks if the conversion between the input image and the requested output |
| 218 | * image has been implemented |
| 219 | * Sets the output color space |
| 220 | */ |
| 221 | bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dst) { |
| 222 | const SkImageInfo& src = this->getInfo(); |
| 223 | |
| 224 | // Ensure that the profile type is unchanged |
| 225 | if (dst.profileType() != src.profileType()) { |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | // Ensure that the alpha type is opaque |
| 230 | if (kOpaque_SkAlphaType != dst.alphaType()) { |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | // Check if we will decode to CMYK because a conversion to RGBA is not supported |
| 235 | J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->jpeg_color_space; |
| 236 | bool isCMYK = JCS_CMYK == colorSpace || JCS_YCCK == colorSpace; |
| 237 | |
| 238 | // Check for valid color types and set the output color space |
| 239 | switch (dst.colorType()) { |
| 240 | case kN32_SkColorType: |
| 241 | if (isCMYK) { |
| 242 | fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
| 243 | } else { |
| 244 | // Check the byte ordering of the RGBA color space for the |
| 245 | // current platform |
| 246 | #if defined(SK_PMCOLOR_IS_RGBA) |
| 247 | fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; |
| 248 | #else |
| 249 | fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA; |
| 250 | #endif |
| 251 | } |
| 252 | return true; |
| 253 | case kRGB_565_SkColorType: |
| 254 | if (isCMYK) { |
| 255 | return false; |
| 256 | } else { |
| 257 | fDecoderMgr->dinfo()->out_color_space = JCS_RGB565; |
| 258 | } |
| 259 | return true; |
| 260 | case kGray_8_SkColorType: |
| 261 | if (isCMYK) { |
| 262 | return false; |
| 263 | } else { |
| 264 | // We will enable decodes to gray even if the image is color because this is |
| 265 | // much faster than decoding to color and then converting |
| 266 | fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE; |
| 267 | } |
| 268 | return true; |
| 269 | default: |
| 270 | return false; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /* |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 275 | * Checks if we can scale to the requested dimensions and scales the dimensions |
| 276 | * if possible |
| 277 | */ |
| 278 | bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 279 | // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1 |
| 280 | fDecoderMgr->dinfo()->scale_denom = 8; |
| 281 | fDecoderMgr->dinfo()->scale_num = 8; |
| 282 | turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 283 | while (fDecoderMgr->dinfo()->output_width != dstWidth || |
| 284 | fDecoderMgr->dinfo()->output_height != dstHeight) { |
| 285 | |
| 286 | // Return a failure if we have tried all of the possible scales |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 287 | if (1 == fDecoderMgr->dinfo()->scale_num || |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 288 | dstWidth > fDecoderMgr->dinfo()->output_width || |
| 289 | dstHeight > fDecoderMgr->dinfo()->output_height) { |
| 290 | return fDecoderMgr->returnFalse("could not scale to requested dimensions"); |
| 291 | } |
| 292 | |
| 293 | // Try the next scale |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 294 | fDecoderMgr->dinfo()->scale_num -= 1; |
| 295 | turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 296 | } |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | /* |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 301 | * Performs the jpeg decode |
| 302 | */ |
| 303 | SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 304 | void* dst, size_t dstRowBytes, |
| 305 | const Options& options, SkPMColor*, int*) { |
| 306 | // Rewind the stream if needed |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 307 | if (!this->handleRewind()) { |
msarett | c0e80c1 | 2015-07-01 06:50:35 -0700 | [diff] [blame] | 308 | return fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 309 | } |
| 310 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 311 | if (options.fSubset) { |
| 312 | // Subsets are not supported. |
| 313 | return kUnimplemented; |
| 314 | } |
| 315 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 316 | // Get a pointer to the decompress info since we will use it quite frequently |
| 317 | jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); |
| 318 | |
| 319 | // Set the jump location for libjpeg errors |
| 320 | if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 321 | return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
| 322 | } |
| 323 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 324 | // Check if we can decode to the requested destination and set the output color space |
| 325 | if (!this->setOutputColorSpace(dstInfo)) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 326 | return fDecoderMgr->returnFailure("conversion_possible", kInvalidConversion); |
| 327 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 328 | |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 329 | // Perform the necessary scaling |
| 330 | if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 331 | return fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidScale); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | // Now, given valid output dimensions, we can start the decompress |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 335 | if (!turbo_jpeg_start_decompress(dinfo)) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 336 | return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); |
| 337 | } |
| 338 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 339 | // The recommended output buffer height should always be 1 in high quality modes. |
| 340 | // If it's not, we want to know because it means our strategy is not optimal. |
| 341 | SkASSERT(1 == dinfo->rec_outbuf_height); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 342 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 343 | // Perform the decode a single row at a time |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 344 | uint32_t dstHeight = dstInfo.height(); |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 345 | JSAMPLE* dstRow = (JSAMPLE*) dst; |
| 346 | for (uint32_t y = 0; y < dstHeight; y++) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 347 | // Read rows of the image |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 348 | uint32_t rowsDecoded = turbo_jpeg_read_scanlines(dinfo, &dstRow, 1); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 349 | |
| 350 | // If we cannot read enough rows, assume the input is incomplete |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 351 | if (rowsDecoded != 1) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 352 | // Fill the remainder of the image with black. This error handling |
| 353 | // behavior is unspecified but SkCodec consistently uses black as |
| 354 | // the fill color for opaque images. If the destination is kGray, |
| 355 | // the low 8 bits of SK_ColorBLACK will be used. Conveniently, |
| 356 | // these are zeros, which is the representation for black in kGray. |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 357 | SkSwizzler::Fill(dstRow, dstInfo, dstRowBytes, dstHeight - y, SK_ColorBLACK, NULL); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 358 | |
| 359 | // Prevent libjpeg from failing on incomplete decode |
| 360 | dinfo->output_scanline = dstHeight; |
| 361 | |
| 362 | // Finish the decode and indicate that the input was incomplete. |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 363 | turbo_jpeg_finish_decompress(dinfo); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 364 | return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput); |
| 365 | } |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 366 | |
| 367 | // Convert to RGBA if necessary |
| 368 | if (JCS_CMYK == dinfo->out_color_space) { |
| 369 | convert_CMYK_to_RGBA(dstRow, dstInfo.width()); |
| 370 | } |
| 371 | |
| 372 | // Move to the next row |
| 373 | dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 374 | } |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 375 | turbo_jpeg_finish_decompress(dinfo); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 376 | |
| 377 | return kSuccess; |
| 378 | } |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 379 | |
| 380 | /* |
| 381 | * Enable scanline decoding for jpegs |
| 382 | */ |
| 383 | class SkJpegScanlineDecoder : public SkScanlineDecoder { |
| 384 | public: |
| 385 | SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec) |
| 386 | : INHERITED(dstInfo) |
| 387 | , fCodec(codec) |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 388 | {} |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 389 | |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 390 | virtual ~SkJpegScanlineDecoder() { |
| 391 | if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
| 392 | SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a |
| 397 | // partial decode. |
| 398 | fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height(); |
| 399 | turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); |
| 400 | } |
| 401 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 402 | SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) override { |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 403 | // Set the jump location for libjpeg errors |
| 404 | if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 405 | return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvalidInput); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | // Read rows one at a time |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 409 | JSAMPLE* dstRow = (JSAMPLE*) dst; |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 410 | for (int y = 0; y < count; y++) { |
| 411 | // Read row of the image |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 412 | uint32_t rowsDecoded = |
| 413 | turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dstRow, 1); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 414 | if (rowsDecoded != 1) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 415 | SkSwizzler::Fill( |
| 416 | dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBLACK, NULL); |
| 417 | fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height(); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 418 | return SkCodec::kIncompleteInput; |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 419 | } |
| 420 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 421 | // Convert to RGBA if necessary |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 422 | if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 423 | convert_CMYK_to_RGBA(dstRow, this->dstInfo().width()); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 424 | } |
| 425 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 426 | // Move to the next row |
| 427 | dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 428 | } |
| 429 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 430 | return SkCodec::kSuccess; |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 431 | } |
| 432 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 433 | #ifndef TURBO_HAS_SKIP |
| 434 | #define turbo_jpeg_skip_scanlines(dinfo, count) \ |
| 435 | SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \ |
| 436 | uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \ |
| 437 | for (int y = 0; y < count; y++) { \ |
| 438 | turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \ |
| 439 | } |
| 440 | #endif |
| 441 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 442 | SkCodec::Result onSkipScanlines(int count) override { |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 443 | // Set the jump location for libjpeg errors |
| 444 | if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 445 | return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvalidInput); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 446 | } |
| 447 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 448 | turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 449 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 450 | return SkCodec::kSuccess; |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 451 | } |
| 452 | |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 453 | private: |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 454 | SkAutoTDelete<SkJpegCodec> fCodec; |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 455 | |
| 456 | typedef SkScanlineDecoder INHERITED; |
| 457 | }; |
| 458 | |
| 459 | SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, |
| 460 | const Options& options, SkPMColor ctable[], int* ctableCount) { |
| 461 | |
| 462 | // Rewind the stream if needed |
| 463 | if (!this->handleRewind()) { |
| 464 | SkCodecPrintf("Could not rewind\n"); |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | // Set the jump location for libjpeg errors |
| 469 | if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 470 | SkCodecPrintf("setjmp: Error from libjpeg\n"); |
| 471 | return NULL; |
| 472 | } |
| 473 | |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 474 | SkStream* stream = this->stream()->duplicate(); |
| 475 | if (!stream) { |
| 476 | return NULL; |
| 477 | } |
| 478 | SkAutoTDelete<SkJpegCodec> codec(static_cast<SkJpegCodec*>(SkJpegCodec::NewFromStream(stream))); |
| 479 | if (!codec) { |
| 480 | return NULL; |
| 481 | } |
| 482 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 483 | // Check if we can decode to the requested destination and set the output color space |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 484 | if (!codec->setOutputColorSpace(dstInfo)) { |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 485 | SkCodecPrintf("Cannot convert to output type\n"); |
| 486 | return NULL; |
| 487 | } |
| 488 | |
| 489 | // Perform the necessary scaling |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 490 | if (!codec->scaleToDimensions(dstInfo.width(), dstInfo.height())) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 491 | SkCodecPrintf("Cannot scale to output dimensions\n"); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 492 | return NULL; |
| 493 | } |
| 494 | |
| 495 | // Now, given valid output dimensions, we can start the decompress |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 496 | if (!turbo_jpeg_start_decompress(codec->fDecoderMgr->dinfo())) { |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 497 | SkCodecPrintf("start decompress failed\n"); |
| 498 | return NULL; |
| 499 | } |
| 500 | |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 501 | // Return the new scanline decoder |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 502 | return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, codec.detach())); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 503 | } |