Chromium Code Reviews| 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 750 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 761 static SkISize compute_yuv_size(const jpeg_decompress_struct& info, int componen t, | 761 static SkISize compute_yuv_size(const jpeg_decompress_struct& info, int componen t, |
| 762 SizeType sizeType) { | 762 SizeType sizeType) { |
| 763 if (sizeType == kSizeForMemoryAllocation_SizeType) { | 763 if (sizeType == kSizeForMemoryAllocation_SizeType) { |
| 764 return SkISize::Make(info.cur_comp_info[component]->width_in_blocks * DC TSIZE, | 764 return SkISize::Make(info.cur_comp_info[component]->width_in_blocks * DC TSIZE, |
| 765 info.cur_comp_info[component]->height_in_blocks * D CTSIZE); | 765 info.cur_comp_info[component]->height_in_blocks * D CTSIZE); |
| 766 } | 766 } |
| 767 return SkISize::Make(info.cur_comp_info[component]->downsampled_width, | 767 return SkISize::Make(info.cur_comp_info[component]->downsampled_width, |
| 768 info.cur_comp_info[component]->downsampled_height); | 768 info.cur_comp_info[component]->downsampled_height); |
| 769 } | 769 } |
| 770 | 770 |
| 771 static bool appears_to_be_yuv(const jpeg_decompress_struct& info) { | |
| 772 return (info.jpeg_color_space == JCS_YCbCr) | |
| 773 && (DCTSIZE == 8) | |
| 774 && (info.num_components == 3) | |
| 775 && (info.comps_in_scan >= info.num_components) | |
| 776 && (info.scale_denom <= 8) | |
| 777 && (info.cur_comp_info[0]) | |
| 778 && (info.cur_comp_info[1]) | |
| 779 && (info.cur_comp_info[2]) | |
| 780 && (info.cur_comp_info[1]->h_samp_factor == 1) | |
| 781 && (info.cur_comp_info[1]->v_samp_factor == 1) | |
| 782 && (info.cur_comp_info[2]->h_samp_factor == 1) | |
| 783 && (info.cur_comp_info[2]->v_samp_factor == 1); | |
| 784 } | |
| 785 | |
| 771 static void update_components_sizes(const jpeg_decompress_struct& cinfo, SkISize componentSizes[3], | 786 static void update_components_sizes(const jpeg_decompress_struct& cinfo, SkISize componentSizes[3], |
| 772 SizeType sizeType) { | 787 SizeType sizeType) { |
| 788 SkASSERT(appears_to_be_yuv(cinfo)); | |
| 773 for (int i = 0; i < 3; ++i) { | 789 for (int i = 0; i < 3; ++i) { |
| 774 componentSizes[i] = compute_yuv_size(cinfo, i, sizeType); | 790 componentSizes[i] = compute_yuv_size(cinfo, i, sizeType); |
| 775 } | 791 } |
| 776 } | 792 } |
| 777 | 793 |
| 778 static bool output_raw_data(jpeg_decompress_struct& cinfo, void* planes[3], size _t rowBytes[3]) { | 794 static bool output_raw_data(jpeg_decompress_struct& cinfo, void* planes[3], size _t rowBytes[3]) { |
| 795 SkASSERT(appears_to_be_yuv(cinfo)); | |
| 779 // U size and V size have to be the same if we're calling output_raw_data() | 796 // U size and V size have to be the same if we're calling output_raw_data() |
| 780 SkISize uvSize = compute_yuv_size(cinfo, 1, kSizeForMemoryAllocation_SizeTyp e); | 797 SkISize uvSize = compute_yuv_size(cinfo, 1, kSizeForMemoryAllocation_SizeTyp e); |
| 781 SkASSERT(uvSize == compute_yuv_size(cinfo, 2, kSizeForMemoryAllocation_SizeT ype)); | 798 SkASSERT(uvSize == compute_yuv_size(cinfo, 2, kSizeForMemoryAllocation_SizeT ype)); |
| 782 | 799 |
| 783 JSAMPARRAY bufferraw[3]; | 800 JSAMPARRAY bufferraw[3]; |
| 784 JSAMPROW bufferraw2[32]; | 801 JSAMPROW bufferraw2[32]; |
| 785 bufferraw[0] = &bufferraw2[0]; // Y channel rows (8 or 16) | 802 bufferraw[0] = &bufferraw2[0]; // Y channel rows (8 or 16) |
| 786 bufferraw[1] = &bufferraw2[16]; // U channel rows (8) | 803 bufferraw[1] = &bufferraw2[16]; // U channel rows (8) |
| 787 bufferraw[2] = &bufferraw2[24]; // V channel rows (8) | 804 bufferraw[2] = &bufferraw2[24]; // V channel rows (8) |
| 788 int yWidth = cinfo.output_width; | 805 int yWidth = cinfo.output_width; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 854 | 871 |
| 855 return true; | 872 return true; |
| 856 } | 873 } |
| 857 | 874 |
| 858 bool SkJPEGImageDecoder::onDecodeYUV8Planes(SkStream* stream, SkISize componentS izes[3], | 875 bool SkJPEGImageDecoder::onDecodeYUV8Planes(SkStream* stream, SkISize componentS izes[3], |
| 859 void* planes[3], size_t rowBytes[3], | 876 void* planes[3], size_t rowBytes[3], |
| 860 SkYUVColorSpace* colorSpace) { | 877 SkYUVColorSpace* colorSpace) { |
| 861 #ifdef TIME_DECODE | 878 #ifdef TIME_DECODE |
| 862 SkAutoTime atm("JPEG YUV8 Decode"); | 879 SkAutoTime atm("JPEG YUV8 Decode"); |
| 863 #endif | 880 #endif |
| 864 | |
| 865 if (this->getSampleSize() != 1) { | 881 if (this->getSampleSize() != 1) { |
| 866 return false; // Resizing not supported | 882 return false; // Resizing not supported |
| 867 } | 883 } |
| 868 | 884 |
| 869 JPEGAutoClean autoClean; | 885 JPEGAutoClean autoClean; |
| 870 | 886 |
| 871 jpeg_decompress_struct cinfo; | 887 jpeg_decompress_struct cinfo; |
| 872 skjpeg_source_mgr srcManager(stream, this); | 888 skjpeg_source_mgr srcManager(stream, this); |
| 873 | 889 |
| 874 skjpeg_error_mgr errorManager; | 890 skjpeg_error_mgr errorManager; |
| 875 set_error_mgr(&cinfo, &errorManager); | 891 set_error_mgr(&cinfo, &errorManager); |
| 876 | 892 |
| 877 // All objects need to be instantiated before this setjmp call so that | 893 // All objects need to be instantiated before this setjmp call so that |
| 878 // they will be cleaned up properly if an error occurs. | 894 // they will be cleaned up properly if an error occurs. |
| 879 if (setjmp(errorManager.fJmpBuf)) { | 895 if (setjmp(errorManager.fJmpBuf)) { |
| 880 return return_false(cinfo, "setjmp YUV8"); | 896 return return_false(cinfo, "setjmp YUV8"); |
| 881 } | 897 } |
| 882 | 898 |
| 883 initialize_info(&cinfo, &srcManager); | 899 initialize_info(&cinfo, &srcManager); |
| 884 autoClean.set(&cinfo); | 900 autoClean.set(&cinfo); |
| 885 | 901 |
| 886 int status = jpeg_read_header(&cinfo, true); | 902 int status = jpeg_read_header(&cinfo, true); |
| 887 if (status != JPEG_HEADER_OK) { | 903 if (status != JPEG_HEADER_OK) { |
| 888 return return_false(cinfo, "read_header YUV8"); | 904 return return_false(cinfo, "read_header YUV8"); |
| 889 } | 905 } |
| 890 | 906 |
| 891 if (cinfo.jpeg_color_space != JCS_YCbCr) { | 907 if (!appears_to_be_yuv(cinfo)) { |
| 892 // It's not an error to not be encoded in YUV, so no need to use return_ false() | 908 // It's not an error to not be encoded in YUV, so no need to use return_ false() |
| 893 return false; | 909 return false; |
| 894 } | 910 } |
| 895 | 911 |
| 896 cinfo.out_color_space = JCS_YCbCr; | 912 cinfo.out_color_space = JCS_YCbCr; |
| 897 cinfo.raw_data_out = TRUE; | 913 cinfo.raw_data_out = TRUE; |
| 898 | 914 |
| 899 if (!planes || !planes[0] || !rowBytes || !rowBytes[0]) { // Compute size on ly | 915 if (!planes || !planes[0] || !rowBytes || !rowBytes[0]) { // Compute size on ly |
| 900 update_components_sizes(cinfo, componentSizes, kSizeForMemoryAllocation_ SizeType); | 916 update_components_sizes(cinfo, componentSizes, kSizeForMemoryAllocation_ SizeType); |
| 901 return true; | 917 return true; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 913 #endif | 929 #endif |
| 914 | 930 |
| 915 /* image_width and image_height are the original dimensions, available | 931 /* image_width and image_height are the original dimensions, available |
| 916 after jpeg_read_header(). To see the scaled dimensions, we have to call | 932 after jpeg_read_header(). To see the scaled dimensions, we have to call |
| 917 jpeg_start_decompress(), and then read output_width and output_height. | 933 jpeg_start_decompress(), and then read output_width and output_height. |
| 918 */ | 934 */ |
| 919 if (!jpeg_start_decompress(&cinfo)) { | 935 if (!jpeg_start_decompress(&cinfo)) { |
| 920 return return_false(cinfo, "start_decompress YUV8"); | 936 return return_false(cinfo, "start_decompress YUV8"); |
| 921 } | 937 } |
| 922 | 938 |
| 939 // Seems like jpeg_start_decompress is updating our opinion of whether cinfo represents YUV. | |
|
msarett
2015/12/21 22:35:57
Mike, I realize this is a shot in the dark, but is
mtklein
2015/12/21 23:44:52
Not sure what image triggered this, but I'm pretty
| |
| 940 // Again, not really an error. | |
| 941 if (!appears_to_be_yuv(cinfo)) { | |
| 942 return false; | |
| 943 } | |
| 944 | |
| 923 if (!output_raw_data(cinfo, planes, rowBytes)) { | 945 if (!output_raw_data(cinfo, planes, rowBytes)) { |
| 924 return return_false(cinfo, "output_raw_data"); | 946 return return_false(cinfo, "output_raw_data"); |
| 925 } | 947 } |
| 926 | 948 |
| 927 update_components_sizes(cinfo, componentSizes, kActualSize_SizeType); | 949 update_components_sizes(cinfo, componentSizes, kActualSize_SizeType); |
| 928 jpeg_finish_decompress(&cinfo); | 950 jpeg_finish_decompress(&cinfo); |
| 929 | 951 |
| 930 if (NULL != colorSpace) { | 952 if (NULL != colorSpace) { |
| 931 *colorSpace = kJPEG_SkYUVColorSpace; | 953 *colorSpace = kJPEG_SkYUVColorSpace; |
| 932 } | 954 } |
| (...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1445 return SkImageDecoder::kUnknown_Format; | 1467 return SkImageDecoder::kUnknown_Format; |
| 1446 } | 1468 } |
| 1447 | 1469 |
| 1448 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { | 1470 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { |
| 1449 return (SkImageEncoder::kJPEG_Type == t) ? SkNEW(SkJPEGImageEncoder) : NULL; | 1471 return (SkImageEncoder::kJPEG_Type == t) ? SkNEW(SkJPEGImageEncoder) : NULL; |
| 1450 } | 1472 } |
| 1451 | 1473 |
| 1452 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory); | 1474 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory); |
| 1453 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg); | 1475 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg); |
| 1454 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); | 1476 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); |
| OLD | NEW |