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 // Rewind the stream if needed | 300 // Rewind the stream if needed |
307 if (!this->handleRewind()) { | 301 if (!this->handleRewind()) { |
308 fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind); | 302 fDecoderMgr->returnFailure("could not rewind stream", kCouldNotRewind); |
309 } | 303 } |
310 | 304 |
311 // Get a pointer to the decompress info since we will use it quite frequentl
y | 305 // Get a pointer to the decompress info since we will use it quite frequentl
y |
312 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); | 306 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); |
313 | 307 |
314 // Set the jump location for libjpeg errors | 308 // Set the jump location for libjpeg errors |
315 if (setjmp(fDecoderMgr->getJmpBuf())) { | 309 if (setjmp(fDecoderMgr->getJmpBuf())) { |
316 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 310 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
317 } | 311 } |
318 | 312 |
319 // Check if we can decode to the requested destination and set the output co
lor space | 313 // Check if we can decode to the requested destination |
320 if (!this->setOutputColorSpace(dstInfo)) { | 314 if (!conversion_possible(dstInfo, this->getInfo())) { |
321 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers
ion); | 315 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers
ion); |
322 } | 316 } |
323 | 317 |
324 // Perform the necessary scaling | 318 // Perform the necessary scaling |
325 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { | 319 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { |
326 return fDecoderMgr->returnFailure("cannot scale to requested dims", kInv
alidScale); | 320 fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidSca
le); |
327 } | 321 } |
328 | 322 |
329 // Now, given valid output dimensions, we can start the decompress | 323 // Now, given valid output dimensions, we can start the decompress |
330 if (!turbo_jpeg_start_decompress(dinfo)) { | 324 if (!jpeg_start_decompress(dinfo)) { |
331 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); | 325 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); |
332 } | 326 } |
333 | 327 |
334 // The recommended output buffer height should always be 1 in high quality m
odes. | 328 // Create the swizzler |
335 // If it's not, we want to know because it means our strategy is not optimal
. | 329 this->initializeSwizzler(dstInfo, dst, dstRowBytes, options); |
336 SkASSERT(1 == dinfo->rec_outbuf_height); | 330 if (NULL == fSwizzler) { |
| 331 return fDecoderMgr->returnFailure("getSwizzler", kUnimplemented); |
| 332 } |
337 | 333 |
338 // Perform the decode a single row at a time | 334 // This is usually 1, but can also be 2 or 4. |
| 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 |
339 uint32_t dstHeight = dstInfo.height(); | 352 uint32_t dstHeight = dstInfo.height(); |
340 JSAMPLE* dstRow = (JSAMPLE*) dst; | 353 for (uint32_t y = 0; y < dstHeight + rowsPerDecode - 1; y += rowsPerDecode)
{ |
341 for (uint32_t y = 0; y < dstHeight; y++) { | |
342 // Read rows of the image | 354 // Read rows of the image |
343 uint32_t rowsDecoded = turbo_jpeg_read_scanlines(dinfo, &dstRow, 1); | 355 uint32_t rowsDecoded = jpeg_read_scanlines(dinfo, srcRows, rowsPerDecode
); |
| 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 } |
344 | 366 |
345 // If we cannot read enough rows, assume the input is incomplete | 367 // If we cannot read enough rows, assume the input is incomplete |
346 if (rowsDecoded != 1) { | 368 if (rowsDecoded < rowsPerDecode && y + rowsDecoded < dstHeight) { |
347 // Fill the remainder of the image with black. This error handling | 369 // Fill the remainder of the image with black. This error handling |
348 // behavior is unspecified but SkCodec consistently uses black as | 370 // behavior is unspecified but SkCodec consistently uses black as |
349 // the fill color for opaque images. If the destination is kGray, | 371 // the fill color for opaque images. If the destination is kGray, |
350 // the low 8 bits of SK_ColorBLACK will be used. Conveniently, | 372 // the low 8 bits of SK_ColorBLACK will be used. Conveniently, |
351 // these are zeros, which is the representation for black in kGray. | 373 // these are zeros, which is the representation for black in kGray. |
352 SkSwizzler::Fill(dstRow, dstInfo, dstRowBytes, dstHeight - y, SK_Col
orBLACK, NULL); | 374 SkSwizzler::Fill(fSwizzler->getDstRow(), dstInfo, dstRowBytes, |
| 375 dstHeight - y - rowsDecoded, SK_ColorBLACK, NULL); |
353 | 376 |
354 // Prevent libjpeg from failing on incomplete decode | 377 // Prevent libjpeg from failing on incomplete decode |
355 dinfo->output_scanline = dstHeight; | 378 dinfo->output_scanline = dstHeight; |
356 | 379 |
357 // Finish the decode and indicate that the input was incomplete. | 380 // Finish the decode and indicate that the input was incomplete. |
358 turbo_jpeg_finish_decompress(dinfo); | 381 jpeg_finish_decompress(dinfo); |
359 return fDecoderMgr->returnFailure("Incomplete image data", kIncomple
teInput); | 382 return fDecoderMgr->returnFailure("Incomplete image data", kIncomple
teInput); |
360 } | 383 } |
361 | |
362 // Convert to RGBA if necessary | |
363 if (JCS_CMYK == dinfo->out_color_space) { | |
364 convert_CMYK_to_RGBA(dstRow, dstInfo.width()); | |
365 } | |
366 | |
367 // Move to the next row | |
368 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes); | |
369 } | 384 } |
370 turbo_jpeg_finish_decompress(dinfo); | 385 jpeg_finish_decompress(dinfo); |
371 | 386 |
372 return kSuccess; | 387 return kSuccess; |
373 } | 388 } |
374 | 389 |
375 /* | 390 /* |
376 * Enable scanline decoding for jpegs | 391 * Enable scanline decoding for jpegs |
377 */ | 392 */ |
378 class SkJpegScanlineDecoder : public SkScanlineDecoder { | 393 class SkJpegScanlineDecoder : public SkScanlineDecoder { |
379 public: | 394 public: |
380 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec) | 395 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec) |
381 : INHERITED(dstInfo) | 396 : INHERITED(dstInfo) |
382 , fCodec(codec) | 397 , fCodec(codec) |
383 {} | 398 { |
| 399 fStorage.reset(fCodec->fSrcRowBytes); |
| 400 fSrcRow = static_cast<uint8_t*>(fStorage.get()); |
| 401 } |
384 | 402 |
385 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowByte
s) override { | 403 SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowByte
s) override { |
386 // Set the jump location for libjpeg errors | 404 // Set the jump location for libjpeg errors |
387 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 405 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
388 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator
::kInvalidInput); | 406 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator
::kInvalidInput); |
389 } | 407 } |
390 | 408 |
391 // Read rows one at a time | 409 // Read rows one at a time |
392 JSAMPLE* dstRow = (JSAMPLE*) dst; | |
393 for (int y = 0; y < count; y++) { | 410 for (int y = 0; y < count; y++) { |
394 // Read row of the image | 411 // Read row of the image |
395 uint32_t rowsDecoded = | 412 uint32_t rowsDecoded = jpeg_read_scanlines(fCodec->fDecoderMgr->dinf
o(), &fSrcRow, 1); |
396 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dst
Row, 1); | |
397 if (rowsDecoded != 1) { | 413 if (rowsDecoded != 1) { |
398 SkSwizzler::Fill( | 414 SkSwizzler::Fill(dst, this->dstInfo(), rowBytes, count - y, SK_C
olorBLACK, NULL); |
399 dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBL
ACK, NULL); | |
400 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo().
height(); | |
401 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); | |
402 return SkImageGenerator::kIncompleteInput; | 415 return SkImageGenerator::kIncompleteInput; |
403 } | 416 } |
404 | 417 |
405 // Convert to RGBA if necessary | 418 // Convert to RGB if necessary |
406 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { | 419 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { |
407 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width()); | 420 convert_CMYK_to_RGB(fSrcRow, dstInfo().width()); |
408 } | 421 } |
409 | 422 |
410 // Move to the next row | 423 // Swizzle to output destination |
411 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes); | 424 fCodec->fSwizzler->setDstRow(dst); |
| 425 fCodec->fSwizzler->next(fSrcRow); |
| 426 dst = SkTAddOffset<void>(dst, rowBytes); |
412 } | 427 } |
413 | 428 |
414 return SkImageGenerator::kSuccess; | 429 return SkImageGenerator::kSuccess; |
415 } | 430 } |
416 | 431 |
417 #ifndef TURBO_HAS_SKIP | |
418 #define turbo_jpeg_skip_scanlines(dinfo, count) \ | |
419 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \ | |
420 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \ | |
421 for (int y = 0; y < count; y++) { \ | |
422 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \ | |
423 } | |
424 #endif | |
425 | |
426 SkImageGenerator::Result onSkipScanlines(int count) override { | 432 SkImageGenerator::Result onSkipScanlines(int count) override { |
427 // Set the jump location for libjpeg errors | 433 // Set the jump location for libjpeg errors |
428 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 434 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
429 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator
::kInvalidInput); | 435 return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator
::kInvalidInput); |
430 } | 436 } |
431 | 437 |
432 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); | 438 // Read rows but ignore the output |
| 439 for (int y = 0; y < count; y++) { |
| 440 jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &fSrcRow, 1); |
| 441 } |
433 | 442 |
434 return SkImageGenerator::kSuccess; | 443 return SkImageGenerator::kSuccess; |
435 } | 444 } |
436 | 445 |
437 void onFinish() override { | 446 void onFinish() override { |
438 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 447 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
439 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); | 448 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); |
440 return; | 449 return; |
441 } | 450 } |
442 | 451 |
443 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); | 452 jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); |
444 } | 453 } |
445 | 454 |
446 private: | 455 private: |
447 SkJpegCodec* fCodec; // unowned | 456 SkJpegCodec* fCodec; // unowned |
| 457 SkAutoMalloc fStorage; |
| 458 uint8_t* fSrcRow; // ptr into fStorage |
448 | 459 |
449 typedef SkScanlineDecoder INHERITED; | 460 typedef SkScanlineDecoder INHERITED; |
450 }; | 461 }; |
451 | 462 |
452 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, | 463 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, |
453 const Options& options, SkPMColor ctable[], int* ctableCount) { | 464 const Options& options, SkPMColor ctable[], int* ctableCount) { |
454 | 465 |
455 // Rewind the stream if needed | 466 // Rewind the stream if needed |
456 if (!this->handleRewind()) { | 467 if (!this->handleRewind()) { |
457 SkCodecPrintf("Could not rewind\n"); | 468 SkCodecPrintf("Could not rewind\n"); |
458 return NULL; | 469 return NULL; |
459 } | 470 } |
460 | 471 |
461 // Set the jump location for libjpeg errors | 472 // Set the jump location for libjpeg errors |
462 if (setjmp(fDecoderMgr->getJmpBuf())) { | 473 if (setjmp(fDecoderMgr->getJmpBuf())) { |
463 SkCodecPrintf("setjmp: Error from libjpeg\n"); | 474 SkCodecPrintf("setjmp: Error from libjpeg\n"); |
464 return NULL; | 475 return NULL; |
465 } | 476 } |
466 | 477 |
467 // Check if we can decode to the requested destination and set the output co
lor space | 478 // Check if we can decode to the requested destination |
468 if (!this->setOutputColorSpace(dstInfo)) { | 479 if (!conversion_possible(dstInfo, this->getInfo())) { |
469 SkCodecPrintf("Cannot convert to output type\n"); | 480 SkCodecPrintf("Cannot convert to output type\n"); |
470 return NULL; | 481 return NULL; |
471 } | 482 } |
472 | 483 |
473 // Perform the necessary scaling | 484 // Perform the necessary scaling |
474 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { | 485 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { |
475 SkCodecPrintf("Cannot scale to output dimensions\n"); | 486 SkCodecPrintf("Cannot scale ot output dimensions\n"); |
476 return NULL; | 487 return NULL; |
477 } | 488 } |
478 | 489 |
479 // Now, given valid output dimensions, we can start the decompress | 490 // Now, given valid output dimensions, we can start the decompress |
480 if (!turbo_jpeg_start_decompress(fDecoderMgr->dinfo())) { | 491 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { |
481 SkCodecPrintf("start decompress failed\n"); | 492 SkCodecPrintf("start decompress failed\n"); |
482 return NULL; | 493 return NULL; |
483 } | 494 } |
484 | 495 |
| 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 |
485 // Return the new scanline decoder | 503 // Return the new scanline decoder |
486 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this)); | 504 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this)); |
487 } | 505 } |
OLD | NEW |