Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCodec.h" | 8 #include "SkCodec.h" |
| 9 #include "SkJpegCodec.h" | 9 #include "SkJpegCodec.h" |
| 10 #include "SkJpegDecoderMgr.h" | 10 #include "SkJpegDecoderMgr.h" |
| 11 #include "SkJpegUtility.h" | 11 #include "SkJpegUtility.h" |
| 12 #include "SkCodecPriv.h" | 12 #include "SkCodecPriv.h" |
| 13 #include "SkColorPriv.h" | 13 #include "SkColorPriv.h" |
| 14 #include "SkStream.h" | 14 #include "SkStream.h" |
| 15 #include "SkTemplates.h" | 15 #include "SkTemplates.h" |
| 16 #include "SkTypes.h" | 16 #include "SkTypes.h" |
| 17 | 17 |
| 18 // stdio is needed for jpeglib | 18 // stdio is needed for libjpeg-turbo |
| 19 #include <stdio.h> | 19 #include <stdio.h> |
| 20 | 20 |
| 21 extern "C" { | 21 extern "C" { |
| 22 #include "jerror.h" | 22 #include "jerror.h" |
| 23 #include "jmorecfg.h" | |
| 24 #include "jpegint.h" | 23 #include "jpegint.h" |
| 25 #include "jpeglib.h" | 24 #include "jpeglib.h" |
| 26 } | 25 } |
| 27 | 26 |
| 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 | |
| 32 /* | |
| 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_spac e) { | |
| 53 return SkSwizzler::kGray; | |
| 54 } | |
| 55 return SkSwizzler::kUnknown; | |
| 56 } | |
| 57 | |
| 58 /* | 27 /* |
| 59 * Convert a row of CMYK samples to RGBX in place. | 28 * Convert a row of CMYK samples to RGBX in place. |
| 60 * Note that this method moves the row pointer. | 29 * Note that this method moves the row pointer. |
| 61 * @param width the number of pixels in the row that is being converted | 30 * @param width the number of pixels in the row that is being converted |
| 62 * CMYK is stored as four bytes per pixel | 31 * CMYK is stored as four bytes per pixel |
| 63 */ | 32 */ |
| 64 static void convert_CMYK_to_RGB(uint8_t* row, uint32_t width) { | 33 static void convert_CMYK_to_RGBA(uint8_t* row, uint32_t width) { |
|
scroggo
2015/06/16 20:39:22
The comment says RGBX. I think X implies A isn't t
msarett
2015/06/18 21:06:10
Done.
| |
| 65 // We will implement a crude conversion from CMYK -> RGB using formulas | 34 // We will implement a crude conversion from CMYK -> RGB using formulas |
| 66 // from easyrgb.com. | 35 // from easyrgb.com. |
| 67 // | 36 // |
| 68 // CMYK -> CMY | 37 // CMYK -> CMY |
| 69 // C = C * (1 - K) + K | 38 // C = C * (1 - K) + K |
| 70 // M = M * (1 - K) + K | 39 // M = M * (1 - K) + K |
| 71 // Y = Y * (1 - K) + K | 40 // Y = Y * (1 - K) + K |
| 72 // | 41 // |
| 73 // libjpeg actually gives us inverted CMYK, so we must subtract the | 42 // libjpeg actually gives us inverted CMYK, so we must subtract the |
| 74 // original terms from 1. | 43 // original terms from 1. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 streamDeleter.detach(); | 130 streamDeleter.detach(); |
| 162 return codec; | 131 return codec; |
| 163 } | 132 } |
| 164 return NULL; | 133 return NULL; |
| 165 } | 134 } |
| 166 | 135 |
| 167 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, | 136 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, |
| 168 JpegDecoderMgr* decoderMgr) | 137 JpegDecoderMgr* decoderMgr) |
| 169 : INHERITED(srcInfo, stream) | 138 : INHERITED(srcInfo, stream) |
| 170 , fDecoderMgr(decoderMgr) | 139 , fDecoderMgr(decoderMgr) |
| 171 , fSwizzler(NULL) | |
| 172 , fSrcRowBytes(0) | |
| 173 {} | 140 {} |
| 174 | 141 |
| 175 /* | 142 /* |
| 176 * Return a valid set of output dimensions for this decoder, given an input scal e | 143 * Return a valid set of output dimensions for this decoder, given an input scal e |
| 177 */ | 144 */ |
| 178 SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const { | 145 SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const { |
| 179 // libjpeg supports scaling by 1/1, 1/2, 1/4, and 1/8, so we will support th ese as well | 146 // libjpeg supports scaling by 1/1, 1/2, 1/4, and 1/8, so we will support th ese as well |
|
scroggo
2015/06/16 20:39:22
Update this comment?
msarett
2015/06/18 21:06:10
Done.
| |
| 180 long scale; | 147 long num; |
| 181 if (desiredScale > 0.75f) { | 148 long denom = 8; |
| 182 scale = 1; | 149 if (desiredScale > 1.875f) { |
|
scroggo
2015/06/16 20:39:22
Wait, libjpeg scales up? What is the advantage to
msarett
2015/06/18 21:06:10
Oops, I added this for testing jpeg_skip_scanlines
| |
| 150 num = 16; | |
| 151 } else if (desiredScale > 1.75f) { | |
| 152 num = 15; | |
| 153 } else if (desiredScale > 1.625f) { | |
| 154 num = 14; | |
| 155 } else if (desiredScale > 1.5f) { | |
| 156 num = 13; | |
| 157 } else if (desiredScale > 1.375f) { | |
| 158 num = 12; | |
| 159 } else if (desiredScale > 1.25f) { | |
| 160 num = 11; | |
| 161 } else if (desiredScale > 1.125f) { | |
| 162 num = 10; | |
| 163 } else if (desiredScale > 1.0f) { | |
| 164 num = 9; | |
| 165 } else if (desiredScale > 0.875f) { | |
| 166 num = 8; | |
| 167 } else if (desiredScale > 0.75f) { | |
| 168 num = 7; | |
| 169 } else if (desiredScale > 0.625f) { | |
| 170 num = 6; | |
| 171 } else if (desiredScale > 0.5f) { | |
| 172 num = 5; | |
| 183 } else if (desiredScale > 0.375f) { | 173 } else if (desiredScale > 0.375f) { |
| 184 scale = 2; | 174 num = 4; |
| 185 } else if (desiredScale > 0.1875f) { | 175 } else if (desiredScale > 0.25f) { |
| 186 scale = 4; | 176 num = 3; |
| 177 } else if (desiredScale > 0.125f) { | |
| 178 num = 2; | |
| 187 } else { | 179 } else { |
| 188 scale = 8; | 180 num = 1; |
| 189 } | 181 } |
| 190 | 182 |
| 191 // Set up a fake decompress struct in order to use libjpeg to calculate outp ut dimensions | 183 // Set up a fake decompress struct in order to use libjpeg to calculate outp ut dimensions |
| 192 jpeg_decompress_struct dinfo; | 184 jpeg_decompress_struct dinfo; |
| 193 sk_bzero(&dinfo, sizeof(dinfo)); | 185 sk_bzero(&dinfo, sizeof(dinfo)); |
| 194 dinfo.image_width = this->getInfo().width(); | 186 dinfo.image_width = this->getInfo().width(); |
| 195 dinfo.image_height = this->getInfo().height(); | 187 dinfo.image_height = this->getInfo().height(); |
| 196 dinfo.global_state = DSTATE_READY; | 188 dinfo.global_state = DSTATE_READY; |
| 197 dinfo.num_components = 0; | 189 dinfo.num_components = 0; |
| 198 dinfo.scale_num = 1; | 190 dinfo.scale_num = num; |
| 199 dinfo.scale_denom = scale; | 191 dinfo.scale_denom = denom; |
| 200 jpeg_calc_output_dimensions(&dinfo); | 192 jpeg_calc_output_dimensions(&dinfo); |
| 201 | 193 |
| 202 // Return the calculated output dimensions for the given scale | 194 // Return the calculated output dimensions for the given scale |
| 203 return SkISize::Make(dinfo.output_width, dinfo.output_height); | 195 return SkISize::Make(dinfo.output_width, dinfo.output_height); |
| 204 } | 196 } |
| 205 | 197 |
| 206 /* | 198 /* |
| 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 recommendatio n | |
| 228 return dst.colorType() == src.colorType(); | |
| 229 } | |
| 230 | |
| 231 /* | |
| 232 * Handles rewinding the input stream if it is necessary | 199 * Handles rewinding the input stream if it is necessary |
| 233 */ | 200 */ |
| 234 bool SkJpegCodec::handleRewind() { | 201 bool SkJpegCodec::handleRewind() { |
| 235 switch(this->rewindIfNeeded()) { | 202 switch(this->rewindIfNeeded()) { |
| 236 case kCouldNotRewind_RewindState: | 203 case kCouldNotRewind_RewindState: |
| 237 return fDecoderMgr->returnFalse("could not rewind"); | 204 return fDecoderMgr->returnFalse("could not rewind"); |
| 238 case kRewound_RewindState: { | 205 case kRewound_RewindState: { |
| 239 JpegDecoderMgr* decoderMgr = NULL; | 206 JpegDecoderMgr* decoderMgr = NULL; |
| 240 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) { | 207 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) { |
| 241 return fDecoderMgr->returnFalse("could not rewind"); | 208 return fDecoderMgr->returnFalse("could not rewind"); |
| 242 } | 209 } |
| 243 SkASSERT(NULL != decoderMgr); | 210 SkASSERT(NULL != decoderMgr); |
| 244 fDecoderMgr.reset(decoderMgr); | 211 fDecoderMgr.reset(decoderMgr); |
| 245 return true; | 212 return true; |
| 246 } | 213 } |
| 247 case kNoRewindNecessary_RewindState: | 214 case kNoRewindNecessary_RewindState: |
| 248 return true; | 215 return true; |
| 249 default: | 216 default: |
| 250 SkASSERT(false); | 217 SkASSERT(false); |
| 251 return false; | 218 return false; |
| 252 } | 219 } |
| 253 } | 220 } |
| 254 | 221 |
| 255 /* | 222 /* |
| 223 * Checks if the conversion between the input image and the requested output | |
| 224 * image has been implemented | |
| 225 * Sets the output color space | |
| 226 */ | |
| 227 bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dst) { | |
| 228 const SkImageInfo& src = this->getInfo(); | |
| 229 | |
| 230 // Ensure that the profile type is unchanged | |
| 231 if (dst.profileType() != src.profileType()) { | |
| 232 return false; | |
| 233 } | |
| 234 | |
| 235 // Ensure that the alpha type is opaque | |
| 236 if (kOpaque_SkAlphaType != dst.alphaType()) { | |
| 237 return false; | |
| 238 } | |
| 239 | |
| 240 // Check if we will decode to CMYK because a conversion to RGBA is not suppo rted | |
| 241 bool isCMYK = JCS_CMYK == fDecoderMgr->dinfo()->jpeg_color_space || | |
|
scroggo
2015/06/16 20:39:22
nit: I find this more readable if you use a tempor
msarett
2015/06/18 21:06:10
Done.
| |
| 242 JCS_YCCK == fDecoderMgr->dinfo()->jpeg_color_space; | |
| 243 | |
| 244 // Check for valid color types and set the output color space | |
| 245 switch (dst.colorType()) { | |
| 246 case kN32_SkColorType: | |
| 247 if (isCMYK) { | |
| 248 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; | |
| 249 } else { | |
| 250 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; | |
| 251 } | |
| 252 return true; | |
| 253 case kRGB_565_SkColorType: | |
| 254 if (isCMYK) { | |
| 255 return false; | |
| 256 } else { | |
| 257 //fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; | |
| 258 // TODO (msarett): | |
|
scroggo
2015/06/16 20:39:22
What is the TODO here?
msarett
2015/06/18 21:06:10
This has been done! The original issue was caused
| |
| 259 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565; | |
| 260 } | |
| 261 return true; | |
| 262 case kGray_8_SkColorType: | |
| 263 if (isCMYK) { | |
| 264 return false; | |
| 265 } else { | |
| 266 // We will enable decodes to gray even if the image is color bec ause this is | |
|
scroggo
2015/06/16 20:39:22
Does libjpeg(-turbo) support converting full color
msarett
2015/06/18 21:06:10
I have no recollection of writing this comment, an
| |
| 267 // much faster than decoding to color and then converting | |
| 268 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE; | |
| 269 } | |
| 270 return true; | |
| 271 default: | |
| 272 return false; | |
| 273 } | |
| 274 } | |
| 275 | |
| 276 /* | |
| 256 * Checks if we can scale to the requested dimensions and scales the dimensions | 277 * Checks if we can scale to the requested dimensions and scales the dimensions |
| 257 * if possible | 278 * if possible |
| 258 */ | 279 */ |
| 259 bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) { | 280 bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) { |
| 260 // libjpeg can scale to 1/1, 1/2, 1/4, and 1/8 | 281 // libjpeg can scale to 1/1, 1/2, 1/4, and 1/8 |
| 261 SkASSERT(1 == fDecoderMgr->dinfo()->scale_num); | 282 SkASSERT(1 == fDecoderMgr->dinfo()->scale_num); |
| 262 SkASSERT(1 == fDecoderMgr->dinfo()->scale_denom); | 283 SkASSERT(1 == fDecoderMgr->dinfo()->scale_denom); |
| 284 fDecoderMgr->dinfo()->scale_denom = 8; | |
| 263 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); | 285 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); |
| 264 while (fDecoderMgr->dinfo()->output_width != dstWidth || | 286 while (fDecoderMgr->dinfo()->output_width != dstWidth || |
| 265 fDecoderMgr->dinfo()->output_height != dstHeight) { | 287 fDecoderMgr->dinfo()->output_height != dstHeight) { |
| 266 | 288 |
| 267 // Return a failure if we have tried all of the possible scales | 289 // Return a failure if we have tried all of the possible scales |
| 268 if (8 == fDecoderMgr->dinfo()->scale_denom || | 290 if (16 == fDecoderMgr->dinfo()->scale_num || |
| 269 dstWidth > fDecoderMgr->dinfo()->output_width || | 291 dstWidth < fDecoderMgr->dinfo()->output_width || |
| 270 dstHeight > fDecoderMgr->dinfo()->output_height) { | 292 dstHeight < fDecoderMgr->dinfo()->output_height) { |
| 271 return fDecoderMgr->returnFalse("could not scale to requested dimens ions"); | 293 return fDecoderMgr->returnFalse("could not scale to requested dimens ions"); |
| 272 } | 294 } |
| 273 | 295 |
| 274 // Try the next scale | 296 // Try the next scale |
| 275 fDecoderMgr->dinfo()->scale_denom *= 2; | 297 fDecoderMgr->dinfo()->scale_num += 1; |
| 276 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); | 298 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); |
| 277 } | 299 } |
| 278 return true; | 300 return true; |
| 279 } | 301 } |
| 280 | 302 |
| 281 /* | 303 /* |
| 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, ds tRowBytes, | |
| 289 options.fZeroInitialized)); | |
| 290 fSrcRowBytes = SkSwizzler::BytesPerPixel(srcConfig) * dstInfo.width(); | |
| 291 } | |
| 292 | |
| 293 /* | |
| 294 * Performs the jpeg decode | 304 * Performs the jpeg decode |
| 295 */ | 305 */ |
| 296 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, | 306 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 297 void* dst, size_t dstRowBytes, | 307 void* dst, size_t dstRowBytes, |
| 298 const Options& options, SkPMColor*, int *) { | 308 const Options& options, SkPMColor*, int *) { |
| 299 | 309 |
| 300 // Rewind the stream if needed | 310 // Rewind the stream if needed |
| 301 if (!this->handleRewind()) { | 311 if (!this->handleRewind()) { |
| 302 fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind); | 312 fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind); |
| 303 } | 313 } |
| 304 | 314 |
| 305 // Get a pointer to the decompress info since we will use it quite frequentl y | 315 // Get a pointer to the decompress info since we will use it quite frequentl y |
| 306 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); | 316 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); |
| 307 | 317 |
| 308 // Set the jump location for libjpeg errors | 318 // Set the jump location for libjpeg errors |
| 309 if (setjmp(fDecoderMgr->getJmpBuf())) { | 319 if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 310 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 320 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
| 311 } | 321 } |
| 312 | 322 |
| 313 // Check if we can decode to the requested destination | 323 // Check if we can decode to the requested destination and set the output co lor space |
| 314 if (!conversion_possible(dstInfo, this->getInfo())) { | 324 if (!this->setOutputColorSpace(dstInfo)) { |
| 315 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers ion); | 325 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers ion); |
| 316 } | 326 } |
| 317 | 327 |
| 318 // Perform the necessary scaling | 328 // Perform the necessary scaling |
| 319 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { | 329 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { |
| 330 SkDebugf("POOPY\n"); | |
|
scroggo
2015/06/16 20:39:22
lol
msarett
2015/06/18 21:06:10
Probably not necessary...
| |
| 320 fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidSca le); | 331 fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidSca le); |
|
scroggo
2015/06/16 20:39:22
Should this say *return* fDecoderMgr->returnFailur
msarett
2015/06/18 21:06:10
Yes!
| |
| 321 } | 332 } |
| 322 | 333 |
| 323 // Now, given valid output dimensions, we can start the decompress | 334 // Now, given valid output dimensions, we can start the decompress |
| 324 if (!jpeg_start_decompress(dinfo)) { | 335 if (!jpeg_start_decompress(dinfo)) { |
| 325 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); | 336 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); |
| 326 } | 337 } |
| 327 | 338 |
| 328 // Create the swizzler | 339 // The recommended output buffer height should always be 1 in high quality m odes |
| 329 this->initializeSwizzler(dstInfo, dst, dstRowBytes, options); | 340 SkDEBUGCODE(const uint32_t rowsPerDecode = dinfo->rec_outbuf_height;) |
|
scroggo
2015/06/16 20:39:22
This variable is never used after the assert. How
msarett
2015/06/18 21:06:10
Done.
| |
| 330 if (NULL == fSwizzler) { | 341 SkASSERT(1 == rowsPerDecode); |
| 331 return fDecoderMgr->returnFailure("getSwizzler", kUnimplemented); | |
| 332 } | |
| 333 | 342 |
| 334 // This is usually 1, but can also be 2 or 4. | 343 // Perform the decode a single row at a time |
| 335 // If we wanted to always read one row at a time, we could, but we will save space and time | |
| 336 // by using the recommendation from libjpeg. | |
| 337 const uint32_t rowsPerDecode = dinfo->rec_outbuf_height; | |
| 338 SkASSERT(rowsPerDecode <= 4); | |
| 339 | |
| 340 // Create a buffer to contain decoded rows (libjpeg requires a 2D array) | |
| 341 SkASSERT(0 != fSrcRowBytes); | |
| 342 SkAutoTDeleteArray<uint8_t> srcBuffer(SkNEW_ARRAY(uint8_t, fSrcRowBytes * ro wsPerDecode)); | |
| 343 JSAMPLE* srcRows[4]; | |
| 344 uint8_t* srcPtr = srcBuffer.get(); | |
| 345 for (uint8_t i = 0; i < rowsPerDecode; i++) { | |
| 346 srcRows[i] = (JSAMPLE*) srcPtr; | |
| 347 srcPtr += fSrcRowBytes; | |
| 348 } | |
| 349 | |
| 350 // Ensure that we loop enough times to decode all of the rows | |
| 351 // libjpeg will prevent us from reading past the bottom of the image | |
| 352 uint32_t dstHeight = dstInfo.height(); | 344 uint32_t dstHeight = dstInfo.height(); |
| 353 for (uint32_t y = 0; y < dstHeight + rowsPerDecode - 1; y += rowsPerDecode) { | 345 JSAMPLE* dstRow = (JSAMPLE*) dst; |
| 346 for (uint32_t y = 0; y < dstHeight; y++) { | |
| 354 // Read rows of the image | 347 // Read rows of the image |
| 355 uint32_t rowsDecoded = jpeg_read_scanlines(dinfo, srcRows, rowsPerDecode ); | 348 uint32_t rowsDecoded = jpeg_read_scanlines(dinfo, &dstRow, 1); |
| 356 | |
| 357 // Convert to RGB if necessary | |
| 358 if (JCS_CMYK == dinfo->out_color_space) { | |
| 359 convert_CMYK_to_RGB(srcRows[0], dstInfo.width() * rowsDecoded); | |
| 360 } | |
| 361 | |
| 362 // Swizzle to output destination | |
| 363 for (uint32_t i = 0; i < rowsDecoded; i++) { | |
| 364 fSwizzler->next(srcRows[i]); | |
| 365 } | |
| 366 | 349 |
| 367 // If we cannot read enough rows, assume the input is incomplete | 350 // If we cannot read enough rows, assume the input is incomplete |
| 368 if (rowsDecoded < rowsPerDecode && y + rowsDecoded < dstHeight) { | 351 if (rowsDecoded != 1) { |
| 369 // Fill the remainder of the image with black. This error handling | 352 // Fill the remainder of the image with black. This error handling |
| 370 // behavior is unspecified but SkCodec consistently uses black as | 353 // behavior is unspecified but SkCodec consistently uses black as |
| 371 // the fill color for opaque images. If the destination is kGray, | 354 // the fill color for opaque images. If the destination is kGray, |
| 372 // the low 8 bits of SK_ColorBLACK will be used. Conveniently, | 355 // the low 8 bits of SK_ColorBLACK will be used. Conveniently, |
| 373 // these are zeros, which is the representation for black in kGray. | 356 // these are zeros, which is the representation for black in kGray. |
| 374 SkSwizzler::Fill(fSwizzler->getDstRow(), dstInfo, dstRowBytes, | 357 SkSwizzler::Fill(dstRow, dstInfo, dstRowBytes, dstHeight - y, SK_Col orBLACK, NULL); |
| 375 dstHeight - y - rowsDecoded, SK_ColorBLACK, NULL); | |
| 376 | 358 |
| 377 // Prevent libjpeg from failing on incomplete decode | 359 // Prevent libjpeg from failing on incomplete decode |
| 378 dinfo->output_scanline = dstHeight; | 360 dinfo->output_scanline = dstHeight; |
| 379 | 361 |
| 380 // Finish the decode and indicate that the input was incomplete. | 362 // Finish the decode and indicate that the input was incomplete. |
| 381 jpeg_finish_decompress(dinfo); | 363 jpeg_finish_decompress(dinfo); |
| 382 return fDecoderMgr->returnFailure("Incomplete image data", kIncomple teInput); | 364 return fDecoderMgr->returnFailure("Incomplete image data", kIncomple teInput); |
| 383 } | 365 } |
| 366 | |
| 367 // Convert to RGBA if necessary | |
| 368 if (JCS_CMYK == dinfo->out_color_space) { | |
| 369 convert_CMYK_to_RGBA(dstRow, dstInfo.width()); | |
| 370 } | |
| 371 | |
| 372 // Move to the next row | |
| 373 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes); | |
| 384 } | 374 } |
| 385 jpeg_finish_decompress(dinfo); | 375 jpeg_finish_decompress(dinfo); |
| 386 | 376 |
| 387 return kSuccess; | 377 return kSuccess; |
| 388 } | 378 } |
| 389 | 379 |
| 390 /* | 380 /* |
| 391 * Enable scanline decoding for jpegs | 381 * Enable scanline decoding for jpegs |
| 392 */ | 382 */ |
| 393 class SkJpegScanlineDecoder : public SkScanlineDecoder { | 383 class SkJpegScanlineDecoder : public SkScanlineDecoder { |
| 394 public: | 384 public: |
| 395 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec) | 385 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec) |
| 396 : INHERITED(dstInfo) | 386 : INHERITED(dstInfo) |
| 397 , fCodec(codec) | 387 , fCodec(codec) |
| 398 { | 388 { |
| 399 fStorage.reset(fCodec->fSrcRowBytes); | |
| 400 fSrcRow = static_cast<uint8_t*>(fStorage.get()); | |
| 401 } | 389 } |
| 402 | 390 |
| 403 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowByte s) override { | 391 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowByte s) override { |
| 404 // Set the jump location for libjpeg errors | 392 // Set the jump location for libjpeg errors |
| 405 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 393 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
| 406 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator ::kInvalidInput); | 394 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator ::kInvalidInput); |
| 407 } | 395 } |
| 408 | 396 |
| 409 // Read rows one at a time | 397 // Read rows one at a time |
| 398 JSAMPLE* dstRow = (JSAMPLE*) dst; | |
| 410 for (int y = 0; y < count; y++) { | 399 for (int y = 0; y < count; y++) { |
| 411 // Read row of the image | 400 // Read row of the image |
| 412 uint32_t rowsDecoded = jpeg_read_scanlines(fCodec->fDecoderMgr->dinf o(), &fSrcRow, 1); | 401 uint32_t rowsDecoded = jpeg_read_scanlines(fCodec->fDecoderMgr->dinf o(), &dstRow, 1); |
|
scroggo
2015/06/16 20:39:23
Wait, we don't need to do any conversion? (besides
msarett
2015/06/18 21:06:10
Yes we do. As it happens, this works on Android b
| |
| 413 if (rowsDecoded != 1) { | 402 if (rowsDecoded != 1) { |
| 414 SkSwizzler::Fill(dst, this->dstInfo(), rowBytes, count - y, SK_C olorBLACK, NULL); | 403 SkSwizzler::Fill(dstRow, this->dstInfo(), rowBytes, count - y, S K_ColorBLACK, NULL); |
| 404 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo(). height(); | |
|
scroggo
2015/06/16 20:39:22
Do we need to call jpeg_finish_decompress? (whethe
msarett
2015/06/18 21:06:10
Yes we should call this! I think I wrongly assume
| |
| 415 return SkImageGenerator::kIncompleteInput; | 405 return SkImageGenerator::kIncompleteInput; |
| 416 } | 406 } |
| 417 | 407 |
| 418 // Convert to RGB if necessary | 408 // Convert to RGBA if necessary |
| 419 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { | 409 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { |
| 420 convert_CMYK_to_RGB(fSrcRow, dstInfo().width()); | 410 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width()); |
| 421 } | 411 } |
| 422 | 412 |
| 423 // Swizzle to output destination | 413 // Move to the next row |
| 424 fCodec->fSwizzler->setDstRow(dst); | 414 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes); |
| 425 fCodec->fSwizzler->next(fSrcRow); | |
| 426 dst = SkTAddOffset<void>(dst, rowBytes); | |
| 427 } | 415 } |
| 428 | 416 |
| 429 return SkImageGenerator::kSuccess; | 417 return SkImageGenerator::kSuccess; |
| 430 } | 418 } |
| 431 | 419 |
| 432 SkImageGenerator::Result onSkipScanlines(int count) override { | 420 SkImageGenerator::Result onSkipScanlines(int count) override { |
| 433 // Set the jump location for libjpeg errors | 421 // Set the jump location for libjpeg errors |
| 434 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 422 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
| 435 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator ::kInvalidInput); | 423 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator ::kInvalidInput); |
| 436 } | 424 } |
| 437 | 425 |
| 438 // Read rows but ignore the output | 426 jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); |
| 439 for (int y = 0; y < count; y++) { | |
| 440 jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &fSrcRow, 1); | |
| 441 } | |
| 442 | 427 |
| 443 return SkImageGenerator::kSuccess; | 428 return SkImageGenerator::kSuccess; |
| 444 } | 429 } |
| 445 | 430 |
| 446 void onFinish() override { | 431 void onFinish() override { |
| 447 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 432 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
| 448 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); | 433 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); |
| 449 return; | 434 return; |
| 450 } | 435 } |
| 451 | 436 |
| 452 jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); | 437 jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); |
| 453 } | 438 } |
| 454 | 439 |
| 455 private: | 440 private: |
| 456 SkJpegCodec* fCodec; // unowned | 441 SkJpegCodec* fCodec; // unowned |
| 457 SkAutoMalloc fStorage; | |
| 458 uint8_t* fSrcRow; // ptr into fStorage | |
| 459 | 442 |
| 460 typedef SkScanlineDecoder INHERITED; | 443 typedef SkScanlineDecoder INHERITED; |
| 461 }; | 444 }; |
| 462 | 445 |
| 463 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, | 446 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, |
| 464 const Options& options, SkPMColor ctable[], int* ctableCount) { | 447 const Options& options, SkPMColor ctable[], int* ctableCount) { |
| 465 | 448 |
| 466 // Rewind the stream if needed | 449 // Rewind the stream if needed |
| 467 if (!this->handleRewind()) { | 450 if (!this->handleRewind()) { |
| 468 SkCodecPrintf("Could not rewind\n"); | 451 SkCodecPrintf("Could not rewind\n"); |
| 469 return NULL; | 452 return NULL; |
| 470 } | 453 } |
| 471 | 454 |
| 472 // Set the jump location for libjpeg errors | 455 // Set the jump location for libjpeg errors |
| 473 if (setjmp(fDecoderMgr->getJmpBuf())) { | 456 if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 474 SkCodecPrintf("setjmp: Error from libjpeg\n"); | 457 SkCodecPrintf("setjmp: Error from libjpeg\n"); |
| 475 return NULL; | 458 return NULL; |
| 476 } | 459 } |
| 477 | 460 |
| 478 // Check if we can decode to the requested destination | 461 // Check if we can decode to the requested destination and set the output co lor space |
| 479 if (!conversion_possible(dstInfo, this->getInfo())) { | 462 if (!this->setOutputColorSpace(dstInfo)) { |
| 480 SkCodecPrintf("Cannot convert to output type\n"); | 463 SkCodecPrintf("Cannot convert to output type\n"); |
| 481 return NULL; | 464 return NULL; |
| 482 } | 465 } |
| 483 | 466 |
| 484 // Perform the necessary scaling | 467 // Perform the necessary scaling |
| 485 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { | 468 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { |
| 486 SkCodecPrintf("Cannot scale ot output dimensions\n"); | 469 SkCodecPrintf("Cannot scale to output dimensions\n"); |
| 487 return NULL; | 470 return NULL; |
| 488 } | 471 } |
| 489 | 472 |
| 490 // Now, given valid output dimensions, we can start the decompress | 473 // Now, given valid output dimensions, we can start the decompress |
| 491 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { | 474 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { |
| 492 SkCodecPrintf("start decompress failed\n"); | 475 SkCodecPrintf("start decompress failed\n"); |
| 493 return NULL; | 476 return NULL; |
| 494 } | 477 } |
| 495 | 478 |
| 496 // Create the swizzler | |
| 497 this->initializeSwizzler(dstInfo, NULL, dstInfo.minRowBytes(), options); | |
| 498 if (NULL == fSwizzler) { | |
| 499 SkCodecPrintf("Could not create swizzler\n"); | |
| 500 return NULL; | |
| 501 } | |
| 502 | |
| 503 // Return the new scanline decoder | 479 // Return the new scanline decoder |
| 504 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this)); | 480 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this)); |
| 505 } | 481 } |
| OLD | NEW |