2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 cblock_t MTF( cblock_t in ){
36 out_p = out.data = malloc( in.count + 4 );
39 *out_p++ = in.count & 255;
40 *out_p++ = ( in.count >> 8 ) & 255;
41 *out_p++ = ( in.count >> 16 ) & 255;
42 *out_p++ = ( in.count >> 24 ) & 255;
44 for ( i = 0 ; i < 256 ; i++ )
47 for ( i = 0 ; i < in.count ; i++ )
53 // shuffle b indexes to 0
54 for ( j = 0 ; j < 256 ; j++ )
55 if ( index[j] < code ) {
61 out.count = out_p - out.data;
67 //==========================================================================
72 int bwtCompare( const void *elem1, const void *elem2 ){
80 for ( i = 0 ; i < bwt_size ; i++ )
90 if ( ++i1 == bwt_size ) {
93 if ( ++i2 == bwt_size ) {
106 cblock_t BWT( cblock_t in ){
115 sorted = malloc( in.count * sizeof( *sorted ) );
116 for ( i = 0 ; i < in.count ; i++ )
118 qsort( sorted, in.count, sizeof( *sorted ), bwtCompare );
120 out_p = out.data = malloc( in.count + 8 );
123 *out_p++ = in.count & 255;
124 *out_p++ = ( in.count >> 8 ) & 255;
125 *out_p++ = ( in.count >> 16 ) & 255;
126 *out_p++ = ( in.count >> 24 ) & 255;
129 for ( i = 0 ; i < in.count ; i++ )
130 if ( sorted[i] == 0 ) {
134 *out_p++ = ( i >> 8 ) & 255;
135 *out_p++ = ( i >> 16 ) & 255;
136 *out_p++ = ( i >> 24 ) & 255;
138 // write the L column
139 for ( i = 0 ; i < in.count ; i++ )
140 *out_p++ = in.data[( sorted[i] + in.count - 1 ) % in.count];
144 out.count = out_p - out.data;
149 //==========================================================================
151 typedef struct hnode_s
160 unsigned charbits[256];
161 int charbitscount[256];
163 int SmallestNode( void ){
169 for ( i = 0 ; i < numhnodes ; i++ )
171 if ( hnodes[i].used ) {
174 if ( !hnodes[i].count ) {
177 if ( hnodes[i].count < best ) {
178 best = hnodes[i].count;
183 if ( bestnode == -1 ) {
187 hnodes[bestnode].used = true;
191 void BuildChars( int nodenum, unsigned bits, int bitcount ){
194 if ( nodenum < 256 ) {
195 if ( bitcount > 32 ) {
196 Error( "bitcount > 32" );
198 charbits[nodenum] = bits;
199 charbitscount[nodenum] = bitcount;
203 node = &hnodes[nodenum];
205 BuildChars( node->children[0], bits, bitcount + 1 );
207 BuildChars( node->children[1], bits, bitcount + 1 );
215 cblock_t Huffman( cblock_t in ){
225 memset( hnodes, 0, sizeof( hnodes ) );
226 for ( i = 0 ; i < in.count ; i++ )
227 hnodes[in.data[i]].count++;
232 for ( i = 0 ; i < 256 ; i++ )
234 if ( hnodes[i].count > max ) {
235 max = hnodes[i].count;
240 Error( "Huffman: max == 0" );
243 for ( i = 0 ; i < 256 ; i++ )
245 hnodes[i].count = ( hnodes[i].count * 255 + max - 1 ) / max;
250 while ( numhnodes != 511 )
252 node = &hnodes[numhnodes];
254 // pick two lowest counts
255 node->children[0] = SmallestNode();
256 if ( node->children[0] == -1 ) {
260 node->children[1] = SmallestNode();
261 if ( node->children[1] == -1 ) {
262 if ( node->children[0] != numhnodes - 1 ) {
263 Error( "Bad smallestnode" );
267 node->count = hnodes[node->children[0]].count +
268 hnodes[node->children[1]].count;
272 BuildChars( numhnodes - 1, 0, 0 );
274 out_p = out.data = malloc( in.count * 2 + 1024 );
275 memset( out_p, 0, in.count * 2 + 1024 );
278 *out_p++ = in.count & 255;
279 *out_p++ = ( in.count >> 8 ) & 255;
280 *out_p++ = ( in.count >> 16 ) & 255;
281 *out_p++ = ( in.count >> 24 ) & 255;
283 // save out the 256 normalized counts so the tree can be recreated
284 for ( i = 0 ; i < 256 ; i++ )
285 *out_p++ = hnodes[i].count;
289 for ( i = 0 ; i < in.count ; i++ )
291 c = charbitscount[in.data[i]];
292 bits = charbits[in.data[i]];
296 if ( bits & ( 1 << c ) ) {
297 out_p[outbits >> 3] |= 1 << ( outbits & 7 );
303 out_p += ( outbits + 7 ) >> 3;
305 out.count = out_p - out.data;
310 //==========================================================================
317 #define RLE_CODE 0xe8
318 #define RLE_TRIPPLE 0xe9
323 cblock_t RLE( cblock_t in ){
330 out_p = out.data = malloc( in.count * 2 );
333 *out_p++ = in.count & 255;
334 *out_p++ = ( in.count >> 8 ) & 255;
335 *out_p++ = ( in.count >> 16 ) & 255;
336 *out_p++ = ( in.count >> 24 ) & 255;
338 for ( i = 0 ; i < in.count ; )
344 while ( i < in.count && repeat < 255 && in.data[i] == val )
349 if ( repeat < 256 ) {
350 rle_counts[repeat]++;
352 if ( repeat > 3 || val == RLE_CODE ) {
364 out.count = out_p - out.data;
368 //==========================================================================
370 unsigned lzss_head[256];
371 unsigned lzss_next[0x20000];
378 #define BACK_WINDOW 0x10000
380 #define FRONT_WINDOW 16
382 cblock_t LZSS( cblock_t in ){
388 int bestlength, beststart;
391 if ( in.count >= sizeof( lzss_next ) / 4 ) {
392 Error( "LZSS: too big" );
395 memset( lzss_head, -1, sizeof( lzss_head ) );
397 out_p = out.data = malloc( in.count * 2 );
398 memset( out.data, 0, in.count * 2 );
401 *out_p++ = in.count & 255;
402 *out_p++ = ( in.count >> 8 ) & 255;
403 *out_p++ = ( in.count >> 16 ) & 255;
404 *out_p++ = ( in.count >> 24 ) & 255;
407 for ( i = 0 ; i < in.count ; )
416 if ( i + max > in.count ) {
420 start = lzss_head[val];
421 while ( start != -1 && start >= i - BACK_WINDOW )
423 // count match length
424 for ( j = 0 ; j < max ; j++ )
425 if ( in.data[start + j] != in.data[i + j] ) {
428 if ( j > bestlength ) {
432 start = lzss_next[start];
436 // slow simple search
437 // search for a match
439 if ( i + max > in.count ) {
443 start = i - BACK_WINDOW;
449 for ( ; start < i ; start++ )
451 if ( in.data[start] != val ) {
454 // count match length
455 for ( j = 0 ; j < max ; j++ )
456 if ( in.data[start + j] != in.data[i + j] ) {
459 if ( j > bestlength ) {
465 beststart = BACK_WINDOW - ( i - beststart );
467 if ( bestlength < 3 ) { // output a single char
470 out_p[outbits >> 3] |= 1 << ( outbits & 7 ); // set bit to mark char
472 for ( j = 0 ; j < 8 ; j++, outbits++ )
473 if ( val & ( 1 << j ) ) {
474 out_p[outbits >> 3] |= 1 << ( outbits & 7 );
479 outbits++; // leave a 0 bit to mark phrase
480 for ( j = 0 ; j < BACK_BITS ; j++, outbits++ )
481 if ( beststart & ( 1 << j ) ) {
482 out_p[outbits >> 3] |= 1 << ( outbits & 7 );
484 for ( j = 0 ; j < FRONT_BITS ; j++, outbits++ )
485 if ( bestlength & ( 1 << j ) ) {
486 out_p[outbits >> 3] |= 1 << ( outbits & 7 );
490 while ( bestlength-- )
493 lzss_next[i] = lzss_head[val];
499 out_p += ( outbits + 7 ) >> 3;
500 out.count = out_p - out.data;
504 //==========================================================================
508 #define HUF_TOKENS ( 256 + MAX_REPT )
510 unsigned charbits1[256][HUF_TOKENS];
511 int charbitscount1[256][HUF_TOKENS];
513 hnode_t hnodes1[256][HUF_TOKENS * 2];
516 int order0counts[256];
523 int SmallestNode1( hnode_t *hnodes, int numhnodes ){
529 for ( i = 0 ; i < numhnodes ; i++ )
531 if ( hnodes[i].used ) {
534 if ( !hnodes[i].count ) {
537 if ( hnodes[i].count < best ) {
538 best = hnodes[i].count;
543 if ( bestnode == -1 ) {
547 hnodes[bestnode].used = true;
557 void BuildChars1( int prev, int nodenum, unsigned bits, int bitcount ){
560 if ( nodenum < HUF_TOKENS ) {
561 if ( bitcount > 32 ) {
562 Error( "bitcount > 32" );
564 charbits1[prev][nodenum] = bits;
565 charbitscount1[prev][nodenum] = bitcount;
569 node = &hnodes1[prev][nodenum];
571 BuildChars1( prev, node->children[0], bits, bitcount + 1 );
573 BuildChars1( prev, node->children[1], bits, bitcount + 1 );
582 void BuildTree1( int prev ){
583 hnode_t *node, *nodebase;
587 numhnodes = HUF_TOKENS;
588 nodebase = hnodes1[prev];
591 node = &nodebase[numhnodes];
593 // pick two lowest counts
594 node->children[0] = SmallestNode1( nodebase, numhnodes );
595 if ( node->children[0] == -1 ) {
599 node->children[1] = SmallestNode1( nodebase, numhnodes );
600 if ( node->children[1] == -1 ) {
604 node->count = nodebase[node->children[0]].count +
605 nodebase[node->children[1]].count;
608 numhnodes1[prev] = numhnodes - 1;
609 BuildChars1( prev, numhnodes - 1, 0, 0 );
618 void Huffman1_Count( cblock_t in ){
625 for ( i = 0 ; i < in.count ; i++ )
629 hnodes1[prev][v].count++;
632 for ( rept = 1 ; i + rept < in.count && rept < MAX_REPT ; rept++ )
633 if ( in.data[i + rept] != v ) {
636 if ( rept > MIN_REPT ) {
637 hnodes1[prev][255 + rept].count++;
650 byte scaled[256][HUF_TOKENS];
651 void Huffman1_Build( FILE *f ){
656 for ( i = 0 ; i < 256 ; i++ )
658 // normalize and save the counts
660 for ( j = 0 ; j < HUF_TOKENS ; j++ )
662 if ( hnodes1[i][j].count > max ) {
663 max = hnodes1[i][j].count;
670 for ( j = 0 ; j < HUF_TOKENS ; j++ )
671 { // easy to overflow 32 bits here!
672 v = ( hnodes1[i][j].count * (double)255 + max - 1 ) / max;
676 scaled[i][j] = hnodes1[i][j].count = v;
681 if ( total == 1 ) { // must have two tokens
682 if ( !scaled[i][0] ) {
683 scaled[i][0] = hnodes1[i][0].count = 1;
686 scaled[i][1] = hnodes1[i][1].count = 1;
694 // count up the total bits
696 for ( i = 0 ; i < 256 ; i++ )
697 for ( j = 0 ; j < 256 ; j++ )
698 total += charbitscount1[i][j] * hnodes1[i][j].count;
700 total = ( total + 7 ) / 8;
701 printf( "%i bytes huffman1 compressed\n", total );
704 fwrite( scaled, 1, sizeof( scaled ), f );
711 Order 1 compression with pre-built table
714 cblock_t Huffman1( cblock_t in ){
724 out_p = out.data = malloc( in.count * 2 + 1024 );
725 memset( out_p, 0, in.count * 2 + 1024 );
728 *out_p++ = in.count & 255;
729 *out_p++ = ( in.count >> 8 ) & 255;
730 *out_p++ = ( in.count >> 16 ) & 255;
731 *out_p++ = ( in.count >> 24 ) & 255;
736 for ( i = 0 ; i < in.count ; i++ )
740 c = charbitscount1[prev][v];
741 bits = charbits1[prev][v];
748 if ( bits & ( 1 << c ) ) {
749 out_p[outbits >> 3] |= 1 << ( outbits & 7 );
756 // check for repeat encodes
757 for ( rept = 1 ; i + rept < in.count && rept < MAX_REPT ; rept++ )
758 if ( in.data[i + rept] != v ) {
761 if ( rept > MIN_REPT ) {
762 c = charbitscount1[prev][255 + rept];
763 bits = charbits1[prev][255 + rept];
770 if ( bits & ( 1 << c ) ) {
771 out_p[outbits >> 3] |= 1 << ( outbits & 7 );
780 out_p += ( outbits + 7 ) >> 3;
782 out.count = out_p - out.data;