diff options
author | Nicolas Pena <npm@chromium.org> | 2017-07-14 13:24:12 -0400 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-07-17 14:59:48 +0000 |
commit | 18531230381465592e1a037e6dfc39d4a6fb5def (patch) | |
tree | 34403ecdb01f38df91cab9fc3071c8878b561179 | |
parent | f768baf129fcafc4342193477e0c41c082ef5ca5 (diff) | |
download | pdfium-18531230381465592e1a037e6dfc39d4a6fb5def.tar.xz |
Simplify CJBig2_Context::huffman_assign_code
This CL removes duplicate huffman_assign_code()s and changes some
return values and members to std::vector.
Change-Id: I47a1e0e2e88ff54ec799c97e92ec9ff5ca87c6c7
Reviewed-on: https://pdfium-review.googlesource.com/7910
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
-rw-r--r-- | core/fxcodec/jbig2/JBig2_Context.cpp | 123 | ||||
-rw-r--r-- | core/fxcodec/jbig2/JBig2_Context.h | 6 | ||||
-rw-r--r-- | core/fxcodec/jbig2/JBig2_SddProc.cpp | 12 | ||||
-rw-r--r-- | core/fxcodec/jbig2/JBig2_TrdProc.cpp | 4 | ||||
-rw-r--r-- | core/fxcodec/jbig2/JBig2_TrdProc.h | 6 |
5 files changed, 51 insertions, 100 deletions
diff --git a/core/fxcodec/jbig2/JBig2_Context.cpp b/core/fxcodec/jbig2/JBig2_Context.cpp index 219946d219..eaaed312d6 100644 --- a/core/fxcodec/jbig2/JBig2_Context.cpp +++ b/core/fxcodec/jbig2/JBig2_Context.cpp @@ -721,15 +721,14 @@ int32_t CJBig2_Context::parseTextRegion(CJBig2_Segment* pSegment) { pTRD->SBSYMS = nullptr; } - std::unique_ptr<JBig2HuffmanCode, FxFreeDeleter> SBSYMCODES; if (pTRD->SBHUFF == 1) { - SBSYMCODES.reset( - decodeSymbolIDHuffmanTable(m_pStream.get(), pTRD->SBNUMSYMS)); - if (!SBSYMCODES) + std::vector<JBig2HuffmanCode> SBSYMCODES = + decodeSymbolIDHuffmanTable(m_pStream.get(), pTRD->SBNUMSYMS); + if (SBSYMCODES.empty()) return JBIG2_ERROR_FATAL; m_pStream->alignByte(); - pTRD->SBSYMCODES = SBSYMCODES.get(); + pTRD->SBSYMCODES = std::move(SBSYMCODES); } else { dwTemp = 0; while ((uint32_t)(1 << dwTemp) < pTRD->SBNUMSYMS) { @@ -1235,35 +1234,33 @@ int32_t CJBig2_Context::parseRegionInfo(JBig2RegionInfo* pRI) { return JBIG2_SUCCESS; } -JBig2HuffmanCode* CJBig2_Context::decodeSymbolIDHuffmanTable( +std::vector<JBig2HuffmanCode> CJBig2_Context::decodeSymbolIDHuffmanTable( CJBig2_BitStream* pStream, uint32_t SBNUMSYMS) { const size_t kRunCodesSize = 35; - int32_t runcodes[kRunCodesSize]; - int32_t runcodes_len[kRunCodesSize]; + JBig2HuffmanCode huffman_codes[kRunCodesSize]; for (size_t i = 0; i < kRunCodesSize; ++i) { - if (pStream->readNBits(4, &runcodes_len[i]) != 0) - return nullptr; + if (pStream->readNBits(4, &huffman_codes[i].codelen) != 0) + return std::vector<JBig2HuffmanCode>(); } - huffman_assign_code(runcodes, runcodes_len, kRunCodesSize); + huffman_assign_code(huffman_codes, kRunCodesSize); - std::unique_ptr<JBig2HuffmanCode, FxFreeDeleter> SBSYMCODES( - FX_Alloc(JBig2HuffmanCode, SBNUMSYMS)); + std::vector<JBig2HuffmanCode> SBSYMCODES(SBNUMSYMS); int32_t run = 0; int32_t i = 0; - while (i < (int)SBNUMSYMS) { + while (i < static_cast<int>(SBNUMSYMS)) { size_t j; int32_t nVal = 0; int32_t nBits = 0; uint32_t nTemp; while (true) { if (pStream->read1Bit(&nTemp) != 0) - return nullptr; + return std::vector<JBig2HuffmanCode>(); nVal = (nVal << 1) | nTemp; ++nBits; for (j = 0; j < kRunCodesSize; ++j) { - if (nBits == runcodes_len[j] && nVal == runcodes[j]) + if (nBits == huffman_codes[j].codelen && nVal == huffman_codes[j].code) break; } if (j < kRunCodesSize) @@ -1271,110 +1268,58 @@ JBig2HuffmanCode* CJBig2_Context::decodeSymbolIDHuffmanTable( } int32_t runcode = static_cast<int32_t>(j); if (runcode < 32) { - SBSYMCODES.get()[i].codelen = runcode; + SBSYMCODES[i].codelen = runcode; run = 0; } else if (runcode == 32) { if (pStream->readNBits(2, &nTemp) != 0) - return nullptr; + return std::vector<JBig2HuffmanCode>(); run = nTemp + 3; } else if (runcode == 33) { if (pStream->readNBits(3, &nTemp) != 0) - return nullptr; + return std::vector<JBig2HuffmanCode>(); run = nTemp + 3; } else if (runcode == 34) { if (pStream->readNBits(7, &nTemp) != 0) - return nullptr; + return std::vector<JBig2HuffmanCode>(); run = nTemp + 11; } if (run > 0) { if (i + run > (int)SBNUMSYMS) - return nullptr; + return std::vector<JBig2HuffmanCode>(); for (int32_t k = 0; k < run; ++k) { - if (runcode == 32 && i > 0) { - SBSYMCODES.get()[i + k].codelen = SBSYMCODES.get()[i - 1].codelen; - } else { - SBSYMCODES.get()[i + k].codelen = 0; - } + if (runcode == 32 && i > 0) + SBSYMCODES[i + k].codelen = SBSYMCODES[i - 1].codelen; + else + SBSYMCODES[i + k].codelen = 0; } i += run; } else { ++i; } } - huffman_assign_code(SBSYMCODES.get(), SBNUMSYMS); - return SBSYMCODES.release(); -} - -void CJBig2_Context::huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP) { - // TODO(thestig) CJBig2_HuffmanTable::parseFromCodedBuffer() has similar code. - int CURLEN, LENMAX, CURCODE, CURTEMP, i; - int* LENCOUNT; - int* FIRSTCODE; - LENMAX = 0; - for (i = 0; i < NTEMP; ++i) { - if (PREFLEN[i] > LENMAX) { - LENMAX = PREFLEN[i]; - } - } - LENCOUNT = FX_Alloc(int, LENMAX + 1); - JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); - FIRSTCODE = FX_Alloc(int, LENMAX + 1); - for (i = 0; i < NTEMP; ++i) { - ++LENCOUNT[PREFLEN[i]]; - } - CURLEN = 1; - FIRSTCODE[0] = 0; - LENCOUNT[0] = 0; - while (CURLEN <= LENMAX) { - FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1; - CURCODE = FIRSTCODE[CURLEN]; - CURTEMP = 0; - while (CURTEMP < NTEMP) { - if (PREFLEN[CURTEMP] == CURLEN) { - CODES[CURTEMP] = CURCODE; - CURCODE = CURCODE + 1; - } - CURTEMP = CURTEMP + 1; - } - CURLEN = CURLEN + 1; - } - FX_Free(LENCOUNT); - FX_Free(FIRSTCODE); + huffman_assign_code(SBSYMCODES.data(), SBNUMSYMS); + return SBSYMCODES; } void CJBig2_Context::huffman_assign_code(JBig2HuffmanCode* SBSYMCODES, int NTEMP) { - int CURLEN, LENMAX, CURCODE, CURTEMP, i; - int* LENCOUNT; - int* FIRSTCODE; - LENMAX = 0; - for (i = 0; i < NTEMP; ++i) { - if (SBSYMCODES[i].codelen > LENMAX) { - LENMAX = SBSYMCODES[i].codelen; - } - } - LENCOUNT = FX_Alloc(int, (LENMAX + 1)); - JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); - FIRSTCODE = FX_Alloc(int, (LENMAX + 1)); - for (i = 0; i < NTEMP; ++i) { + // TODO(thestig) CJBig2_HuffmanTable::parseFromCodedBuffer() has similar code. + int LENMAX = 0; + for (int i = 0; i < NTEMP; ++i) + LENMAX = std::max(LENMAX, SBSYMCODES[i].codelen); + std::vector<int> LENCOUNT(LENMAX + 1); + std::vector<int> FIRSTCODE(LENMAX + 1); + for (int i = 0; i < NTEMP; ++i) ++LENCOUNT[SBSYMCODES[i].codelen]; - } - CURLEN = 1; - FIRSTCODE[0] = 0; LENCOUNT[0] = 0; - while (CURLEN <= LENMAX) { + for (int CURLEN = 1; CURLEN <= LENMAX; ++CURLEN) { FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1; - CURCODE = FIRSTCODE[CURLEN]; - CURTEMP = 0; - while (CURTEMP < NTEMP) { + int CURCODE = FIRSTCODE[CURLEN]; + for (int CURTEMP = 0; CURTEMP < NTEMP; ++CURTEMP) { if (SBSYMCODES[CURTEMP].codelen == CURLEN) { SBSYMCODES[CURTEMP].code = CURCODE; CURCODE = CURCODE + 1; } - CURTEMP = CURTEMP + 1; } - CURLEN = CURLEN + 1; } - FX_Free(LENCOUNT); - FX_Free(FIRSTCODE); } diff --git a/core/fxcodec/jbig2/JBig2_Context.h b/core/fxcodec/jbig2/JBig2_Context.h index e4cf33ca96..9224d325b5 100644 --- a/core/fxcodec/jbig2/JBig2_Context.h +++ b/core/fxcodec/jbig2/JBig2_Context.h @@ -79,10 +79,10 @@ class CJBig2_Context { int32_t parseTable(CJBig2_Segment* pSegment); int32_t parseRegionInfo(JBig2RegionInfo* pRI); - JBig2HuffmanCode* decodeSymbolIDHuffmanTable(CJBig2_BitStream* pStream, - uint32_t SBNUMSYMS); + std::vector<JBig2HuffmanCode> decodeSymbolIDHuffmanTable( + CJBig2_BitStream* pStream, + uint32_t SBNUMSYMS); - void huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP); void huffman_assign_code(JBig2HuffmanCode* SBSYMCODES, int NTEMP); std::unique_ptr<CJBig2_Context> m_pGlobalContext; diff --git a/core/fxcodec/jbig2/JBig2_SddProc.cpp b/core/fxcodec/jbig2/JBig2_SddProc.cpp index d23fb049b1..1a98b17d34 100644 --- a/core/fxcodec/jbig2/JBig2_SddProc.cpp +++ b/core/fxcodec/jbig2/JBig2_SddProc.cpp @@ -279,7 +279,6 @@ std::unique_ptr<CJBig2_SymbolDict> CJBig2_SDDProc::decode_Huffman( uint32_t nTmp; uint32_t SBNUMSYMS; uint8_t SBSYMCODELEN; - std::vector<JBig2HuffmanCode> SBSYMCODES; uint32_t IDI; int32_t RDXI, RDYI; uint32_t BMSIZE; @@ -345,16 +344,15 @@ std::unique_ptr<CJBig2_SymbolDict> CJBig2_SDDProc::decode_Huffman( pDecoder->SBSTRIPS = 1; pDecoder->SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED; SBNUMSYMS = pDecoder->SBNUMSYMS; - SBSYMCODES.resize(SBNUMSYMS); + std::vector<JBig2HuffmanCode> SBSYMCODES(SBNUMSYMS); nTmp = 1; - while ((uint32_t)(1 << nTmp) < SBNUMSYMS) { - nTmp++; - } - for (I = 0; I < SBNUMSYMS; I++) { + while (static_cast<uint32_t>(1 << nTmp) < SBNUMSYMS) + ++nTmp; + for (I = 0; I < SBNUMSYMS; ++I) { SBSYMCODES[I].codelen = nTmp; SBSYMCODES[I].code = I; } - pDecoder->SBSYMCODES = SBSYMCODES.data(); + pDecoder->SBSYMCODES = std::move(SBSYMCODES); SBSYMS.resize(SBNUMSYMS); std::copy(SDINSYMS, SDINSYMS + SDNUMINSYMS, SBSYMS.begin()); for (size_t i = 0; i < NSYMSDECODED; ++i) diff --git a/core/fxcodec/jbig2/JBig2_TrdProc.cpp b/core/fxcodec/jbig2/JBig2_TrdProc.cpp index f65332050b..3fc43c067e 100644 --- a/core/fxcodec/jbig2/JBig2_TrdProc.cpp +++ b/core/fxcodec/jbig2/JBig2_TrdProc.cpp @@ -15,6 +15,10 @@ #include "core/fxcrt/cfx_maybe_owned.h" #include "third_party/base/ptr_util.h" +CJBig2_TRDProc::CJBig2_TRDProc() {} + +CJBig2_TRDProc::~CJBig2_TRDProc() {} + std::unique_ptr<CJBig2_Image> CJBig2_TRDProc::decode_Huffman( CJBig2_BitStream* pStream, JBig2ArithCtx* grContext) { diff --git a/core/fxcodec/jbig2/JBig2_TrdProc.h b/core/fxcodec/jbig2/JBig2_TrdProc.h index db35a8870a..a66415b0bd 100644 --- a/core/fxcodec/jbig2/JBig2_TrdProc.h +++ b/core/fxcodec/jbig2/JBig2_TrdProc.h @@ -8,6 +8,7 @@ #define CORE_FXCODEC_JBIG2_JBIG2_TRDPROC_H_ #include <memory> +#include <vector> #include "core/fxcodec/jbig2/JBig2_Image.h" #include "core/fxcrt/fx_system.h" @@ -42,6 +43,9 @@ enum JBig2Corner { class CJBig2_TRDProc { public: + CJBig2_TRDProc(); + ~CJBig2_TRDProc(); + std::unique_ptr<CJBig2_Image> decode_Huffman(CJBig2_BitStream* pStream, JBig2ArithCtx* grContext); @@ -57,7 +61,7 @@ class CJBig2_TRDProc { uint32_t SBSTRIPS; uint32_t SBNUMSYMS; - JBig2HuffmanCode* SBSYMCODES; + std::vector<JBig2HuffmanCode> SBSYMCODES; uint8_t SBSYMCODELEN; CJBig2_Image** SBSYMS; |