| 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_codec.h" | 11 #include "SkJpegUtility_codec.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 "jpeglibmangler.h" |
| 22 #include "jerror.h" | 23 #include "jerror.h" |
| 23 #include "jmorecfg.h" | |
| 24 #include "jpegint.h" | 24 #include "jpegint.h" |
| 25 #include "jpeglib.h" | 25 #include "jpeglib.h" |
| 26 } | 26 } |
| 27 | 27 |
| 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 /* | 28 /* |
| 33 * Get the source configuarion for the swizzler | 29 * Convert a row of CMYK samples to RGBA in place. |
| 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 /* | |
| 59 * Convert a row of CMYK samples to RGBX in place. | |
| 60 * Note that this method moves the row pointer. | 30 * Note that this method moves the row pointer. |
| 61 * @param width the number of pixels in the row that is being converted | 31 * @param width the number of pixels in the row that is being converted |
| 62 * CMYK is stored as four bytes per pixel | 32 * CMYK is stored as four bytes per pixel |
| 63 */ | 33 */ |
| 64 static void convert_CMYK_to_RGB(uint8_t* row, uint32_t width) { | 34 static void convert_CMYK_to_RGBA(uint8_t* row, uint32_t width) { |
| 65 // We will implement a crude conversion from CMYK -> RGB using formulas | 35 // We will implement a crude conversion from CMYK -> RGB using formulas |
| 66 // from easyrgb.com. | 36 // from easyrgb.com. |
| 67 // | 37 // |
| 68 // CMYK -> CMY | 38 // CMYK -> CMY |
| 69 // C = C * (1 - K) + K | 39 // C = C * (1 - K) + K |
| 70 // M = M * (1 - K) + K | 40 // M = M * (1 - K) + K |
| 71 // Y = Y * (1 - K) + K | 41 // Y = Y * (1 - K) + K |
| 72 // | 42 // |
| 73 // libjpeg actually gives us inverted CMYK, so we must subtract the | 43 // libjpeg actually gives us inverted CMYK, so we must subtract the |
| 74 // original terms from 1. | 44 // original terms from 1. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 97 // | 67 // |
| 98 // As a final note, we have treated the CMYK values as if they were on | 68 // 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. | 69 // 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 | 70 // We must divide each CMYK component by 255 to obtain the true conversion |
| 101 // we should perform. | 71 // we should perform. |
| 102 // CMYK -> RGB | 72 // CMYK -> RGB |
| 103 // R = C * K / 255 | 73 // R = C * K / 255 |
| 104 // G = M * K / 255 | 74 // G = M * K / 255 |
| 105 // B = Y * K / 255 | 75 // B = Y * K / 255 |
| 106 for (uint32_t x = 0; x < width; x++, row += 4) { | 76 for (uint32_t x = 0; x < width; x++, row += 4) { |
| 77 #if defined(SK_PMCOLOR_IS_RGBA) |
| 107 row[0] = SkMulDiv255Round(row[0], row[3]); | 78 row[0] = SkMulDiv255Round(row[0], row[3]); |
| 108 row[1] = SkMulDiv255Round(row[1], row[3]); | 79 row[1] = SkMulDiv255Round(row[1], row[3]); |
| 109 row[2] = SkMulDiv255Round(row[2], row[3]); | 80 row[2] = SkMulDiv255Round(row[2], row[3]); |
| 81 #else |
| 82 uint8_t tmp = row[0]; |
| 83 row[0] = SkMulDiv255Round(row[2], row[3]); |
| 84 row[1] = SkMulDiv255Round(row[1], row[3]); |
| 85 row[2] = SkMulDiv255Round(tmp, row[3]); |
| 86 #endif |
| 110 row[3] = 0xFF; | 87 row[3] = 0xFF; |
| 111 } | 88 } |
| 112 } | 89 } |
| 113 | 90 |
| 114 bool SkJpegCodec::IsJpeg(SkStream* stream) { | 91 bool SkJpegCodec::IsJpeg(SkStream* stream) { |
| 115 static const uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF }; | 92 static const uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF }; |
| 116 char buffer[sizeof(jpegSig)]; | 93 char buffer[sizeof(jpegSig)]; |
| 117 return stream->read(buffer, sizeof(jpegSig)) == sizeof(jpegSig) && | 94 return stream->read(buffer, sizeof(jpegSig)) == sizeof(jpegSig) && |
| 118 !memcmp(buffer, jpegSig, sizeof(jpegSig)); | 95 !memcmp(buffer, jpegSig, sizeof(jpegSig)); |
| 119 } | 96 } |
| 120 | 97 |
| 121 bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut, | 98 bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut, |
| 122 JpegDecoderMgr** decoderMgrOut) { | 99 JpegDecoderMgr** decoderMgrOut) { |
| 123 | 100 |
| 124 // Create a JpegDecoderMgr to own all of the decompress information | 101 // Create a JpegDecoderMgr to own all of the decompress information |
| 125 SkAutoTDelete<JpegDecoderMgr> decoderMgr(SkNEW_ARGS(JpegDecoderMgr, (stream)
)); | 102 SkAutoTDelete<JpegDecoderMgr> decoderMgr(SkNEW_ARGS(JpegDecoderMgr, (stream)
)); |
| 126 | 103 |
| 127 // libjpeg errors will be caught and reported here | 104 // libjpeg errors will be caught and reported here |
| 128 if (setjmp(decoderMgr->getJmpBuf())) { | 105 if (setjmp(decoderMgr->getJmpBuf())) { |
| 129 return decoderMgr->returnFalse("setjmp"); | 106 return decoderMgr->returnFalse("setjmp"); |
| 130 } | 107 } |
| 131 | 108 |
| 132 // Initialize the decompress info and the source manager | 109 // Initialize the decompress info and the source manager |
| 133 decoderMgr->init(); | 110 decoderMgr->init(); |
| 134 | 111 |
| 135 // Read the jpeg header | 112 // Read the jpeg header |
| 136 if (JPEG_HEADER_OK != jpeg_read_header(decoderMgr->dinfo(), true)) { | 113 if (JPEG_HEADER_OK != turbo_jpeg_read_header(decoderMgr->dinfo(), true)) { |
| 137 return decoderMgr->returnFalse("read_header"); | 114 return decoderMgr->returnFalse("read_header"); |
| 138 } | 115 } |
| 139 | 116 |
| 140 if (NULL != codecOut) { | 117 if (NULL != codecOut) { |
| 141 // Recommend the color type to decode to | 118 // Recommend the color type to decode to |
| 142 const SkColorType colorType = decoderMgr->getColorType(); | 119 const SkColorType colorType = decoderMgr->getColorType(); |
| 143 | 120 |
| 144 // Create image info object and the codec | 121 // Create image info object and the codec |
| 145 const SkImageInfo& imageInfo = SkImageInfo::Make(decoderMgr->dinfo()->im
age_width, | 122 const SkImageInfo& imageInfo = SkImageInfo::Make(decoderMgr->dinfo()->im
age_width, |
| 146 decoderMgr->dinfo()->image_height, colorType, kOpaque_SkAlphaTyp
e); | 123 decoderMgr->dinfo()->image_height, colorType, kOpaque_SkAlphaTyp
e); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 161 streamDeleter.detach(); | 138 streamDeleter.detach(); |
| 162 return codec; | 139 return codec; |
| 163 } | 140 } |
| 164 return NULL; | 141 return NULL; |
| 165 } | 142 } |
| 166 | 143 |
| 167 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, | 144 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, |
| 168 JpegDecoderMgr* decoderMgr) | 145 JpegDecoderMgr* decoderMgr) |
| 169 : INHERITED(srcInfo, stream) | 146 : INHERITED(srcInfo, stream) |
| 170 , fDecoderMgr(decoderMgr) | 147 , fDecoderMgr(decoderMgr) |
| 171 , fSwizzler(NULL) | |
| 172 , fSrcRowBytes(0) | |
| 173 {} | 148 {} |
| 174 | 149 |
| 175 /* | 150 /* |
| 176 * Return a valid set of output dimensions for this decoder, given an input scal
e | 151 * Return a valid set of output dimensions for this decoder, given an input scal
e |
| 177 */ | 152 */ |
| 178 SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const { | 153 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 | 154 // libjpeg-turbo supports scaling by 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and
1/1, so we will |
| 180 long scale; | 155 // support these as well |
| 181 if (desiredScale > 0.75f) { | 156 long num; |
| 182 scale = 1; | 157 long denom = 8; |
| 158 if (desiredScale > 0.875f) { |
| 159 num = 8; |
| 160 } else if (desiredScale > 0.75f) { |
| 161 num = 7; |
| 162 } else if (desiredScale > 0.625f) { |
| 163 num = 6; |
| 164 } else if (desiredScale > 0.5f) { |
| 165 num = 5; |
| 183 } else if (desiredScale > 0.375f) { | 166 } else if (desiredScale > 0.375f) { |
| 184 scale = 2; | 167 num = 4; |
| 185 } else if (desiredScale > 0.1875f) { | 168 } else if (desiredScale > 0.25f) { |
| 186 scale = 4; | 169 num = 3; |
| 170 } else if (desiredScale > 0.125f) { |
| 171 num = 2; |
| 187 } else { | 172 } else { |
| 188 scale = 8; | 173 num = 1; |
| 189 } | 174 } |
| 190 | 175 |
| 191 // Set up a fake decompress struct in order to use libjpeg to calculate outp
ut dimensions | 176 // Set up a fake decompress struct in order to use libjpeg to calculate outp
ut dimensions |
| 192 jpeg_decompress_struct dinfo; | 177 jpeg_decompress_struct dinfo; |
| 193 sk_bzero(&dinfo, sizeof(dinfo)); | 178 sk_bzero(&dinfo, sizeof(dinfo)); |
| 194 dinfo.image_width = this->getInfo().width(); | 179 dinfo.image_width = this->getInfo().width(); |
| 195 dinfo.image_height = this->getInfo().height(); | 180 dinfo.image_height = this->getInfo().height(); |
| 196 dinfo.global_state = DSTATE_READY; | 181 dinfo.global_state = DSTATE_READY; |
| 197 dinfo.num_components = 0; | 182 dinfo.num_components = 0; |
| 198 dinfo.scale_num = 1; | 183 dinfo.scale_num = num; |
| 199 dinfo.scale_denom = scale; | 184 dinfo.scale_denom = denom; |
| 200 jpeg_calc_output_dimensions(&dinfo); | 185 turbo_jpeg_calc_output_dimensions(&dinfo); |
| 201 | 186 |
| 202 // Return the calculated output dimensions for the given scale | 187 // Return the calculated output dimensions for the given scale |
| 203 return SkISize::Make(dinfo.output_width, dinfo.output_height); | 188 return SkISize::Make(dinfo.output_width, dinfo.output_height); |
| 204 } | 189 } |
| 205 | 190 |
| 206 /* | 191 /* |
| 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 | 192 * Handles rewinding the input stream if it is necessary |
| 233 */ | 193 */ |
| 234 bool SkJpegCodec::handleRewind() { | 194 bool SkJpegCodec::handleRewind() { |
| 235 switch(this->rewindIfNeeded()) { | 195 switch(this->rewindIfNeeded()) { |
| 236 case kCouldNotRewind_RewindState: | 196 case kCouldNotRewind_RewindState: |
| 237 return fDecoderMgr->returnFalse("could not rewind"); | 197 return fDecoderMgr->returnFalse("could not rewind"); |
| 238 case kRewound_RewindState: { | 198 case kRewound_RewindState: { |
| 239 JpegDecoderMgr* decoderMgr = NULL; | 199 JpegDecoderMgr* decoderMgr = NULL; |
| 240 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) { | 200 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) { |
| 241 return fDecoderMgr->returnFalse("could not rewind"); | 201 return fDecoderMgr->returnFalse("could not rewind"); |
| 242 } | 202 } |
| 243 SkASSERT(NULL != decoderMgr); | 203 SkASSERT(NULL != decoderMgr); |
| 244 fDecoderMgr.reset(decoderMgr); | 204 fDecoderMgr.reset(decoderMgr); |
| 245 return true; | 205 return true; |
| 246 } | 206 } |
| 247 case kNoRewindNecessary_RewindState: | 207 case kNoRewindNecessary_RewindState: |
| 248 return true; | 208 return true; |
| 249 default: | 209 default: |
| 250 SkASSERT(false); | 210 SkASSERT(false); |
| 251 return false; | 211 return false; |
| 252 } | 212 } |
| 253 } | 213 } |
| 254 | 214 |
| 255 /* | 215 /* |
| 216 * Checks if the conversion between the input image and the requested output |
| 217 * image has been implemented |
| 218 * Sets the output color space |
| 219 */ |
| 220 bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dst) { |
| 221 const SkImageInfo& src = this->getInfo(); |
| 222 |
| 223 // Ensure that the profile type is unchanged |
| 224 if (dst.profileType() != src.profileType()) { |
| 225 return false; |
| 226 } |
| 227 |
| 228 // Ensure that the alpha type is opaque |
| 229 if (kOpaque_SkAlphaType != dst.alphaType()) { |
| 230 return false; |
| 231 } |
| 232 |
| 233 // Check if we will decode to CMYK because a conversion to RGBA is not suppo
rted |
| 234 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->jpeg_color_space; |
| 235 bool isCMYK = JCS_CMYK == colorSpace || JCS_YCCK == colorSpace; |
| 236 |
| 237 // Check for valid color types and set the output color space |
| 238 switch (dst.colorType()) { |
| 239 case kN32_SkColorType: |
| 240 if (isCMYK) { |
| 241 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
| 242 } else { |
| 243 // Check the byte ordering of the RGBA color space for the |
| 244 // current platform |
| 245 #if defined(SK_PMCOLOR_IS_RGBA) |
| 246 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; |
| 247 #else |
| 248 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA; |
| 249 #endif |
| 250 } |
| 251 return true; |
| 252 case kRGB_565_SkColorType: |
| 253 if (isCMYK) { |
| 254 return false; |
| 255 } else { |
| 256 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565; |
| 257 } |
| 258 return true; |
| 259 case kGray_8_SkColorType: |
| 260 if (isCMYK) { |
| 261 return false; |
| 262 } else { |
| 263 // We will enable decodes to gray even if the image is color bec
ause this is |
| 264 // much faster than decoding to color and then converting |
| 265 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE; |
| 266 } |
| 267 return true; |
| 268 default: |
| 269 return false; |
| 270 } |
| 271 } |
| 272 |
| 273 /* |
| 256 * Checks if we can scale to the requested dimensions and scales the dimensions | 274 * Checks if we can scale to the requested dimensions and scales the dimensions |
| 257 * if possible | 275 * if possible |
| 258 */ | 276 */ |
| 259 bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) { | 277 bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) { |
| 260 // libjpeg can scale to 1/1, 1/2, 1/4, and 1/8 | 278 // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1 |
| 261 SkASSERT(1 == fDecoderMgr->dinfo()->scale_num); | 279 fDecoderMgr->dinfo()->scale_denom = 8; |
| 262 SkASSERT(1 == fDecoderMgr->dinfo()->scale_denom); | 280 fDecoderMgr->dinfo()->scale_num = 8; |
| 263 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); | 281 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); |
| 264 while (fDecoderMgr->dinfo()->output_width != dstWidth || | 282 while (fDecoderMgr->dinfo()->output_width != dstWidth || |
| 265 fDecoderMgr->dinfo()->output_height != dstHeight) { | 283 fDecoderMgr->dinfo()->output_height != dstHeight) { |
| 266 | 284 |
| 267 // Return a failure if we have tried all of the possible scales | 285 // Return a failure if we have tried all of the possible scales |
| 268 if (8 == fDecoderMgr->dinfo()->scale_denom || | 286 if (1 == fDecoderMgr->dinfo()->scale_num || |
| 269 dstWidth > fDecoderMgr->dinfo()->output_width || | 287 dstWidth > fDecoderMgr->dinfo()->output_width || |
| 270 dstHeight > fDecoderMgr->dinfo()->output_height) { | 288 dstHeight > fDecoderMgr->dinfo()->output_height) { |
| 271 return fDecoderMgr->returnFalse("could not scale to requested dimens
ions"); | 289 return fDecoderMgr->returnFalse("could not scale to requested dimens
ions"); |
| 272 } | 290 } |
| 273 | 291 |
| 274 // Try the next scale | 292 // Try the next scale |
| 275 fDecoderMgr->dinfo()->scale_denom *= 2; | 293 fDecoderMgr->dinfo()->scale_num -= 1; |
| 276 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); | 294 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); |
| 277 } | 295 } |
| 278 return true; | 296 return true; |
| 279 } | 297 } |
| 280 | 298 |
| 281 /* | 299 /* |
| 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 | 300 * Performs the jpeg decode |
| 295 */ | 301 */ |
| 296 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, | 302 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 297 void* dst, size_t dstRowBytes, | 303 void* dst, size_t dstRowBytes, |
| 298 const Options& options, SkPMColor*, int
*) { | 304 const Options& options, SkPMColor*, int
*) { |
| 299 | 305 |
| 300 // Do not allow a regular decode if the caller has asked for a scanline deco
der | 306 // Do not allow a regular decode if the caller has asked for a scanline deco
der |
| 301 if (NULL != this->scanlineDecoder()) { | 307 if (NULL != this->scanlineDecoder()) { |
| 302 return fDecoderMgr->returnFailure("cannot getPixels() if a scanline deco
der has been" | 308 return fDecoderMgr->returnFailure("cannot getPixels() if a scanline deco
der has been" |
| 303 "created", kInvalidParameters); | 309 "created", kInvalidParameters); |
| 304 } | 310 } |
| 305 | 311 |
| 306 // Rewind the stream if needed | 312 // Rewind the stream if needed |
| 307 if (!this->handleRewind()) { | 313 if (!this->handleRewind()) { |
| 308 return fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRe
wind); | 314 return fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRe
wind); |
| 309 } | 315 } |
| 310 | 316 |
| 311 // Get a pointer to the decompress info since we will use it quite frequentl
y | 317 // Get a pointer to the decompress info since we will use it quite frequentl
y |
| 312 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); | 318 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); |
| 313 | 319 |
| 314 // Set the jump location for libjpeg errors | 320 // Set the jump location for libjpeg errors |
| 315 if (setjmp(fDecoderMgr->getJmpBuf())) { | 321 if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 316 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 322 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
| 317 } | 323 } |
| 318 | 324 |
| 319 // Check if we can decode to the requested destination | 325 // Check if we can decode to the requested destination and set the output co
lor space |
| 320 if (!conversion_possible(dstInfo, this->getInfo())) { | 326 if (!this->setOutputColorSpace(dstInfo)) { |
| 321 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers
ion); | 327 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers
ion); |
| 322 } | 328 } |
| 323 | 329 |
| 324 // Perform the necessary scaling | 330 // Perform the necessary scaling |
| 325 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { | 331 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { |
| 326 fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidSca
le); | 332 return fDecoderMgr->returnFailure("cannot scale to requested dims", kInv
alidScale); |
| 327 } | 333 } |
| 328 | 334 |
| 329 // Now, given valid output dimensions, we can start the decompress | 335 // Now, given valid output dimensions, we can start the decompress |
| 330 if (!jpeg_start_decompress(dinfo)) { | 336 if (!turbo_jpeg_start_decompress(dinfo)) { |
| 331 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); | 337 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); |
| 332 } | 338 } |
| 333 | 339 |
| 334 // Create the swizzler | 340 // The recommended output buffer height should always be 1 in high quality m
odes. |
| 335 this->initializeSwizzler(dstInfo, dst, dstRowBytes, options); | 341 // If it's not, we want to know because it means our strategy is not optimal
. |
| 336 if (NULL == fSwizzler) { | 342 SkASSERT(1 == dinfo->rec_outbuf_height); |
| 337 return fDecoderMgr->returnFailure("getSwizzler", kUnimplemented); | |
| 338 } | |
| 339 | 343 |
| 340 // This is usually 1, but can also be 2 or 4. | 344 // Perform the decode a single row at a time |
| 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 * ro
wsPerDecode)); | |
| 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 | |
| 358 uint32_t dstHeight = dstInfo.height(); | 345 uint32_t dstHeight = dstInfo.height(); |
| 359 for (uint32_t y = 0; y < dstHeight + rowsPerDecode - 1; y += rowsPerDecode)
{ | 346 JSAMPLE* dstRow = (JSAMPLE*) dst; |
| 347 for (uint32_t y = 0; y < dstHeight; y++) { |
| 360 // Read rows of the image | 348 // Read rows of the image |
| 361 uint32_t rowsDecoded = jpeg_read_scanlines(dinfo, srcRows, rowsPerDecode
); | 349 uint32_t rowsDecoded = turbo_jpeg_read_scanlines(dinfo, &dstRow, 1); |
| 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 } | |
| 372 | 350 |
| 373 // If we cannot read enough rows, assume the input is incomplete | 351 // If we cannot read enough rows, assume the input is incomplete |
| 374 if (rowsDecoded < rowsPerDecode && y + rowsDecoded < dstHeight) { | 352 if (rowsDecoded != 1) { |
| 375 // Fill the remainder of the image with black. This error handling | 353 // Fill the remainder of the image with black. This error handling |
| 376 // behavior is unspecified but SkCodec consistently uses black as | 354 // behavior is unspecified but SkCodec consistently uses black as |
| 377 // the fill color for opaque images. If the destination is kGray, | 355 // the fill color for opaque images. If the destination is kGray, |
| 378 // the low 8 bits of SK_ColorBLACK will be used. Conveniently, | 356 // the low 8 bits of SK_ColorBLACK will be used. Conveniently, |
| 379 // these are zeros, which is the representation for black in kGray. | 357 // these are zeros, which is the representation for black in kGray. |
| 380 SkSwizzler::Fill(fSwizzler->getDstRow(), dstInfo, dstRowBytes, | 358 SkSwizzler::Fill(dstRow, dstInfo, dstRowBytes, dstHeight - y, SK_Col
orBLACK, NULL); |
| 381 dstHeight - y - rowsDecoded, SK_ColorBLACK, NULL); | |
| 382 | 359 |
| 383 // Prevent libjpeg from failing on incomplete decode | 360 // Prevent libjpeg from failing on incomplete decode |
| 384 dinfo->output_scanline = dstHeight; | 361 dinfo->output_scanline = dstHeight; |
| 385 | 362 |
| 386 // Finish the decode and indicate that the input was incomplete. | 363 // Finish the decode and indicate that the input was incomplete. |
| 387 jpeg_finish_decompress(dinfo); | 364 turbo_jpeg_finish_decompress(dinfo); |
| 388 return fDecoderMgr->returnFailure("Incomplete image data", kIncomple
teInput); | 365 return fDecoderMgr->returnFailure("Incomplete image data", kIncomple
teInput); |
| 389 } | 366 } |
| 367 |
| 368 // Convert to RGBA if necessary |
| 369 if (JCS_CMYK == dinfo->out_color_space) { |
| 370 convert_CMYK_to_RGBA(dstRow, dstInfo.width()); |
| 371 } |
| 372 |
| 373 // Move to the next row |
| 374 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes); |
| 390 } | 375 } |
| 391 jpeg_finish_decompress(dinfo); | 376 turbo_jpeg_finish_decompress(dinfo); |
| 392 | 377 |
| 393 return kSuccess; | 378 return kSuccess; |
| 394 } | 379 } |
| 395 | 380 |
| 396 /* | 381 /* |
| 397 * We override the destructor to ensure that the scanline decoder is left in a | 382 * We override the destructor to ensure that the scanline decoder is left in a |
| 398 * finished state before destroying the decode manager. | 383 * finished state before destroying the decode manager. |
| 399 */ | 384 */ |
| 400 SkJpegCodec::~SkJpegCodec() { | 385 SkJpegCodec::~SkJpegCodec() { |
| 401 SkAutoTDelete<SkScanlineDecoder> decoder(this->detachScanlineDecoder()); | 386 SkAutoTDelete<SkScanlineDecoder> decoder(this->detachScanlineDecoder()); |
| 402 if (NULL != decoder) { | 387 if (NULL != decoder) { |
| 403 if (setjmp(fDecoderMgr->getJmpBuf())) { | 388 if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 404 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); | 389 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); |
| 405 return; | 390 return; |
| 406 } | 391 } |
| 407 | 392 |
| 408 // We may not have decoded the entire image. Prevent libjpeg-turbo from
failing on a | 393 // We may not have decoded the entire image. Prevent libjpeg-turbo from
failing on a |
| 409 // partial decode. | 394 // partial decode. |
| 410 fDecoderMgr->dinfo()->output_scanline = this->getInfo().height(); | 395 fDecoderMgr->dinfo()->output_scanline = this->getInfo().height(); |
| 411 jpeg_finish_decompress(fDecoderMgr->dinfo()); | 396 turbo_jpeg_finish_decompress(fDecoderMgr->dinfo()); |
| 412 } | 397 } |
| 413 } | 398 } |
| 414 | 399 |
| 415 /* | 400 /* |
| 416 * Enable scanline decoding for jpegs | 401 * Enable scanline decoding for jpegs |
| 417 */ | 402 */ |
| 418 class SkJpegScanlineDecoder : public SkScanlineDecoder { | 403 class SkJpegScanlineDecoder : public SkScanlineDecoder { |
| 419 public: | 404 public: |
| 420 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec) | 405 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec) |
| 421 : INHERITED(dstInfo) | 406 : INHERITED(dstInfo) |
| 422 , fCodec(codec) | 407 , fCodec(codec) |
| 423 { | 408 {} |
| 424 fStorage.reset(fCodec->fSrcRowBytes); | |
| 425 fSrcRow = static_cast<uint8_t*>(fStorage.get()); | |
| 426 } | |
| 427 | 409 |
| 428 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowByte
s) override { | 410 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowByte
s) override { |
| 429 // Set the jump location for libjpeg errors | 411 // Set the jump location for libjpeg errors |
| 430 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 412 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
| 431 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator
::kInvalidInput); | 413 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator
::kInvalidInput); |
| 432 } | 414 } |
| 433 | 415 |
| 434 // Read rows one at a time | 416 // Read rows one at a time |
| 417 JSAMPLE* dstRow = (JSAMPLE*) dst; |
| 435 for (int y = 0; y < count; y++) { | 418 for (int y = 0; y < count; y++) { |
| 436 // Read row of the image | 419 // Read row of the image |
| 437 uint32_t rowsDecoded = jpeg_read_scanlines(fCodec->fDecoderMgr->dinf
o(), &fSrcRow, 1); | 420 uint32_t rowsDecoded = |
| 421 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dst
Row, 1); |
| 438 if (rowsDecoded != 1) { | 422 if (rowsDecoded != 1) { |
| 439 SkSwizzler::Fill(dst, this->dstInfo(), rowBytes, count - y, SK_C
olorBLACK, NULL); | 423 SkSwizzler::Fill( |
| 424 dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBL
ACK, NULL); |
| 425 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo().
height(); |
| 440 return SkImageGenerator::kIncompleteInput; | 426 return SkImageGenerator::kIncompleteInput; |
| 441 } | 427 } |
| 442 | 428 |
| 443 // Convert to RGB if necessary | 429 // Convert to RGBA if necessary |
| 444 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { | 430 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { |
| 445 convert_CMYK_to_RGB(fSrcRow, dstInfo().width()); | 431 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width()); |
| 446 } | 432 } |
| 447 | 433 |
| 448 // Swizzle to output destination | 434 // Move to the next row |
| 449 fCodec->fSwizzler->setDstRow(dst); | 435 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes); |
| 450 fCodec->fSwizzler->next(fSrcRow); | |
| 451 dst = SkTAddOffset<void>(dst, rowBytes); | |
| 452 } | 436 } |
| 453 | 437 |
| 454 return SkImageGenerator::kSuccess; | 438 return SkImageGenerator::kSuccess; |
| 455 } | 439 } |
| 456 | 440 |
| 441 #ifndef TURBO_HAS_SKIP |
| 442 #define turbo_jpeg_skip_scanlines(dinfo, count) \ |
| 443 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \ |
| 444 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \ |
| 445 for (int y = 0; y < count; y++) { \ |
| 446 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \ |
| 447 } |
| 448 #endif |
| 449 |
| 457 SkImageGenerator::Result onSkipScanlines(int count) override { | 450 SkImageGenerator::Result onSkipScanlines(int count) override { |
| 458 // Set the jump location for libjpeg errors | 451 // Set the jump location for libjpeg errors |
| 459 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 452 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
| 460 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator
::kInvalidInput); | 453 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator
::kInvalidInput); |
| 461 } | 454 } |
| 462 | 455 |
| 463 // Read rows but ignore the output | 456 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); |
| 464 for (int y = 0; y < count; y++) { | |
| 465 jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &fSrcRow, 1); | |
| 466 } | |
| 467 | 457 |
| 468 return SkImageGenerator::kSuccess; | 458 return SkImageGenerator::kSuccess; |
| 469 } | 459 } |
| 470 | 460 |
| 471 private: | 461 private: |
| 472 SkJpegCodec* fCodec; // unowned | 462 SkJpegCodec* fCodec; // unowned |
| 473 SkAutoMalloc fStorage; | |
| 474 uint8_t* fSrcRow; // ptr into fStorage | |
| 475 | 463 |
| 476 typedef SkScanlineDecoder INHERITED; | 464 typedef SkScanlineDecoder INHERITED; |
| 477 }; | 465 }; |
| 478 | 466 |
| 479 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, | 467 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, |
| 480 const Options& options, SkPMColor ctable[], int* ctableCount) { | 468 const Options& options, SkPMColor ctable[], int* ctableCount) { |
| 481 | 469 |
| 482 // Rewind the stream if needed | 470 // Rewind the stream if needed |
| 483 if (!this->handleRewind()) { | 471 if (!this->handleRewind()) { |
| 484 SkCodecPrintf("Could not rewind\n"); | 472 SkCodecPrintf("Could not rewind\n"); |
| 485 return NULL; | 473 return NULL; |
| 486 } | 474 } |
| 487 | 475 |
| 488 // Set the jump location for libjpeg errors | 476 // Set the jump location for libjpeg errors |
| 489 if (setjmp(fDecoderMgr->getJmpBuf())) { | 477 if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 490 SkCodecPrintf("setjmp: Error from libjpeg\n"); | 478 SkCodecPrintf("setjmp: Error from libjpeg\n"); |
| 491 return NULL; | 479 return NULL; |
| 492 } | 480 } |
| 493 | 481 |
| 494 // Check if we can decode to the requested destination | 482 // Check if we can decode to the requested destination and set the output co
lor space |
| 495 if (!conversion_possible(dstInfo, this->getInfo())) { | 483 if (!this->setOutputColorSpace(dstInfo)) { |
| 496 SkCodecPrintf("Cannot convert to output type\n"); | 484 SkCodecPrintf("Cannot convert to output type\n"); |
| 497 return NULL; | 485 return NULL; |
| 498 } | 486 } |
| 499 | 487 |
| 500 // Perform the necessary scaling | 488 // Perform the necessary scaling |
| 501 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { | 489 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { |
| 502 SkCodecPrintf("Cannot scale ot output dimensions\n"); | 490 SkCodecPrintf("Cannot scale to output dimensions\n"); |
| 503 return NULL; | 491 return NULL; |
| 504 } | 492 } |
| 505 | 493 |
| 506 // Now, given valid output dimensions, we can start the decompress | 494 // Now, given valid output dimensions, we can start the decompress |
| 507 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { | 495 if (!turbo_jpeg_start_decompress(fDecoderMgr->dinfo())) { |
| 508 SkCodecPrintf("start decompress failed\n"); | 496 SkCodecPrintf("start decompress failed\n"); |
| 509 return NULL; | 497 return NULL; |
| 510 } | 498 } |
| 511 | 499 |
| 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 | |
| 519 // Return the new scanline decoder | 500 // Return the new scanline decoder |
| 520 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this)); | 501 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this)); |
| 521 } | 502 } |
| OLD | NEW |