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 "SkMSAN.h" | 9 #include "SkMSAN.h" |
10 #include "SkJpegCodec.h" | 10 #include "SkJpegCodec.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 JpegDecoderMgr* decoderMgr) | 79 JpegDecoderMgr* decoderMgr) |
80 : INHERITED(srcInfo, stream) | 80 : INHERITED(srcInfo, stream) |
81 , fDecoderMgr(decoderMgr) | 81 , fDecoderMgr(decoderMgr) |
82 , fReadyState(decoderMgr->dinfo()->global_state) | 82 , fReadyState(decoderMgr->dinfo()->global_state) |
83 {} | 83 {} |
84 | 84 |
85 /* | 85 /* |
86 * Return the row bytes of a particular image type and width | 86 * Return the row bytes of a particular image type and width |
87 */ | 87 */ |
88 static size_t get_row_bytes(const j_decompress_ptr dinfo) { | 88 static size_t get_row_bytes(const j_decompress_ptr dinfo) { |
89 size_t colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 : dinfo->out_
color_components; | 89 #ifdef TURBO_HAS_565 |
| 90 const size_t colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 : |
| 91 dinfo->out_color_components; |
| 92 #else |
| 93 const size_t colorBytes = dinfo->out_color_components; |
| 94 #endif |
90 return dinfo->output_width * colorBytes; | 95 return dinfo->output_width * colorBytes; |
91 | 96 |
92 } | 97 } |
93 | 98 |
94 /* | 99 /* |
95 * Calculate output dimensions based on the provided factors. | 100 * Calculate output dimensions based on the provided factors. |
96 * | 101 * |
97 * Not to be used on the actual jpeg_decompress_struct used for decoding, since
it will | 102 * Not to be used on the actual jpeg_decompress_struct used for decoding, since
it will |
98 * incorrectly modify num_components. | 103 * incorrectly modify num_components. |
99 */ | 104 */ |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 bool isCMYK = JCS_CMYK == colorSpace || JCS_YCCK == colorSpace; | 184 bool isCMYK = JCS_CMYK == colorSpace || JCS_YCCK == colorSpace; |
180 | 185 |
181 // Check for valid color types and set the output color space | 186 // Check for valid color types and set the output color space |
182 switch (dst.colorType()) { | 187 switch (dst.colorType()) { |
183 case kN32_SkColorType: | 188 case kN32_SkColorType: |
184 if (isCMYK) { | 189 if (isCMYK) { |
185 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; | 190 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
186 } else { | 191 } else { |
187 // Check the byte ordering of the RGBA color space for the | 192 // Check the byte ordering of the RGBA color space for the |
188 // current platform | 193 // current platform |
189 #if defined(SK_PMCOLOR_IS_RGBA) | 194 #if defined(SK_PMCOLOR_IS_RGBA) && defined(LIBJPEG_TURBO_VERSION) |
190 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; | 195 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; |
| 196 #elif defined(LIBJPEG_TURBO_VERSION) |
| 197 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA; |
191 #else | 198 #else |
192 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA; | 199 fDecoderMgr->dinfo()->out_color_space = JCS_RGB; |
193 #endif | 200 #endif |
194 } | 201 } |
195 return true; | 202 return true; |
196 case kRGB_565_SkColorType: | 203 case kRGB_565_SkColorType: |
197 if (isCMYK) { | 204 if (isCMYK) { |
198 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; | 205 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; |
199 } else { | 206 } else { |
| 207 #ifdef TURBO_HAS_565 |
200 fDecoderMgr->dinfo()->dither_mode = JDITHER_NONE; | 208 fDecoderMgr->dinfo()->dither_mode = JDITHER_NONE; |
201 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565; | 209 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565; |
| 210 #else |
| 211 fDecoderMgr->dinfo()->out_color_space = JCS_RGB; |
| 212 #endif |
202 } | 213 } |
203 return true; | 214 return true; |
204 case kGray_8_SkColorType: | 215 case kGray_8_SkColorType: |
205 if (isCMYK) { | 216 if (isCMYK) { |
206 return false; | 217 return false; |
207 } else { | 218 } else { |
208 // We will enable decodes to gray even if the image is color bec
ause this is | 219 // We will enable decodes to gray even if the image is color bec
ause this is |
209 // much faster than decoding to color and then converting | 220 // much faster than decoding to color and then converting |
210 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE; | 221 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE; |
211 } | 222 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 | 294 |
284 // Now, given valid output dimensions, we can start the decompress | 295 // Now, given valid output dimensions, we can start the decompress |
285 if (!jpeg_start_decompress(dinfo)) { | 296 if (!jpeg_start_decompress(dinfo)) { |
286 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); | 297 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); |
287 } | 298 } |
288 | 299 |
289 // The recommended output buffer height should always be 1 in high quality m
odes. | 300 // The recommended output buffer height should always be 1 in high quality m
odes. |
290 // If it's not, we want to know because it means our strategy is not optimal
. | 301 // If it's not, we want to know because it means our strategy is not optimal
. |
291 SkASSERT(1 == dinfo->rec_outbuf_height); | 302 SkASSERT(1 == dinfo->rec_outbuf_height); |
292 | 303 |
293 if (JCS_CMYK == dinfo->out_color_space) { | 304 J_COLOR_SPACE colorSpace = dinfo->out_color_space; |
| 305 #ifdef TURBO_HAS_565 |
| 306 if (JCS_CMYK == colorSpace) { |
| 307 #else |
| 308 if (JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { |
| 309 #endif |
294 this->initializeSwizzler(dstInfo, options); | 310 this->initializeSwizzler(dstInfo, options); |
295 } | 311 } |
296 | 312 |
297 // Perform the decode a single row at a time | 313 // Perform the decode a single row at a time |
298 uint32_t dstHeight = dstInfo.height(); | 314 uint32_t dstHeight = dstInfo.height(); |
299 | 315 |
300 JSAMPLE* dstRow; | 316 JSAMPLE* dstRow; |
301 if (fSwizzler) { | 317 if (fSwizzler) { |
302 // write data to storage row, then sample using swizzler | 318 // write data to storage row, then sample using swizzler |
303 dstRow = fSrcRow; | 319 dstRow = fSrcRow; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 break; | 362 break; |
347 case kRGB_565_SkColorType: | 363 case kRGB_565_SkColorType: |
348 srcConfig = SkSwizzler::kNoOp16; | 364 srcConfig = SkSwizzler::kNoOp16; |
349 break; | 365 break; |
350 default: | 366 default: |
351 // This function should only be called if the colorType is suppo
rted by jpeg | 367 // This function should only be called if the colorType is suppo
rted by jpeg |
352 SkASSERT(false); | 368 SkASSERT(false); |
353 } | 369 } |
354 } | 370 } |
355 | 371 |
| 372 #ifndef TURBO_HAS_565 |
| 373 if (JCS_RGB == fDecoderMgr->dinfo()->out_color_space) { |
| 374 srcConfig = SkSwizzler::kRGB; |
| 375 } |
| 376 #endif |
| 377 |
356 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, dstInfo, opti
ons)); | 378 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, dstInfo, opti
ons)); |
357 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); | 379 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); |
358 fSrcRow = fStorage.get(); | 380 fSrcRow = fStorage.get(); |
359 } | 381 } |
360 | 382 |
361 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { | 383 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { |
362 if (!createIfNecessary || fSwizzler) { | 384 if (!createIfNecessary || fSwizzler) { |
363 SkASSERT(!fSwizzler || (fSrcRow && fStorage.get() == fSrcRow)); | 385 SkASSERT(!fSwizzler || (fSrcRow && fStorage.get() == fSrcRow)); |
364 return fSwizzler; | 386 return fSwizzler; |
365 } | 387 } |
(...skipping 21 matching lines...) Expand all Loading... |
387 fStorage.free(); | 409 fStorage.free(); |
388 | 410 |
389 // Now, given valid output dimensions, we can start the decompress | 411 // Now, given valid output dimensions, we can start the decompress |
390 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { | 412 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { |
391 SkCodecPrintf("start decompress failed\n"); | 413 SkCodecPrintf("start decompress failed\n"); |
392 return kInvalidInput; | 414 return kInvalidInput; |
393 } | 415 } |
394 | 416 |
395 // We will need a swizzler if we are performing a subset decode or | 417 // We will need a swizzler if we are performing a subset decode or |
396 // converting from CMYK. | 418 // converting from CMYK. |
397 if (options.fSubset || JCS_CMYK == fDecoderMgr->dinfo()->out_color_space) { | 419 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->out_color_space; |
| 420 #ifdef TURBO_HAS_565 |
| 421 if (options.fSubset || JCS_CMYK == colorSpace) { |
| 422 #else |
| 423 if (options.fSubset || JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { |
| 424 #endif |
398 this->initializeSwizzler(dstInfo, options); | 425 this->initializeSwizzler(dstInfo, options); |
399 } | 426 } |
400 | 427 |
401 return kSuccess; | 428 return kSuccess; |
402 } | 429 } |
403 | 430 |
404 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { | 431 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { |
405 // Set the jump location for libjpeg errors | 432 // Set the jump location for libjpeg errors |
406 if (setjmp(fDecoderMgr->getJmpBuf())) { | 433 if (setjmp(fDecoderMgr->getJmpBuf())) { |
407 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 434 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
646 | 673 |
647 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); | 674 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); |
648 if (linesRead < remainingRows) { | 675 if (linesRead < remainingRows) { |
649 // FIXME: Handle incomplete YUV decodes without signalling an error. | 676 // FIXME: Handle incomplete YUV decodes without signalling an error. |
650 return kInvalidInput; | 677 return kInvalidInput; |
651 } | 678 } |
652 } | 679 } |
653 | 680 |
654 return kSuccess; | 681 return kSuccess; |
655 } | 682 } |
OLD | NEW |