| 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 "../../../../third_party/base/nonstd_unique_ptr.h" | 7 #include "../../../../third_party/base/nonstd_unique_ptr.h" |
| 8 #include "../../../../third_party/zlib_v128/zlib.h" | 8 #include "../../../../third_party/zlib_v128/zlib.h" |
| 9 #include "../../../include/fxcodec/fx_codec.h" | 9 #include "../../../include/fxcodec/fx_codec.h" |
| 10 #include "../../../include/fxcodec/fx_codec_flate.h" | 10 #include "../../../include/fxcodec/fx_codec_flate.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 void FPDFAPI_FlateEnd(void* context) | 77 void FPDFAPI_FlateEnd(void* context) |
| 78 { | 78 { |
| 79 inflateEnd((z_stream*)context); | 79 inflateEnd((z_stream*)context); |
| 80 ((z_stream*)context)->zfree(0, context); | 80 ((z_stream*)context)->zfree(0, context); |
| 81 } | 81 } |
| 82 } // extern "C" | 82 } // extern "C" |
| 83 | 83 |
| 84 class CLZWDecoder | 84 class CLZWDecoder |
| 85 { | 85 { |
| 86 public: | 86 public: |
| 87 FX_BOOL Decode(uint8_t* output, FX_DWORD& outlen, const uint8_t* input, FX_D
WORD& size, FX_BOOL bEarlyChange); | 87 int Decode(uint8_t* output, FX_DWORD& outlen, const uint8_t* input, FX_DWORD
& size, bool bEarlyChange); |
| 88 |
| 88 private: | 89 private: |
| 90 void AddCode(FX_DWORD prefix_code, uint8_t append_char); |
| 91 void DecodeString(FX_DWORD code); |
| 92 |
| 89 FX_DWORD m_InPos; | 93 FX_DWORD m_InPos; |
| 90 FX_DWORD m_OutPos; | 94 FX_DWORD m_OutPos; |
| 91 uint8_t* m_pOutput; | 95 uint8_t* m_pOutput; |
| 92 const uint8_t* m_pInput; | 96 const uint8_t* m_pInput; |
| 93 FX_BOOL» » m_Early; | 97 bool» » m_Early; |
| 94 void» » AddCode(FX_DWORD prefix_code, uint8_t append_char); | |
| 95 FX_DWORD m_CodeArray[5021]; | 98 FX_DWORD m_CodeArray[5021]; |
| 96 FX_DWORD m_nCodes; | 99 FX_DWORD m_nCodes; |
| 97 uint8_t m_DecodeStack[4000]; | 100 uint8_t m_DecodeStack[4000]; |
| 98 FX_DWORD m_StackLen; | 101 FX_DWORD m_StackLen; |
| 99 void DecodeString(FX_DWORD code); | |
| 100 int m_CodeLen; | 102 int m_CodeLen; |
| 101 }; | 103 }; |
| 102 void CLZWDecoder::AddCode(FX_DWORD prefix_code, uint8_t append_char) | 104 void CLZWDecoder::AddCode(FX_DWORD prefix_code, uint8_t append_char) |
| 103 { | 105 { |
| 104 if (m_nCodes + m_Early == 4094) { | 106 if (m_nCodes + m_Early == 4094) { |
| 105 return; | 107 return; |
| 106 } | 108 } |
| 107 m_CodeArray[m_nCodes ++] = (prefix_code << 16) | append_char; | 109 m_CodeArray[m_nCodes ++] = (prefix_code << 16) | append_char; |
| 108 if (m_nCodes + m_Early == 512 - 258) { | 110 if (m_nCodes + m_Early == 512 - 258) { |
| 109 m_CodeLen = 10; | 111 m_CodeLen = 10; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 125 return; | 127 return; |
| 126 } | 128 } |
| 127 m_DecodeStack[m_StackLen++] = (uint8_t)data; | 129 m_DecodeStack[m_StackLen++] = (uint8_t)data; |
| 128 code = data >> 16; | 130 code = data >> 16; |
| 129 } | 131 } |
| 130 if (m_StackLen >= sizeof(m_DecodeStack)) { | 132 if (m_StackLen >= sizeof(m_DecodeStack)) { |
| 131 return; | 133 return; |
| 132 } | 134 } |
| 133 m_DecodeStack[m_StackLen++] = (uint8_t)code; | 135 m_DecodeStack[m_StackLen++] = (uint8_t)code; |
| 134 } | 136 } |
| 135 int CLZWDecoder::Decode(uint8_t* dest_buf, FX_DWORD& dest_size, const uint8_t* s
rc_buf, FX_DWORD& src_size, FX_BOOL bEarlyChange) | 137 int CLZWDecoder::Decode(uint8_t* dest_buf, FX_DWORD& dest_size, const uint8_t* s
rc_buf, FX_DWORD& src_size, bool bEarlyChange) |
| 136 { | 138 { |
| 137 m_CodeLen = 9; | 139 m_CodeLen = 9; |
| 138 m_InPos = 0; | 140 m_InPos = 0; |
| 139 m_OutPos = 0; | 141 m_OutPos = 0; |
| 140 m_pInput = src_buf; | 142 m_pInput = src_buf; |
| 141 m_pOutput = dest_buf; | 143 m_pOutput = dest_buf; |
| 142 m_Early = bEarlyChange ? 1 : 0; | 144 m_Early = bEarlyChange ? 1 : 0; |
| 143 m_nCodes = 0; | 145 m_nCodes = 0; |
| 144 FX_DWORD old_code = (FX_DWORD) - 1; | 146 FX_DWORD old_code = (FX_DWORD) - 1; |
| 145 uint8_t last_char; | 147 uint8_t last_char; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 int pb = FXSYS_abs(p - b); | 231 int pb = FXSYS_abs(p - b); |
| 230 int pc = FXSYS_abs(p - c); | 232 int pc = FXSYS_abs(p - c); |
| 231 if (pa <= pb && pa <= pc) { | 233 if (pa <= pb && pa <= pc) { |
| 232 return (uint8_t)a; | 234 return (uint8_t)a; |
| 233 } | 235 } |
| 234 if (pb <= pc) { | 236 if (pb <= pc) { |
| 235 return (uint8_t)b; | 237 return (uint8_t)b; |
| 236 } | 238 } |
| 237 return (uint8_t)c; | 239 return (uint8_t)c; |
| 238 } | 240 } |
| 239 static FX_BOOL PNG_PredictorEncode(uint8_t*& data_buf, FX_DWORD& data_size, | 241 static bool PNG_PredictorEncode(uint8_t*& data_buf, FX_DWORD& data_size, |
| 240 int predictor, int Colors, | 242 int predictor, int Colors, |
| 241 int BitsPerComponent, int Columns) | 243 int BitsPerComponent, int Columns) |
| 242 { | 244 { |
| 243 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; | 245 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; |
| 244 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; | 246 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; |
| 245 if (row_size <= 0) | 247 if (row_size <= 0) |
| 246 return FALSE; | 248 return false; |
| 247 const int row_count = (data_size + row_size - 1) / row_size; | 249 const int row_count = (data_size + row_size - 1) / row_size; |
| 248 const int last_row_size = data_size % row_size; | 250 const int last_row_size = data_size % row_size; |
| 249 uint8_t* dest_buf = FX_Alloc2D(uint8_t, row_size + 1, row_count); | 251 uint8_t* dest_buf = FX_Alloc2D(uint8_t, row_size + 1, row_count); |
| 250 int byte_cnt = 0; | 252 int byte_cnt = 0; |
| 251 uint8_t* pSrcData = data_buf; | 253 uint8_t* pSrcData = data_buf; |
| 252 uint8_t* pDestData = dest_buf; | 254 uint8_t* pDestData = dest_buf; |
| 253 for (int row = 0; row < row_count; row++) { | 255 for (int row = 0; row < row_count; row++) { |
| 254 if (predictor == 10) { | 256 if (predictor == 10) { |
| 255 pDestData[0] = 0; | 257 pDestData[0] = 0; |
| 256 int move_size = row_size; | 258 int move_size = row_size; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 break; | 321 break; |
| 320 } | 322 } |
| 321 byte_cnt++; | 323 byte_cnt++; |
| 322 } | 324 } |
| 323 pDestData += (row_size + 1); | 325 pDestData += (row_size + 1); |
| 324 pSrcData += row_size; | 326 pSrcData += row_size; |
| 325 } | 327 } |
| 326 FX_Free(data_buf); | 328 FX_Free(data_buf); |
| 327 data_buf = dest_buf; | 329 data_buf = dest_buf; |
| 328 data_size = (row_size + 1) * row_count - (last_row_size > 0 ? (row_size - la
st_row_size) : 0); | 330 data_size = (row_size + 1) * row_count - (last_row_size > 0 ? (row_size - la
st_row_size) : 0); |
| 329 return TRUE; | 331 return true; |
| 330 } | 332 } |
| 331 static void PNG_PredictLine(uint8_t* pDestData, const uint8_t* pSrcData, const u
int8_t* pLastLine, | 333 static void PNG_PredictLine(uint8_t* pDestData, const uint8_t* pSrcData, const u
int8_t* pLastLine, |
| 332 int bpc, int nColors, int nPixels) | 334 int bpc, int nColors, int nPixels) |
| 333 { | 335 { |
| 334 int row_size = (nPixels * bpc * nColors + 7) / 8; | 336 int row_size = (nPixels * bpc * nColors + 7) / 8; |
| 335 int BytesPerPixel = (bpc * nColors + 7) / 8; | 337 int BytesPerPixel = (bpc * nColors + 7) / 8; |
| 336 uint8_t tag = pSrcData[0]; | 338 uint8_t tag = pSrcData[0]; |
| 337 if (tag == 0) { | 339 if (tag == 0) { |
| 338 FXSYS_memmove(pDestData, pSrcData + 1, row_size); | 340 FXSYS_memmove(pDestData, pSrcData + 1, row_size); |
| 339 return; | 341 return; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 } | 386 } |
| 385 pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_
left); | 387 pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_
left); |
| 386 break; | 388 break; |
| 387 } | 389 } |
| 388 default: | 390 default: |
| 389 pDestData[byte] = raw_byte; | 391 pDestData[byte] = raw_byte; |
| 390 break; | 392 break; |
| 391 } | 393 } |
| 392 } | 394 } |
| 393 } | 395 } |
| 394 static FX_BOOL PNG_Predictor(uint8_t*& data_buf, FX_DWORD& data_size, | 396 static bool PNG_Predictor(uint8_t*& data_buf, FX_DWORD& data_size, |
| 395 int Colors, int BitsPerComponent, int Columns) | 397 int Colors, int BitsPerComponent, int Columns) |
| 396 { | 398 { |
| 397 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; | 399 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; |
| 398 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; | 400 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; |
| 399 if (row_size <= 0) | 401 if (row_size <= 0) |
| 400 return FALSE; | 402 return false; |
| 401 const int row_count = (data_size + row_size) / (row_size + 1); | 403 const int row_count = (data_size + row_size) / (row_size + 1); |
| 402 const int last_row_size = data_size % (row_size + 1); | 404 const int last_row_size = data_size % (row_size + 1); |
| 403 uint8_t* dest_buf = FX_Alloc2D(uint8_t, row_size, row_count); | 405 uint8_t* dest_buf = FX_Alloc2D(uint8_t, row_size, row_count); |
| 404 int byte_cnt = 0; | 406 int byte_cnt = 0; |
| 405 uint8_t* pSrcData = data_buf; | 407 uint8_t* pSrcData = data_buf; |
| 406 uint8_t* pDestData = dest_buf; | 408 uint8_t* pDestData = dest_buf; |
| 407 for (int row = 0; row < row_count; row ++) { | 409 for (int row = 0; row < row_count; row ++) { |
| 408 uint8_t tag = pSrcData[0]; | 410 uint8_t tag = pSrcData[0]; |
| 409 byte_cnt++; | 411 byte_cnt++; |
| 410 if (tag == 0) { | 412 if (tag == 0) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 break; | 472 break; |
| 471 } | 473 } |
| 472 byte_cnt++; | 474 byte_cnt++; |
| 473 } | 475 } |
| 474 pSrcData += row_size + 1; | 476 pSrcData += row_size + 1; |
| 475 pDestData += row_size; | 477 pDestData += row_size; |
| 476 } | 478 } |
| 477 FX_Free(data_buf); | 479 FX_Free(data_buf); |
| 478 data_buf = dest_buf; | 480 data_buf = dest_buf; |
| 479 data_size = row_size * row_count - (last_row_size > 0 ? (row_size + 1 - last
_row_size) : 0); | 481 data_size = row_size * row_count - (last_row_size > 0 ? (row_size + 1 - last
_row_size) : 0); |
| 480 return TRUE; | 482 return true; |
| 481 } | 483 } |
| 482 static void TIFF_PredictorEncodeLine(uint8_t* dest_buf, int row_size, int BitsPe
rComponent, int Colors, int Columns) | 484 static void TIFF_PredictorEncodeLine(uint8_t* dest_buf, int row_size, int BitsPe
rComponent, int Colors, int Columns) |
| 483 { | 485 { |
| 484 int BytesPerPixel = BitsPerComponent * Colors / 8; | 486 int BytesPerPixel = BitsPerComponent * Colors / 8; |
| 485 if (BitsPerComponent < 8) { | 487 if (BitsPerComponent < 8) { |
| 486 uint8_t mask = 0x01; | 488 uint8_t mask = 0x01; |
| 487 if (BitsPerComponent == 2) { | 489 if (BitsPerComponent == 2) { |
| 488 mask = 0x03; | 490 mask = 0x03; |
| 489 } else if (BitsPerComponent == 4) { | 491 } else if (BitsPerComponent == 4) { |
| 490 mask = 0x0F; | 492 mask = 0x0F; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 509 } | 511 } |
| 510 } else { | 512 } else { |
| 511 for (int i = row_size - BytesPerPixel; i >= BytesPerPixel; i -= BytesPer
Pixel) { | 513 for (int i = row_size - BytesPerPixel; i >= BytesPerPixel; i -= BytesPer
Pixel) { |
| 512 FX_WORD pixel = (dest_buf[i] << 8) | dest_buf[i + 1]; | 514 FX_WORD pixel = (dest_buf[i] << 8) | dest_buf[i + 1]; |
| 513 pixel -= (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerP
ixel + 1]; | 515 pixel -= (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerP
ixel + 1]; |
| 514 dest_buf[i] = pixel >> 8; | 516 dest_buf[i] = pixel >> 8; |
| 515 dest_buf[i + 1] = (uint8_t)pixel; | 517 dest_buf[i + 1] = (uint8_t)pixel; |
| 516 } | 518 } |
| 517 } | 519 } |
| 518 } | 520 } |
| 519 static FX_BOOL TIFF_PredictorEncode(uint8_t*& data_buf, FX_DWORD& data_size, | 521 static bool TIFF_PredictorEncode(uint8_t*& data_buf, FX_DWORD& data_size, |
| 520 int Colors, int BitsPerComponent, int Column
s) | 522 int Colors, int BitsPerComponent, int Column
s) |
| 521 { | 523 { |
| 522 int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; | 524 int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; |
| 523 if (row_size == 0) | 525 if (row_size == 0) |
| 524 return FALSE; | 526 return false; |
| 525 const int row_count = (data_size + row_size - 1) / row_size; | 527 const int row_count = (data_size + row_size - 1) / row_size; |
| 526 const int last_row_size = data_size % row_size; | 528 const int last_row_size = data_size % row_size; |
| 527 for (int row = 0; row < row_count; row++) { | 529 for (int row = 0; row < row_count; row++) { |
| 528 uint8_t* scan_line = data_buf + row * row_size; | 530 uint8_t* scan_line = data_buf + row * row_size; |
| 529 if ((row + 1) * row_size > (int)data_size) { | 531 if ((row + 1) * row_size > (int)data_size) { |
| 530 row_size = last_row_size; | 532 row_size = last_row_size; |
| 531 } | 533 } |
| 532 TIFF_PredictorEncodeLine(scan_line, row_size, BitsPerComponent, Colors,
Columns); | 534 TIFF_PredictorEncodeLine(scan_line, row_size, BitsPerComponent, Colors,
Columns); |
| 533 } | 535 } |
| 534 return TRUE; | 536 return true; |
| 535 } | 537 } |
| 536 static void TIFF_PredictLine(uint8_t* dest_buf, int row_size, int BitsPerCompone
nt, int Colors, int Columns) | 538 static void TIFF_PredictLine(uint8_t* dest_buf, int row_size, int BitsPerCompone
nt, int Colors, int Columns) |
| 537 { | 539 { |
| 538 if (BitsPerComponent == 1) { | 540 if (BitsPerComponent == 1) { |
| 539 int row_bits = FX_MIN(BitsPerComponent * Colors * Columns, row_size * 8)
; | 541 int row_bits = FX_MIN(BitsPerComponent * Colors * Columns, row_size * 8)
; |
| 540 int index_pre = 0; | 542 int index_pre = 0; |
| 541 int col_pre = 0; | 543 int col_pre = 0; |
| 542 for(int i = 1; i < row_bits; i ++) { | 544 for(int i = 1; i < row_bits; i ++) { |
| 543 int col = i % 8; | 545 int col = i % 8; |
| 544 int index = i / 8; | 546 int index = i / 8; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 559 pixel += (dest_buf[i] << 8) | dest_buf[i + 1]; | 561 pixel += (dest_buf[i] << 8) | dest_buf[i + 1]; |
| 560 dest_buf[i] = pixel >> 8; | 562 dest_buf[i] = pixel >> 8; |
| 561 dest_buf[i + 1] = (uint8_t)pixel; | 563 dest_buf[i + 1] = (uint8_t)pixel; |
| 562 } | 564 } |
| 563 } else { | 565 } else { |
| 564 for (int i = BytesPerPixel; i < row_size; i ++) { | 566 for (int i = BytesPerPixel; i < row_size; i ++) { |
| 565 dest_buf[i] += dest_buf[i - BytesPerPixel]; | 567 dest_buf[i] += dest_buf[i - BytesPerPixel]; |
| 566 } | 568 } |
| 567 } | 569 } |
| 568 } | 570 } |
| 569 static FX_BOOL TIFF_Predictor(uint8_t*& data_buf, FX_DWORD& data_size, | 571 static bool TIFF_Predictor(uint8_t*& data_buf, FX_DWORD& data_size, |
| 570 int Colors, int BitsPerComponent, int Columns) | 572 int Colors, int BitsPerComponent, int Columns) |
| 571 { | 573 { |
| 572 int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; | 574 int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; |
| 573 if (row_size == 0) | 575 if (row_size == 0) |
| 574 return FALSE; | 576 return false; |
| 575 const int row_count = (data_size + row_size - 1) / row_size; | 577 const int row_count = (data_size + row_size - 1) / row_size; |
| 576 const int last_row_size = data_size % row_size; | 578 const int last_row_size = data_size % row_size; |
| 577 for (int row = 0; row < row_count; row ++) { | 579 for (int row = 0; row < row_count; row ++) { |
| 578 uint8_t* scan_line = data_buf + row * row_size; | 580 uint8_t* scan_line = data_buf + row * row_size; |
| 579 if ((row + 1) * row_size > (int)data_size) { | 581 if ((row + 1) * row_size > (int)data_size) { |
| 580 row_size = last_row_size; | 582 row_size = last_row_size; |
| 581 } | 583 } |
| 582 TIFF_PredictLine(scan_line, row_size, BitsPerComponent, Colors, Columns)
; | 584 TIFF_PredictLine(scan_line, row_size, BitsPerComponent, Colors, Columns)
; |
| 583 } | 585 } |
| 584 return TRUE; | 586 return true; |
| 585 } | 587 } |
| 586 class CCodec_FlateScanlineDecoder : public CCodec_ScanlineDecoder | 588 class CCodec_FlateScanlineDecoder : public CCodec_ScanlineDecoder |
| 587 { | 589 { |
| 588 public: | 590 public: |
| 589 CCodec_FlateScanlineDecoder(); | 591 CCodec_FlateScanlineDecoder(); |
| 590 ~CCodec_FlateScanlineDecoder(); | 592 ~CCodec_FlateScanlineDecoder(); |
| 591 void Create(const uint8_t* src_buf, FX_DWORD src_size, int width, int height
, int nComps, int bpc, | 593 void Create(const uint8_t* src_buf, FX_DWORD src_size, int width, int height
, int nComps, int bpc, |
| 592 int predictor, int Colors, int BitsPerComponent, int Columns); | 594 int predictor, int Colors, int BitsPerComponent, int Columns); |
| 593 virtual void Destroy() | 595 virtual void Destroy() |
| 594 { | 596 { |
| 595 delete this; | 597 delete this; |
| 596 } | 598 } |
| 597 virtual void v_DownScale(int dest_width, int dest_height) {} | 599 virtual void v_DownScale(int dest_width, int dest_height) {} |
| 598 virtual FX_BOOL» » v_Rewind(); | 600 virtual bool» » v_Rewind(); |
| 599 virtual uint8_t* v_GetNextLine(); | 601 virtual uint8_t* v_GetNextLine(); |
| 600 virtual FX_DWORD GetSrcOffset(); | 602 virtual FX_DWORD GetSrcOffset(); |
| 601 void* m_pFlate; | 603 void* m_pFlate; |
| 602 const uint8_t* m_SrcBuf; | 604 const uint8_t* m_SrcBuf; |
| 603 FX_DWORD m_SrcSize; | 605 FX_DWORD m_SrcSize; |
| 604 uint8_t* m_pScanline; | 606 uint8_t* m_pScanline; |
| 605 uint8_t* m_pLastLine; | 607 uint8_t* m_pLastLine; |
| 606 uint8_t* m_pPredictBuffer; | 608 uint8_t* m_pPredictBuffer; |
| 607 uint8_t* m_pPredictRaw; | 609 uint8_t* m_pPredictRaw; |
| 608 int m_Predictor; | 610 int m_Predictor; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 637 } | 639 } |
| 638 void CCodec_FlateScanlineDecoder::Create(const uint8_t* src_buf, FX_DWORD src_si
ze, int width, int height, | 640 void CCodec_FlateScanlineDecoder::Create(const uint8_t* src_buf, FX_DWORD src_si
ze, int width, int height, |
| 639 int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, in
t Columns) | 641 int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, in
t Columns) |
| 640 { | 642 { |
| 641 m_SrcBuf = src_buf; | 643 m_SrcBuf = src_buf; |
| 642 m_SrcSize = src_size; | 644 m_SrcSize = src_size; |
| 643 m_OutputWidth = m_OrigWidth = width; | 645 m_OutputWidth = m_OrigWidth = width; |
| 644 m_OutputHeight = m_OrigHeight = height; | 646 m_OutputHeight = m_OrigHeight = height; |
| 645 m_nComps = nComps; | 647 m_nComps = nComps; |
| 646 m_bpc = bpc; | 648 m_bpc = bpc; |
| 647 m_bColorTransformed = FALSE; | 649 m_bColorTransformed = false; |
| 648 m_Pitch = (width * nComps * bpc + 7) / 8; | 650 m_Pitch = (width * nComps * bpc + 7) / 8; |
| 649 m_pScanline = FX_Alloc(uint8_t, m_Pitch); | 651 m_pScanline = FX_Alloc(uint8_t, m_Pitch); |
| 650 m_Predictor = 0; | 652 m_Predictor = 0; |
| 651 if (predictor) { | 653 if (predictor) { |
| 652 if (predictor >= 10) { | 654 if (predictor >= 10) { |
| 653 m_Predictor = 2; | 655 m_Predictor = 2; |
| 654 } else if (predictor == 2) { | 656 } else if (predictor == 2) { |
| 655 m_Predictor = 1; | 657 m_Predictor = 1; |
| 656 } | 658 } |
| 657 if (m_Predictor) { | 659 if (m_Predictor) { |
| 658 if (BitsPerComponent * Colors * Columns == 0) { | 660 if (BitsPerComponent * Colors * Columns == 0) { |
| 659 BitsPerComponent = m_bpc; | 661 BitsPerComponent = m_bpc; |
| 660 Colors = m_nComps; | 662 Colors = m_nComps; |
| 661 Columns = m_OrigWidth; | 663 Columns = m_OrigWidth; |
| 662 } | 664 } |
| 663 m_Colors = Colors; | 665 m_Colors = Colors; |
| 664 m_BitsPerComponent = BitsPerComponent; | 666 m_BitsPerComponent = BitsPerComponent; |
| 665 m_Columns = Columns; | 667 m_Columns = Columns; |
| 666 m_PredictPitch = (m_BitsPerComponent * m_Colors * m_Columns + 7) / 8
; | 668 m_PredictPitch = (m_BitsPerComponent * m_Colors * m_Columns + 7) / 8
; |
| 667 m_pLastLine = FX_Alloc(uint8_t, m_PredictPitch); | 669 m_pLastLine = FX_Alloc(uint8_t, m_PredictPitch); |
| 668 m_pPredictRaw = FX_Alloc(uint8_t, m_PredictPitch + 1); | 670 m_pPredictRaw = FX_Alloc(uint8_t, m_PredictPitch + 1); |
| 669 m_pPredictBuffer = FX_Alloc(uint8_t, m_PredictPitch); | 671 m_pPredictBuffer = FX_Alloc(uint8_t, m_PredictPitch); |
| 670 } | 672 } |
| 671 } | 673 } |
| 672 } | 674 } |
| 673 FX_BOOL CCodec_FlateScanlineDecoder::v_Rewind() | 675 bool CCodec_FlateScanlineDecoder::v_Rewind() |
| 674 { | 676 { |
| 675 if (m_pFlate) { | 677 if (m_pFlate) { |
| 676 FPDFAPI_FlateEnd(m_pFlate); | 678 FPDFAPI_FlateEnd(m_pFlate); |
| 677 } | 679 } |
| 678 m_pFlate = FPDFAPI_FlateInit(my_alloc_func, my_free_func); | 680 m_pFlate = FPDFAPI_FlateInit(my_alloc_func, my_free_func); |
| 679 if (m_pFlate == NULL) { | 681 if (m_pFlate == NULL) { |
| 680 return FALSE; | 682 return false; |
| 681 } | 683 } |
| 682 FPDFAPI_FlateInput(m_pFlate, m_SrcBuf, m_SrcSize); | 684 FPDFAPI_FlateInput(m_pFlate, m_SrcBuf, m_SrcSize); |
| 683 m_LeftOver = 0; | 685 m_LeftOver = 0; |
| 684 return TRUE; | 686 return true; |
| 685 } | 687 } |
| 686 uint8_t* CCodec_FlateScanlineDecoder::v_GetNextLine() | 688 uint8_t* CCodec_FlateScanlineDecoder::v_GetNextLine() |
| 687 { | 689 { |
| 688 if (m_Predictor) { | 690 if (m_Predictor) { |
| 689 if (m_Pitch == m_PredictPitch) { | 691 if (m_Pitch == m_PredictPitch) { |
| 690 if (m_Predictor == 2) { | 692 if (m_Predictor == 2) { |
| 691 FPDFAPI_FlateOutput(m_pFlate, m_pPredictRaw, m_PredictPitch + 1)
; | 693 FPDFAPI_FlateOutput(m_pFlate, m_pPredictRaw, m_PredictPitch + 1)
; |
| 692 PNG_PredictLine(m_pScanline, m_pPredictRaw, m_pLastLine, m_BitsP
erComponent, m_Colors, m_Columns); | 694 PNG_PredictLine(m_pScanline, m_pPredictRaw, m_pLastLine, m_BitsP
erComponent, m_Colors, m_Columns); |
| 693 FXSYS_memcpy(m_pLastLine, m_pScanline, m_PredictPitch); | 695 FXSYS_memcpy(m_pLastLine, m_pScanline, m_PredictPitch); |
| 694 } else { | 696 } else { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 723 } | 725 } |
| 724 return m_pScanline; | 726 return m_pScanline; |
| 725 } | 727 } |
| 726 FX_DWORD CCodec_FlateScanlineDecoder::GetSrcOffset() | 728 FX_DWORD CCodec_FlateScanlineDecoder::GetSrcOffset() |
| 727 { | 729 { |
| 728 return FPDFAPI_FlateGetTotalIn(m_pFlate); | 730 return FPDFAPI_FlateGetTotalIn(m_pFlate); |
| 729 } | 731 } |
| 730 static void FlateUncompress(const uint8_t* src_buf, FX_DWORD src_size, FX_DWORD
orig_size, | 732 static void FlateUncompress(const uint8_t* src_buf, FX_DWORD src_size, FX_DWORD
orig_size, |
| 731 uint8_t*& dest_buf, FX_DWORD& dest_size, FX_DWORD& o
ffset) | 733 uint8_t*& dest_buf, FX_DWORD& dest_size, FX_DWORD& o
ffset) |
| 732 { | 734 { |
| 733 const FX_BOOL useOldImpl = src_size < 10240; | 735 const bool useOldImpl = src_size < 10240; |
| 734 FX_DWORD guess_size = orig_size ? orig_size : src_size * 2; | 736 FX_DWORD guess_size = orig_size ? orig_size : src_size * 2; |
| 735 FX_DWORD alloc_step = orig_size ? 10240 : (src_size < 10240 ? 10240 : src_si
ze); | 737 FX_DWORD alloc_step = orig_size ? 10240 : (src_size < 10240 ? 10240 : src_si
ze); |
| 736 static const FX_DWORD kMaxInitialAllocSize = 10000000; | 738 static const FX_DWORD kMaxInitialAllocSize = 10000000; |
| 737 if (guess_size > kMaxInitialAllocSize) { | 739 if (guess_size > kMaxInitialAllocSize) { |
| 738 guess_size = kMaxInitialAllocSize; | 740 guess_size = kMaxInitialAllocSize; |
| 739 alloc_step = kMaxInitialAllocSize; | 741 alloc_step = kMaxInitialAllocSize; |
| 740 } | 742 } |
| 741 FX_DWORD buf_size = guess_size; | 743 FX_DWORD buf_size = guess_size; |
| 742 FX_DWORD last_buf_size = buf_size; | 744 FX_DWORD last_buf_size = buf_size; |
| 743 void* context = nullptr; | 745 void* context = nullptr; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 830 dest_size = 0; | 832 dest_size = 0; |
| 831 return; | 833 return; |
| 832 } | 834 } |
| 833 ICodec_ScanlineDecoder* CCodec_FlateModule::CreateDecoder(const uint8_t* src_buf
, FX_DWORD src_size, int width, int height, | 835 ICodec_ScanlineDecoder* CCodec_FlateModule::CreateDecoder(const uint8_t* src_buf
, FX_DWORD src_size, int width, int height, |
| 834 int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, in
t Columns) | 836 int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, in
t Columns) |
| 835 { | 837 { |
| 836 CCodec_FlateScanlineDecoder* pDecoder = new CCodec_FlateScanlineDecoder; | 838 CCodec_FlateScanlineDecoder* pDecoder = new CCodec_FlateScanlineDecoder; |
| 837 pDecoder->Create(src_buf, src_size, width, height, nComps, bpc, predictor, C
olors, BitsPerComponent, Columns); | 839 pDecoder->Create(src_buf, src_size, width, height, nComps, bpc, predictor, C
olors, BitsPerComponent, Columns); |
| 838 return pDecoder; | 840 return pDecoder; |
| 839 } | 841 } |
| 840 FX_DWORD CCodec_FlateModule::FlateOrLZWDecode(FX_BOOL bLZW, const uint8_t* src_b
uf, FX_DWORD src_size, FX_BOOL bEarlyChange, | 842 FX_DWORD CCodec_FlateModule::FlateOrLZWDecode(bool bLZW, const uint8_t* src_buf,
FX_DWORD src_size, bool bEarlyChange, |
| 841 int predictor, int Colors, int BitsPerComponent, int Columns, | 843 int predictor, int Colors, int BitsPerComponent, int Columns, |
| 842 FX_DWORD estimated_size, uint8_t*& dest_buf, FX_DWORD& dest_size) | 844 FX_DWORD estimated_size, uint8_t*& dest_buf, FX_DWORD& dest_size) |
| 843 { | 845 { |
| 844 dest_buf = NULL; | 846 dest_buf = NULL; |
| 845 FX_DWORD offset = 0; | 847 FX_DWORD offset = 0; |
| 846 int predictor_type = 0; | 848 int predictor_type = 0; |
| 847 if (predictor) { | 849 if (predictor) { |
| 848 if (predictor >= 10) { | 850 if (predictor >= 10) { |
| 849 predictor_type = 2; | 851 predictor_type = 2; |
| 850 } else if (predictor == 2) { | 852 } else if (predictor == 2) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 867 dest_buf = FX_Alloc( uint8_t, dest_size + 1); | 869 dest_buf = FX_Alloc( uint8_t, dest_size + 1); |
| 868 dest_buf[dest_size] = '\0'; | 870 dest_buf[dest_size] = '\0'; |
| 869 decoder->Decode(dest_buf, dest_size, src_buf, offset, bEarlyChange); | 871 decoder->Decode(dest_buf, dest_size, src_buf, offset, bEarlyChange); |
| 870 } | 872 } |
| 871 } else { | 873 } else { |
| 872 FlateUncompress(src_buf, src_size, estimated_size, dest_buf, dest_size,
offset); | 874 FlateUncompress(src_buf, src_size, estimated_size, dest_buf, dest_size,
offset); |
| 873 } | 875 } |
| 874 if (predictor_type == 0) { | 876 if (predictor_type == 0) { |
| 875 return offset; | 877 return offset; |
| 876 } | 878 } |
| 877 FX_BOOL ret = TRUE; | 879 bool ret = true; |
| 878 if (predictor_type == 2) { | 880 if (predictor_type == 2) { |
| 879 ret = PNG_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, | 881 ret = PNG_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, |
| 880 Columns); | 882 Columns); |
| 881 } else if (predictor_type == 1) { | 883 } else if (predictor_type == 1) { |
| 882 ret = TIFF_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, | 884 ret = TIFF_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, |
| 883 Columns); | 885 Columns); |
| 884 } | 886 } |
| 885 return ret ? offset : -1; | 887 return ret ? offset : -1; |
| 886 } | 888 } |
| 887 FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, FX_DWORD src_size, | 889 bool CCodec_FlateModule::Encode(const uint8_t* src_buf, FX_DWORD src_size, |
| 888 int predictor, int Colors, int BitsPerCompone
nt, int Columns, | 890 int predictor, int Colors, int BitsPerCompone
nt, int Columns, |
| 889 uint8_t*& dest_buf, FX_DWORD& dest_size) | 891 uint8_t*& dest_buf, FX_DWORD& dest_size) |
| 890 { | 892 { |
| 891 if (predictor != 2 && predictor < 10) { | 893 if (predictor != 2 && predictor < 10) { |
| 892 return Encode(src_buf, src_size, dest_buf, dest_size); | 894 return Encode(src_buf, src_size, dest_buf, dest_size); |
| 893 } | 895 } |
| 894 uint8_t* pSrcBuf = NULL; | 896 uint8_t* pSrcBuf = NULL; |
| 895 pSrcBuf = FX_Alloc(uint8_t, src_size); | 897 pSrcBuf = FX_Alloc(uint8_t, src_size); |
| 896 FXSYS_memcpy(pSrcBuf, src_buf, src_size); | 898 FXSYS_memcpy(pSrcBuf, src_buf, src_size); |
| 897 FX_BOOL ret = TRUE; | 899 bool ret = true; |
| 898 if (predictor == 2) { | 900 if (predictor == 2) { |
| 899 ret = TIFF_PredictorEncode(pSrcBuf, src_size, Colors, BitsPerComponent, | 901 ret = TIFF_PredictorEncode(pSrcBuf, src_size, Colors, BitsPerComponent, |
| 900 Columns); | 902 Columns); |
| 901 } else if (predictor >= 10) { | 903 } else if (predictor >= 10) { |
| 902 ret = PNG_PredictorEncode(pSrcBuf, src_size, predictor, Colors, | 904 ret = PNG_PredictorEncode(pSrcBuf, src_size, predictor, Colors, |
| 903 BitsPerComponent, Columns); | 905 BitsPerComponent, Columns); |
| 904 } | 906 } |
| 905 if (ret) | 907 if (ret) |
| 906 ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); | 908 ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); |
| 907 FX_Free(pSrcBuf); | 909 FX_Free(pSrcBuf); |
| 908 return ret; | 910 return ret; |
| 909 } | 911 } |
| 910 FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, FX_DWORD src_size, ui
nt8_t*& dest_buf, FX_DWORD& dest_size) | 912 bool CCodec_FlateModule::Encode(const uint8_t* src_buf, FX_DWORD src_size, uint8
_t*& dest_buf, FX_DWORD& dest_size) |
| 911 { | 913 { |
| 912 dest_size = src_size + src_size / 1000 + 12; | 914 dest_size = src_size + src_size / 1000 + 12; |
| 913 dest_buf = FX_Alloc( uint8_t, dest_size); | 915 dest_buf = FX_Alloc( uint8_t, dest_size); |
| 914 unsigned long temp_size = dest_size; | 916 unsigned long temp_size = dest_size; |
| 915 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); | 917 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); |
| 916 dest_size = (FX_DWORD)temp_size; | 918 dest_size = (FX_DWORD)temp_size; |
| 917 return TRUE; | 919 return true; |
| 918 } | 920 } |
| OLD | NEW |