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_faxmodule.h | 9 | ||||
-rw-r--r-- | core/fxcodec/codec/ccodec_jpegmodule.h | 6 | ||||
-rw-r--r-- | core/fxcodec/codec/fx_codec.cpp | 14 | ||||
-rw-r--r-- | core/fxcodec/codec/fx_codec_fax.cpp | 212 | ||||
-rw-r--r-- | core/fxcodec/codec/fx_codec_jpeg.cpp | 136 |
6 files changed, 362 insertions, 23 deletions
diff --git a/core/fxcodec/codec/ccodec_basicmodule.h b/core/fxcodec/codec/ccodec_basicmodule.h index 425b5d7229..015f41a4a4 100644 --- a/core/fxcodec/codec/ccodec_basicmodule.h +++ b/core/fxcodec/codec/ccodec_basicmodule.h @@ -22,6 +22,14 @@ class CCodec_BasicModule { int height, int nComps, int bpc); + bool RunLengthEncode(const uint8_t* src_buf, + uint32_t src_size, + uint8_t** dest_buf, + uint32_t* dest_size); + bool A85Encode(const uint8_t* src_buf, + uint32_t src_size, + uint8_t** dest_buf, + uint32_t* dest_size); }; #endif // CORE_FXCODEC_CODEC_CCODEC_BASICMODULE_H_ diff --git a/core/fxcodec/codec/ccodec_faxmodule.h b/core/fxcodec/codec/ccodec_faxmodule.h index ce9e97bbe9..92e45e6719 100644 --- a/core/fxcodec/codec/ccodec_faxmodule.h +++ b/core/fxcodec/codec/ccodec_faxmodule.h @@ -9,6 +9,7 @@ #include <memory> +#include "core/fxcrt/fx_memory.h" #include "core/fxcrt/fx_system.h" class CCodec_ScanlineDecoder; @@ -25,6 +26,14 @@ class CCodec_FaxModule { bool BlackIs1, int Columns, int Rows); +#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_DESKTOP_ + static void FaxEncode(const uint8_t* src_buf, + int width, + int height, + int pitch, + std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf, + uint32_t* dest_size); +#endif }; #endif // CORE_FXCODEC_CODEC_CCODEC_FAXMODULE_H_ diff --git a/core/fxcodec/codec/ccodec_jpegmodule.h b/core/fxcodec/codec/ccodec_jpegmodule.h index b2ae731bb4..bad6fa6a94 100644 --- a/core/fxcodec/codec/ccodec_jpegmodule.h +++ b/core/fxcodec/codec/ccodec_jpegmodule.h @@ -58,6 +58,12 @@ class CCodec_JpegModule { bool StartScanline(FXJPEG_Context* pContext, int down_scale); bool ReadScanline(FXJPEG_Context* pContext, uint8_t* dest_buf); uint32_t GetAvailInput(FXJPEG_Context* pContext, uint8_t** avail_buf_ptr); + +#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_DESKTOP_ + static bool JpegEncode(const CFX_DIBSource* pSource, + uint8_t** dest_buf, + FX_STRSIZE* dest_size); +#endif }; #endif // CORE_FXCODEC_CODEC_CCODEC_JPEGMODULE_H_ diff --git a/core/fxcodec/codec/fx_codec.cpp b/core/fxcodec/codec/fx_codec.cpp index aa2ecef345..6b6c723388 100644 --- a/core/fxcodec/codec/fx_codec.cpp +++ b/core/fxcodec/codec/fx_codec.cpp @@ -97,6 +97,20 @@ uint8_t* CCodec_ScanlineDecoder::ReadNextLine() { return v_GetNextLine(); } +bool CCodec_BasicModule::RunLengthEncode(const uint8_t* src_buf, + uint32_t src_size, + uint8_t** dest_buf, + uint32_t* dest_size) { + return false; +} + +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 62ad38e19f..8a9c3efbc7 100644 --- a/core/fxcodec/codec/fx_codec_fax.cpp +++ b/core/fxcodec/codec/fx_codec_fax.cpp @@ -10,6 +10,7 @@ #include "core/fxcodec/codec/codec_int.h" #include "core/fxcodec/fx_codec.h" +#include "core/fxcrt/fx_memory.h" #include "third_party/base/ptr_util.h" namespace { @@ -44,10 +45,7 @@ const uint8_t ZeroLeadPos[256] = { // Limit of image dimension, an arbitrary large number. const int kMaxImageDimension = 0x01FFFF; -int FindBit(const std::vector<uint8_t>& data_buf, - int max_pos, - int start_pos, - int bit) { +int FindBit(const uint8_t* data_buf, int max_pos, int start_pos, int bit) { ASSERT(start_pos >= 0); if (start_pos >= max_pos) return max_pos; @@ -88,20 +86,20 @@ void FaxG4FindB1B2(const std::vector<uint8_t>& ref_buf, int* b2) { uint8_t first_bit = (a0 < 0) ? 1 : ((ref_buf[a0 / 8] & (1 << (7 - a0 % 8))) != 0); - *b1 = FindBit(ref_buf, columns, a0 + 1, !first_bit); + *b1 = FindBit(ref_buf.data(), columns, a0 + 1, !first_bit); if (*b1 >= columns) { *b1 = *b2 = columns; return; } if (first_bit == !a0color) { - *b1 = FindBit(ref_buf, columns, *b1 + 1, first_bit); + *b1 = FindBit(ref_buf.data(), columns, *b1 + 1, first_bit); first_bit = !first_bit; } if (*b1 >= columns) { *b1 = *b2 = columns; return; } - *b2 = FindBit(ref_buf, columns, *b1 + 1, first_bit); + *b2 = FindBit(ref_buf.data(), columns, *b1 + 1, first_bit); } void FaxFillBits(uint8_t* dest_buf, int columns, int startpos, int endpos) { @@ -609,3 +607,203 @@ std::unique_ptr<CCodec_ScanlineDecoder> CCodec_FaxModule::CreateDecoder( src_buf, src_size, actual_width, actual_height, pitch, K, EndOfLine, EncodedByteAlign, BlackIs1); } + +#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_DESKTOP_ +namespace { +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 std::vector<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; + int 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; + } + } +} + +class CCodec_FaxEncoder { + public: + CCodec_FaxEncoder(const uint8_t* src_buf, int width, int height, int pitch); + ~CCodec_FaxEncoder(); + void Encode(std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf, + uint32_t* dest_size); + + private: + CFX_BinaryBuf m_DestBuf; + std::vector<uint8_t> m_RefLine; + uint8_t* m_pLineBuf; + const int m_Cols; + const int m_Rows; + const int m_Pitch; + const uint8_t* m_pSrcBuf; +}; + +CCodec_FaxEncoder::CCodec_FaxEncoder(const uint8_t* src_buf, + int width, + int height, + int pitch) + : m_Cols(width), m_Rows(height), m_Pitch(pitch), m_pSrcBuf(src_buf) { + m_RefLine.resize(m_Pitch); + FXSYS_memset(m_RefLine.data(), 0xff, m_Pitch); + m_pLineBuf = FX_Alloc2D(uint8_t, m_Pitch, 8); + m_DestBuf.EstimateSize(0, 10240); +} + +CCodec_FaxEncoder::~CCodec_FaxEncoder() { + FX_Free(m_pLineBuf); +} + +void CCodec_FaxEncoder::Encode( + std::unique_ptr<uint8_t, FxFreeDeleter>* 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_RefLine, m_Cols); + m_DestBuf.AppendBlock(m_pLineBuf, dest_bitpos / 8); + last_byte = m_pLineBuf[dest_bitpos / 8]; + dest_bitpos %= 8; + FXSYS_memcpy(m_RefLine.data(), scan_line, m_Pitch); + } + if (dest_bitpos) { + m_DestBuf.AppendByte(last_byte); + } + *dest_size = m_DestBuf.GetSize(); + *dest_buf = m_DestBuf.DetachBuffer(); +} + +} // namespace + +void CCodec_FaxModule::FaxEncode( + const uint8_t* src_buf, + int width, + int height, + int pitch, + std::unique_ptr<uint8_t, FxFreeDeleter>* dest_buf, + uint32_t* dest_size) { + CCodec_FaxEncoder encoder(src_buf, width, height, pitch); + encoder.Encode(dest_buf, dest_size); +} + +#endif diff --git a/core/fxcodec/codec/fx_codec_jpeg.cpp b/core/fxcodec/codec/fx_codec_jpeg.cpp index fdfdd4faeb..c797605575 100644 --- a/core/fxcodec/codec/fx_codec_jpeg.cpp +++ b/core/fxcodec/codec/fx_codec_jpeg.cpp @@ -27,6 +27,7 @@ extern "C" { } extern "C" { + static void JpegScanSOI(const uint8_t** src_buf, uint32_t* src_size) { if (*src_size == 0) return; @@ -41,16 +42,13 @@ static void JpegScanSOI(const uint8_t** src_buf, uint32_t* src_size) { offset++; } } -}; -extern "C" { + static void _src_do_nothing(struct jpeg_decompress_struct* cinfo) {} -}; -extern "C" { + static void _error_fatal(j_common_ptr cinfo) { longjmp(*(jmp_buf*)cinfo->client_data, -1); } -}; -extern "C" { + static void _src_skip_data(struct jpeg_decompress_struct* cinfo, long num) { if (num > (long)cinfo->src->bytes_in_buffer) { _error_fatal((j_common_ptr)cinfo); @@ -58,25 +56,28 @@ static void _src_skip_data(struct jpeg_decompress_struct* cinfo, long num) { cinfo->src->next_input_byte += num; cinfo->src->bytes_in_buffer -= num; } -}; -extern "C" { + static boolean _src_fill_buffer(j_decompress_ptr cinfo) { return 0; } -}; -extern "C" { + static boolean _src_resync(j_decompress_ptr cinfo, int desired) { return 0; } -}; -extern "C" { + static void _error_do_nothing(j_common_ptr cinfo) {} -}; -extern "C" { + static void _error_do_nothing1(j_common_ptr cinfo, int) {} -}; -extern "C" { + static void _error_do_nothing2(j_common_ptr cinfo, char*) {} + +#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_DESKTOP_ +static void _dest_do_nothing(j_compress_ptr cinfo) {} + +static boolean _dest_empty(j_compress_ptr cinfo) { + return false; +} +#endif }; #define JPEG_MARKER_ICC (JPEG_APP0 + 2) @@ -480,3 +481,106 @@ uint32_t CCodec_JpegModule::GetAvailInput(FXJPEG_Context* ctx, } return (uint32_t)ctx->m_SrcMgr.bytes_in_buffer; } + +#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_DESKTOP_ +#define JPEG_BLOCK_SIZE 1048576 +bool CCodec_JpegModule::JpegEncode(const CFX_DIBSource* pSource, + uint8_t** dest_buf, + FX_STRSIZE* dest_size) { + 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 (!safe_buf_len.IsValid()) + return false; + + uint32_t 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)) + return false; + + 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 = nullptr; + if (nComponents > 1) + line_buf = FX_Alloc2D(uint8_t, width, nComponents); + + jpeg_set_defaults(&cinfo); + jpeg_start_compress(&cinfo, TRUE); + 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; + + return true; +} +#endif |