OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2007 The Android Open Source Project | 2 * Copyright 2007 The Android Open Source Project |
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 | 8 |
9 #include "SkImageDecoder.h" | 9 #include "SkImageDecoder.h" |
10 #include "SkImageEncoder.h" | 10 #include "SkImageEncoder.h" |
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 * Common code for setting the error manager. | 376 * Common code for setting the error manager. |
377 */ | 377 */ |
378 static void set_error_mgr(jpeg_decompress_struct* cinfo, skjpeg_error_mgr* error
Manager) { | 378 static void set_error_mgr(jpeg_decompress_struct* cinfo, skjpeg_error_mgr* error
Manager) { |
379 SkASSERT(cinfo != nullptr); | 379 SkASSERT(cinfo != nullptr); |
380 SkASSERT(errorManager != nullptr); | 380 SkASSERT(errorManager != nullptr); |
381 cinfo->err = jpeg_std_error(errorManager); | 381 cinfo->err = jpeg_std_error(errorManager); |
382 errorManager->error_exit = skjpeg_error_exit; | 382 errorManager->error_exit = skjpeg_error_exit; |
383 } | 383 } |
384 | 384 |
385 /** | 385 /** |
| 386 * Common code for turning off upsampling and smoothing. Turning these |
| 387 * off helps performance without showing noticable differences in the |
| 388 * resulting bitmap. |
| 389 */ |
| 390 static void turn_off_visual_optimizations(jpeg_decompress_struct* cinfo) { |
| 391 SkASSERT(cinfo != nullptr); |
| 392 /* this gives about 30% performance improvement. In theory it may |
| 393 reduce the visual quality, in practice I'm not seeing a difference |
| 394 */ |
| 395 cinfo->do_fancy_upsampling = 0; |
| 396 |
| 397 /* this gives another few percents */ |
| 398 cinfo->do_block_smoothing = 0; |
| 399 } |
| 400 |
| 401 /** |
386 * Common code for setting the dct method. | 402 * Common code for setting the dct method. |
387 */ | 403 */ |
388 static void set_dct_method(const SkImageDecoder& decoder, jpeg_decompress_struct
* cinfo) { | 404 static void set_dct_method(const SkImageDecoder& decoder, jpeg_decompress_struct
* cinfo) { |
389 SkASSERT(cinfo != nullptr); | 405 SkASSERT(cinfo != nullptr); |
| 406 #ifdef DCT_IFAST_SUPPORTED |
| 407 if (decoder.getPreferQualityOverSpeed()) { |
| 408 cinfo->dct_method = JDCT_ISLOW; |
| 409 } else { |
| 410 cinfo->dct_method = JDCT_IFAST; |
| 411 } |
| 412 #else |
390 cinfo->dct_method = JDCT_ISLOW; | 413 cinfo->dct_method = JDCT_ISLOW; |
| 414 #endif |
391 } | 415 } |
392 | 416 |
393 SkColorType SkJPEGImageDecoder::getBitmapColorType(jpeg_decompress_struct* cinfo
) { | 417 SkColorType SkJPEGImageDecoder::getBitmapColorType(jpeg_decompress_struct* cinfo
) { |
394 SkASSERT(cinfo != nullptr); | 418 SkASSERT(cinfo != nullptr); |
395 | 419 |
396 SrcDepth srcDepth = k32Bit_SrcDepth; | 420 SrcDepth srcDepth = k32Bit_SrcDepth; |
397 if (JCS_GRAYSCALE == cinfo->jpeg_color_space) { | 421 if (JCS_GRAYSCALE == cinfo->jpeg_color_space) { |
398 srcDepth = k8BitGray_SrcDepth; | 422 srcDepth = k8BitGray_SrcDepth; |
399 } | 423 } |
400 | 424 |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 can) much faster that we, just use their num/denom api to approximate | 573 can) much faster that we, just use their num/denom api to approximate |
550 the size. | 574 the size. |
551 */ | 575 */ |
552 int sampleSize = this->getSampleSize(); | 576 int sampleSize = this->getSampleSize(); |
553 | 577 |
554 set_dct_method(*this, &cinfo); | 578 set_dct_method(*this, &cinfo); |
555 | 579 |
556 SkASSERT(1 == cinfo.scale_num); | 580 SkASSERT(1 == cinfo.scale_num); |
557 cinfo.scale_denom = sampleSize; | 581 cinfo.scale_denom = sampleSize; |
558 | 582 |
| 583 turn_off_visual_optimizations(&cinfo); |
| 584 |
559 const SkColorType colorType = this->getBitmapColorType(&cinfo); | 585 const SkColorType colorType = this->getBitmapColorType(&cinfo); |
560 const SkAlphaType alphaType = kAlpha_8_SkColorType == colorType ? | 586 const SkAlphaType alphaType = kAlpha_8_SkColorType == colorType ? |
561 kPremul_SkAlphaType : kOpaque_SkAlphaType; | 587 kPremul_SkAlphaType : kOpaque_SkAlphaType; |
562 | 588 |
563 adjust_out_color_space_and_dither(&cinfo, colorType, *this); | 589 adjust_out_color_space_and_dither(&cinfo, colorType, *this); |
564 | 590 |
565 if (1 == sampleSize && SkImageDecoder::kDecodeBounds_Mode == mode) { | 591 if (1 == sampleSize && SkImageDecoder::kDecodeBounds_Mode == mode) { |
566 // Assume an A8 bitmap is not opaque to avoid the check of each | 592 // Assume an A8 bitmap is not opaque to avoid the check of each |
567 // individual pixel. It is very unlikely to be opaque, since | 593 // individual pixel. It is very unlikely to be opaque, since |
568 // an opaque A8 bitmap would not be very interesting. | 594 // an opaque A8 bitmap would not be very interesting. |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
874 if (!planes || !planes[0] || !rowBytes || !rowBytes[0]) { // Compute size on
ly | 900 if (!planes || !planes[0] || !rowBytes || !rowBytes[0]) { // Compute size on
ly |
875 update_components_sizes(cinfo, componentSizes, kSizeForMemoryAllocation_
SizeType); | 901 update_components_sizes(cinfo, componentSizes, kSizeForMemoryAllocation_
SizeType); |
876 return true; | 902 return true; |
877 } | 903 } |
878 | 904 |
879 set_dct_method(*this, &cinfo); | 905 set_dct_method(*this, &cinfo); |
880 | 906 |
881 SkASSERT(1 == cinfo.scale_num); | 907 SkASSERT(1 == cinfo.scale_num); |
882 cinfo.scale_denom = 1; | 908 cinfo.scale_denom = 1; |
883 | 909 |
| 910 turn_off_visual_optimizations(&cinfo); |
| 911 |
884 #ifdef ANDROID_RGB | 912 #ifdef ANDROID_RGB |
885 cinfo.dither_mode = JDITHER_NONE; | 913 cinfo.dither_mode = JDITHER_NONE; |
886 #endif | 914 #endif |
887 | 915 |
888 /* image_width and image_height are the original dimensions, available | 916 /* image_width and image_height are the original dimensions, available |
889 after jpeg_read_header(). To see the scaled dimensions, we have to call | 917 after jpeg_read_header(). To see the scaled dimensions, we have to call |
890 jpeg_start_decompress(), and then read output_width and output_height. | 918 jpeg_start_decompress(), and then read output_width and output_height. |
891 */ | 919 */ |
892 if (!jpeg_start_decompress(&cinfo)) { | 920 if (!jpeg_start_decompress(&cinfo)) { |
893 return return_false(cinfo, "start_decompress YUV8"); | 921 return return_false(cinfo, "start_decompress YUV8"); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
948 jpeg_decompress_struct* cinfo = imageIndex->cinfo(); | 976 jpeg_decompress_struct* cinfo = imageIndex->cinfo(); |
949 // We have a new cinfo, so set the error mgr again. | 977 // We have a new cinfo, so set the error mgr again. |
950 set_error_mgr(cinfo, &sk_err); | 978 set_error_mgr(cinfo, &sk_err); |
951 | 979 |
952 // FIXME: This sets cinfo->out_color_space, which we may change later | 980 // FIXME: This sets cinfo->out_color_space, which we may change later |
953 // based on the config in onDecodeSubset. This should be fine, since | 981 // based on the config in onDecodeSubset. This should be fine, since |
954 // jpeg_init_read_tile_scanline will check out_color_space again after | 982 // jpeg_init_read_tile_scanline will check out_color_space again after |
955 // that change (when it calls jinit_color_deconverter). | 983 // that change (when it calls jinit_color_deconverter). |
956 (void) this->getBitmapColorType(cinfo); | 984 (void) this->getBitmapColorType(cinfo); |
957 | 985 |
| 986 turn_off_visual_optimizations(cinfo); |
| 987 |
958 // instead of jpeg_start_decompress() we start a tiled decompress | 988 // instead of jpeg_start_decompress() we start a tiled decompress |
959 if (!imageIndex->startTileDecompress()) { | 989 if (!imageIndex->startTileDecompress()) { |
960 return false; | 990 return false; |
961 } | 991 } |
962 | 992 |
963 SkASSERT(1 == cinfo->scale_num); | 993 SkASSERT(1 == cinfo->scale_num); |
964 fImageWidth = cinfo->output_width; | 994 fImageWidth = cinfo->output_width; |
965 fImageHeight = cinfo->output_height; | 995 fImageHeight = cinfo->output_height; |
966 | 996 |
967 if (width) { | 997 if (width) { |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1420 return SkImageDecoder::kUnknown_Format; | 1450 return SkImageDecoder::kUnknown_Format; |
1421 } | 1451 } |
1422 | 1452 |
1423 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { | 1453 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { |
1424 return (SkImageEncoder::kJPEG_Type == t) ? new SkJPEGImageEncoder : nullptr; | 1454 return (SkImageEncoder::kJPEG_Type == t) ? new SkJPEGImageEncoder : nullptr; |
1425 } | 1455 } |
1426 | 1456 |
1427 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory); | 1457 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory); |
1428 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg); | 1458 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg); |
1429 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); | 1459 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); |
OLD | NEW |