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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | f8bf918 | 2015-06-29 10:14:11 -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 | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 306 | // Rewind the stream if needed |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 307 | if (!this->handleRewind()) { |
| 308 | fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // Get a pointer to the decompress info since we will use it quite frequently |
| 312 | jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); |
| 313 | |
| 314 | // Set the jump location for libjpeg errors |
| 315 | if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 316 | return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
| 317 | } |
| 318 | |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 319 | // Check if we can decode to the requested destination and set the output color space |
| 320 | if (!this->setOutputColorSpace(dstInfo)) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 321 | return fDecoderMgr->returnFailure("conversion_possible", kInvalidConversion); |
| 322 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 323 | |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 324 | // Perform the necessary scaling |
| 325 | if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 326 | return fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidScale); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // Now, given valid output dimensions, we can start the decompress |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 330 | if (!turbo_jpeg_start_decompress(dinfo)) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 331 | return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); |
| 332 | } |
| 333 | |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 334 | // The recommended output buffer height should always be 1 in high quality modes. |
| 335 | // If it's not, we want to know because it means our strategy is not optimal. |
| 336 | SkASSERT(1 == dinfo->rec_outbuf_height); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 337 | |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 338 | // Perform the decode a single row at a time |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 339 | uint32_t dstHeight = dstInfo.height(); |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 340 | JSAMPLE* dstRow = (JSAMPLE*) dst; |
| 341 | for (uint32_t y = 0; y < dstHeight; y++) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 342 | // Read rows of the image |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 343 | uint32_t rowsDecoded = turbo_jpeg_read_scanlines(dinfo, &dstRow, 1); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 344 | |
| 345 | // If we cannot read enough rows, assume the input is incomplete |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 346 | if (rowsDecoded != 1) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 347 | // Fill the remainder of the image with black. This error handling |
| 348 | // behavior is unspecified but SkCodec consistently uses black as |
| 349 | // the fill color for opaque images. If the destination is kGray, |
| 350 | // the low 8 bits of SK_ColorBLACK will be used. Conveniently, |
| 351 | // these are zeros, which is the representation for black in kGray. |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 352 | SkSwizzler::Fill(dstRow, dstInfo, dstRowBytes, dstHeight - y, SK_ColorBLACK, NULL); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 353 | |
| 354 | // Prevent libjpeg from failing on incomplete decode |
| 355 | dinfo->output_scanline = dstHeight; |
| 356 | |
| 357 | // Finish the decode and indicate that the input was incomplete. |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 358 | turbo_jpeg_finish_decompress(dinfo); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 359 | return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput); |
| 360 | } |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 361 | |
| 362 | // Convert to RGBA if necessary |
| 363 | if (JCS_CMYK == dinfo->out_color_space) { |
| 364 | convert_CMYK_to_RGBA(dstRow, dstInfo.width()); |
| 365 | } |
| 366 | |
| 367 | // Move to the next row |
| 368 | dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 369 | } |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 370 | turbo_jpeg_finish_decompress(dinfo); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 371 | |
| 372 | return kSuccess; |
| 373 | } |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 374 | |
| 375 | /* |
| 376 | * Enable scanline decoding for jpegs |
| 377 | */ |
| 378 | class SkJpegScanlineDecoder : public SkScanlineDecoder { |
| 379 | public: |
| 380 | SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec) |
| 381 | : INHERITED(dstInfo) |
| 382 | , fCodec(codec) |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 383 | {} |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 384 | |
| 385 | SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowBytes) override { |
| 386 | // Set the jump location for libjpeg errors |
| 387 | if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
| 388 | return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator::kInvalidInput); |
| 389 | } |
| 390 | |
| 391 | // Read rows one at a time |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 392 | JSAMPLE* dstRow = (JSAMPLE*) dst; |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 393 | for (int y = 0; y < count; y++) { |
| 394 | // Read row of the image |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 395 | uint32_t rowsDecoded = |
| 396 | turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dstRow, 1); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 397 | if (rowsDecoded != 1) { |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 398 | SkSwizzler::Fill( |
| 399 | dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBLACK, NULL); |
| 400 | fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height(); |
| 401 | turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 402 | return SkImageGenerator::kIncompleteInput; |
| 403 | } |
| 404 | |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 405 | // Convert to RGBA if necessary |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 406 | if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 407 | convert_CMYK_to_RGBA(dstRow, this->dstInfo().width()); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 408 | } |
| 409 | |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 410 | // Move to the next row |
| 411 | dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | return SkImageGenerator::kSuccess; |
| 415 | } |
| 416 | |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 417 | #ifndef TURBO_HAS_SKIP |
| 418 | #define turbo_jpeg_skip_scanlines(dinfo, count) \ |
| 419 | SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \ |
| 420 | uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \ |
| 421 | for (int y = 0; y < count; y++) { \ |
| 422 | turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \ |
| 423 | } |
| 424 | #endif |
| 425 | |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 426 | SkImageGenerator::Result onSkipScanlines(int count) override { |
| 427 | // Set the jump location for libjpeg errors |
| 428 | if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
| 429 | return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator::kInvalidInput); |
| 430 | } |
| 431 | |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 432 | turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 433 | |
| 434 | return SkImageGenerator::kSuccess; |
| 435 | } |
| 436 | |
| 437 | void onFinish() override { |
| 438 | if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
| 439 | SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); |
| 440 | return; |
| 441 | } |
| 442 | |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 443 | turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | private: |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 447 | SkJpegCodec* fCodec; // unowned |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 448 | |
| 449 | typedef SkScanlineDecoder INHERITED; |
| 450 | }; |
| 451 | |
| 452 | SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, |
| 453 | const Options& options, SkPMColor ctable[], int* ctableCount) { |
| 454 | |
| 455 | // Rewind the stream if needed |
| 456 | if (!this->handleRewind()) { |
| 457 | SkCodecPrintf("Could not rewind\n"); |
| 458 | return NULL; |
| 459 | } |
| 460 | |
| 461 | // Set the jump location for libjpeg errors |
| 462 | if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 463 | SkCodecPrintf("setjmp: Error from libjpeg\n"); |
| 464 | return NULL; |
| 465 | } |
| 466 | |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 467 | // Check if we can decode to the requested destination and set the output color space |
| 468 | if (!this->setOutputColorSpace(dstInfo)) { |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 469 | SkCodecPrintf("Cannot convert to output type\n"); |
| 470 | return NULL; |
| 471 | } |
| 472 | |
| 473 | // Perform the necessary scaling |
| 474 | if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 475 | SkCodecPrintf("Cannot scale to output dimensions\n"); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 476 | return NULL; |
| 477 | } |
| 478 | |
| 479 | // Now, given valid output dimensions, we can start the decompress |
msarett | f8bf918 | 2015-06-29 10:14:11 -0700 | [diff] [blame^] | 480 | if (!turbo_jpeg_start_decompress(fDecoderMgr->dinfo())) { |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 481 | SkCodecPrintf("start decompress failed\n"); |
| 482 | return NULL; |
| 483 | } |
| 484 | |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 485 | // Return the new scanline decoder |
| 486 | return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this)); |
| 487 | } |