OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include <setjmp.h> | 7 #include <setjmp.h> |
8 | 8 |
9 #include "../../../include/fxcodec/fx_codec.h" | 9 #include "../../../include/fxcodec/fx_codec.h" |
10 #include "../../../include/fxcrt/fx_safe_types.h" | 10 #include "../../../include/fxcrt/fx_safe_types.h" |
11 #include "../../../include/fxge/fx_dib.h" | 11 #include "../../../include/fxge/fx_dib.h" |
12 #include "codec_int.h" | 12 #include "codec_int.h" |
13 | 13 |
14 extern "C" { | 14 extern "C" { |
15 static void _JpegScanSOI(const FX_BYTE*& src_buf, FX_DWORD& src_size) | 15 static void _JpegScanSOI(const uint8_t*& src_buf, FX_DWORD& src_size) |
16 { | 16 { |
17 if (src_size == 0) { | 17 if (src_size == 0) { |
18 return; | 18 return; |
19 } | 19 } |
20 FX_DWORD offset = 0; | 20 FX_DWORD offset = 0; |
21 while (offset < src_size - 1) { | 21 while (offset < src_size - 1) { |
22 if (src_buf[offset] == 0xff && src_buf[offset + 1] == 0xd8) { | 22 if (src_buf[offset] == 0xff && src_buf[offset + 1] == 0xd8) { |
23 src_buf += offset; | 23 src_buf += offset; |
24 src_size -= offset; | 24 src_size -= offset; |
25 return; | 25 return; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 } | 88 } |
89 static FX_BOOL _JpegLoadIccProfile(j_decompress_ptr cinfo, FX_LPBYTE* icc_buf_p
tr, FX_DWORD* icc_length) | 89 static FX_BOOL _JpegLoadIccProfile(j_decompress_ptr cinfo, FX_LPBYTE* icc_buf_p
tr, FX_DWORD* icc_length) |
90 { | 90 { |
91 if(icc_buf_ptr == NULL || icc_length == NULL) { | 91 if(icc_buf_ptr == NULL || icc_length == NULL) { |
92 return FALSE; | 92 return FALSE; |
93 } | 93 } |
94 *icc_buf_ptr = NULL; | 94 *icc_buf_ptr = NULL; |
95 *icc_length = 0; | 95 *icc_length = 0; |
96 FX_LPBYTE icc_data_ptr = NULL; | 96 FX_LPBYTE icc_data_ptr = NULL; |
97 FX_DWORD icc_data_len = 0; | 97 FX_DWORD icc_data_len = 0; |
98 FX_BYTE count_icc_marker = 0; | 98 uint8_t count_icc_marker = 0; |
99 FX_BYTE num_icc_marker = 0; | 99 uint8_t num_icc_marker = 0; |
100 jpeg_saved_marker_ptr marker_list[256] = {NULL}; | 100 jpeg_saved_marker_ptr marker_list[256] = {NULL}; |
101 for (jpeg_saved_marker_ptr cur_marker = cinfo->marker_list; | 101 for (jpeg_saved_marker_ptr cur_marker = cinfo->marker_list; |
102 cur_marker != NULL; | 102 cur_marker != NULL; |
103 cur_marker = cur_marker->next) { | 103 cur_marker = cur_marker->next) { |
104 if(_JpegIsIccMarker(cur_marker)) { | 104 if(_JpegIsIccMarker(cur_marker)) { |
105 if(count_icc_marker == 0) { | 105 if(count_icc_marker == 0) { |
106 num_icc_marker = cur_marker->data[13]; | 106 num_icc_marker = cur_marker->data[13]; |
107 } else if(num_icc_marker != cur_marker->data[13]) { | 107 } else if(num_icc_marker != cur_marker->data[13]) { |
108 return FALSE; | 108 return FALSE; |
109 } | 109 } |
110 int sn = cur_marker->data[12] - 1; | 110 int sn = cur_marker->data[12] - 1; |
111 if(sn < 0 || sn >= num_icc_marker) { | 111 if(sn < 0 || sn >= num_icc_marker) { |
112 return FALSE; | 112 return FALSE; |
113 } | 113 } |
114 if(marker_list[sn] == NULL) { | 114 if(marker_list[sn] == NULL) { |
115 marker_list[sn] = cur_marker; | 115 marker_list[sn] = cur_marker; |
116 } else { | 116 } else { |
117 return FALSE; | 117 return FALSE; |
118 } | 118 } |
119 count_icc_marker ++; | 119 count_icc_marker ++; |
120 icc_data_len += (cur_marker->data_length - JPEG_OVERHEAD_LEN); | 120 icc_data_len += (cur_marker->data_length - JPEG_OVERHEAD_LEN); |
121 } | 121 } |
122 } | 122 } |
123 if(count_icc_marker != num_icc_marker) { | 123 if(count_icc_marker != num_icc_marker) { |
124 return FALSE; | 124 return FALSE; |
125 } | 125 } |
126 if(num_icc_marker == 0) { | 126 if(num_icc_marker == 0) { |
127 return TRUE; | 127 return TRUE; |
128 } | 128 } |
129 icc_data_ptr = FX_Alloc(FX_BYTE, icc_data_len); | 129 icc_data_ptr = FX_Alloc(uint8_t, icc_data_len); |
130 if(icc_buf_ptr == NULL) { | 130 if(icc_buf_ptr == NULL) { |
131 return FALSE; | 131 return FALSE; |
132 } | 132 } |
133 *icc_buf_ptr = icc_data_ptr; | 133 *icc_buf_ptr = icc_data_ptr; |
134 *icc_length = icc_data_len; | 134 *icc_length = icc_data_len; |
135 for (int idx = 0; idx < num_icc_marker; idx++) { | 135 for (int idx = 0; idx < num_icc_marker; idx++) { |
136 icc_data_len = marker_list[idx]->data_length - JPEG_OVERHEAD_LEN; | 136 icc_data_len = marker_list[idx]->data_length - JPEG_OVERHEAD_LEN; |
137 FXSYS_memcpy32(icc_data_ptr, marker_list[idx]->data + JPEG_OVERHEAD_LEN,
icc_data_len); | 137 FXSYS_memcpy32(icc_data_ptr, marker_list[idx]->data + JPEG_OVERHEAD_LEN,
icc_data_len); |
138 icc_data_ptr += icc_data_len; | 138 icc_data_ptr += icc_data_len; |
139 } | 139 } |
140 return TRUE; | 140 return TRUE; |
141 } | 141 } |
142 static FX_BOOL _JpegEmbedIccProfile(j_compress_ptr cinfo, FX_LPCBYTE icc_buf_pt
r, FX_DWORD icc_length) | 142 static FX_BOOL _JpegEmbedIccProfile(j_compress_ptr cinfo, FX_LPCBYTE icc_buf_pt
r, FX_DWORD icc_length) |
143 { | 143 { |
144 if(icc_buf_ptr == NULL || icc_length == 0) { | 144 if(icc_buf_ptr == NULL || icc_length == 0) { |
145 return FALSE; | 145 return FALSE; |
146 } | 146 } |
147 FX_DWORD icc_segment_size = (JPEG_MARKER_MAXSIZE - 2 - JPEG_OVERHEAD_LEN); | 147 FX_DWORD icc_segment_size = (JPEG_MARKER_MAXSIZE - 2 - JPEG_OVERHEAD_LEN); |
148 FX_DWORD icc_segment_num = (icc_length / icc_segment_size) + 1; | 148 FX_DWORD icc_segment_num = (icc_length / icc_segment_size) + 1; |
149 if (icc_segment_num > 255) { | 149 if (icc_segment_num > 255) { |
150 return FALSE; | 150 return FALSE; |
151 } | 151 } |
152 FX_DWORD icc_data_length = JPEG_OVERHEAD_LEN + (icc_segment_num > 1 ? icc_se
gment_size : icc_length); | 152 FX_DWORD icc_data_length = JPEG_OVERHEAD_LEN + (icc_segment_num > 1 ? icc_se
gment_size : icc_length); |
153 FX_LPBYTE icc_data = FX_Alloc(FX_BYTE, icc_data_length); | 153 FX_LPBYTE icc_data = FX_Alloc(uint8_t, icc_data_length); |
154 FXSYS_memcpy32(icc_data, "\x49\x43\x43\x5f\x50\x52\x4f\x46\x49\x4c\x45\x00",
12); | 154 FXSYS_memcpy32(icc_data, "\x49\x43\x43\x5f\x50\x52\x4f\x46\x49\x4c\x45\x00",
12); |
155 icc_data[13] = (FX_BYTE)icc_segment_num; | 155 icc_data[13] = (uint8_t)icc_segment_num; |
156 for (FX_BYTE i = 0; i < (icc_segment_num - 1); i++) { | 156 for (uint8_t i = 0; i < (icc_segment_num - 1); i++) { |
157 icc_data[12] = i + 1; | 157 icc_data[12] = i + 1; |
158 FXSYS_memcpy32(icc_data + JPEG_OVERHEAD_LEN, icc_buf_ptr + i * icc_segme
nt_size, icc_segment_size); | 158 FXSYS_memcpy32(icc_data + JPEG_OVERHEAD_LEN, icc_buf_ptr + i * icc_segme
nt_size, icc_segment_size); |
159 jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, icc_data_length); | 159 jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, icc_data_length); |
160 } | 160 } |
161 icc_data[12] = (FX_BYTE)icc_segment_num; | 161 icc_data[12] = (uint8_t)icc_segment_num; |
162 FX_DWORD icc_size = (icc_segment_num - 1) * icc_segment_size; | 162 FX_DWORD icc_size = (icc_segment_num - 1) * icc_segment_size; |
163 FXSYS_memcpy32(icc_data + JPEG_OVERHEAD_LEN, icc_buf_ptr + icc_size, icc_len
gth - icc_size); | 163 FXSYS_memcpy32(icc_data + JPEG_OVERHEAD_LEN, icc_buf_ptr + icc_size, icc_len
gth - icc_size); |
164 jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, JPEG_OVERHEAD_LEN + icc_
length - icc_size); | 164 jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, JPEG_OVERHEAD_LEN + icc_
length - icc_size); |
165 FX_Free(icc_data); | 165 FX_Free(icc_data); |
166 return TRUE; | 166 return TRUE; |
167 } | 167 } |
168 extern "C" { | 168 extern "C" { |
169 static void _dest_do_nothing(j_compress_ptr cinfo) {} | 169 static void _dest_do_nothing(j_compress_ptr cinfo) {} |
170 }; | 170 }; |
171 extern "C" { | 171 extern "C" { |
(...skipping 27 matching lines...) Expand all Loading... |
199 safe_buf_len += 1024; | 199 safe_buf_len += 1024; |
200 if (icc_length) { | 200 if (icc_length) { |
201 safe_buf_len += 255 * 18; | 201 safe_buf_len += 255 * 18; |
202 safe_buf_len += icc_length; | 202 safe_buf_len += icc_length; |
203 } | 203 } |
204 FX_DWORD dest_buf_length = 0; | 204 FX_DWORD dest_buf_length = 0; |
205 if (!safe_buf_len.IsValid()) { | 205 if (!safe_buf_len.IsValid()) { |
206 dest_buf = nullptr; | 206 dest_buf = nullptr; |
207 } else { | 207 } else { |
208 dest_buf_length = safe_buf_len.ValueOrDie(); | 208 dest_buf_length = safe_buf_len.ValueOrDie(); |
209 dest_buf = FX_TryAlloc(FX_BYTE, dest_buf_length); | 209 dest_buf = FX_TryAlloc(uint8_t, dest_buf_length); |
210 const int MIN_TRY_BUF_LEN = 1024; | 210 const int MIN_TRY_BUF_LEN = 1024; |
211 while (!dest_buf && dest_buf_length > MIN_TRY_BUF_LEN) { | 211 while (!dest_buf && dest_buf_length > MIN_TRY_BUF_LEN) { |
212 dest_buf_length >>= 1; | 212 dest_buf_length >>= 1; |
213 dest_buf = FX_TryAlloc(FX_BYTE, dest_buf_length); | 213 dest_buf = FX_TryAlloc(uint8_t, dest_buf_length); |
214 } | 214 } |
215 } | 215 } |
216 if (!dest_buf) { | 216 if (!dest_buf) { |
217 FX_OutOfMemoryTerminate(); | 217 FX_OutOfMemoryTerminate(); |
218 } | 218 } |
219 struct jpeg_destination_mgr dest; | 219 struct jpeg_destination_mgr dest; |
220 dest.init_destination = _dest_do_nothing; | 220 dest.init_destination = _dest_do_nothing; |
221 dest.term_destination = _dest_do_nothing; | 221 dest.term_destination = _dest_do_nothing; |
222 dest.empty_output_buffer = _dest_empty; | 222 dest.empty_output_buffer = _dest_empty; |
223 dest.next_output_byte = dest_buf; | 223 dest.next_output_byte = dest_buf; |
224 dest.free_in_buffer = dest_buf_length; | 224 dest.free_in_buffer = dest_buf_length; |
225 cinfo.dest = &dest; | 225 cinfo.dest = &dest; |
226 cinfo.image_width = width; | 226 cinfo.image_width = width; |
227 cinfo.image_height = height; | 227 cinfo.image_height = height; |
228 cinfo.input_components = nComponents; | 228 cinfo.input_components = nComponents; |
229 if (nComponents == 1) { | 229 if (nComponents == 1) { |
230 cinfo.in_color_space = JCS_GRAYSCALE; | 230 cinfo.in_color_space = JCS_GRAYSCALE; |
231 } else if (nComponents == 3) { | 231 } else if (nComponents == 3) { |
232 cinfo.in_color_space = JCS_RGB; | 232 cinfo.in_color_space = JCS_RGB; |
233 } else { | 233 } else { |
234 cinfo.in_color_space = JCS_CMYK; | 234 cinfo.in_color_space = JCS_CMYK; |
235 } | 235 } |
236 FX_LPBYTE line_buf = NULL; | 236 FX_LPBYTE line_buf = NULL; |
237 if (nComponents > 1) { | 237 if (nComponents > 1) { |
238 line_buf = FX_Alloc2D(FX_BYTE, width, nComponents); | 238 line_buf = FX_Alloc2D(uint8_t, width, nComponents); |
239 } | 239 } |
240 jpeg_set_defaults(&cinfo); | 240 jpeg_set_defaults(&cinfo); |
241 if(quality != 75) { | 241 if(quality != 75) { |
242 jpeg_set_quality(&cinfo, quality, TRUE); | 242 jpeg_set_quality(&cinfo, quality, TRUE); |
243 } | 243 } |
244 jpeg_start_compress(&cinfo, TRUE); | 244 jpeg_start_compress(&cinfo, TRUE); |
245 _JpegEmbedIccProfile(&cinfo, icc_buf, icc_length); | 245 _JpegEmbedIccProfile(&cinfo, icc_buf, icc_length); |
246 JSAMPROW row_pointer[1]; | 246 JSAMPROW row_pointer[1]; |
247 JDIMENSION row; | 247 JDIMENSION row; |
248 while (cinfo.next_scanline < cinfo.image_height) { | 248 while (cinfo.next_scanline < cinfo.image_height) { |
(...skipping 13 matching lines...) Expand all Loading... |
262 *dest_scan++ = ~*src_scan++; | 262 *dest_scan++ = ~*src_scan++; |
263 } | 263 } |
264 } | 264 } |
265 row_pointer[0] = line_buf; | 265 row_pointer[0] = line_buf; |
266 } else { | 266 } else { |
267 row_pointer[0] = (FX_LPBYTE)src_scan; | 267 row_pointer[0] = (FX_LPBYTE)src_scan; |
268 } | 268 } |
269 row = cinfo.next_scanline; | 269 row = cinfo.next_scanline; |
270 jpeg_write_scanlines(&cinfo, row_pointer, 1); | 270 jpeg_write_scanlines(&cinfo, row_pointer, 1); |
271 if (cinfo.next_scanline == row) { | 271 if (cinfo.next_scanline == row) { |
272 dest_buf = FX_Realloc(FX_BYTE, dest_buf, dest_buf_length + JPEG_BLOC
K_SIZE); | 272 dest_buf = FX_Realloc(uint8_t, dest_buf, dest_buf_length + JPEG_BLOC
K_SIZE); |
273 dest.next_output_byte = dest_buf + dest_buf_length - dest.free_in_bu
ffer; | 273 dest.next_output_byte = dest_buf + dest_buf_length - dest.free_in_bu
ffer; |
274 dest_buf_length += JPEG_BLOCK_SIZE; | 274 dest_buf_length += JPEG_BLOCK_SIZE; |
275 dest.free_in_buffer += JPEG_BLOCK_SIZE; | 275 dest.free_in_buffer += JPEG_BLOCK_SIZE; |
276 } | 276 } |
277 } | 277 } |
278 jpeg_finish_compress(&cinfo); | 278 jpeg_finish_compress(&cinfo); |
279 jpeg_destroy_compress(&cinfo); | 279 jpeg_destroy_compress(&cinfo); |
280 if (line_buf) { | 280 if (line_buf) { |
281 FX_Free(line_buf); | 281 FX_Free(line_buf); |
282 } | 282 } |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 if (!InitDecode()) { | 470 if (!InitDecode()) { |
471 return FALSE; | 471 return FALSE; |
472 } | 472 } |
473 if (cinfo.num_components < nComps) { | 473 if (cinfo.num_components < nComps) { |
474 return FALSE; | 474 return FALSE; |
475 } | 475 } |
476 if ((int)cinfo.image_width < width) { | 476 if ((int)cinfo.image_width < width) { |
477 return FALSE; | 477 return FALSE; |
478 } | 478 } |
479 m_Pitch = (cinfo.image_width * cinfo.num_components + 3) / 4 * 4; | 479 m_Pitch = (cinfo.image_width * cinfo.num_components + 3) / 4 * 4; |
480 m_pScanlineBuf = FX_Alloc(FX_BYTE, m_Pitch); | 480 m_pScanlineBuf = FX_Alloc(uint8_t, m_Pitch); |
481 m_nComps = cinfo.num_components; | 481 m_nComps = cinfo.num_components; |
482 m_bpc = 8; | 482 m_bpc = 8; |
483 m_bColorTransformed = FALSE; | 483 m_bColorTransformed = FALSE; |
484 m_bStarted = FALSE; | 484 m_bStarted = FALSE; |
485 return TRUE; | 485 return TRUE; |
486 } | 486 } |
487 extern "C" { | 487 extern "C" { |
488 FX_INT32 FX_GetDownsampleRatio(FX_INT32 originWidth, FX_INT32 originHeight,
FX_INT32 downsampleWidth, FX_INT32 downsampleHeight) | 488 int32_t FX_GetDownsampleRatio(int32_t originWidth, int32_t originHeight, int
32_t downsampleWidth, int32_t downsampleHeight) |
489 { | 489 { |
490 int iratio_w = originWidth / downsampleWidth; | 490 int iratio_w = originWidth / downsampleWidth; |
491 int iratio_h = originHeight / downsampleHeight; | 491 int iratio_h = originHeight / downsampleHeight; |
492 int ratio = (iratio_w > iratio_h) ? iratio_h : iratio_w; | 492 int ratio = (iratio_w > iratio_h) ? iratio_h : iratio_w; |
493 if (ratio >= 8) { | 493 if (ratio >= 8) { |
494 return 8; | 494 return 8; |
495 } else if (ratio >= 4) { | 495 } else if (ratio >= 4) { |
496 return 4; | 496 return 4; |
497 } else if (ratio >= 2) { | 497 } else if (ratio >= 2) { |
498 return 2; | 498 return 2; |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 } | 630 } |
631 static void jpeg_free_func(void* p) | 631 static void jpeg_free_func(void* p) |
632 { | 632 { |
633 FX_Free(p); | 633 FX_Free(p); |
634 } | 634 } |
635 void* CCodec_JpegModule::Start() | 635 void* CCodec_JpegModule::Start() |
636 { | 636 { |
637 if (m_pExtProvider) { | 637 if (m_pExtProvider) { |
638 return m_pExtProvider->Start(); | 638 return m_pExtProvider->Start(); |
639 } | 639 } |
640 FXJPEG_Context* p = (FXJPEG_Context*)FX_Alloc(FX_BYTE, sizeof(FXJPEG_Context
)); | 640 FXJPEG_Context* p = (FXJPEG_Context*)FX_Alloc(uint8_t, sizeof(FXJPEG_Context
)); |
641 p->m_AllocFunc = jpeg_alloc_func; | 641 p->m_AllocFunc = jpeg_alloc_func; |
642 p->m_FreeFunc = jpeg_free_func; | 642 p->m_FreeFunc = jpeg_free_func; |
643 p->m_ErrMgr.error_exit = _error_fatal1; | 643 p->m_ErrMgr.error_exit = _error_fatal1; |
644 p->m_ErrMgr.emit_message = _error_do_nothing1; | 644 p->m_ErrMgr.emit_message = _error_do_nothing1; |
645 p->m_ErrMgr.output_message = _error_do_nothing; | 645 p->m_ErrMgr.output_message = _error_do_nothing; |
646 p->m_ErrMgr.format_message = _error_do_nothing2; | 646 p->m_ErrMgr.format_message = _error_do_nothing2; |
647 p->m_ErrMgr.reset_error_mgr = _error_do_nothing; | 647 p->m_ErrMgr.reset_error_mgr = _error_do_nothing; |
648 p->m_SrcMgr.init_source = _src_do_nothing; | 648 p->m_SrcMgr.init_source = _src_do_nothing; |
649 p->m_SrcMgr.term_source = _src_do_nothing; | 649 p->m_SrcMgr.term_source = _src_do_nothing; |
650 p->m_SrcMgr.skip_input_data = _src_skip_data1; | 650 p->m_SrcMgr.skip_input_data = _src_skip_data1; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 return m_pExtProvider->GetAvailInput(pContext, avail_buf_ptr); | 742 return m_pExtProvider->GetAvailInput(pContext, avail_buf_ptr); |
743 } | 743 } |
744 if(avail_buf_ptr != NULL) { | 744 if(avail_buf_ptr != NULL) { |
745 *avail_buf_ptr = NULL; | 745 *avail_buf_ptr = NULL; |
746 if(((FXJPEG_Context*)pContext)->m_SrcMgr.bytes_in_buffer > 0) { | 746 if(((FXJPEG_Context*)pContext)->m_SrcMgr.bytes_in_buffer > 0) { |
747 *avail_buf_ptr = (FX_LPBYTE)((FXJPEG_Context*)pContext)->m_SrcMgr.ne
xt_input_byte; | 747 *avail_buf_ptr = (FX_LPBYTE)((FXJPEG_Context*)pContext)->m_SrcMgr.ne
xt_input_byte; |
748 } | 748 } |
749 } | 749 } |
750 return (FX_DWORD)((FXJPEG_Context*)pContext)->m_SrcMgr.bytes_in_buffer; | 750 return (FX_DWORD)((FXJPEG_Context*)pContext)->m_SrcMgr.bytes_in_buffer; |
751 } | 751 } |
OLD | NEW |