diff options
Diffstat (limited to 'core/fxcodec/codec')
-rw-r--r-- | core/fxcodec/codec/ccodec_basicmodule.h | 8 | ||||
-rw-r--r-- | core/fxcodec/codec/ccodec_flatemodule.h | 18 | ||||
-rw-r--r-- | core/fxcodec/codec/ccodec_jpegmodule.h | 23 | ||||
-rw-r--r-- | core/fxcodec/codec/fx_codec.cpp | 14 | ||||
-rw-r--r-- | core/fxcodec/codec/fx_codec_fax.cpp | 254 | ||||
-rw-r--r-- | core/fxcodec/codec/fx_codec_flate.cpp | 248 | ||||
-rw-r--r-- | core/fxcodec/codec/fx_codec_jpeg.cpp | 325 |
7 files changed, 175 insertions, 715 deletions
diff --git a/core/fxcodec/codec/ccodec_basicmodule.h b/core/fxcodec/codec/ccodec_basicmodule.h index 4151b4f0f1..641cd00cf4 100644 --- a/core/fxcodec/codec/ccodec_basicmodule.h +++ b/core/fxcodec/codec/ccodec_basicmodule.h @@ -13,14 +13,6 @@ class CCodec_ScanlineDecoder; class CCodec_BasicModule { public: - FX_BOOL RunLengthEncode(const uint8_t* src_buf, - uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size); - FX_BOOL A85Encode(const uint8_t* src_buf, - uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size); CCodec_ScanlineDecoder* CreateRunLengthDecoder(const uint8_t* src_buf, uint32_t src_size, int width, diff --git a/core/fxcodec/codec/ccodec_flatemodule.h b/core/fxcodec/codec/ccodec_flatemodule.h index aee03719de..01797e7e68 100644 --- a/core/fxcodec/codec/ccodec_flatemodule.h +++ b/core/fxcodec/codec/ccodec_flatemodule.h @@ -34,18 +34,14 @@ class CCodec_FlateModule { uint32_t estimated_size, uint8_t*& dest_buf, uint32_t& dest_size); - FX_BOOL Encode(const uint8_t* src_buf, + bool Encode(const uint8_t* src_buf, + uint32_t src_size, + uint8_t** dest_buf, + uint32_t* dest_size); + bool PngEncode(const uint8_t* src_buf, uint32_t src_size, - int predictor, - int Colors, - int BitsPerComponent, - int Columns, - uint8_t*& dest_buf, - uint32_t& dest_size); - FX_BOOL Encode(const uint8_t* src_buf, - uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size); + uint8_t** dest_buf, + uint32_t* dest_size); }; #endif // CORE_FXCODEC_CODEC_CCODEC_FLATEMODULE_H_ diff --git a/core/fxcodec/codec/ccodec_jpegmodule.h b/core/fxcodec/codec/ccodec_jpegmodule.h index 98554fea1c..52fc4d8e81 100644 --- a/core/fxcodec/codec/ccodec_jpegmodule.h +++ b/core/fxcodec/codec/ccodec_jpegmodule.h @@ -20,27 +20,20 @@ class CFX_DIBAttribute; class CCodec_JpegModule { public: CCodec_JpegModule() {} + CCodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf, uint32_t src_size, int width, int height, int nComps, FX_BOOL ColorTransform); - FX_BOOL LoadInfo(const uint8_t* src_buf, - uint32_t src_size, - int& width, - int& height, - int& num_components, - int& bits_per_components, - FX_BOOL& color_transform, - uint8_t** icc_buf_ptr = nullptr, - uint32_t* icc_length = nullptr); - FX_BOOL Encode(const CFX_DIBSource* pSource, - uint8_t*& dest_buf, - FX_STRSIZE& dest_size, - int quality = 75, - const uint8_t* icc_buf = nullptr, - uint32_t icc_length = 0); + bool LoadInfo(const uint8_t* src_buf, + uint32_t src_size, + int* width, + int* height, + int* num_components, + int* bits_per_components, + bool* color_transform); FXJPEG_Context* Start(); void Finish(FXJPEG_Context* pContext); void Input(FXJPEG_Context* pContext, diff --git a/core/fxcodec/codec/fx_codec.cpp b/core/fxcodec/codec/fx_codec.cpp index 997d61f25b..6a45737b10 100644 --- a/core/fxcodec/codec/fx_codec.cpp +++ b/core/fxcodec/codec/fx_codec.cpp @@ -77,20 +77,6 @@ uint8_t* CCodec_ScanlineDecoder::ReadNextLine() { return v_GetNextLine(); } -FX_BOOL CCodec_BasicModule::RunLengthEncode(const uint8_t* src_buf, - uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size) { - return FALSE; -} - -FX_BOOL CCodec_BasicModule::A85Encode(const uint8_t* src_buf, - uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size) { - return FALSE; -} - #ifdef PDF_ENABLE_XFA CFX_DIBAttribute::CFX_DIBAttribute() : m_nXDPI(-1), diff --git a/core/fxcodec/codec/fx_codec_fax.cpp b/core/fxcodec/codec/fx_codec_fax.cpp index 59010391c5..90d3b24cab 100644 --- a/core/fxcodec/codec/fx_codec_fax.cpp +++ b/core/fxcodec/codec/fx_codec_fax.cpp @@ -461,151 +461,22 @@ FX_BOOL FaxGet1DLine(const uint8_t* src_buf, return TRUE; } -const uint8_t BlackRunTerminator[128] = { - 0x37, 10, 0x02, 3, 0x03, 2, 0x02, 2, 0x03, 3, 0x03, 4, 0x02, 4, - 0x03, 5, 0x05, 6, 0x04, 6, 0x04, 7, 0x05, 7, 0x07, 7, 0x04, 8, - 0x07, 8, 0x18, 9, 0x17, 10, 0x18, 10, 0x08, 10, 0x67, 11, 0x68, 11, - 0x6c, 11, 0x37, 11, 0x28, 11, 0x17, 11, 0x18, 11, 0xca, 12, 0xcb, 12, - 0xcc, 12, 0xcd, 12, 0x68, 12, 0x69, 12, 0x6a, 12, 0x6b, 12, 0xd2, 12, - 0xd3, 12, 0xd4, 12, 0xd5, 12, 0xd6, 12, 0xd7, 12, 0x6c, 12, 0x6d, 12, - 0xda, 12, 0xdb, 12, 0x54, 12, 0x55, 12, 0x56, 12, 0x57, 12, 0x64, 12, - 0x65, 12, 0x52, 12, 0x53, 12, 0x24, 12, 0x37, 12, 0x38, 12, 0x27, 12, - 0x28, 12, 0x58, 12, 0x59, 12, 0x2b, 12, 0x2c, 12, 0x5a, 12, 0x66, 12, - 0x67, 12, -}; - -const uint8_t BlackRunMarkup[80] = { - 0x0f, 10, 0xc8, 12, 0xc9, 12, 0x5b, 12, 0x33, 12, 0x34, 12, 0x35, 12, - 0x6c, 13, 0x6d, 13, 0x4a, 13, 0x4b, 13, 0x4c, 13, 0x4d, 13, 0x72, 13, - 0x73, 13, 0x74, 13, 0x75, 13, 0x76, 13, 0x77, 13, 0x52, 13, 0x53, 13, - 0x54, 13, 0x55, 13, 0x5a, 13, 0x5b, 13, 0x64, 13, 0x65, 13, 0x08, 11, - 0x0c, 11, 0x0d, 11, 0x12, 12, 0x13, 12, 0x14, 12, 0x15, 12, 0x16, 12, - 0x17, 12, 0x1c, 12, 0x1d, 12, 0x1e, 12, 0x1f, 12, -}; - -const uint8_t WhiteRunTerminator[128] = { - 0x35, 8, 0x07, 6, 0x07, 4, 0x08, 4, 0x0B, 4, 0x0C, 4, 0x0E, 4, 0x0F, 4, - 0x13, 5, 0x14, 5, 0x07, 5, 0x08, 5, 0x08, 6, 0x03, 6, 0x34, 6, 0x35, 6, - 0x2a, 6, 0x2B, 6, 0x27, 7, 0x0c, 7, 0x08, 7, 0x17, 7, 0x03, 7, 0x04, 7, - 0x28, 7, 0x2B, 7, 0x13, 7, 0x24, 7, 0x18, 7, 0x02, 8, 0x03, 8, 0x1a, 8, - 0x1b, 8, 0x12, 8, 0x13, 8, 0x14, 8, 0x15, 8, 0x16, 8, 0x17, 8, 0x28, 8, - 0x29, 8, 0x2a, 8, 0x2b, 8, 0x2c, 8, 0x2d, 8, 0x04, 8, 0x05, 8, 0x0a, 8, - 0x0b, 8, 0x52, 8, 0x53, 8, 0x54, 8, 0x55, 8, 0x24, 8, 0x25, 8, 0x58, 8, - 0x59, 8, 0x5a, 8, 0x5b, 8, 0x4a, 8, 0x4b, 8, 0x32, 8, 0x33, 8, 0x34, 8, -}; - -const uint8_t WhiteRunMarkup[80] = { - 0x1b, 5, 0x12, 5, 0x17, 6, 0x37, 7, 0x36, 8, 0x37, 8, 0x64, 8, - 0x65, 8, 0x68, 8, 0x67, 8, 0xcc, 9, 0xcd, 9, 0xd2, 9, 0xd3, 9, - 0xd4, 9, 0xd5, 9, 0xd6, 9, 0xd7, 9, 0xd8, 9, 0xd9, 9, 0xda, 9, - 0xdb, 9, 0x98, 9, 0x99, 9, 0x9a, 9, 0x18, 6, 0x9b, 9, 0x08, 11, - 0x0c, 11, 0x0d, 11, 0x12, 12, 0x13, 12, 0x14, 12, 0x15, 12, 0x16, 12, - 0x17, 12, 0x1c, 12, 0x1d, 12, 0x1e, 12, 0x1f, 12, -}; - -void AddBitStream(uint8_t* dest_buf, int& dest_bitpos, int data, int bitlen) { - for (int i = bitlen - 1; i >= 0; i--) { - if (data & (1 << i)) { - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - } - dest_bitpos++; - } -} - -void FaxEncodeRun(uint8_t* dest_buf, int& dest_bitpos, int run, bool bWhite) { - while (run >= 2560) { - AddBitStream(dest_buf, dest_bitpos, 0x1f, 12); - run -= 2560; - } - if (run >= 64) { - int markup = run - run % 64; - const uint8_t* p = bWhite ? WhiteRunMarkup : BlackRunMarkup; - p += (markup / 64 - 1) * 2; - AddBitStream(dest_buf, dest_bitpos, *p, p[1]); - } - run %= 64; - const uint8_t* p = bWhite ? WhiteRunTerminator : BlackRunTerminator; - p += run * 2; - AddBitStream(dest_buf, dest_bitpos, *p, p[1]); -} - -void FaxEncode2DLine(uint8_t* dest_buf, - int& dest_bitpos, - const uint8_t* src_buf, - const uint8_t* ref_buf, - int cols) { - int a0 = -1; - bool a0color = true; - while (1) { - int a1 = FindBit(src_buf, cols, a0 + 1, !a0color); - int b1, b2; - FaxG4FindB1B2(ref_buf, cols, a0, a0color, b1, b2); - if (b2 < a1) { - dest_bitpos += 3; - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - dest_bitpos++; - a0 = b2; - } else if (a1 - b1 <= 3 && b1 - a1 <= 3) { - int delta = a1 - b1; - switch (delta) { - case 0: - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - break; - case 1: - case 2: - case 3: - dest_bitpos += delta == 1 ? 1 : delta + 2; - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - dest_bitpos++; - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - break; - case -1: - case -2: - case -3: - dest_bitpos += delta == -1 ? 1 : -delta + 2; - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - dest_bitpos++; - break; - } - dest_bitpos++; - a0 = a1; - a0color = !a0color; - } else { - int a2 = FindBit(src_buf, cols, a1 + 1, a0color); - dest_bitpos++; - dest_bitpos++; - dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8); - dest_bitpos++; - if (a0 < 0) { - a0 = 0; - } - FaxEncodeRun(dest_buf, dest_bitpos, a1 - a0, a0color); - FaxEncodeRun(dest_buf, dest_bitpos, a2 - a1, !a0color); - a0 = a2; - } - if (a0 >= cols) { - return; - } - } -} - } // namespace class CCodec_FaxDecoder : public CCodec_ScanlineDecoder { public: - CCodec_FaxDecoder(); + CCodec_FaxDecoder(const uint8_t* src_buf, + uint32_t src_size, + int width, + int height, + int K, + FX_BOOL EndOfLine, + FX_BOOL EncodedByteAlign, + FX_BOOL BlackIs1, + int Columns, + int Rows); ~CCodec_FaxDecoder() override; - FX_BOOL Create(const uint8_t* src_buf, - uint32_t src_size, - int width, - int height, - int K, - FX_BOOL EndOfLine, - FX_BOOL EncodedByteAlign, - FX_BOOL BlackIs1, - int Columns, - int Rows); // CCodec_ScanlineDecoder FX_BOOL v_Rewind() override; @@ -620,36 +491,26 @@ class CCodec_FaxDecoder : public CCodec_ScanlineDecoder { uint8_t* m_pRefBuf; }; -CCodec_FaxDecoder::CCodec_FaxDecoder() { - m_pScanlineBuf = NULL; - m_pRefBuf = NULL; -} -CCodec_FaxDecoder::~CCodec_FaxDecoder() { - FX_Free(m_pScanlineBuf); - FX_Free(m_pRefBuf); -} -FX_BOOL CCodec_FaxDecoder::Create(const uint8_t* src_buf, - uint32_t src_size, - int width, - int height, - int K, - FX_BOOL EndOfLine, - FX_BOOL EncodedByteAlign, - FX_BOOL BlackIs1, - int Columns, - int Rows) { +CCodec_FaxDecoder::CCodec_FaxDecoder(const uint8_t* src_buf, + uint32_t src_size, + int width, + int height, + int K, + FX_BOOL EndOfLine, + FX_BOOL EncodedByteAlign, + FX_BOOL BlackIs1, + int Columns, + int Rows) { m_Encoding = K; m_bEndOfLine = EndOfLine; m_bByteAlign = EncodedByteAlign; m_bBlack = BlackIs1; m_OrigWidth = Columns; m_OrigHeight = Rows; - if (m_OrigWidth == 0) { + if (m_OrigWidth == 0) m_OrigWidth = width; - } - if (m_OrigHeight == 0) { + if (m_OrigHeight == 0) m_OrigHeight = height; - } // Should not overflow. Checked by FPDFAPI_CreateFaxDecoder. m_Pitch = (static_cast<uint32_t>(m_OrigWidth) + 31) / 32 * 4; m_OutputWidth = m_OrigWidth; @@ -660,8 +521,13 @@ FX_BOOL CCodec_FaxDecoder::Create(const uint8_t* src_buf, m_SrcSize = src_size; m_nComps = 1; m_bpc = 1; - return TRUE; } + +CCodec_FaxDecoder::~CCodec_FaxDecoder() { + FX_Free(m_pScanlineBuf); + FX_Free(m_pRefBuf); +} + FX_BOOL CCodec_FaxDecoder::v_Rewind() { FXSYS_memset(m_pRefBuf, 0xff, m_Pitch); bitpos = 0; @@ -747,64 +613,6 @@ void FaxG4Decode(const uint8_t* src_buf, *pbitpos = bitpos; } -class CCodec_FaxEncoder { - public: - CCodec_FaxEncoder(const uint8_t* src_buf, int width, int height, int pitch); - ~CCodec_FaxEncoder(); - void Encode(uint8_t*& dest_buf, uint32_t& dest_size); - void Encode2DLine(const uint8_t* scan_line); - CFX_BinaryBuf m_DestBuf; - uint8_t* m_pRefLine; - uint8_t* m_pLineBuf; - int m_Cols, m_Rows, m_Pitch; - const uint8_t* m_pSrcBuf; -}; -CCodec_FaxEncoder::CCodec_FaxEncoder(const uint8_t* src_buf, - int width, - int height, - int pitch) { - m_pSrcBuf = src_buf; - m_Cols = width; - m_Rows = height; - m_Pitch = pitch; - m_pRefLine = FX_Alloc(uint8_t, m_Pitch); - FXSYS_memset(m_pRefLine, 0xff, m_Pitch); - m_pLineBuf = FX_Alloc2D(uint8_t, m_Pitch, 8); - m_DestBuf.EstimateSize(0, 10240); -} -CCodec_FaxEncoder::~CCodec_FaxEncoder() { - FX_Free(m_pRefLine); - FX_Free(m_pLineBuf); -} -void CCodec_FaxEncoder::Encode(uint8_t*& dest_buf, uint32_t& dest_size) { - int dest_bitpos = 0; - uint8_t last_byte = 0; - for (int i = 0; i < m_Rows; i++) { - const uint8_t* scan_line = m_pSrcBuf + i * m_Pitch; - FXSYS_memset(m_pLineBuf, 0, m_Pitch * 8); - m_pLineBuf[0] = last_byte; - FaxEncode2DLine(m_pLineBuf, dest_bitpos, scan_line, m_pRefLine, m_Cols); - m_DestBuf.AppendBlock(m_pLineBuf, dest_bitpos / 8); - last_byte = m_pLineBuf[dest_bitpos / 8]; - dest_bitpos %= 8; - FXSYS_memcpy(m_pRefLine, scan_line, m_Pitch); - } - if (dest_bitpos) { - m_DestBuf.AppendByte(last_byte); - } - dest_size = m_DestBuf.GetSize(); - dest_buf = m_DestBuf.DetachBuffer(); -} -FX_BOOL CCodec_FaxModule::Encode(const uint8_t* src_buf, - int width, - int height, - int pitch, - uint8_t*& dest_buf, - uint32_t& dest_size) { - CCodec_FaxEncoder encoder(src_buf, width, height, pitch); - encoder.Encode(dest_buf, dest_size); - return TRUE; -} CCodec_ScanlineDecoder* CCodec_FaxModule::CreateDecoder( const uint8_t* src_buf, uint32_t src_size, @@ -816,8 +624,6 @@ CCodec_ScanlineDecoder* CCodec_FaxModule::CreateDecoder( FX_BOOL BlackIs1, int Columns, int Rows) { - CCodec_FaxDecoder* pDecoder = new CCodec_FaxDecoder; - pDecoder->Create(src_buf, src_size, width, height, K, EndOfLine, - EncodedByteAlign, BlackIs1, Columns, Rows); - return pDecoder; + return new CCodec_FaxDecoder(src_buf, src_size, width, height, K, EndOfLine, + EncodedByteAlign, BlackIs1, Columns, Rows); } diff --git a/core/fxcodec/codec/fx_codec_flate.cpp b/core/fxcodec/codec/fx_codec_flate.cpp index c23de277fb..cd24b6624c 100644 --- a/core/fxcodec/codec/fx_codec_flate.cpp +++ b/core/fxcodec/codec/fx_codec_flate.cpp @@ -29,30 +29,34 @@ static int FPDFAPI_FlateGetTotalOut(void* context) { static int FPDFAPI_FlateGetTotalIn(void* context) { return ((z_stream*)context)->total_in; } -static void FPDFAPI_FlateCompress(unsigned char* dest_buf, + +static bool FPDFAPI_FlateCompress(unsigned char* dest_buf, unsigned long* dest_size, const unsigned char* src_buf, unsigned long src_size) { - compress(dest_buf, dest_size, src_buf, src_size); + return compress(dest_buf, dest_size, src_buf, src_size) == Z_OK; } + void* FPDFAPI_FlateInit(void* (*alloc_func)(void*, unsigned int, unsigned int), void (*free_func)(void*, void*)) { z_stream* p = (z_stream*)alloc_func(0, 1, sizeof(z_stream)); - if (!p) { - return NULL; - } + if (!p) + return nullptr; + FXSYS_memset(p, 0, sizeof(z_stream)); p->zalloc = alloc_func; p->zfree = free_func; inflateInit(p); return p; } + void FPDFAPI_FlateInput(void* context, const unsigned char* src_buf, unsigned int src_size) { ((z_stream*)context)->next_in = (unsigned char*)src_buf; ((z_stream*)context)->avail_in = src_size; } + int FPDFAPI_FlateOutput(void* context, unsigned char* dest_buf, unsigned int dest_size) { @@ -67,16 +71,20 @@ int FPDFAPI_FlateOutput(void* context, } return ret; } + int FPDFAPI_FlateGetAvailIn(void* context) { return ((z_stream*)context)->avail_in; } + int FPDFAPI_FlateGetAvailOut(void* context) { return ((z_stream*)context)->avail_out; } + void FPDFAPI_FlateEnd(void* context) { inflateEnd((z_stream*)context); ((z_stream*)context)->zfree(0, context); } + } // extern "C" namespace { @@ -104,6 +112,7 @@ class CLZWDecoder { uint32_t m_StackLen; int m_CodeLen; }; + void CLZWDecoder::AddCode(uint32_t prefix_code, uint8_t append_char) { if (m_nCodes + m_Early == 4094) { return; @@ -229,108 +238,42 @@ int CLZWDecoder::Decode(uint8_t* dest_buf, return 0; } -uint8_t PaethPredictor(int a, int b, int c) { +uint8_t PathPredictor(int a, int b, int c) { int p = a + b - c; int pa = FXSYS_abs(p - a); int pb = FXSYS_abs(p - b); int pc = FXSYS_abs(p - c); - if (pa <= pb && pa <= pc) { + if (pa <= pb && pa <= pc) return (uint8_t)a; - } - if (pb <= pc) { + if (pb <= pc) return (uint8_t)b; - } return (uint8_t)c; } -FX_BOOL PNG_PredictorEncode(uint8_t*& data_buf, - uint32_t& data_size, - int predictor, - int Colors, - int BitsPerComponent, - int Columns) { - const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; - const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; - if (row_size <= 0) - return FALSE; - const int row_count = (data_size + row_size - 1) / row_size; - const int last_row_size = data_size % row_size; +void PNG_PredictorEncode(uint8_t** data_buf, uint32_t* data_size) { + const int row_size = 7; + const int row_count = (*data_size + row_size - 1) / row_size; + const int last_row_size = *data_size % row_size; uint8_t* dest_buf = FX_Alloc2D(uint8_t, row_size + 1, row_count); int byte_cnt = 0; - uint8_t* pSrcData = data_buf; + uint8_t* pSrcData = *data_buf; uint8_t* pDestData = dest_buf; for (int row = 0; row < row_count; row++) { - if (predictor == 10) { - pDestData[0] = 0; - int move_size = row_size; - if (move_size * (row + 1) > (int)data_size) { - move_size = data_size - (move_size * row); - } - FXSYS_memmove(pDestData + 1, pSrcData, move_size); - pDestData += (move_size + 1); - pSrcData += move_size; - byte_cnt += move_size; - continue; - } - for (int byte = 0; byte < row_size && byte_cnt < (int)data_size; byte++) { - switch (predictor) { - case 11: { - pDestData[0] = 1; - uint8_t left = 0; - if (byte >= BytesPerPixel) { - left = pSrcData[byte - BytesPerPixel]; - } - pDestData[byte + 1] = pSrcData[byte] - left; - } break; - case 12: { - pDestData[0] = 2; - uint8_t up = 0; - if (row) { - up = pSrcData[byte - row_size]; - } - pDestData[byte + 1] = pSrcData[byte] - up; - } break; - case 13: { - pDestData[0] = 3; - uint8_t left = 0; - if (byte >= BytesPerPixel) { - left = pSrcData[byte - BytesPerPixel]; - } - uint8_t up = 0; - if (row) { - up = pSrcData[byte - row_size]; - } - pDestData[byte + 1] = pSrcData[byte] - (left + up) / 2; - } break; - case 14: { - pDestData[0] = 4; - uint8_t left = 0; - if (byte >= BytesPerPixel) { - left = pSrcData[byte - BytesPerPixel]; - } - uint8_t up = 0; - if (row) { - up = pSrcData[byte - row_size]; - } - uint8_t upper_left = 0; - if (byte >= BytesPerPixel && row) { - upper_left = pSrcData[byte - row_size - BytesPerPixel]; - } - pDestData[byte + 1] = - pSrcData[byte] - PaethPredictor(left, up, upper_left); - } break; - default: { pDestData[byte + 1] = pSrcData[byte]; } break; - } - byte_cnt++; + for (int byte = 0; byte < row_size && byte_cnt < (int)*data_size; byte++) { + pDestData[0] = 2; + uint8_t up = 0; + if (row) + up = pSrcData[byte - row_size]; + pDestData[byte + 1] = pSrcData[byte] - up; + ++byte_cnt; } pDestData += (row_size + 1); pSrcData += row_size; } - FX_Free(data_buf); - data_buf = dest_buf; - data_size = (row_size + 1) * row_count - - (last_row_size > 0 ? (row_size - last_row_size) : 0); - return TRUE; + FX_Free(*data_buf); + *data_buf = dest_buf; + *data_size = (row_size + 1) * row_count - + (last_row_size > 0 ? (row_size - last_row_size) : 0); } void PNG_PredictLine(uint8_t* pDestData, @@ -390,7 +333,7 @@ void PNG_PredictLine(uint8_t* pDestData, if (byte >= BytesPerPixel && pLastLine) { upper_left = pLastLine[byte - BytesPerPixel]; } - pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_left); + pDestData[byte] = raw_byte + PathPredictor(left, up, upper_left); break; } default: @@ -475,7 +418,7 @@ FX_BOOL PNG_Predictor(uint8_t*& data_buf, if (byte >= BytesPerPixel && row) { upper_left = pDestData[byte - row_size - BytesPerPixel]; } - pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_left); + pDestData[byte] = raw_byte + PathPredictor(left, up, upper_left); break; } default: @@ -494,73 +437,6 @@ FX_BOOL PNG_Predictor(uint8_t*& data_buf, return TRUE; } -void TIFF_PredictorEncodeLine(uint8_t* dest_buf, - int row_size, - int BitsPerComponent, - int Colors, - int Columns) { - int BytesPerPixel = BitsPerComponent * Colors / 8; - if (BitsPerComponent < 8) { - uint8_t mask = 0x01; - if (BitsPerComponent == 2) { - mask = 0x03; - } else if (BitsPerComponent == 4) { - mask = 0x0F; - } - int row_bits = Colors * BitsPerComponent * Columns; - for (int i = row_bits - BitsPerComponent; i >= BitsPerComponent; - i -= BitsPerComponent) { - int col = i % 8; - int index = i / 8; - int col_pre = - (col == 0) ? (8 - BitsPerComponent) : (col - BitsPerComponent); - int index_pre = (col == 0) ? (index - 1) : index; - uint8_t cur = (dest_buf[index] >> (8 - col - BitsPerComponent)) & mask; - uint8_t left = - (dest_buf[index_pre] >> (8 - col_pre - BitsPerComponent)) & mask; - cur -= left; - cur &= mask; - cur <<= (8 - col - BitsPerComponent); - dest_buf[index] &= ~(mask << ((8 - col - BitsPerComponent))); - dest_buf[index] |= cur; - } - } else if (BitsPerComponent == 8) { - for (int i = row_size - 1; i >= BytesPerPixel; i--) { - dest_buf[i] -= dest_buf[i - BytesPerPixel]; - } - } else { - for (int i = row_size - BytesPerPixel; i >= BytesPerPixel; - i -= BytesPerPixel) { - uint16_t pixel = (dest_buf[i] << 8) | dest_buf[i + 1]; - pixel -= - (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerPixel + 1]; - dest_buf[i] = pixel >> 8; - dest_buf[i + 1] = (uint8_t)pixel; - } - } -} - -FX_BOOL TIFF_PredictorEncode(uint8_t*& data_buf, - uint32_t& data_size, - int Colors, - int BitsPerComponent, - int Columns) { - int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; - if (row_size == 0) - return FALSE; - const int row_count = (data_size + row_size - 1) / row_size; - const int last_row_size = data_size % row_size; - for (int row = 0; row < row_count; row++) { - uint8_t* scan_line = data_buf + row * row_size; - if ((row + 1) * row_size > (int)data_size) { - row_size = last_row_size; - } - TIFF_PredictorEncodeLine(scan_line, row_size, BitsPerComponent, Colors, - Columns); - } - return TRUE; -} - void TIFF_PredictLine(uint8_t* dest_buf, uint32_t row_size, int BitsPerComponent, @@ -965,41 +841,29 @@ uint32_t CCodec_FlateModule::FlateOrLZWDecode(FX_BOOL bLZW, } return ret ? offset : FX_INVALID_OFFSET; } -FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, + +bool CCodec_FlateModule::Encode(const uint8_t* src_buf, + uint32_t src_size, + uint8_t** dest_buf, + uint32_t* dest_size) { + *dest_size = src_size + src_size / 1000 + 12; + *dest_buf = FX_Alloc(uint8_t, *dest_size); + unsigned long temp_size = *dest_size; + if (!FPDFAPI_FlateCompress(*dest_buf, &temp_size, src_buf, src_size)) + return false; + + *dest_size = (uint32_t)temp_size; + return true; +} + +bool CCodec_FlateModule::PngEncode(const uint8_t* src_buf, uint32_t src_size, - int predictor, - int Colors, - int BitsPerComponent, - int Columns, - uint8_t*& dest_buf, - uint32_t& dest_size) { - if (predictor != 2 && predictor < 10) { - return Encode(src_buf, src_size, dest_buf, dest_size); - } - uint8_t* pSrcBuf = NULL; - pSrcBuf = FX_Alloc(uint8_t, src_size); + uint8_t** dest_buf, + uint32_t* dest_size) { + uint8_t* pSrcBuf = FX_Alloc(uint8_t, src_size); FXSYS_memcpy(pSrcBuf, src_buf, src_size); - FX_BOOL ret = TRUE; - if (predictor == 2) { - ret = TIFF_PredictorEncode(pSrcBuf, src_size, Colors, BitsPerComponent, - Columns); - } else if (predictor >= 10) { - ret = PNG_PredictorEncode(pSrcBuf, src_size, predictor, Colors, - BitsPerComponent, Columns); - } - if (ret) - ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); + PNG_PredictorEncode(&pSrcBuf, &src_size); + bool ret = Encode(pSrcBuf, src_size, dest_buf, dest_size); FX_Free(pSrcBuf); return ret; } -FX_BOOL CCodec_FlateModule::Encode(const uint8_t* src_buf, - uint32_t src_size, - uint8_t*& dest_buf, - uint32_t& dest_size) { - dest_size = src_size + src_size / 1000 + 12; - dest_buf = FX_Alloc(uint8_t, dest_size); - unsigned long temp_size = dest_size; - FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); - dest_size = (uint32_t)temp_size; - return TRUE; -} diff --git a/core/fxcodec/codec/fx_codec_jpeg.cpp b/core/fxcodec/codec/fx_codec_jpeg.cpp index c41dde943f..cbfea59329 100644 --- a/core/fxcodec/codec/fx_codec_jpeg.cpp +++ b/core/fxcodec/codec/fx_codec_jpeg.cpp @@ -23,15 +23,15 @@ extern "C" { } extern "C" { -static void _JpegScanSOI(const uint8_t*& src_buf, uint32_t& src_size) { - if (src_size == 0) { +static void JpegScanSOI(const uint8_t** src_buf, uint32_t* src_size) { + if (*src_size == 0) return; - } + uint32_t offset = 0; - while (offset < src_size - 1) { - if (src_buf[offset] == 0xff && src_buf[offset + 1] == 0xd8) { - src_buf += offset; - src_size -= offset; + while (offset < *src_size - 1) { + if ((*src_buf)[offset] == 0xff && (*src_buf)[offset + 1] == 0xd8) { + *src_buf += offset; + *src_size -= offset; return; } offset++; @@ -74,187 +74,31 @@ static void _error_do_nothing1(j_common_ptr cinfo, int) {} extern "C" { static void _error_do_nothing2(j_common_ptr cinfo, char*) {} }; -#define JPEG_MARKER_EXIF (JPEG_APP0 + 1) + #define JPEG_MARKER_ICC (JPEG_APP0 + 2) -#define JPEG_MARKER_AUTHORTIME (JPEG_APP0 + 3) #define JPEG_MARKER_MAXSIZE 0xFFFF -#define JPEG_OVERHEAD_LEN 14 -static FX_BOOL _JpegEmbedIccProfile(j_compress_ptr cinfo, - const uint8_t* icc_buf_ptr, - uint32_t icc_length) { - if (!icc_buf_ptr || icc_length == 0) { - return FALSE; - } - uint32_t icc_segment_size = (JPEG_MARKER_MAXSIZE - 2 - JPEG_OVERHEAD_LEN); - uint32_t icc_segment_num = (icc_length / icc_segment_size) + 1; - if (icc_segment_num > 255) { - return FALSE; - } - uint32_t icc_data_length = - JPEG_OVERHEAD_LEN + (icc_segment_num > 1 ? icc_segment_size : icc_length); - uint8_t* icc_data = FX_Alloc(uint8_t, icc_data_length); - FXSYS_memcpy(icc_data, "\x49\x43\x43\x5f\x50\x52\x4f\x46\x49\x4c\x45\x00", - 12); - icc_data[13] = (uint8_t)icc_segment_num; - for (uint8_t i = 0; i < (icc_segment_num - 1); i++) { - icc_data[12] = i + 1; - FXSYS_memcpy(icc_data + JPEG_OVERHEAD_LEN, - icc_buf_ptr + i * icc_segment_size, icc_segment_size); - jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, icc_data_length); - } - icc_data[12] = (uint8_t)icc_segment_num; - uint32_t icc_size = (icc_segment_num - 1) * icc_segment_size; - FXSYS_memcpy(icc_data + JPEG_OVERHEAD_LEN, icc_buf_ptr + icc_size, - icc_length - icc_size); - jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, - JPEG_OVERHEAD_LEN + icc_length - icc_size); - FX_Free(icc_data); - return TRUE; -} -extern "C" { -static void _dest_do_nothing(j_compress_ptr cinfo) {} -}; -extern "C" { -static boolean _dest_empty(j_compress_ptr cinfo) { - return FALSE; -} -}; -#define JPEG_BLOCK_SIZE 1048576 -static void _JpegEncode(const CFX_DIBSource* pSource, - uint8_t*& dest_buf, - FX_STRSIZE& dest_size, - int quality, - const uint8_t* icc_buf, - uint32_t icc_length) { - struct jpeg_error_mgr jerr; - jerr.error_exit = _error_do_nothing; - jerr.emit_message = _error_do_nothing1; - jerr.output_message = _error_do_nothing; - jerr.format_message = _error_do_nothing2; - jerr.reset_error_mgr = _error_do_nothing; - struct jpeg_compress_struct cinfo; - memset(&cinfo, 0, sizeof(cinfo)); - cinfo.err = &jerr; - jpeg_create_compress(&cinfo); - int Bpp = pSource->GetBPP() / 8; - uint32_t nComponents = Bpp >= 3 ? (pSource->IsCmykImage() ? 4 : 3) : 1; - uint32_t pitch = pSource->GetPitch(); - uint32_t width = pdfium::base::checked_cast<uint32_t>(pSource->GetWidth()); - uint32_t height = pdfium::base::checked_cast<uint32_t>(pSource->GetHeight()); - FX_SAFE_UINT32 safe_buf_len = width; - safe_buf_len *= height; - safe_buf_len *= nComponents; - safe_buf_len += 1024; - if (icc_length) { - safe_buf_len += 255 * 18; - safe_buf_len += icc_length; - } - uint32_t dest_buf_length = 0; - if (!safe_buf_len.IsValid()) { - dest_buf = nullptr; - } else { - dest_buf_length = safe_buf_len.ValueOrDie(); - dest_buf = FX_TryAlloc(uint8_t, dest_buf_length); - const int MIN_TRY_BUF_LEN = 1024; - while (!dest_buf && dest_buf_length > MIN_TRY_BUF_LEN) { - dest_buf_length >>= 1; - dest_buf = FX_TryAlloc(uint8_t, dest_buf_length); - } - } - if (!dest_buf) { - FX_OutOfMemoryTerminate(); - } - struct jpeg_destination_mgr dest; - dest.init_destination = _dest_do_nothing; - dest.term_destination = _dest_do_nothing; - dest.empty_output_buffer = _dest_empty; - dest.next_output_byte = dest_buf; - dest.free_in_buffer = dest_buf_length; - cinfo.dest = &dest; - cinfo.image_width = width; - cinfo.image_height = height; - cinfo.input_components = nComponents; - if (nComponents == 1) { - cinfo.in_color_space = JCS_GRAYSCALE; - } else if (nComponents == 3) { - cinfo.in_color_space = JCS_RGB; - } else { - cinfo.in_color_space = JCS_CMYK; - } - uint8_t* line_buf = NULL; - if (nComponents > 1) { - line_buf = FX_Alloc2D(uint8_t, width, nComponents); - } - jpeg_set_defaults(&cinfo); - if (quality != 75) { - jpeg_set_quality(&cinfo, quality, TRUE); - } - jpeg_start_compress(&cinfo, TRUE); - _JpegEmbedIccProfile(&cinfo, icc_buf, icc_length); - JSAMPROW row_pointer[1]; - JDIMENSION row; - while (cinfo.next_scanline < cinfo.image_height) { - const uint8_t* src_scan = pSource->GetScanline(cinfo.next_scanline); - if (nComponents > 1) { - uint8_t* dest_scan = line_buf; - if (nComponents == 3) { - for (uint32_t i = 0; i < width; i++) { - dest_scan[0] = src_scan[2]; - dest_scan[1] = src_scan[1]; - dest_scan[2] = src_scan[0]; - dest_scan += 3; - src_scan += Bpp; - } - } else { - for (uint32_t i = 0; i < pitch; i++) { - *dest_scan++ = ~*src_scan++; - } - } - row_pointer[0] = line_buf; - } else { - row_pointer[0] = (uint8_t*)src_scan; - } - row = cinfo.next_scanline; - jpeg_write_scanlines(&cinfo, row_pointer, 1); - if (cinfo.next_scanline == row) { - dest_buf = - FX_Realloc(uint8_t, dest_buf, dest_buf_length + JPEG_BLOCK_SIZE); - dest.next_output_byte = dest_buf + dest_buf_length - dest.free_in_buffer; - dest_buf_length += JPEG_BLOCK_SIZE; - dest.free_in_buffer += JPEG_BLOCK_SIZE; - } - } - jpeg_finish_compress(&cinfo); - jpeg_destroy_compress(&cinfo); - FX_Free(line_buf); - dest_size = dest_buf_length - (FX_STRSIZE)dest.free_in_buffer; -} #ifdef PDF_ENABLE_XFA -static void _JpegLoadAttribute(struct jpeg_decompress_struct* pInfo, - CFX_DIBAttribute* pAttribute) { - if (pInfo == NULL || pAttribute == NULL) { +static void JpegLoadAttribute(struct jpeg_decompress_struct* pInfo, + CFX_DIBAttribute* pAttribute) { + if (!pInfo || !pAttribute) return; - } - if (pAttribute) { - pAttribute->m_nXDPI = pInfo->X_density; - pAttribute->m_nYDPI = pInfo->Y_density; - pAttribute->m_wDPIUnit = pInfo->density_unit; - } + + pAttribute->m_nXDPI = pInfo->X_density; + pAttribute->m_nYDPI = pInfo->Y_density; + pAttribute->m_wDPIUnit = pInfo->density_unit; } #endif // PDF_ENABLE_XFA -static FX_BOOL _JpegLoadInfo(const uint8_t* src_buf, - uint32_t src_size, - int& width, - int& height, - int& num_components, - int& bits_per_components, - FX_BOOL& color_transform, - uint8_t** icc_buf_ptr, - uint32_t* icc_length) { - _JpegScanSOI(src_buf, src_size); +static bool JpegLoadInfo(const uint8_t* src_buf, + uint32_t src_size, + int* width, + int* height, + int* num_components, + int* bits_per_components, + bool* color_transform) { + JpegScanSOI(&src_buf, &src_size); struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; jerr.error_exit = _error_fatal; @@ -266,9 +110,9 @@ static FX_BOOL _JpegLoadInfo(const uint8_t* src_buf, cinfo.err = &jerr; jmp_buf mark; cinfo.client_data = &mark; - if (setjmp(mark) == -1) { - return FALSE; - } + if (setjmp(mark) == -1) + return false; + jpeg_create_decompress(&cinfo); struct jpeg_source_mgr src; src.init_source = _src_do_nothing; @@ -281,30 +125,21 @@ static FX_BOOL _JpegLoadInfo(const uint8_t* src_buf, cinfo.src = &src; if (setjmp(mark) == -1) { jpeg_destroy_decompress(&cinfo); - return FALSE; - } - if (icc_buf_ptr && icc_length) { - jpeg_save_markers(&cinfo, JPEG_MARKER_ICC, JPEG_MARKER_MAXSIZE); + return false; } int ret = jpeg_read_header(&cinfo, TRUE); if (ret != JPEG_HEADER_OK) { jpeg_destroy_decompress(&cinfo); - return FALSE; + return false; } - width = cinfo.image_width; - height = cinfo.image_height; - num_components = cinfo.num_components; - color_transform = + *width = cinfo.image_width; + *height = cinfo.image_height; + *num_components = cinfo.num_components; + *color_transform = cinfo.jpeg_color_space == JCS_YCbCr || cinfo.jpeg_color_space == JCS_YCCK; - bits_per_components = cinfo.data_precision; - if (icc_buf_ptr) { - *icc_buf_ptr = NULL; - } - if (icc_length) { - *icc_length = 0; - } + *bits_per_components = cinfo.data_precision; jpeg_destroy_decompress(&cinfo); - return TRUE; + return true; } class CCodec_JpegDecoder : public CCodec_ScanlineDecoder { @@ -351,18 +186,19 @@ CCodec_JpegDecoder::CCodec_JpegDecoder() { FXSYS_memset(&src, 0, sizeof(src)); m_nDefaultScaleDenom = 1; } + CCodec_JpegDecoder::~CCodec_JpegDecoder() { FX_Free(m_pScanlineBuf); - if (m_bInited) { + if (m_bInited) jpeg_destroy_decompress(&cinfo); - } } + FX_BOOL CCodec_JpegDecoder::InitDecode() { cinfo.err = &jerr; cinfo.client_data = &m_JmpBuf; - if (setjmp(m_JmpBuf) == -1) { + if (setjmp(m_JmpBuf) == -1) return FALSE; - } + jpeg_create_decompress(&cinfo); m_bInited = TRUE; cinfo.src = &src; @@ -376,15 +212,15 @@ FX_BOOL CCodec_JpegDecoder::InitDecode() { cinfo.image_width = m_OrigWidth; cinfo.image_height = m_OrigHeight; int ret = jpeg_read_header(&cinfo, TRUE); - if (ret != JPEG_HEADER_OK) { + if (ret != JPEG_HEADER_OK) return FALSE; - } - if (cinfo.saw_Adobe_marker) { + + if (cinfo.saw_Adobe_marker) m_bJpegTransform = TRUE; - } - if (cinfo.num_components == 3 && !m_bJpegTransform) { + + if (cinfo.num_components == 3 && !m_bJpegTransform) cinfo.out_color_space = cinfo.jpeg_color_space; - } + m_OrigWidth = cinfo.image_width; m_OrigHeight = cinfo.image_height; m_OutputWidth = m_OrigWidth; @@ -392,13 +228,14 @@ FX_BOOL CCodec_JpegDecoder::InitDecode() { m_nDefaultScaleDenom = cinfo.scale_denom; return TRUE; } + FX_BOOL CCodec_JpegDecoder::Create(const uint8_t* src_buf, uint32_t src_size, int width, int height, int nComps, FX_BOOL ColorTransform) { - _JpegScanSOI(src_buf, src_size); + JpegScanSOI(&src_buf, &src_size); m_SrcBuf = src_buf; m_SrcSize = src_size; jerr.error_exit = _error_fatal; @@ -419,15 +256,15 @@ FX_BOOL CCodec_JpegDecoder::Create(const uint8_t* src_buf, } m_OutputWidth = m_OrigWidth = width; m_OutputHeight = m_OrigHeight = height; - if (!InitDecode()) { + if (!InitDecode()) return FALSE; - } - if (cinfo.num_components < nComps) { + + if (cinfo.num_components < nComps) return FALSE; - } - if ((int)cinfo.image_width < width) { + + if ((int)cinfo.image_width < width) return FALSE; - } + m_Pitch = (static_cast<uint32_t>(cinfo.image_width) * cinfo.num_components + 3) / 4 * 4; @@ -462,19 +299,19 @@ FX_BOOL CCodec_JpegDecoder::v_Rewind() { m_bStarted = TRUE; return TRUE; } + uint8_t* CCodec_JpegDecoder::v_GetNextLine() { if (setjmp(m_JmpBuf) == -1) return nullptr; int nlines = jpeg_read_scanlines(&cinfo, &m_pScanlineBuf, 1); - if (nlines < 1) { - return nullptr; - } - return m_pScanlineBuf; + return nlines > 0 ? m_pScanlineBuf : nullptr; } + uint32_t CCodec_JpegDecoder::GetSrcOffset() { return (uint32_t)(m_SrcSize - src.bytes_in_buffer); } + CCodec_ScanlineDecoder* CCodec_JpegModule::CreateDecoder( const uint8_t* src_buf, uint32_t src_size, @@ -482,42 +319,28 @@ CCodec_ScanlineDecoder* CCodec_JpegModule::CreateDecoder( int height, int nComps, FX_BOOL ColorTransform) { - if (!src_buf || src_size == 0) { - return NULL; - } - CCodec_JpegDecoder* pDecoder = new CCodec_JpegDecoder; + if (!src_buf || src_size == 0) + return nullptr; + + std::unique_ptr<CCodec_JpegDecoder> pDecoder(new CCodec_JpegDecoder); if (!pDecoder->Create(src_buf, src_size, width, height, nComps, ColorTransform)) { - delete pDecoder; - return NULL; + return nullptr; } - return pDecoder; -} -FX_BOOL CCodec_JpegModule::LoadInfo(const uint8_t* src_buf, - uint32_t src_size, - int& width, - int& height, - int& num_components, - int& bits_per_components, - FX_BOOL& color_transform, - uint8_t** icc_buf_ptr, - uint32_t* icc_length) { - return _JpegLoadInfo(src_buf, src_size, width, height, num_components, - bits_per_components, color_transform, icc_buf_ptr, - icc_length); + return pDecoder.release(); } -FX_BOOL CCodec_JpegModule::Encode(const CFX_DIBSource* pSource, - uint8_t*& dest_buf, - FX_STRSIZE& dest_size, - int quality, - const uint8_t* icc_buf, - uint32_t icc_length) { - if (pSource->GetBPP() < 8 || pSource->GetPalette()) - return FALSE; - _JpegEncode(pSource, dest_buf, dest_size, quality, icc_buf, icc_length); - return TRUE; +bool CCodec_JpegModule::LoadInfo(const uint8_t* src_buf, + uint32_t src_size, + int* width, + int* height, + int* num_components, + int* bits_per_components, + bool* color_transform) { + return JpegLoadInfo(src_buf, src_size, width, height, num_components, + bits_per_components, color_transform); } + struct FXJPEG_Context { jmp_buf m_JumpMark; jpeg_decompress_struct m_Info; @@ -622,7 +445,7 @@ int CCodec_JpegModule::ReadHeader(FXJPEG_Context* ctx, *height = ctx->m_Info.image_height; *nComps = ctx->m_Info.num_components; #ifdef PDF_ENABLE_XFA - _JpegLoadAttribute(&ctx->m_Info, pAttribute); + JpegLoadAttribute(&ctx->m_Info, pAttribute); #endif return 0; } |