summaryrefslogtreecommitdiff
path: root/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
blob: ea1fdee3beee0ac324ec52fdb66e130ce5dddc0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "JBig2_HuffmanTable.h"

#include <string.h>

#include "../../../include/fxcrt/fx_memory.h"
#include "JBig2_BitStream.h"

CJBig2_HuffmanTable::CJBig2_HuffmanTable(const JBig2TableLine* pTable,
                                         int nLines,
                                         FX_BOOL bHTOOB) {
  init();
  m_bOK = parseFromStandardTable(pTable, nLines, bHTOOB);
}

CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_BitStream* pStream) {
  init();
  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 = NULL;
  PREFLEN = NULL;
  RANGELEN = NULL;
  RANGELOW = NULL;
}
int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine* pTable,
                                                int nLines,
                                                FX_BOOL bHTOOB) {
  int CURLEN, LENMAX, CURCODE, CURTEMP, i;
  int* LENCOUNT;
  int* FIRSTCODE;
  HTOOB = bHTOOB;
  NTEMP = nLines;
  CODES = (int*)FX_AllocOrDie(sizeof(int), NTEMP);
  PREFLEN = (int*)FX_AllocOrDie(sizeof(int), NTEMP);
  RANGELEN = (int*)FX_AllocOrDie(sizeof(int), NTEMP);
  RANGELOW = (int*)FX_AllocOrDie(sizeof(int), NTEMP);
  LENMAX = 0;
  for (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];
    }
  }
  LENCOUNT = (int*)FX_AllocOrDie(sizeof(int), (LENMAX + 1));
  JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
  FIRSTCODE = (int*)FX_AllocOrDie(sizeof(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);
  return 1;
}

#define HT_CHECK_MEMORY_ADJUST                                           \
  if (NTEMP >= nSize) {                                                  \
    nSize += 16;                                                         \
    PREFLEN = (int*)FX_Realloc(uint8_t, PREFLEN, sizeof(int) * nSize);   \
    RANGELEN = (int*)FX_Realloc(uint8_t, RANGELEN, sizeof(int) * nSize); \
    RANGELOW = (int*)FX_Realloc(uint8_t, RANGELOW, sizeof(int) * nSize); \
  }
int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) {
  unsigned char HTPS, HTRS;
  FX_DWORD HTLOW, HTHIGH;
  FX_DWORD CURRANGELOW;
  FX_DWORD nSize = 16;
  int CURLEN, LENMAX, CURCODE, CURTEMP;
  int* LENCOUNT;
  int* FIRSTCODE;
  unsigned char cTemp;
  if (pStream->read1Byte(&cTemp) == -1) {
    goto failed;
  }
  HTOOB = cTemp & 0x01;
  HTPS = ((cTemp >> 1) & 0x07) + 1;
  HTRS = ((cTemp >> 4) & 0x07) + 1;
  if (pStream->readInteger(&HTLOW) == -1 ||
      pStream->readInteger(&HTHIGH) == -1 || HTLOW > HTHIGH) {
    goto failed;
  }
  PREFLEN = (int*)FX_AllocOrDie(sizeof(int), nSize);
  RANGELEN = (int*)FX_AllocOrDie(sizeof(int), nSize);
  RANGELOW = (int*)FX_AllocOrDie(sizeof(int), nSize);
  CURRANGELOW = HTLOW;
  NTEMP = 0;
  do {
    HT_CHECK_MEMORY_ADJUST
    if ((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) ||
        (pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1)) {
      goto failed;
    }
    RANGELOW[NTEMP] = CURRANGELOW;
    CURRANGELOW = CURRANGELOW + (1 << RANGELEN[NTEMP]);
    NTEMP = NTEMP + 1;
  } while (CURRANGELOW < HTHIGH);
  HT_CHECK_MEMORY_ADJUST
  if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) {
    goto failed;
  }
  RANGELEN[NTEMP] = 32;
  RANGELOW[NTEMP] = HTLOW - 1;
  NTEMP = NTEMP + 1;
  HT_CHECK_MEMORY_ADJUST
  if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) {
    goto failed;
  }
  RANGELEN[NTEMP] = 32;
  RANGELOW[NTEMP] = HTHIGH;
  NTEMP = NTEMP + 1;
  if (HTOOB) {
    HT_CHECK_MEMORY_ADJUST
    if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) {
      goto failed;
    }
    NTEMP = NTEMP + 1;
  }
  CODES = (int*)FX_AllocOrDie(sizeof(int), NTEMP);
  LENMAX = 0;
  for (int i = 0; i < NTEMP; i++) {
    if (PREFLEN[i] > LENMAX) {
      LENMAX = PREFLEN[i];
    }
  }
  LENCOUNT = (int*)FX_AllocOrDie(sizeof(int), (LENMAX + 1));
  JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
  FIRSTCODE = (int*)FX_AllocOrDie(sizeof(int), (LENMAX + 1));
  for (int 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);
  return TRUE;
failed:
  return FALSE;
}