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