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 "core/fxcodec/codec/codec_int.h" | 7 #include "core/fxcodec/codec/codec_int.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 11 matching lines...) Expand all Loading... |
22 } | 22 } |
23 static void my_free_func(void* opaque, void* address) { | 23 static void my_free_func(void* opaque, void* address) { |
24 FX_Free(address); | 24 FX_Free(address); |
25 } | 25 } |
26 static int FPDFAPI_FlateGetTotalOut(void* context) { | 26 static int FPDFAPI_FlateGetTotalOut(void* context) { |
27 return ((z_stream*)context)->total_out; | 27 return ((z_stream*)context)->total_out; |
28 } | 28 } |
29 static int FPDFAPI_FlateGetTotalIn(void* context) { | 29 static int FPDFAPI_FlateGetTotalIn(void* context) { |
30 return ((z_stream*)context)->total_in; | 30 return ((z_stream*)context)->total_in; |
31 } | 31 } |
32 static void FPDFAPI_FlateCompress(unsigned char* dest_buf, | 32 |
| 33 static bool FPDFAPI_FlateCompress(unsigned char* dest_buf, |
33 unsigned long* dest_size, | 34 unsigned long* dest_size, |
34 const unsigned char* src_buf, | 35 const unsigned char* src_buf, |
35 unsigned long src_size) { | 36 unsigned long src_size) { |
36 compress(dest_buf, dest_size, src_buf, src_size); | 37 return compress(dest_buf, dest_size, src_buf, src_size) == Z_OK; |
37 } | 38 } |
| 39 |
38 void* FPDFAPI_FlateInit(void* (*alloc_func)(void*, unsigned int, unsigned int), | 40 void* FPDFAPI_FlateInit(void* (*alloc_func)(void*, unsigned int, unsigned int), |
39 void (*free_func)(void*, void*)) { | 41 void (*free_func)(void*, void*)) { |
40 z_stream* p = (z_stream*)alloc_func(0, 1, sizeof(z_stream)); | 42 z_stream* p = (z_stream*)alloc_func(0, 1, sizeof(z_stream)); |
41 if (!p) { | 43 if (!p) |
42 return NULL; | 44 return nullptr; |
43 } | 45 |
44 FXSYS_memset(p, 0, sizeof(z_stream)); | 46 FXSYS_memset(p, 0, sizeof(z_stream)); |
45 p->zalloc = alloc_func; | 47 p->zalloc = alloc_func; |
46 p->zfree = free_func; | 48 p->zfree = free_func; |
47 inflateInit(p); | 49 inflateInit(p); |
48 return p; | 50 return p; |
49 } | 51 } |
| 52 |
50 void FPDFAPI_FlateInput(void* context, | 53 void FPDFAPI_FlateInput(void* context, |
51 const unsigned char* src_buf, | 54 const unsigned char* src_buf, |
52 unsigned int src_size) { | 55 unsigned int src_size) { |
53 ((z_stream*)context)->next_in = (unsigned char*)src_buf; | 56 ((z_stream*)context)->next_in = (unsigned char*)src_buf; |
54 ((z_stream*)context)->avail_in = src_size; | 57 ((z_stream*)context)->avail_in = src_size; |
55 } | 58 } |
| 59 |
56 int FPDFAPI_FlateOutput(void* context, | 60 int FPDFAPI_FlateOutput(void* context, |
57 unsigned char* dest_buf, | 61 unsigned char* dest_buf, |
58 unsigned int dest_size) { | 62 unsigned int dest_size) { |
59 ((z_stream*)context)->next_out = dest_buf; | 63 ((z_stream*)context)->next_out = dest_buf; |
60 ((z_stream*)context)->avail_out = dest_size; | 64 ((z_stream*)context)->avail_out = dest_size; |
61 unsigned int pre_pos = (unsigned int)FPDFAPI_FlateGetTotalOut(context); | 65 unsigned int pre_pos = (unsigned int)FPDFAPI_FlateGetTotalOut(context); |
62 int ret = inflate((z_stream*)context, Z_SYNC_FLUSH); | 66 int ret = inflate((z_stream*)context, Z_SYNC_FLUSH); |
63 unsigned int post_pos = (unsigned int)FPDFAPI_FlateGetTotalOut(context); | 67 unsigned int post_pos = (unsigned int)FPDFAPI_FlateGetTotalOut(context); |
64 unsigned int written = post_pos - pre_pos; | 68 unsigned int written = post_pos - pre_pos; |
65 if (written < dest_size) { | 69 if (written < dest_size) { |
66 FXSYS_memset(dest_buf + written, '\0', dest_size - written); | 70 FXSYS_memset(dest_buf + written, '\0', dest_size - written); |
67 } | 71 } |
68 return ret; | 72 return ret; |
69 } | 73 } |
| 74 |
70 int FPDFAPI_FlateGetAvailIn(void* context) { | 75 int FPDFAPI_FlateGetAvailIn(void* context) { |
71 return ((z_stream*)context)->avail_in; | 76 return ((z_stream*)context)->avail_in; |
72 } | 77 } |
| 78 |
73 int FPDFAPI_FlateGetAvailOut(void* context) { | 79 int FPDFAPI_FlateGetAvailOut(void* context) { |
74 return ((z_stream*)context)->avail_out; | 80 return ((z_stream*)context)->avail_out; |
75 } | 81 } |
| 82 |
76 void FPDFAPI_FlateEnd(void* context) { | 83 void FPDFAPI_FlateEnd(void* context) { |
77 inflateEnd((z_stream*)context); | 84 inflateEnd((z_stream*)context); |
78 ((z_stream*)context)->zfree(0, context); | 85 ((z_stream*)context)->zfree(0, context); |
79 } | 86 } |
| 87 |
80 } // extern "C" | 88 } // extern "C" |
81 | 89 |
82 namespace { | 90 namespace { |
83 | 91 |
84 class CLZWDecoder { | 92 class CLZWDecoder { |
85 public: | 93 public: |
86 int Decode(uint8_t* output, | 94 int Decode(uint8_t* output, |
87 uint32_t& outlen, | 95 uint32_t& outlen, |
88 const uint8_t* input, | 96 const uint8_t* input, |
89 uint32_t& size, | 97 uint32_t& size, |
90 FX_BOOL bEarlyChange); | 98 FX_BOOL bEarlyChange); |
91 | 99 |
92 private: | 100 private: |
93 void AddCode(uint32_t prefix_code, uint8_t append_char); | 101 void AddCode(uint32_t prefix_code, uint8_t append_char); |
94 void DecodeString(uint32_t code); | 102 void DecodeString(uint32_t code); |
95 | 103 |
96 uint32_t m_InPos; | 104 uint32_t m_InPos; |
97 uint32_t m_OutPos; | 105 uint32_t m_OutPos; |
98 uint8_t* m_pOutput; | 106 uint8_t* m_pOutput; |
99 const uint8_t* m_pInput; | 107 const uint8_t* m_pInput; |
100 FX_BOOL m_Early; | 108 FX_BOOL m_Early; |
101 uint32_t m_CodeArray[5021]; | 109 uint32_t m_CodeArray[5021]; |
102 uint32_t m_nCodes; | 110 uint32_t m_nCodes; |
103 uint8_t m_DecodeStack[4000]; | 111 uint8_t m_DecodeStack[4000]; |
104 uint32_t m_StackLen; | 112 uint32_t m_StackLen; |
105 int m_CodeLen; | 113 int m_CodeLen; |
106 }; | 114 }; |
| 115 |
107 void CLZWDecoder::AddCode(uint32_t prefix_code, uint8_t append_char) { | 116 void CLZWDecoder::AddCode(uint32_t prefix_code, uint8_t append_char) { |
108 if (m_nCodes + m_Early == 4094) { | 117 if (m_nCodes + m_Early == 4094) { |
109 return; | 118 return; |
110 } | 119 } |
111 m_CodeArray[m_nCodes++] = (prefix_code << 16) | append_char; | 120 m_CodeArray[m_nCodes++] = (prefix_code << 16) | append_char; |
112 if (m_nCodes + m_Early == 512 - 258) { | 121 if (m_nCodes + m_Early == 512 - 258) { |
113 m_CodeLen = 10; | 122 m_CodeLen = 10; |
114 } else if (m_nCodes + m_Early == 1024 - 258) { | 123 } else if (m_nCodes + m_Early == 1024 - 258) { |
115 m_CodeLen = 11; | 124 m_CodeLen = 11; |
116 } else if (m_nCodes + m_Early == 2048 - 258) { | 125 } else if (m_nCodes + m_Early == 2048 - 258) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 AddCode(old_code, last_char); | 231 AddCode(old_code, last_char); |
223 } | 232 } |
224 old_code = code; | 233 old_code = code; |
225 } | 234 } |
226 } | 235 } |
227 dest_size = m_OutPos; | 236 dest_size = m_OutPos; |
228 src_size = (m_InPos + 7) / 8; | 237 src_size = (m_InPos + 7) / 8; |
229 return 0; | 238 return 0; |
230 } | 239 } |
231 | 240 |
232 uint8_t PaethPredictor(int a, int b, int c) { | 241 uint8_t PathPredictor(int a, int b, int c) { |
233 int p = a + b - c; | 242 int p = a + b - c; |
234 int pa = FXSYS_abs(p - a); | 243 int pa = FXSYS_abs(p - a); |
235 int pb = FXSYS_abs(p - b); | 244 int pb = FXSYS_abs(p - b); |
236 int pc = FXSYS_abs(p - c); | 245 int pc = FXSYS_abs(p - c); |
237 if (pa <= pb && pa <= pc) { | 246 if (pa <= pb && pa <= pc) |
238 return (uint8_t)a; | 247 return (uint8_t)a; |
239 } | 248 if (pb <= pc) |
240 if (pb <= pc) { | |
241 return (uint8_t)b; | 249 return (uint8_t)b; |
242 } | |
243 return (uint8_t)c; | 250 return (uint8_t)c; |
244 } | 251 } |
245 | 252 |
246 FX_BOOL PNG_PredictorEncode(uint8_t*& data_buf, | 253 void PNG_PredictorEncode(uint8_t** data_buf, uint32_t* data_size) { |
247 uint32_t& data_size, | 254 const int row_size = 7; |
248 int predictor, | 255 const int row_count = (*data_size + row_size - 1) / row_size; |
249 int Colors, | 256 const int last_row_size = *data_size % row_size; |
250 int BitsPerComponent, | |
251 int Columns) { | |
252 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; | |
253 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; | |
254 if (row_size <= 0) | |
255 return FALSE; | |
256 const int row_count = (data_size + row_size - 1) / row_size; | |
257 const int last_row_size = data_size % row_size; | |
258 uint8_t* dest_buf = FX_Alloc2D(uint8_t, row_size + 1, row_count); | 257 uint8_t* dest_buf = FX_Alloc2D(uint8_t, row_size + 1, row_count); |
259 int byte_cnt = 0; | 258 int byte_cnt = 0; |
260 uint8_t* pSrcData = data_buf; | 259 uint8_t* pSrcData = *data_buf; |
261 uint8_t* pDestData = dest_buf; | 260 uint8_t* pDestData = dest_buf; |
262 for (int row = 0; row < row_count; row++) { | 261 for (int row = 0; row < row_count; row++) { |
263 if (predictor == 10) { | 262 for (int byte = 0; byte < row_size && byte_cnt < (int)*data_size; byte++) { |
264 pDestData[0] = 0; | 263 pDestData[0] = 2; |
265 int move_size = row_size; | 264 uint8_t up = 0; |
266 if (move_size * (row + 1) > (int)data_size) { | 265 if (row) |
267 move_size = data_size - (move_size * row); | 266 up = pSrcData[byte - row_size]; |
268 } | 267 pDestData[byte + 1] = pSrcData[byte] - up; |
269 FXSYS_memmove(pDestData + 1, pSrcData, move_size); | 268 ++byte_cnt; |
270 pDestData += (move_size + 1); | |
271 pSrcData += move_size; | |
272 byte_cnt += move_size; | |
273 continue; | |
274 } | |
275 for (int byte = 0; byte < row_size && byte_cnt < (int)data_size; byte++) { | |
276 switch (predictor) { | |
277 case 11: { | |
278 pDestData[0] = 1; | |
279 uint8_t left = 0; | |
280 if (byte >= BytesPerPixel) { | |
281 left = pSrcData[byte - BytesPerPixel]; | |
282 } | |
283 pDestData[byte + 1] = pSrcData[byte] - left; | |
284 } break; | |
285 case 12: { | |
286 pDestData[0] = 2; | |
287 uint8_t up = 0; | |
288 if (row) { | |
289 up = pSrcData[byte - row_size]; | |
290 } | |
291 pDestData[byte + 1] = pSrcData[byte] - up; | |
292 } break; | |
293 case 13: { | |
294 pDestData[0] = 3; | |
295 uint8_t left = 0; | |
296 if (byte >= BytesPerPixel) { | |
297 left = pSrcData[byte - BytesPerPixel]; | |
298 } | |
299 uint8_t up = 0; | |
300 if (row) { | |
301 up = pSrcData[byte - row_size]; | |
302 } | |
303 pDestData[byte + 1] = pSrcData[byte] - (left + up) / 2; | |
304 } break; | |
305 case 14: { | |
306 pDestData[0] = 4; | |
307 uint8_t left = 0; | |
308 if (byte >= BytesPerPixel) { | |
309 left = pSrcData[byte - BytesPerPixel]; | |
310 } | |
311 uint8_t up = 0; | |
312 if (row) { | |
313 up = pSrcData[byte - row_size]; | |
314 } | |
315 uint8_t upper_left = 0; | |
316 if (byte >= BytesPerPixel && row) { | |
317 upper_left = pSrcData[byte - row_size - BytesPerPixel]; | |
318 } | |
319 pDestData[byte + 1] = | |
320 pSrcData[byte] - PaethPredictor(left, up, upper_left); | |
321 } break; | |
322 default: { pDestData[byte + 1] = pSrcData[byte]; } break; | |
323 } | |
324 byte_cnt++; | |
325 } | 269 } |
326 pDestData += (row_size + 1); | 270 pDestData += (row_size + 1); |
327 pSrcData += row_size; | 271 pSrcData += row_size; |
328 } | 272 } |
329 FX_Free(data_buf); | 273 FX_Free(*data_buf); |
330 data_buf = dest_buf; | 274 *data_buf = dest_buf; |
331 data_size = (row_size + 1) * row_count - | 275 *data_size = (row_size + 1) * row_count - |
332 (last_row_size > 0 ? (row_size - last_row_size) : 0); | 276 (last_row_size > 0 ? (row_size - last_row_size) : 0); |
333 return TRUE; | |
334 } | 277 } |
335 | 278 |
336 void PNG_PredictLine(uint8_t* pDestData, | 279 void PNG_PredictLine(uint8_t* pDestData, |
337 const uint8_t* pSrcData, | 280 const uint8_t* pSrcData, |
338 const uint8_t* pLastLine, | 281 const uint8_t* pLastLine, |
339 int bpc, | 282 int bpc, |
340 int nColors, | 283 int nColors, |
341 int nPixels) { | 284 int nPixels) { |
342 int row_size = (nPixels * bpc * nColors + 7) / 8; | 285 int row_size = (nPixels * bpc * nColors + 7) / 8; |
343 int BytesPerPixel = (bpc * nColors + 7) / 8; | 286 int BytesPerPixel = (bpc * nColors + 7) / 8; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 left = pDestData[byte - BytesPerPixel]; | 326 left = pDestData[byte - BytesPerPixel]; |
384 } | 327 } |
385 uint8_t up = 0; | 328 uint8_t up = 0; |
386 if (pLastLine) { | 329 if (pLastLine) { |
387 up = pLastLine[byte]; | 330 up = pLastLine[byte]; |
388 } | 331 } |
389 uint8_t upper_left = 0; | 332 uint8_t upper_left = 0; |
390 if (byte >= BytesPerPixel && pLastLine) { | 333 if (byte >= BytesPerPixel && pLastLine) { |
391 upper_left = pLastLine[byte - BytesPerPixel]; | 334 upper_left = pLastLine[byte - BytesPerPixel]; |
392 } | 335 } |
393 pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_left); | 336 pDestData[byte] = raw_byte + PathPredictor(left, up, upper_left); |
394 break; | 337 break; |
395 } | 338 } |
396 default: | 339 default: |
397 pDestData[byte] = raw_byte; | 340 pDestData[byte] = raw_byte; |
398 break; | 341 break; |
399 } | 342 } |
400 } | 343 } |
401 } | 344 } |
402 | 345 |
403 FX_BOOL PNG_Predictor(uint8_t*& data_buf, | 346 FX_BOOL PNG_Predictor(uint8_t*& data_buf, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 left = pDestData[byte - BytesPerPixel]; | 411 left = pDestData[byte - BytesPerPixel]; |
469 } | 412 } |
470 uint8_t up = 0; | 413 uint8_t up = 0; |
471 if (row) { | 414 if (row) { |
472 up = pDestData[byte - row_size]; | 415 up = pDestData[byte - row_size]; |
473 } | 416 } |
474 uint8_t upper_left = 0; | 417 uint8_t upper_left = 0; |
475 if (byte >= BytesPerPixel && row) { | 418 if (byte >= BytesPerPixel && row) { |
476 upper_left = pDestData[byte - row_size - BytesPerPixel]; | 419 upper_left = pDestData[byte - row_size - BytesPerPixel]; |
477 } | 420 } |
478 pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_left); | 421 pDestData[byte] = raw_byte + PathPredictor(left, up, upper_left); |
479 break; | 422 break; |
480 } | 423 } |
481 default: | 424 default: |
482 pDestData[byte] = raw_byte; | 425 pDestData[byte] = raw_byte; |
483 break; | 426 break; |
484 } | 427 } |
485 byte_cnt++; | 428 byte_cnt++; |
486 } | 429 } |
487 pSrcData += row_size + 1; | 430 pSrcData += row_size + 1; |
488 pDestData += row_size; | 431 pDestData += row_size; |
489 } | 432 } |
490 FX_Free(data_buf); | 433 FX_Free(data_buf); |
491 data_buf = dest_buf; | 434 data_buf = dest_buf; |
492 data_size = row_size * row_count - | 435 data_size = row_size * row_count - |
493 (last_row_size > 0 ? (row_size + 1 - last_row_size) : 0); | 436 (last_row_size > 0 ? (row_size + 1 - last_row_size) : 0); |
494 return TRUE; | 437 return TRUE; |
495 } | 438 } |
496 | 439 |
497 void TIFF_PredictorEncodeLine(uint8_t* dest_buf, | |
498 int row_size, | |
499 int BitsPerComponent, | |
500 int Colors, | |
501 int Columns) { | |
502 int BytesPerPixel = BitsPerComponent * Colors / 8; | |
503 if (BitsPerComponent < 8) { | |
504 uint8_t mask = 0x01; | |
505 if (BitsPerComponent == 2) { | |
506 mask = 0x03; | |
507 } else if (BitsPerComponent == 4) { | |
508 mask = 0x0F; | |
509 } | |
510 int row_bits = Colors * BitsPerComponent * Columns; | |
511 for (int i = row_bits - BitsPerComponent; i >= BitsPerComponent; | |
512 i -= BitsPerComponent) { | |
513 int col = i % 8; | |
514 int index = i / 8; | |
515 int col_pre = | |
516 (col == 0) ? (8 - BitsPerComponent) : (col - BitsPerComponent); | |
517 int index_pre = (col == 0) ? (index - 1) : index; | |
518 uint8_t cur = (dest_buf[index] >> (8 - col - BitsPerComponent)) & mask; | |
519 uint8_t left = | |
520 (dest_buf[index_pre] >> (8 - col_pre - BitsPerComponent)) & mask; | |
521 cur -= left; | |
522 cur &= mask; | |
523 cur <<= (8 - col - BitsPerComponent); | |
524 dest_buf[index] &= ~(mask << ((8 - col - BitsPerComponent))); | |
525 dest_buf[index] |= cur; | |
526 } | |
527 } else if (BitsPerComponent == 8) { | |
528 for (int i = row_size - 1; i >= BytesPerPixel; i--) { | |
529 dest_buf[i] -= dest_buf[i - BytesPerPixel]; | |
530 } | |
531 } else { | |
532 for (int i = row_size - BytesPerPixel; i >= BytesPerPixel; | |
533 i -= BytesPerPixel) { | |
534 uint16_t pixel = (dest_buf[i] << 8) | dest_buf[i + 1]; | |
535 pixel -= | |
536 (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerPixel + 1]; | |
537 dest_buf[i] = pixel >> 8; | |
538 dest_buf[i + 1] = (uint8_t)pixel; | |
539 } | |
540 } | |
541 } | |
542 | |
543 FX_BOOL TIFF_PredictorEncode(uint8_t*& data_buf, | |
544 uint32_t& data_size, | |
545 int Colors, | |
546 int BitsPerComponent, | |
547 int Columns) { | |
548 int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; | |
549 if (row_size == 0) | |
550 return FALSE; | |
551 const int row_count = (data_size + row_size - 1) / row_size; | |
552 const int last_row_size = data_size % row_size; | |
553 for (int row = 0; row < row_count; row++) { | |
554 uint8_t* scan_line = data_buf + row * row_size; | |
555 if ((row + 1) * row_size > (int)data_size) { | |
556 row_size = last_row_size; | |
557 } | |
558 TIFF_PredictorEncodeLine(scan_line, row_size, BitsPerComponent, Colors, | |
559 Columns); | |
560 } | |
561 return TRUE; | |
562 } | |
563 | |
564 void TIFF_PredictLine(uint8_t* dest_buf, | 440 void TIFF_PredictLine(uint8_t* dest_buf, |
565 uint32_t row_size, | 441 uint32_t row_size, |
566 int BitsPerComponent, | 442 int BitsPerComponent, |
567 int Colors, | 443 int Colors, |
568 int Columns) { | 444 int Columns) { |
569 if (BitsPerComponent == 1) { | 445 if (BitsPerComponent == 1) { |
570 int row_bits = std::min(BitsPerComponent * Colors * Columns, | 446 int row_bits = std::min(BitsPerComponent * Colors * Columns, |
571 pdfium::base::checked_cast<int>(row_size * 8)); | 447 pdfium::base::checked_cast<int>(row_size * 8)); |
572 int index_pre = 0; | 448 int index_pre = 0; |
573 int col_pre = 0; | 449 int col_pre = 0; |
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
958 } | 834 } |
959 FX_BOOL ret = TRUE; | 835 FX_BOOL ret = TRUE; |
960 if (predictor_type == 2) { | 836 if (predictor_type == 2) { |
961 ret = PNG_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns); | 837 ret = PNG_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns); |
962 } else if (predictor_type == 1) { | 838 } else if (predictor_type == 1) { |
963 ret = | 839 ret = |
964 TIFF_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns); | 840 TIFF_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns); |
965 } | 841 } |
966 return ret ? offset : FX_INVALID_OFFSET; | 842 return ret ? offset : FX_INVALID_OFFSET; |
967 } | 843 } |
968 FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, | 844 |
| 845 bool CCodec_FlateModule::Encode(const uint8_t* src_buf, |
| 846 uint32_t src_size, |
| 847 uint8_t** dest_buf, |
| 848 uint32_t* dest_size) { |
| 849 *dest_size = src_size + src_size / 1000 + 12; |
| 850 *dest_buf = FX_Alloc(uint8_t, *dest_size); |
| 851 unsigned long temp_size = *dest_size; |
| 852 if (!FPDFAPI_FlateCompress(*dest_buf, &temp_size, src_buf, src_size)) |
| 853 return false; |
| 854 |
| 855 *dest_size = (uint32_t)temp_size; |
| 856 return true; |
| 857 } |
| 858 |
| 859 bool CCodec_FlateModule::PngEncode(const uint8_t* src_buf, |
969 uint32_t src_size, | 860 uint32_t src_size, |
970 int predictor, | 861 uint8_t** dest_buf, |
971 int Colors, | 862 uint32_t* dest_size) { |
972 int BitsPerComponent, | 863 uint8_t* pSrcBuf = FX_Alloc(uint8_t, src_size); |
973 int Columns, | |
974 uint8_t*& dest_buf, | |
975 uint32_t& dest_size) { | |
976 if (predictor != 2 && predictor < 10) { | |
977 return Encode(src_buf, src_size, dest_buf, dest_size); | |
978 } | |
979 uint8_t* pSrcBuf = NULL; | |
980 pSrcBuf = FX_Alloc(uint8_t, src_size); | |
981 FXSYS_memcpy(pSrcBuf, src_buf, src_size); | 864 FXSYS_memcpy(pSrcBuf, src_buf, src_size); |
982 FX_BOOL ret = TRUE; | 865 PNG_PredictorEncode(&pSrcBuf, &src_size); |
983 if (predictor == 2) { | 866 bool ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); |
984 ret = TIFF_PredictorEncode(pSrcBuf, src_size, Colors, BitsPerComponent, | |
985 Columns); | |
986 } else if (predictor >= 10) { | |
987 ret = PNG_PredictorEncode(pSrcBuf, src_size, predictor, Colors, | |
988 BitsPerComponent, Columns); | |
989 } | |
990 if (ret) | |
991 ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); | |
992 FX_Free(pSrcBuf); | 867 FX_Free(pSrcBuf); |
993 return ret; | 868 return ret; |
994 } | 869 } |
995 FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, | |
996 uint32_t src_size, | |
997 uint8_t*& dest_buf, | |
998 uint32_t& dest_size) { | |
999 dest_size = src_size + src_size / 1000 + 12; | |
1000 dest_buf = FX_Alloc(uint8_t, dest_size); | |
1001 unsigned long temp_size = dest_size; | |
1002 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); | |
1003 dest_size = (uint32_t)temp_size; | |
1004 return TRUE; | |
1005 } | |
OLD | NEW |