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