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