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