Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1511)

Unified Diff: core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp

Issue 1536923002: Cleanup CJBig2_HuffmanTable and friends. (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: rebase Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « core/src/fxcodec/jbig2/JBig2_HuffmanTable.h ('k') | core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
index 8aaebf46a211ec06a963eef200a15479b93dbd40..5833f118fe6c38de2d363d3e98fb9b5cc20b37c5 100644
--- a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
@@ -4,107 +4,56 @@
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-#include "JBig2_HuffmanTable.h"
-
-#include <string.h>
+#include "core/src/fxcodec/jbig2/JBig2_HuffmanTable.h"
+#include <algorithm>
#include <vector>
-#include "JBig2_BitStream.h"
-#include "JBig2_Define.h"
#include "core/include/fxcrt/fx_memory.h"
+#include "core/src/fxcodec/jbig2/JBig2_BitStream.h"
+#include "core/src/fxcodec/jbig2/JBig2_Define.h"
+#include "core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h"
CJBig2_HuffmanTable::CJBig2_HuffmanTable(const JBig2TableLine* pTable,
- int nLines,
- FX_BOOL bHTOOB) {
- init();
- m_bOK = parseFromStandardTable(pTable, nLines, bHTOOB);
+ FX_DWORD nLines,
+ bool bHTOOB)
+ : m_bOK(true), HTOOB(bHTOOB), NTEMP(nLines) {
+ ParseFromStandardTable(pTable);
}
-CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_BitStream* pStream) {
- init();
- m_bOK = parseFromCodedBuffer(pStream);
+CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_BitStream* pStream)
+ : HTOOB(false), NTEMP(0) {
+ m_bOK = ParseFromCodedBuffer(pStream);
}
CJBig2_HuffmanTable::~CJBig2_HuffmanTable() {
- FX_Free(CODES);
- FX_Free(PREFLEN);
- FX_Free(RANGELEN);
- FX_Free(RANGELOW);
-}
-void CJBig2_HuffmanTable::init() {
- HTOOB = FALSE;
- NTEMP = 0;
- CODES = nullptr;
- PREFLEN = nullptr;
- RANGELEN = nullptr;
- RANGELOW = nullptr;
}
-int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine* pTable,
- int nLines,
- FX_BOOL bHTOOB) {
- HTOOB = bHTOOB;
- NTEMP = nLines;
- CODES = FX_Alloc(int, NTEMP);
- PREFLEN = FX_Alloc(int, NTEMP);
- RANGELEN = FX_Alloc(int, NTEMP);
- RANGELOW = FX_Alloc(int, NTEMP);
- int LENMAX = 0;
+
+void CJBig2_HuffmanTable::ParseFromStandardTable(const JBig2TableLine* pTable) {
+ PREFLEN.resize(NTEMP);
+ RANGELEN.resize(NTEMP);
+ RANGELOW.resize(NTEMP);
for (FX_DWORD i = 0; i < NTEMP; ++i) {
PREFLEN[i] = pTable[i].PREFLEN;
RANGELEN[i] = pTable[i].RANDELEN;
RANGELOW[i] = pTable[i].RANGELOW;
- if (PREFLEN[i] > LENMAX) {
- LENMAX = PREFLEN[i];
- }
}
- int* LENCOUNT = FX_Alloc(int, LENMAX + 1);
- JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
- int* FIRSTCODE = FX_Alloc(int, LENMAX + 1);
- for (FX_DWORD i = 0; i < NTEMP; ++i)
- ++LENCOUNT[PREFLEN[i]];
-
- int CURLEN = 1;
- FIRSTCODE[0] = 0;
- LENCOUNT[0] = 0;
- while (CURLEN <= LENMAX) {
- FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
- int CURCODE = FIRSTCODE[CURLEN];
- FX_DWORD 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);
- return 1;
+ InitCodes();
}
-#define HT_CHECK_MEMORY_ADJUST \
- if (NTEMP >= nSize) { \
- nSize += 16; \
- PREFLEN = FX_Realloc(int, PREFLEN, nSize); \
- RANGELEN = FX_Realloc(int, RANGELEN, nSize); \
- RANGELOW = FX_Realloc(int, RANGELOW, nSize); \
- }
-int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) {
+bool CJBig2_HuffmanTable::ParseFromCodedBuffer(CJBig2_BitStream* pStream) {
unsigned char cTemp;
if (pStream->read1Byte(&cTemp) == -1)
- return FALSE;
+ return false;
- HTOOB = cTemp & 0x01;
+ HTOOB = !!(cTemp & 0x01);
unsigned char HTPS = ((cTemp >> 1) & 0x07) + 1;
unsigned char HTRS = ((cTemp >> 4) & 0x07) + 1;
FX_DWORD HTLOW;
FX_DWORD HTHIGH;
if (pStream->readInteger(&HTLOW) == -1 ||
pStream->readInteger(&HTHIGH) == -1) {
- return FALSE;
+ return false;
}
const int low = static_cast<int>(HTLOW);
@@ -112,56 +61,57 @@ int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) {
if (low > high)
return false;
- FX_DWORD nSize = 16;
- PREFLEN = FX_Alloc(int, nSize);
- RANGELEN = FX_Alloc(int, nSize);
- RANGELOW = FX_Alloc(int, nSize);
+ ExtendBuffers(false);
int cur_low = low;
- NTEMP = 0;
do {
- HT_CHECK_MEMORY_ADJUST
if ((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) ||
(pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1)) {
- return FALSE;
+ return false;
}
RANGELOW[NTEMP] = cur_low;
cur_low += (1 << RANGELEN[NTEMP]);
- NTEMP = NTEMP + 1;
+ ExtendBuffers(true);
} while (cur_low < high);
- HT_CHECK_MEMORY_ADJUST
+
if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1)
- return FALSE;
+ return false;
RANGELEN[NTEMP] = 32;
RANGELOW[NTEMP] = low - 1;
- ++NTEMP;
- HT_CHECK_MEMORY_ADJUST
+ ExtendBuffers(true);
+
if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1)
- return FALSE;
+ return false;
RANGELEN[NTEMP] = 32;
RANGELOW[NTEMP] = high;
- NTEMP = NTEMP + 1;
+ ExtendBuffers(true);
+
if (HTOOB) {
- HT_CHECK_MEMORY_ADJUST
if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1)
- return FALSE;
+ return false;
++NTEMP;
}
- CODES = FX_Alloc(int, NTEMP);
- int LENMAX = 0;
- for (FX_DWORD i = 0; i < NTEMP; ++i)
- LENMAX = std::max(PREFLEN[i], LENMAX);
- std::vector<int> LENCOUNT(LENMAX + 1);
+ InitCodes();
+ return true;
+}
+
+void CJBig2_HuffmanTable::InitCodes() {
+ int lenmax = 0;
for (FX_DWORD i = 0; i < NTEMP; ++i)
- LENCOUNT[PREFLEN[i]]++;
- LENCOUNT[0] = 0;
+ lenmax = std::max(PREFLEN[i], lenmax);
+
+ CODES.resize(NTEMP);
+ std::vector<int> LENCOUNT(lenmax + 1);
+ std::vector<int> FIRSTCODE(lenmax + 1);
+ for (int len : PREFLEN)
+ ++LENCOUNT[len];
- std::vector<int> FIRSTCODE(LENMAX + 1);
FIRSTCODE[0] = 0;
- for (int i = 1; i <= LENMAX; ++i) {
+ LENCOUNT[0] = 0;
+ for (int i = 1; i <= lenmax; ++i) {
FIRSTCODE[i] = (FIRSTCODE[i - 1] + LENCOUNT[i - 1]) << 1;
int CURCODE = FIRSTCODE[i];
for (FX_DWORD j = 0; j < NTEMP; ++j) {
@@ -169,5 +119,19 @@ int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) {
CODES[j] = CURCODE++;
}
}
- return TRUE;
+}
+
+void CJBig2_HuffmanTable::ExtendBuffers(bool increment) {
+ if (increment)
+ ++NTEMP;
+
+ size_t size = PREFLEN.size();
+ if (NTEMP < size)
+ return;
+
+ size += 16;
+ ASSERT(NTEMP < size);
+ PREFLEN.resize(size);
+ RANGELEN.resize(size);
+ RANGELOW.resize(size);
}
« no previous file with comments | « core/src/fxcodec/jbig2/JBig2_HuffmanTable.h ('k') | core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698