table.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <cstdio>
3 
4 #include <imagezero/libiz.h>
5 #include <imagezero/iz_p.h>
6 
7 namespace IZ {
8 
9 const unsigned int staticdCount[1 << (2 * CONTEXT_BITS)] =
10 {
11 #if MAX_CODE_LENGTH >= 6
12 // 0, 1, 2, 3, 4, 5, 6, 7, 8
13  1, 3, 2, 5, 5, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0,
14  3, 2, 2, 2, 4, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0,
15  4, 2, 2, 2, 3, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0,
16  6, 4, 2, 2, 2, 3, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0,
17  6, 6, 3, 2, 2, 2, 4, 6, 6, 0, 0, 0, 0, 0, 0, 0,
18  6, 6, 4, 2, 2, 2, 3, 6, 6, 0, 0, 0, 0, 0, 0, 0,
19  6, 6, 6, 4, 2, 2, 2, 3, 6, 0, 0, 0, 0, 0, 0, 0,
20  6, 6, 6, 6, 3, 2, 2, 2, 4, 0, 0, 0, 0, 0, 0, 0,
21  6, 6, 5, 5, 5, 3, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0,
22 #else
23  // TODO also need static tables for
24  // maximum code lengths of 5, 7, and 8
25  1, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0,
26  3, 3, 3, 3, 3, 3, 3, 4, 4, 0, 0, 0, 0, 0, 0, 0,
27  3, 3, 3, 3, 3, 3, 3, 4, 4, 0, 0, 0, 0, 0, 0, 0,
28  4, 3, 3, 3, 3, 3, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0,
29  4, 3, 3, 3, 3, 3, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0,
30  4, 3, 3, 3, 3, 3, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0,
31  4, 4, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0,
32  4, 4, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0,
33  4, 4, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0,
34 #endif
35 };
36 
37 unsigned int staticdBits[1 << (2 * CONTEXT_BITS)];
38 
40 
41 
42 static int comp_int(const void *p1, const void *p2)
43 {
44  return *((const int *) p1) - *((const int *) p2);
45 }
46 
47 static void initBitsTable()
48 {
49  int sorted[CONTEXT_COUNT];
50  for (int pc = 0; pc < CONTEXT_COUNT; ++pc) {
51  int *p = sorted;
52  for (int nc = 0; nc < CONTEXT_COUNT; ++nc) {
53  unsigned int bits = staticdCount[(pc << CONTEXT_BITS) + nc];
54  *p++ = (bits << CONTEXT_BITS) + nc;
55  }
56  qsort(sorted, CONTEXT_COUNT, sizeof(int), comp_int);
57  unsigned int v = 0;
58 // printf("%d: ", pc);
59  for (int c = 0; c < CONTEXT_COUNT; ++c) {
60  unsigned int bits = sorted[c] >> CONTEXT_BITS;
61  unsigned int nc = sorted[c] & bitMask(CONTEXT_BITS);
62 // printf("(%d,%d,%2d) ", bits, nc, v);
63  staticdBits[(pc << CONTEXT_BITS) + nc] = v;
64  ++v;
65  if (c < CONTEXT_COUNT - 1) {
66  v <<= (sorted[c + 1] >> CONTEXT_BITS) - bits;
67  }
68  }
69 // printf("\n");
70  }
71 }
72 
74 {
75  initBitsTable();
76 }
77 
78 //#define CHECKTABLE
79 
81 {
82  initBitsTable();
83 #ifdef CHECKTABLE
84  for (unsigned int pl = 0; pl < CONTEXT_COUNT; ++pl) {
85  for (unsigned int v = 0; v < MAX_CODE_VALUE; ++v) {
86  decodeTable[pl][v] = 100;
87  }
88  }
89 #endif
90  for (unsigned int pl = 0; pl < CONTEXT_COUNT; ++pl) {
91  for (unsigned int v = 0; v < MAX_CODE_VALUE; ++v) {
92  for (unsigned int nl = 0; nl < CONTEXT_COUNT; ++nl) {
93  int bits = staticdCount[(pl << CONTEXT_BITS) + nl];
94  if ((v >> (MAX_CODE_LENGTH - bits)) == staticdBits[(pl << CONTEXT_BITS) + nl]) {
95 #ifdef CHECKTABLE
96  if (decodeTable[pl][v] == 100) {
97  decodeTable[pl][v] = nl;
98  } else {
99  printf("ERROR duplicate value\n");
100  exit(1);
101  }
102 #else
103  decodeTable[pl][v] = nl;
104  break;
105 #endif
106  }
107  }
108  }
109  }
110 #ifdef CHECKTABLE
111  for (unsigned int pl = 0; pl < CONTEXT_COUNT; ++pl) {
112  printf("%d: ", pl);
113  for (unsigned int v = 0; v < MAX_CODE_VALUE; ++v) {
114  if (decodeTable[pl][v] == 100) {
115  printf("ERROR missing value %d\n", v);
116  exit(1);
117  }
118  printf("%d", decodeTable[pl][v]);
119  }
120  printf("\n");
121  }
122 #endif
123 }
124 
125 } // namespace IZ
const int MAX_CODE_LENGTH
Definition: iz_p.h:16
Definition: bitcoder.h:6
unsigned int staticdBits[1<< (2 *CONTEXT_BITS)]
Definition: table.cpp:37
const int CONTEXT_COUNT
Definition: iz_p.h:11
const unsigned int staticdCount[1<< (2 *CONTEXT_BITS)]
Definition: table.cpp:9
const int CONTEXT_BITS
Definition: iz_p.h:12
static int comp_int(const void *p1, const void *p2)
Definition: table.cpp:42
static unsigned int bitMask(unsigned int bitCount)
Definition: intmacros.h:11
void initEncodeTable()
Definition: table.cpp:73
char decodeTable[1<< CONTEXT_BITS][MAX_CODE_VALUE]
Definition: table.cpp:39
void initDecodeTable()
Definition: table.cpp:80
static void initBitsTable()
Definition: table.cpp:47
const int MAX_CODE_VALUE
Definition: iz_p.h:17


imagezero
Author(s):
autogenerated on Wed Jun 5 2019 22:02:47