From 36f4841d67037ce640273ce357d2d33f3e8567c3 Mon Sep 17 00:00:00 2001 From: Nicolas Pena Date: Fri, 21 Sep 2018 21:09:32 +0000 Subject: Cleanup in CCodec_FaxModule MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This CL moves FaxG4Decode to the class where it's implemented. It should remain there because it calls FaxG4GetRow, declared and used in the cpp file. Do some ++ cleanup while at it. This CL also makes CCodec_FaxModule own some methods that were namespaced since doing so allows removing a bunch of parameters. Change-Id: I24787f5668c7273b9bdb4009c3d0b29590c5552f Reviewed-on: https://pdfium-review.googlesource.com/42950 Commit-Queue: Nicolás Peña Moreno Reviewed-by: Tom Sepez --- core/fxcodec/codec/ccodec_faxmodule.cpp | 169 ++++++++++++++++---------------- core/fxcodec/codec/ccodec_faxmodule.h | 9 ++ 2 files changed, 94 insertions(+), 84 deletions(-) (limited to 'core/fxcodec/codec') diff --git a/core/fxcodec/codec/ccodec_faxmodule.cpp b/core/fxcodec/codec/ccodec_faxmodule.cpp index faa40da6bf..423dfe6857 100644 --- a/core/fxcodec/codec/ccodec_faxmodule.cpp +++ b/core/fxcodec/codec/ccodec_faxmodule.cpp @@ -555,25 +555,26 @@ uint32_t CCodec_FaxDecoder::GetSrcOffset() { return std::min(static_cast((m_bitpos + 7) / 8), m_SrcSpan.size()); } -void FaxG4Decode(const uint8_t* src_buf, - uint32_t src_size, - int* pbitpos, - uint8_t* dest_buf, - int width, - int height, - int pitch) { +// static +int CCodec_FaxModule::FaxG4Decode(const uint8_t* src_buf, + uint32_t src_size, + int starting_bitpos, + int width, + int height, + int pitch, + uint8_t* dest_buf) { if (pitch == 0) pitch = (width + 7) / 8; std::vector ref_buf(pitch, 0xff); - int bitpos = *pbitpos; - for (int iRow = 0; iRow < height; iRow++) { + int bitpos = starting_bitpos; + for (int iRow = 0; iRow < height; ++iRow) { uint8_t* line_buf = dest_buf + iRow * pitch; memset(line_buf, 0xff, pitch); FaxG4GetRow(src_buf, src_size << 3, &bitpos, line_buf, ref_buf, width); memcpy(ref_buf.data(), line_buf, pitch); } - *pbitpos = bitpos; + return bitpos; } std::unique_ptr CCodec_FaxModule::CreateDecoder( @@ -647,138 +648,138 @@ const uint8_t WhiteRunMarkup[80] = { 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--) { +class CCodec_FaxEncoder { + public: + CCodec_FaxEncoder(const uint8_t* src_buf, int width, int height, int pitch); + ~CCodec_FaxEncoder(); + void Encode(std::unique_ptr* dest_buf, + uint32_t* dest_size); + + private: + void FaxEncode2DLine(const uint8_t* src_buf); + void FaxEncodeRun(int run, bool bWhite); + void AddBitStream(int data, int bitlen); + + CFX_BinaryBuf m_DestBuf; + std::vector m_RefLine; + uint8_t* m_pLineBuf; + int m_DestBitpos; + 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); + memset(m_RefLine.data(), 0xff, m_Pitch); + m_pLineBuf = FX_Alloc2D(uint8_t, m_Pitch, 8); + m_DestBuf.SetAllocStep(10240); +} + +CCodec_FaxEncoder::~CCodec_FaxEncoder() { + FX_Free(m_pLineBuf); +} + +void CCodec_FaxEncoder::AddBitStream(int data, int bitlen) { + for (int i = bitlen - 1; i >= 0; --i, ++m_DestBitpos) { if (data & (1 << i)) - dest_buf[*dest_bitpos / 8] |= 1 << (7 - *dest_bitpos % 8); - (*dest_bitpos)++; + m_pLineBuf[m_DestBitpos / 8] |= 1 << (7 - m_DestBitpos % 8); } } -void FaxEncodeRun(uint8_t* dest_buf, int* dest_bitpos, int run, bool bWhite) { +void CCodec_FaxEncoder::FaxEncodeRun(int run, bool bWhite) { while (run >= 2560) { - AddBitStream(dest_buf, dest_bitpos, 0x1f, 12); + AddBitStream(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]); + AddBitStream(*p, p[1]); } run %= 64; const uint8_t* p = bWhite ? WhiteRunTerminator : BlackRunTerminator; p += run * 2; - AddBitStream(dest_buf, dest_bitpos, *p, p[1]); + AddBitStream(*p, p[1]); } -void FaxEncode2DLine(uint8_t* dest_buf, - int* dest_bitpos, - const uint8_t* src_buf, - const std::vector& ref_buf, - int cols) { +void CCodec_FaxEncoder::FaxEncode2DLine(const uint8_t* src_buf) { int a0 = -1; bool a0color = true; while (1) { - int a1 = FindBit(src_buf, cols, a0 + 1, !a0color); + int a1 = FindBit(src_buf, m_Cols, a0 + 1, !a0color); int b1; int b2; - FaxG4FindB1B2(ref_buf, cols, a0, a0color, &b1, &b2); + FaxG4FindB1B2(m_RefLine, m_Cols, a0, a0color, &b1, &b2); if (b2 < a1) { - *dest_bitpos += 3; - dest_buf[*dest_bitpos / 8] |= 1 << (7 - *dest_bitpos % 8); - (*dest_bitpos)++; + m_DestBitpos += 3; + m_pLineBuf[m_DestBitpos / 8] |= 1 << (7 - m_DestBitpos % 8); + ++m_DestBitpos; 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); + m_pLineBuf[m_DestBitpos / 8] |= 1 << (7 - m_DestBitpos % 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); + m_DestBitpos += delta == 1 ? 1 : delta + 2; + m_pLineBuf[m_DestBitpos / 8] |= 1 << (7 - m_DestBitpos % 8); + ++m_DestBitpos; + m_pLineBuf[m_DestBitpos / 8] |= 1 << (7 - m_DestBitpos % 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)++; + m_DestBitpos += delta == -1 ? 1 : -delta + 2; + m_pLineBuf[m_DestBitpos / 8] |= 1 << (7 - m_DestBitpos % 8); + ++m_DestBitpos; break; } - (*dest_bitpos)++; + ++m_DestBitpos; 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)++; + int a2 = FindBit(src_buf, m_Cols, a1 + 1, a0color); + ++m_DestBitpos; + ++m_DestBitpos; + m_pLineBuf[m_DestBitpos / 8] |= 1 << (7 - m_DestBitpos % 8); + ++m_DestBitpos; if (a0 < 0) a0 = 0; - FaxEncodeRun(dest_buf, dest_bitpos, a1 - a0, a0color); - FaxEncodeRun(dest_buf, dest_bitpos, a2 - a1, !a0color); + FaxEncodeRun(a1 - a0, a0color); + FaxEncodeRun(a2 - a1, !a0color); a0 = a2; } - if (a0 >= cols) + if (a0 >= m_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* dest_buf, - uint32_t* dest_size); - - private: - CFX_BinaryBuf m_DestBuf; - std::vector 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); - memset(m_RefLine.data(), 0xff, m_Pitch); - m_pLineBuf = FX_Alloc2D(uint8_t, m_Pitch, 8); - m_DestBuf.SetAllocStep(10240); -} - -CCodec_FaxEncoder::~CCodec_FaxEncoder() { - FX_Free(m_pLineBuf); -} - void CCodec_FaxEncoder::Encode( std::unique_ptr* dest_buf, uint32_t* dest_size) { - int dest_bitpos = 0; + m_DestBitpos = 0; uint8_t last_byte = 0; - for (int i = 0; i < m_Rows; i++) { + for (int i = 0; i < m_Rows; ++i) { const uint8_t* scan_line = m_pSrcBuf + i * m_Pitch; 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; + FaxEncode2DLine(scan_line); + m_DestBuf.AppendBlock(m_pLineBuf, m_DestBitpos / 8); + last_byte = m_pLineBuf[m_DestBitpos / 8]; + m_DestBitpos %= 8; memcpy(m_RefLine.data(), scan_line, m_Pitch); } - if (dest_bitpos) + if (m_DestBitpos) m_DestBuf.AppendByte(last_byte); *dest_size = m_DestBuf.GetSize(); *dest_buf = m_DestBuf.DetachBuffer(); diff --git a/core/fxcodec/codec/ccodec_faxmodule.h b/core/fxcodec/codec/ccodec_faxmodule.h index 3d546d741a..e43aea370b 100644 --- a/core/fxcodec/codec/ccodec_faxmodule.h +++ b/core/fxcodec/codec/ccodec_faxmodule.h @@ -36,6 +36,15 @@ class CCodec_FaxModule { std::unique_ptr* dest_buf, uint32_t* dest_size); #endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ + + // Return the ending bit position. + static int FaxG4Decode(const uint8_t* src_buf, + uint32_t src_size, + int starting_bitpos, + int width, + int height, + int pitch, + uint8_t* dest_buf); }; #endif // CORE_FXCODEC_CODEC_CCODEC_FAXMODULE_H_ -- cgit v1.2.3