table.cpp
Go to the documentation of this file.
00001 #include <cstdlib>
00002 #include <cstdio>
00003 
00004 #include <imagezero/libiz.h>
00005 #include <imagezero/iz_p.h>
00006 
00007 namespace IZ {
00008 
00009 const unsigned int staticdCount[1 << (2 * CONTEXT_BITS)] =
00010 {
00011 #if MAX_CODE_LENGTH >= 6
00012 //  0, 1, 2, 3, 4, 5, 6, 7, 8
00013     1, 3, 2, 5, 5, 6, 6, 6, 6,   0, 0, 0, 0, 0, 0, 0,
00014     3, 2, 2, 2, 4, 6, 6, 6, 6,   0, 0, 0, 0, 0, 0, 0,
00015     4, 2, 2, 2, 3, 6, 6, 6, 6,   0, 0, 0, 0, 0, 0, 0,
00016     6, 4, 2, 2, 2, 3, 6, 6, 6,   0, 0, 0, 0, 0, 0, 0,
00017     6, 6, 3, 2, 2, 2, 4, 6, 6,   0, 0, 0, 0, 0, 0, 0,
00018     6, 6, 4, 2, 2, 2, 3, 6, 6,   0, 0, 0, 0, 0, 0, 0,
00019     6, 6, 6, 4, 2, 2, 2, 3, 6,   0, 0, 0, 0, 0, 0, 0,
00020     6, 6, 6, 6, 3, 2, 2, 2, 4,   0, 0, 0, 0, 0, 0, 0,
00021     6, 6, 5, 5, 5, 3, 2, 2, 2,   0, 0, 0, 0, 0, 0, 0,
00022 #else
00023     // TODO also need static tables for
00024     // maximum code lengths of 5, 7, and 8
00025     1, 4, 4, 4, 4, 4, 4, 4, 4,   0, 0, 0, 0, 0, 0, 0,
00026     3, 3, 3, 3, 3, 3, 3, 4, 4,   0, 0, 0, 0, 0, 0, 0,
00027     3, 3, 3, 3, 3, 3, 3, 4, 4,   0, 0, 0, 0, 0, 0, 0,
00028     4, 3, 3, 3, 3, 3, 3, 3, 4,   0, 0, 0, 0, 0, 0, 0,
00029     4, 3, 3, 3, 3, 3, 3, 3, 4,   0, 0, 0, 0, 0, 0, 0,
00030     4, 3, 3, 3, 3, 3, 3, 3, 4,   0, 0, 0, 0, 0, 0, 0,
00031     4, 4, 3, 3, 3, 3, 3, 3, 3,   0, 0, 0, 0, 0, 0, 0,
00032     4, 4, 3, 3, 3, 3, 3, 3, 3,   0, 0, 0, 0, 0, 0, 0,
00033     4, 4, 3, 3, 3, 3, 3, 3, 3,   0, 0, 0, 0, 0, 0, 0,
00034 #endif
00035 };
00036 
00037 unsigned int staticdBits[1 << (2 * CONTEXT_BITS)];
00038 
00039 char decodeTable[1 << CONTEXT_BITS][MAX_CODE_VALUE];
00040 
00041 
00042 static int comp_int(const void *p1, const void *p2)
00043 {
00044   return *((const int *) p1) - *((const int *) p2);
00045 }
00046 
00047 static void initBitsTable()
00048 {
00049     int sorted[CONTEXT_COUNT];
00050     for (int pc = 0; pc < CONTEXT_COUNT; ++pc) {
00051         int *p = sorted;
00052         for (int nc = 0; nc < CONTEXT_COUNT; ++nc) {
00053             unsigned int bits = staticdCount[(pc << CONTEXT_BITS) + nc];
00054             *p++ = (bits << CONTEXT_BITS) + nc;
00055         }
00056         qsort(sorted, CONTEXT_COUNT, sizeof(int), comp_int);
00057         unsigned int v = 0;
00058 //        printf("%d: ", pc);
00059         for (int c = 0; c < CONTEXT_COUNT; ++c) {
00060             unsigned int bits = sorted[c] >> CONTEXT_BITS;
00061             unsigned int nc = sorted[c] & bitMask(CONTEXT_BITS);
00062 //            printf("(%d,%d,%2d) ", bits, nc, v);
00063             staticdBits[(pc << CONTEXT_BITS) + nc] = v;
00064             ++v;
00065             if (c < CONTEXT_COUNT - 1) {
00066                 v <<= (sorted[c + 1] >> CONTEXT_BITS) - bits;
00067             }
00068         }
00069 //        printf("\n");
00070     }
00071 }
00072 
00073 void initEncodeTable()
00074 {
00075     initBitsTable();
00076 }
00077 
00078 //#define CHECKTABLE
00079 
00080 void initDecodeTable()
00081 {
00082     initBitsTable();
00083 #ifdef CHECKTABLE
00084     for (unsigned int pl = 0; pl < CONTEXT_COUNT; ++pl) {
00085         for (unsigned int v = 0; v < MAX_CODE_VALUE; ++v) {
00086             decodeTable[pl][v] = 100;
00087         }
00088     }
00089 #endif
00090     for (unsigned int pl = 0; pl < CONTEXT_COUNT; ++pl) {
00091         for (unsigned int v = 0; v < MAX_CODE_VALUE; ++v) {
00092             for (unsigned int nl = 0; nl < CONTEXT_COUNT; ++nl) {
00093                 int bits = staticdCount[(pl << CONTEXT_BITS) + nl];
00094                 if ((v >> (MAX_CODE_LENGTH - bits)) == staticdBits[(pl << CONTEXT_BITS) + nl]) {
00095 #ifdef CHECKTABLE
00096                     if (decodeTable[pl][v] == 100) {
00097                         decodeTable[pl][v] = nl;
00098                     } else {
00099                         printf("ERROR duplicate value\n");
00100                         exit(1);
00101                     }
00102 #else
00103                     decodeTable[pl][v] = nl;
00104                     break;
00105 #endif
00106                 }
00107             }
00108         }
00109     }
00110 #ifdef CHECKTABLE
00111     for (unsigned int pl = 0; pl < CONTEXT_COUNT; ++pl) {
00112         printf("%d: ", pl);
00113         for (unsigned int v = 0; v < MAX_CODE_VALUE; ++v) {
00114             if (decodeTable[pl][v] == 100) {
00115                 printf("ERROR missing value %d\n", v);
00116                 exit(1);
00117             }
00118             printf("%d", decodeTable[pl][v]);
00119         }
00120         printf("\n");
00121     }
00122 #endif
00123 }
00124 
00125 } // namespace IZ


imagezero
Author(s):
autogenerated on Thu Jun 6 2019 21:34:51