summaryrefslogtreecommitdiff
path: root/core/fxcodec/jbig2/JBig2_Context.cpp
diff options
context:
space:
mode:
authorNicolas Pena <npm@chromium.org>2017-07-14 13:24:12 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-07-17 14:59:48 +0000
commit18531230381465592e1a037e6dfc39d4a6fb5def (patch)
tree34403ecdb01f38df91cab9fc3071c8878b561179 /core/fxcodec/jbig2/JBig2_Context.cpp
parentf768baf129fcafc4342193477e0c41c082ef5ca5 (diff)
downloadpdfium-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>
Diffstat (limited to 'core/fxcodec/jbig2/JBig2_Context.cpp')
-rw-r--r--core/fxcodec/jbig2/JBig2_Context.cpp123
1 files changed, 34 insertions, 89 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);
}