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 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
489 int srcBytesPerPixel; | 489 int srcBytesPerPixel; |
490 | 490 |
491 if (!get_src_config(cinfo, &sc, &srcBytesPerPixel)) { | 491 if (!get_src_config(cinfo, &sc, &srcBytesPerPixel)) { |
492 return return_failure(cinfo, *bm, "jpeg colorspace"); | 492 return return_failure(cinfo, *bm, "jpeg colorspace"); |
493 } | 493 } |
494 | 494 |
495 if (!sampler.begin(bm, sc, *this)) { | 495 if (!sampler.begin(bm, sc, *this)) { |
496 return return_failure(cinfo, *bm, "sampler.begin"); | 496 return return_failure(cinfo, *bm, "sampler.begin"); |
497 } | 497 } |
498 | 498 |
499 SkAutoMalloc srcStorage(cinfo.output_width * srcBytesPerPixel); | 499 SkAutoTMalloc<uint8_t> srcStorage(cinfo.output_width * srcBytesPerPixel); |
500 uint8_t* srcRow = (uint8_t*)srcStorage.get(); | 500 uint8_t* srcRow = srcStorage.get(); |
501 | 501 |
502 // Possibly skip initial rows [sampler.srcY0] | 502 // Possibly skip initial rows [sampler.srcY0] |
503 if (!skip_src_rows(&cinfo, srcRow, sampler.srcY0())) { | 503 if (!skip_src_rows(&cinfo, srcRow, sampler.srcY0())) { |
504 return return_failure(cinfo, *bm, "skip rows"); | 504 return return_failure(cinfo, *bm, "skip rows"); |
505 } | 505 } |
506 | 506 |
507 // now loop through scanlines until y == bm->height() - 1 | 507 // now loop through scanlines until y == bm->height() - 1 |
508 for (int y = 0;; y++) { | 508 for (int y = 0;; y++) { |
509 JSAMPLE* rowptr = (JSAMPLE*)srcRow; | 509 JSAMPLE* rowptr = (JSAMPLE*)srcRow; |
510 int row_count = jpeg_read_scanlines(&cinfo, &rowptr, 1); | 510 int row_count = jpeg_read_scanlines(&cinfo, &rowptr, 1); |
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
924 SkAutoLockPixels alp(bm); | 924 SkAutoLockPixels alp(bm); |
925 if (nullptr == bm.getPixels()) { | 925 if (nullptr == bm.getPixels()) { |
926 return false; | 926 return false; |
927 } | 927 } |
928 | 928 |
929 jpeg_compress_struct cinfo; | 929 jpeg_compress_struct cinfo; |
930 skjpeg_error_mgr sk_err; | 930 skjpeg_error_mgr sk_err; |
931 skjpeg_destination_mgr sk_wstream(stream); | 931 skjpeg_destination_mgr sk_wstream(stream); |
932 | 932 |
933 // allocate these before set call setjmp | 933 // allocate these before set call setjmp |
934 SkAutoMalloc oneRow; | 934 SkAutoTMalloc<uint8_t> oneRow; |
935 | 935 |
936 cinfo.err = jpeg_std_error(&sk_err); | 936 cinfo.err = jpeg_std_error(&sk_err); |
937 sk_err.error_exit = skjpeg_error_exit; | 937 sk_err.error_exit = skjpeg_error_exit; |
938 if (setjmp(sk_err.fJmpBuf)) { | 938 if (setjmp(sk_err.fJmpBuf)) { |
939 return false; | 939 return false; |
940 } | 940 } |
941 | 941 |
942 // Keep after setjmp or mark volatile. | 942 // Keep after setjmp or mark volatile. |
943 const WriteScanline writer = ChooseWriter(bm); | 943 const WriteScanline writer = ChooseWriter(bm); |
944 if (nullptr == writer) { | 944 if (nullptr == writer) { |
(...skipping 14 matching lines...) Expand all Loading... | |
959 | 959 |
960 jpeg_set_defaults(&cinfo); | 960 jpeg_set_defaults(&cinfo); |
961 jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); | 961 jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); |
962 #ifdef DCT_IFAST_SUPPORTED | 962 #ifdef DCT_IFAST_SUPPORTED |
963 cinfo.dct_method = JDCT_IFAST; | 963 cinfo.dct_method = JDCT_IFAST; |
964 #endif | 964 #endif |
965 | 965 |
966 jpeg_start_compress(&cinfo, TRUE); | 966 jpeg_start_compress(&cinfo, TRUE); |
967 | 967 |
968 const int width = bm.width(); | 968 const int width = bm.width(); |
969 uint8_t* oneRowP = (uint8_t*)oneRow.reset(width * 3); | 969 oneRow.reset(width * 3); |
scroggo
2015/12/10 15:53:34
I could change SkAutoTMalloc::reset to return fPtr
mtklein
2015/12/10 16:00:02
Sure. Seems fine either way.
| |
970 uint8_t* oneRowP = oneRow.get(); | |
970 | 971 |
971 const SkPMColor* colors = bm.getColorTable() ? bm.getColorTable()->readC olors() : nullptr; | 972 const SkPMColor* colors = bm.getColorTable() ? bm.getColorTable()->readC olors() : nullptr; |
972 const void* srcRow = bm.getPixels(); | 973 const void* srcRow = bm.getPixels(); |
973 | 974 |
974 while (cinfo.next_scanline < cinfo.image_height) { | 975 while (cinfo.next_scanline < cinfo.image_height) { |
975 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ | 976 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ |
976 | 977 |
977 writer(oneRowP, srcRow, width, colors); | 978 writer(oneRowP, srcRow, width, colors); |
978 row_pointer[0] = oneRowP; | 979 row_pointer[0] = oneRowP; |
979 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); | 980 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1023 return SkImageDecoder::kUnknown_Format; | 1024 return SkImageDecoder::kUnknown_Format; |
1024 } | 1025 } |
1025 | 1026 |
1026 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { | 1027 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { |
1027 return (SkImageEncoder::kJPEG_Type == t) ? new SkJPEGImageEncoder : nullptr; | 1028 return (SkImageEncoder::kJPEG_Type == t) ? new SkJPEGImageEncoder : nullptr; |
1028 } | 1029 } |
1029 | 1030 |
1030 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory); | 1031 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory); |
1031 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg); | 1032 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg); |
1032 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); | 1033 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); |
OLD | NEW |