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 | 1c8a587 | 2015-07-07 08:50:01 -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" { |
| 22 | #include "jerror.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 23 | #include "jpeglib.h" |
| 24 | } |
| 25 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 26 | bool SkJpegCodec::IsJpeg(const void* buffer, size_t bytesRead) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 27 | static const uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF }; |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 28 | return bytesRead >= 3 && !memcmp(buffer, jpegSig, sizeof(jpegSig)); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut, |
| 32 | JpegDecoderMgr** decoderMgrOut) { |
| 33 | |
| 34 | // Create a JpegDecoderMgr to own all of the decompress information |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 35 | SkAutoTDelete<JpegDecoderMgr> decoderMgr(new JpegDecoderMgr(stream)); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 36 | |
| 37 | // libjpeg errors will be caught and reported here |
| 38 | if (setjmp(decoderMgr->getJmpBuf())) { |
| 39 | return decoderMgr->returnFalse("setjmp"); |
| 40 | } |
| 41 | |
| 42 | // Initialize the decompress info and the source manager |
| 43 | decoderMgr->init(); |
| 44 | |
| 45 | // Read the jpeg header |
msarett | fbccb59 | 2015-09-01 06:43:41 -0700 | [diff] [blame] | 46 | if (JPEG_HEADER_OK != jpeg_read_header(decoderMgr->dinfo(), true)) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 47 | return decoderMgr->returnFalse("read_header"); |
| 48 | } |
| 49 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 50 | if (nullptr != codecOut) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 51 | // Recommend the color type to decode to |
| 52 | const SkColorType colorType = decoderMgr->getColorType(); |
| 53 | |
| 54 | // Create image info object and the codec |
| 55 | const SkImageInfo& imageInfo = SkImageInfo::Make(decoderMgr->dinfo()->image_width, |
| 56 | decoderMgr->dinfo()->image_height, colorType, kOpaque_SkAlphaType); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 57 | *codecOut = new SkJpegCodec(imageInfo, stream, decoderMgr.detach()); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 58 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 59 | SkASSERT(nullptr != decoderMgrOut); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 60 | *decoderMgrOut = decoderMgr.detach(); |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | SkCodec* SkJpegCodec::NewFromStream(SkStream* stream) { |
| 66 | SkAutoTDelete<SkStream> streamDeleter(stream); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 67 | SkCodec* codec = nullptr; |
| 68 | if (ReadHeader(stream, &codec, nullptr)) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 69 | // Codec has taken ownership of the stream, we do not need to delete it |
| 70 | SkASSERT(codec); |
| 71 | streamDeleter.detach(); |
| 72 | return codec; |
| 73 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | return nullptr; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, |
| 78 | JpegDecoderMgr* decoderMgr) |
| 79 | : INHERITED(srcInfo, stream) |
| 80 | , fDecoderMgr(decoderMgr) |
msarett | fbccb59 | 2015-09-01 06:43:41 -0700 | [diff] [blame] | 81 | , fReadyState(decoderMgr->dinfo()->global_state) |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 82 | {} |
| 83 | |
| 84 | /* |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 85 | * Return the row bytes of a particular image type and width |
| 86 | */ |
| 87 | static int get_row_bytes(const j_decompress_ptr dinfo) { |
| 88 | int colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 : dinfo->out_color_components; |
| 89 | return dinfo->output_width * colorBytes; |
| 90 | |
| 91 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Calculate output dimensions based on the provided factors. |
| 95 | * |
| 96 | * Not to be used on the actual jpeg_decompress_struct used for decoding, since it will |
| 97 | * incorrectly modify num_components. |
| 98 | */ |
| 99 | void calc_output_dimensions(jpeg_decompress_struct* dinfo, unsigned int num, unsigned int denom) { |
| 100 | dinfo->num_components = 0; |
| 101 | dinfo->scale_num = num; |
| 102 | dinfo->scale_denom = denom; |
| 103 | jpeg_calc_output_dimensions(dinfo); |
| 104 | } |
| 105 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 106 | /* |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 107 | * Return a valid set of output dimensions for this decoder, given an input scale |
| 108 | */ |
| 109 | SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 110 | // 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 |
| 111 | // support these as well |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 112 | unsigned int num; |
| 113 | unsigned int denom = 8; |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 114 | if (desiredScale >= 0.9375) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 115 | num = 8; |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 116 | } else if (desiredScale >= 0.8125) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 117 | num = 7; |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 118 | } else if (desiredScale >= 0.6875f) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 119 | num = 6; |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 120 | } else if (desiredScale >= 0.5625f) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 121 | num = 5; |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 122 | } else if (desiredScale >= 0.4375f) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 123 | num = 4; |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 124 | } else if (desiredScale >= 0.3125f) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 125 | num = 3; |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 126 | } else if (desiredScale >= 0.1875f) { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 127 | num = 2; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 128 | } else { |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 129 | num = 1; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Set up a fake decompress struct in order to use libjpeg to calculate output dimensions |
| 133 | jpeg_decompress_struct dinfo; |
mtklein | f7aaadb | 2015-04-16 06:09:27 -0700 | [diff] [blame] | 134 | sk_bzero(&dinfo, sizeof(dinfo)); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 135 | dinfo.image_width = this->getInfo().width(); |
| 136 | dinfo.image_height = this->getInfo().height(); |
msarett | fbccb59 | 2015-09-01 06:43:41 -0700 | [diff] [blame] | 137 | dinfo.global_state = fReadyState; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 138 | calc_output_dimensions(&dinfo, num, denom); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 139 | |
| 140 | // Return the calculated output dimensions for the given scale |
| 141 | return SkISize::Make(dinfo.output_width, dinfo.output_height); |
| 142 | } |
| 143 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 144 | bool SkJpegCodec::onRewind() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 145 | JpegDecoderMgr* decoderMgr = nullptr; |
| 146 | if (!ReadHeader(this->stream(), nullptr, &decoderMgr)) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 147 | return fDecoderMgr->returnFalse("could not rewind"); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 148 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 149 | SkASSERT(nullptr != decoderMgr); |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 150 | fDecoderMgr.reset(decoderMgr); |
| 151 | return true; |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 155 | * Checks if the conversion between the input image and the requested output |
| 156 | * image has been implemented |
| 157 | * Sets the output color space |
| 158 | */ |
| 159 | bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dst) { |
| 160 | const SkImageInfo& src = this->getInfo(); |
| 161 | |
| 162 | // Ensure that the profile type is unchanged |
| 163 | if (dst.profileType() != src.profileType()) { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | // Ensure that the alpha type is opaque |
| 168 | if (kOpaque_SkAlphaType != dst.alphaType()) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | // Check if we will decode to CMYK because a conversion to RGBA is not supported |
| 173 | J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->jpeg_color_space; |
| 174 | bool isCMYK = JCS_CMYK == colorSpace || JCS_YCCK == colorSpace; |
| 175 | |
| 176 | // Check for valid color types and set the output color space |
| 177 | switch (dst.colorType()) { |
| 178 | case kN32_SkColorType: |
| 179 | if (isCMYK) { |
| 180 | fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
| 181 | } else { |
| 182 | // Check the byte ordering of the RGBA color space for the |
| 183 | // current platform |
benjaminwagner | aada3e8 | 2015-10-27 09:14:29 -0700 | [diff] [blame] | 184 | #if defined(SK_PMCOLOR_IS_RGBA) |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 185 | fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; |
benjaminwagner | aada3e8 | 2015-10-27 09:14:29 -0700 | [diff] [blame] | 186 | #else |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 187 | fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA; |
| 188 | #endif |
| 189 | } |
| 190 | return true; |
| 191 | case kRGB_565_SkColorType: |
| 192 | if (isCMYK) { |
scroggo | ef27d89 | 2015-10-23 09:29:22 -0700 | [diff] [blame] | 193 | fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 194 | } else { |
msarett | 8ff6ca6 | 2015-09-18 12:06:04 -0700 | [diff] [blame] | 195 | fDecoderMgr->dinfo()->dither_mode = JDITHER_NONE; |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 196 | fDecoderMgr->dinfo()->out_color_space = JCS_RGB565; |
| 197 | } |
| 198 | return true; |
| 199 | case kGray_8_SkColorType: |
| 200 | if (isCMYK) { |
| 201 | return false; |
| 202 | } else { |
| 203 | // We will enable decodes to gray even if the image is color because this is |
| 204 | // much faster than decoding to color and then converting |
| 205 | fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE; |
| 206 | } |
| 207 | return true; |
| 208 | default: |
| 209 | return false; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /* |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 214 | * Checks if we can natively scale to the requested dimensions and natively scales the |
| 215 | * dimensions if possible |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 216 | */ |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 217 | bool SkJpegCodec::onDimensionsSupported(const SkISize& size) { |
| 218 | if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 219 | return fDecoderMgr->returnFalse("onDimensionsSupported/setjmp"); |
| 220 | } |
| 221 | |
| 222 | const unsigned int dstWidth = size.width(); |
| 223 | const unsigned int dstHeight = size.height(); |
| 224 | |
| 225 | // Set up a fake decompress struct in order to use libjpeg to calculate output dimensions |
| 226 | // FIXME: Why is this necessary? |
| 227 | jpeg_decompress_struct dinfo; |
| 228 | sk_bzero(&dinfo, sizeof(dinfo)); |
| 229 | dinfo.image_width = this->getInfo().width(); |
| 230 | dinfo.image_height = this->getInfo().height(); |
| 231 | dinfo.global_state = fReadyState; |
| 232 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 233 | // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1 |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 234 | unsigned int num = 8; |
| 235 | const unsigned int denom = 8; |
| 236 | calc_output_dimensions(&dinfo, num, denom); |
| 237 | while (dinfo.output_width != dstWidth || dinfo.output_height != dstHeight) { |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 238 | |
| 239 | // Return a failure if we have tried all of the possible scales |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 240 | if (1 == num || dstWidth > dinfo.output_width || dstHeight > dinfo.output_height) { |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 241 | return false; |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // Try the next scale |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 245 | num -= 1; |
| 246 | calc_output_dimensions(&dinfo, num, denom); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 247 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 248 | |
| 249 | fDecoderMgr->dinfo()->scale_num = num; |
| 250 | fDecoderMgr->dinfo()->scale_denom = denom; |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 251 | return true; |
| 252 | } |
| 253 | |
| 254 | /* |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 255 | * Performs the jpeg decode |
| 256 | */ |
| 257 | SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 258 | void* dst, size_t dstRowBytes, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 259 | const Options& options, SkPMColor*, int*, |
| 260 | int* rowsDecoded) { |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 261 | if (options.fSubset) { |
| 262 | // Subsets are not supported. |
| 263 | return kUnimplemented; |
| 264 | } |
| 265 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 266 | // Get a pointer to the decompress info since we will use it quite frequently |
| 267 | jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); |
| 268 | |
| 269 | // Set the jump location for libjpeg errors |
| 270 | if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 271 | return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
| 272 | } |
| 273 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 274 | // Check if we can decode to the requested destination and set the output color space |
| 275 | if (!this->setOutputColorSpace(dstInfo)) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 276 | return fDecoderMgr->returnFailure("conversion_possible", kInvalidConversion); |
| 277 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 278 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 279 | // Now, given valid output dimensions, we can start the decompress |
msarett | fbccb59 | 2015-09-01 06:43:41 -0700 | [diff] [blame] | 280 | if (!jpeg_start_decompress(dinfo)) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 281 | return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); |
| 282 | } |
| 283 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 284 | // The recommended output buffer height should always be 1 in high quality modes. |
| 285 | // If it's not, we want to know because it means our strategy is not optimal. |
| 286 | SkASSERT(1 == dinfo->rec_outbuf_height); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 287 | |
scroggo | ef27d89 | 2015-10-23 09:29:22 -0700 | [diff] [blame] | 288 | if (JCS_CMYK == dinfo->out_color_space) { |
| 289 | this->initializeSwizzler(dstInfo, options); |
| 290 | } |
| 291 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 292 | // Perform the decode a single row at a time |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 293 | uint32_t dstHeight = dstInfo.height(); |
scroggo | ef27d89 | 2015-10-23 09:29:22 -0700 | [diff] [blame] | 294 | |
| 295 | JSAMPLE* dstRow; |
| 296 | if (fSwizzler) { |
| 297 | // write data to storage row, then sample using swizzler |
| 298 | dstRow = fSrcRow; |
| 299 | } else { |
| 300 | // write data directly to dst |
| 301 | dstRow = (JSAMPLE*) dst; |
| 302 | } |
| 303 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 304 | for (uint32_t y = 0; y < dstHeight; y++) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 305 | // Read rows of the image |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 306 | uint32_t lines = jpeg_read_scanlines(dinfo, &dstRow, 1); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 307 | |
| 308 | // If we cannot read enough rows, assume the input is incomplete |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 309 | if (lines != 1) { |
| 310 | *rowsDecoded = y; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 311 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 312 | return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput); |
| 313 | } |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 314 | |
scroggo | ef27d89 | 2015-10-23 09:29:22 -0700 | [diff] [blame] | 315 | if (fSwizzler) { |
| 316 | // use swizzler to sample row |
| 317 | fSwizzler->swizzle(dst, dstRow); |
| 318 | dst = SkTAddOffset<JSAMPLE>(dst, dstRowBytes); |
| 319 | } else { |
| 320 | dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes); |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 321 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 322 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 323 | |
| 324 | return kSuccess; |
| 325 | } |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 326 | |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 327 | void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& options) { |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 328 | SkSwizzler::SrcConfig srcConfig = SkSwizzler::kUnknown; |
scroggo | ef27d89 | 2015-10-23 09:29:22 -0700 | [diff] [blame] | 329 | if (JCS_CMYK == fDecoderMgr->dinfo()->out_color_space) { |
| 330 | srcConfig = SkSwizzler::kCMYK; |
| 331 | } else { |
| 332 | switch (dstInfo.colorType()) { |
| 333 | case kGray_8_SkColorType: |
| 334 | srcConfig = SkSwizzler::kGray; |
| 335 | break; |
| 336 | case kRGBA_8888_SkColorType: |
| 337 | srcConfig = SkSwizzler::kRGBX; |
| 338 | break; |
| 339 | case kBGRA_8888_SkColorType: |
| 340 | srcConfig = SkSwizzler::kBGRX; |
| 341 | break; |
| 342 | case kRGB_565_SkColorType: |
| 343 | srcConfig = SkSwizzler::kRGB_565; |
| 344 | break; |
| 345 | default: |
| 346 | // This function should only be called if the colorType is supported by jpeg |
scroggo | ef27d89 | 2015-10-23 09:29:22 -0700 | [diff] [blame] | 347 | SkASSERT(false); |
scroggo | ef27d89 | 2015-10-23 09:29:22 -0700 | [diff] [blame] | 348 | } |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 349 | } |
| 350 | |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 351 | fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, dstInfo, options)); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 352 | fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); |
| 353 | fSrcRow = static_cast<uint8_t*>(fStorage.get()); |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { |
| 357 | if (!createIfNecessary || fSwizzler) { |
| 358 | SkASSERT(!fSwizzler || (fSrcRow && static_cast<uint8_t*>(fStorage.get()) == fSrcRow)); |
| 359 | return fSwizzler; |
| 360 | } |
| 361 | |
| 362 | this->initializeSwizzler(this->dstInfo(), this->options()); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 363 | return fSwizzler; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 364 | } |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 365 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 366 | SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
| 367 | const Options& options, SkPMColor ctable[], int* ctableCount) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 368 | // Set the jump location for libjpeg errors |
| 369 | if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 370 | SkCodecPrintf("setjmp: Error from libjpeg\n"); |
| 371 | return kInvalidInput; |
| 372 | } |
| 373 | |
| 374 | // Check if we can decode to the requested destination and set the output color space |
| 375 | if (!this->setOutputColorSpace(dstInfo)) { |
| 376 | return kInvalidConversion; |
| 377 | } |
| 378 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 379 | // Remove objects used for sampling. |
| 380 | fSwizzler.reset(nullptr); |
| 381 | fSrcRow = nullptr; |
| 382 | fStorage.free(); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 383 | |
| 384 | // Now, given valid output dimensions, we can start the decompress |
| 385 | if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { |
| 386 | SkCodecPrintf("start decompress failed\n"); |
| 387 | return kInvalidInput; |
| 388 | } |
| 389 | |
scroggo | ef27d89 | 2015-10-23 09:29:22 -0700 | [diff] [blame] | 390 | // We will need a swizzler if we are performing a subset decode or |
| 391 | // converting from CMYK. |
| 392 | if (options.fSubset || JCS_CMYK == fDecoderMgr->dinfo()->out_color_space) { |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 393 | this->initializeSwizzler(dstInfo, options); |
| 394 | } |
| 395 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 396 | return kSuccess; |
| 397 | } |
| 398 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 399 | int SkJpegCodec::onGetScanlines(void* dst, int count, size_t rowBytes) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 400 | // Set the jump location for libjpeg errors |
| 401 | if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 402 | return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
| 403 | } |
| 404 | // Read rows one at a time |
| 405 | JSAMPLE* dstRow; |
| 406 | if (fSwizzler) { |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 407 | // write data to storage row, then sample using swizzler |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 408 | dstRow = fSrcRow; |
| 409 | } else { |
| 410 | // write data directly to dst |
| 411 | dstRow = (JSAMPLE*) dst; |
| 412 | } |
| 413 | |
| 414 | for (int y = 0; y < count; y++) { |
| 415 | // Read row of the image |
| 416 | uint32_t rowsDecoded = jpeg_read_scanlines(fDecoderMgr->dinfo(), &dstRow, 1); |
| 417 | if (rowsDecoded != 1) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 418 | fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height(); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 419 | return y; |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 420 | } |
| 421 | |
scroggo | ef27d89 | 2015-10-23 09:29:22 -0700 | [diff] [blame] | 422 | if (fSwizzler) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 423 | // use swizzler to sample row |
| 424 | fSwizzler->swizzle(dst, dstRow); |
| 425 | dst = SkTAddOffset<JSAMPLE>(dst, rowBytes); |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 426 | } else { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 427 | dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes); |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 428 | } |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 429 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 430 | return count; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 431 | } |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 432 | |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 433 | #ifndef TURBO_HAS_SKIP |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 434 | // TODO (msarett): Avoid reallocating the memory buffer on each call to skip. |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 435 | static uint32_t jpeg_skip_scanlines(jpeg_decompress_struct* dinfo, int count) { |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 436 | SkAutoMalloc storage(get_row_bytes(dinfo)); |
| 437 | uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); |
| 438 | for (int y = 0; y < count; y++) { |
| 439 | if (1 != jpeg_read_scanlines(dinfo, &storagePtr, 1)) { |
| 440 | return y; |
| 441 | } |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 442 | } |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 443 | return count; |
| 444 | } |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 445 | #endif |
| 446 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 447 | bool SkJpegCodec::onSkipScanlines(int count) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 448 | // Set the jump location for libjpeg errors |
| 449 | if (setjmp(fDecoderMgr->getJmpBuf())) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 450 | return fDecoderMgr->returnFalse("setjmp"); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 451 | } |
| 452 | |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 453 | return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 454 | } |