39 #define MAXIMUMCODE 4095
55 } strTableNode, *strTableNodePtr, **strTable;
70 int compgif(code_size, os, infun)
77 int clear_code, end_code, cur_code;
78 int i, found, num_colors, prefix, compress_size;
79 int cur_char, end_of_data, bits_per_pix;
80 strTableNodePtr cur_node;
85 heap = (strTable)malloc(
sizeof(strTableNodePtr)*
MAXIMUMCODE);
86 if (heap == NULL) error(
"can't allocate heap");
88 heap[i] = (strTableNodePtr)malloc(
sizeof(strTableNode));
89 if (heap[i] == NULL) error(
"can't allocate heap");
91 bits_per_pix = code_size - 1;
92 compress_size = code_size;
93 num_colors = 1<<(bits_per_pix);
94 clear_code = num_colors;
95 end_code = clear_code + 1;
96 cur_code = end_code + 1;
98 root = (strTable)malloc(
sizeof(strTableNodePtr)*num_colors);
99 if (!root) error(
"memory allocation failure (root)");
100 for(i=0; i<num_colors; i++) {
101 root[i] = heap[heap_index++];
110 pack_bits(compress_size, -1, os);
111 pack_bits(compress_size, clear_code, os);
113 if((cur_char = GIFNextPixel(infun)) == EOF) error(
"premature end of data");
114 while (!end_of_data) {
116 cur_node = root[prefix];
118 if((cur_char = GIFNextPixel(infun)) == EOF) {
119 end_of_data = 1;
break;
127 cur_node
->left = heap[heap_index++];
128 cur_node = cur_node
->left;
135 cur_node
->right = heap[heap_index++];
143 if((cur_char = GIFNextPixel(infun)) == EOF) {
144 end_of_data = 1;
break;
148 if (end_of_data)
break;
159 pack_bits(compress_size, prefix, os);
160 if (cur_code > ((1<<(compress_size))-1))
166 heap_index = num_colors;
167 for (i=0; i < num_colors; i++ ) root[i]
->children = NULL;
168 pack_bits(compress_size, clear_code, os);
169 compress_size = bits_per_pix + 1;
170 cur_code = end_code + 1;
173 pack_bits(compress_size, prefix, os);
174 pack_bits(compress_size, end_code, os);
175 pack_bits(compress_size, -1, os);
192 int pack_bits(compress_size, prefix, os)
197 static int cur_bit = 8;
198 static unsigned char block[
BLOCKSIZE] = { 0 };
199 int i, left_over_bits;
203 if((cur_bit + compress_size > (
BLOCKSIZE-1)*8) || (prefix < 0)) {
205 if ((left_over_bits = (((cur_bit>>3) +
206 ((cur_bit & 7) != 0))<<3) - cur_bit) != 0) {
207 for (i=0; i < left_over_bits; i++) {
208 if(prefix & (1<<i)) block[cur_bit>>3] |= (
char)(1<<(cur_bit & 7));
213 compress_size -= left_over_bits;
214 prefix = prefix>>left_over_bits;
215 block[0] = (
unsigned char)((cur_bit>>3) - 1);
216 if (block[0]) fwrite(block,1,block[0]+1,os);
217 for(i=0; i <
BLOCKSIZE; i++) block[i] = 0;
221 for (i=0; i < compress_size; i++) {
222 if(prefix & (1<<i)) block[cur_bit>>3] |=
223 (
unsigned char)(1<<(cur_bit & 7));
struct str_table_node * children
struct str_table_node * right
struct str_table_node * left