/tools/quake3/q3data/compress.c

https://gitlab.com/illwieckz/netradiant · C · 787 lines · 565 code · 114 blank · 108 comment · 123 complexity · 8607502717146f828491495ed3ae79fe MD5 · raw file

  1. /*
  2. Copyright (C) 1999-2007 id Software, Inc. and contributors.
  3. For a list of contributors, see the accompanying CONTRIBUTORS file.
  4. This file is part of GtkRadiant.
  5. GtkRadiant is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #include "q3data.h"
  18. #if 0
  19. /*
  20. ==================
  21. MTF
  22. ==================
  23. */
  24. cblock_t MTF( cblock_t in ){
  25. int i, j, b, code;
  26. byte *out_p;
  27. int index[256];
  28. cblock_t out;
  29. out_p = out.data = malloc( in.count + 4 );
  30. // write count
  31. *out_p++ = in.count & 255;
  32. *out_p++ = ( in.count >> 8 ) & 255;
  33. *out_p++ = ( in.count >> 16 ) & 255;
  34. *out_p++ = ( in.count >> 24 ) & 255;
  35. for ( i = 0 ; i < 256 ; i++ )
  36. index[i] = i;
  37. for ( i = 0 ; i < in.count ; i++ )
  38. {
  39. b = in.data[i];
  40. code = index[b];
  41. *out_p++ = code;
  42. // shuffle b indexes to 0
  43. for ( j = 0 ; j < 256 ; j++ )
  44. if ( index[j] < code ) {
  45. index[j]++;
  46. }
  47. index[b] = 0;
  48. }
  49. out.count = out_p - out.data;
  50. return out;
  51. }
  52. //==========================================================================
  53. int bwt_size;
  54. byte *bwt_data;
  55. int bwtCompare( const void *elem1, const void *elem2 ){
  56. int i;
  57. int i1, i2;
  58. int b1, b2;
  59. i1 = *(int *)elem1;
  60. i2 = *(int *)elem2;
  61. for ( i = 0 ; i < bwt_size ; i++ )
  62. {
  63. b1 = bwt_data[i1];
  64. b2 = bwt_data[i2];
  65. if ( b1 < b2 ) {
  66. return -1;
  67. }
  68. if ( b1 > b2 ) {
  69. return 1;
  70. }
  71. if ( ++i1 == bwt_size ) {
  72. i1 = 0;
  73. }
  74. if ( ++i2 == bwt_size ) {
  75. i2 = 0;
  76. }
  77. }
  78. return 0;
  79. }
  80. /*
  81. ==================
  82. BWT
  83. ==================
  84. */
  85. cblock_t BWT( cblock_t in ){
  86. int *sorted;
  87. int i;
  88. byte *out_p;
  89. cblock_t out;
  90. bwt_size = in.count;
  91. bwt_data = in.data;
  92. sorted = malloc( in.count * sizeof( *sorted ) );
  93. for ( i = 0 ; i < in.count ; i++ )
  94. sorted[i] = i;
  95. qsort( sorted, in.count, sizeof( *sorted ), bwtCompare );
  96. out_p = out.data = malloc( in.count + 8 );
  97. // write count
  98. *out_p++ = in.count & 255;
  99. *out_p++ = ( in.count >> 8 ) & 255;
  100. *out_p++ = ( in.count >> 16 ) & 255;
  101. *out_p++ = ( in.count >> 24 ) & 255;
  102. // write head index
  103. for ( i = 0 ; i < in.count ; i++ )
  104. if ( sorted[i] == 0 ) {
  105. break;
  106. }
  107. *out_p++ = i & 255;
  108. *out_p++ = ( i >> 8 ) & 255;
  109. *out_p++ = ( i >> 16 ) & 255;
  110. *out_p++ = ( i >> 24 ) & 255;
  111. // write the L column
  112. for ( i = 0 ; i < in.count ; i++ )
  113. *out_p++ = in.data[( sorted[i] + in.count - 1 ) % in.count];
  114. free( sorted );
  115. out.count = out_p - out.data;
  116. return out;
  117. }
  118. //==========================================================================
  119. typedef struct hnode_s
  120. {
  121. int count;
  122. qboolean used;
  123. int children[2];
  124. } hnode_t;
  125. int numhnodes;
  126. hnode_t hnodes[512];
  127. unsigned charbits[256];
  128. int charbitscount[256];
  129. int SmallestNode( void ){
  130. int i;
  131. int best, bestnode;
  132. best = 99999999;
  133. bestnode = -1;
  134. for ( i = 0 ; i < numhnodes ; i++ )
  135. {
  136. if ( hnodes[i].used ) {
  137. continue;
  138. }
  139. if ( !hnodes[i].count ) {
  140. continue;
  141. }
  142. if ( hnodes[i].count < best ) {
  143. best = hnodes[i].count;
  144. bestnode = i;
  145. }
  146. }
  147. if ( bestnode == -1 ) {
  148. return -1;
  149. }
  150. hnodes[bestnode].used = true;
  151. return bestnode;
  152. }
  153. void BuildChars( int nodenum, unsigned bits, int bitcount ){
  154. hnode_t *node;
  155. if ( nodenum < 256 ) {
  156. if ( bitcount > 32 ) {
  157. Error( "bitcount > 32" );
  158. }
  159. charbits[nodenum] = bits;
  160. charbitscount[nodenum] = bitcount;
  161. return;
  162. }
  163. node = &hnodes[nodenum];
  164. bits <<= 1;
  165. BuildChars( node->children[0], bits, bitcount + 1 );
  166. bits |= 1;
  167. BuildChars( node->children[1], bits, bitcount + 1 );
  168. }
  169. /*
  170. ==================
  171. Huffman
  172. ==================
  173. */
  174. cblock_t Huffman( cblock_t in ){
  175. int i;
  176. hnode_t *node;
  177. int outbits, c;
  178. unsigned bits;
  179. byte *out_p;
  180. cblock_t out;
  181. int max, maxchar;
  182. // count
  183. memset( hnodes, 0, sizeof( hnodes ) );
  184. for ( i = 0 ; i < in.count ; i++ )
  185. hnodes[in.data[i]].count++;
  186. // normalize counts
  187. max = 0;
  188. maxchar = 0;
  189. for ( i = 0 ; i < 256 ; i++ )
  190. {
  191. if ( hnodes[i].count > max ) {
  192. max = hnodes[i].count;
  193. maxchar = i;
  194. }
  195. }
  196. if ( max == 0 ) {
  197. Error( "Huffman: max == 0" );
  198. }
  199. for ( i = 0 ; i < 256 ; i++ )
  200. {
  201. hnodes[i].count = ( hnodes[i].count * 255 + max - 1 ) / max;
  202. }
  203. // build the nodes
  204. numhnodes = 256;
  205. while ( numhnodes != 511 )
  206. {
  207. node = &hnodes[numhnodes];
  208. // pick two lowest counts
  209. node->children[0] = SmallestNode();
  210. if ( node->children[0] == -1 ) {
  211. break; // no more
  212. }
  213. node->children[1] = SmallestNode();
  214. if ( node->children[1] == -1 ) {
  215. if ( node->children[0] != numhnodes - 1 ) {
  216. Error( "Bad smallestnode" );
  217. }
  218. break;
  219. }
  220. node->count = hnodes[node->children[0]].count +
  221. hnodes[node->children[1]].count;
  222. numhnodes++;
  223. }
  224. BuildChars( numhnodes - 1, 0, 0 );
  225. out_p = out.data = malloc( in.count * 2 + 1024 );
  226. memset( out_p, 0, in.count * 2 + 1024 );
  227. // write count
  228. *out_p++ = in.count & 255;
  229. *out_p++ = ( in.count >> 8 ) & 255;
  230. *out_p++ = ( in.count >> 16 ) & 255;
  231. *out_p++ = ( in.count >> 24 ) & 255;
  232. // save out the 256 normalized counts so the tree can be recreated
  233. for ( i = 0 ; i < 256 ; i++ )
  234. *out_p++ = hnodes[i].count;
  235. // write bits
  236. outbits = 0;
  237. for ( i = 0 ; i < in.count ; i++ )
  238. {
  239. c = charbitscount[in.data[i]];
  240. bits = charbits[in.data[i]];
  241. while ( c )
  242. {
  243. c--;
  244. if ( bits & ( 1 << c ) ) {
  245. out_p[outbits >> 3] |= 1 << ( outbits & 7 );
  246. }
  247. outbits++;
  248. }
  249. }
  250. out_p += ( outbits + 7 ) >> 3;
  251. out.count = out_p - out.data;
  252. return out;
  253. }
  254. //==========================================================================
  255. /*
  256. ==================
  257. RLE
  258. ==================
  259. */
  260. #define RLE_CODE 0xe8
  261. #define RLE_TRIPPLE 0xe9
  262. int rle_counts[256];
  263. int rle_bytes[256];
  264. cblock_t RLE( cblock_t in ){
  265. int i;
  266. byte *out_p;
  267. int val;
  268. int repeat;
  269. cblock_t out;
  270. out_p = out.data = malloc( in.count * 2 );
  271. // write count
  272. *out_p++ = in.count & 255;
  273. *out_p++ = ( in.count >> 8 ) & 255;
  274. *out_p++ = ( in.count >> 16 ) & 255;
  275. *out_p++ = ( in.count >> 24 ) & 255;
  276. for ( i = 0 ; i < in.count ; )
  277. {
  278. val = in.data[i];
  279. rle_bytes[val]++;
  280. repeat = 1;
  281. i++;
  282. while ( i < in.count && repeat < 255 && in.data[i] == val )
  283. {
  284. repeat++;
  285. i++;
  286. }
  287. if ( repeat < 256 ) {
  288. rle_counts[repeat]++;
  289. }
  290. if ( repeat > 3 || val == RLE_CODE ) {
  291. *out_p++ = RLE_CODE;
  292. *out_p++ = val;
  293. *out_p++ = repeat;
  294. }
  295. else
  296. {
  297. while ( repeat-- )
  298. *out_p++ = val;
  299. }
  300. }
  301. out.count = out_p - out.data;
  302. return out;
  303. }
  304. //==========================================================================
  305. unsigned lzss_head[256];
  306. unsigned lzss_next[0x20000];
  307. /*
  308. ==================
  309. LZSS
  310. ==================
  311. */
  312. #define BACK_WINDOW 0x10000
  313. #define BACK_BITS 16
  314. #define FRONT_WINDOW 16
  315. #define FRONT_BITS 4
  316. cblock_t LZSS( cblock_t in ){
  317. int i;
  318. byte *out_p;
  319. cblock_t out;
  320. int val;
  321. int j, start, max;
  322. int bestlength, beststart;
  323. int outbits;
  324. if ( in.count >= sizeof( lzss_next ) / 4 ) {
  325. Error( "LZSS: too big" );
  326. }
  327. memset( lzss_head, -1, sizeof( lzss_head ) );
  328. out_p = out.data = malloc( in.count * 2 );
  329. memset( out.data, 0, in.count * 2 );
  330. // write count
  331. *out_p++ = in.count & 255;
  332. *out_p++ = ( in.count >> 8 ) & 255;
  333. *out_p++ = ( in.count >> 16 ) & 255;
  334. *out_p++ = ( in.count >> 24 ) & 255;
  335. outbits = 0;
  336. for ( i = 0 ; i < in.count ; )
  337. {
  338. val = in.data[i];
  339. #if 1
  340. // chained search
  341. bestlength = 0;
  342. beststart = 0;
  343. max = FRONT_WINDOW;
  344. if ( i + max > in.count ) {
  345. max = in.count - i;
  346. }
  347. start = lzss_head[val];
  348. while ( start != -1 && start >= i - BACK_WINDOW )
  349. {
  350. // count match length
  351. for ( j = 0 ; j < max ; j++ )
  352. if ( in.data[start + j] != in.data[i + j] ) {
  353. break;
  354. }
  355. if ( j > bestlength ) {
  356. bestlength = j;
  357. beststart = start;
  358. }
  359. start = lzss_next[start];
  360. }
  361. #else
  362. // slow simple search
  363. // search for a match
  364. max = FRONT_WINDOW;
  365. if ( i + max > in.count ) {
  366. max = in.count - i;
  367. }
  368. start = i - BACK_WINDOW;
  369. if ( start < 0 ) {
  370. start = 0;
  371. }
  372. bestlength = 0;
  373. beststart = 0;
  374. for ( ; start < i ; start++ )
  375. {
  376. if ( in.data[start] != val ) {
  377. continue;
  378. }
  379. // count match length
  380. for ( j = 0 ; j < max ; j++ )
  381. if ( in.data[start + j] != in.data[i + j] ) {
  382. break;
  383. }
  384. if ( j > bestlength ) {
  385. bestlength = j;
  386. beststart = start;
  387. }
  388. }
  389. #endif
  390. beststart = BACK_WINDOW - ( i - beststart );
  391. if ( bestlength < 3 ) { // output a single char
  392. bestlength = 1;
  393. out_p[outbits >> 3] |= 1 << ( outbits & 7 ); // set bit to mark char
  394. outbits++;
  395. for ( j = 0 ; j < 8 ; j++, outbits++ )
  396. if ( val & ( 1 << j ) ) {
  397. out_p[outbits >> 3] |= 1 << ( outbits & 7 );
  398. }
  399. }
  400. else
  401. { // output a phrase
  402. outbits++; // leave a 0 bit to mark phrase
  403. for ( j = 0 ; j < BACK_BITS ; j++, outbits++ )
  404. if ( beststart & ( 1 << j ) ) {
  405. out_p[outbits >> 3] |= 1 << ( outbits & 7 );
  406. }
  407. for ( j = 0 ; j < FRONT_BITS ; j++, outbits++ )
  408. if ( bestlength & ( 1 << j ) ) {
  409. out_p[outbits >> 3] |= 1 << ( outbits & 7 );
  410. }
  411. }
  412. while ( bestlength-- )
  413. {
  414. val = in.data[i];
  415. lzss_next[i] = lzss_head[val];
  416. lzss_head[val] = i;
  417. i++;
  418. }
  419. }
  420. out_p += ( outbits + 7 ) >> 3;
  421. out.count = out_p - out.data;
  422. return out;
  423. }
  424. //==========================================================================
  425. #define MIN_REPT 15
  426. #define MAX_REPT 0
  427. #define HUF_TOKENS ( 256 + MAX_REPT )
  428. unsigned charbits1[256][HUF_TOKENS];
  429. int charbitscount1[256][HUF_TOKENS];
  430. hnode_t hnodes1[256][HUF_TOKENS * 2];
  431. int numhnodes1[256];
  432. int order0counts[256];
  433. /*
  434. ==================
  435. SmallestNode1
  436. ==================
  437. */
  438. int SmallestNode1( hnode_t *hnodes, int numhnodes ){
  439. int i;
  440. int best, bestnode;
  441. best = 99999999;
  442. bestnode = -1;
  443. for ( i = 0 ; i < numhnodes ; i++ )
  444. {
  445. if ( hnodes[i].used ) {
  446. continue;
  447. }
  448. if ( !hnodes[i].count ) {
  449. continue;
  450. }
  451. if ( hnodes[i].count < best ) {
  452. best = hnodes[i].count;
  453. bestnode = i;
  454. }
  455. }
  456. if ( bestnode == -1 ) {
  457. return -1;
  458. }
  459. hnodes[bestnode].used = true;
  460. return bestnode;
  461. }
  462. /*
  463. ==================
  464. BuildChars1
  465. ==================
  466. */
  467. void BuildChars1( int prev, int nodenum, unsigned bits, int bitcount ){
  468. hnode_t *node;
  469. if ( nodenum < HUF_TOKENS ) {
  470. if ( bitcount > 32 ) {
  471. Error( "bitcount > 32" );
  472. }
  473. charbits1[prev][nodenum] = bits;
  474. charbitscount1[prev][nodenum] = bitcount;
  475. return;
  476. }
  477. node = &hnodes1[prev][nodenum];
  478. bits <<= 1;
  479. BuildChars1( prev, node->children[0], bits, bitcount + 1 );
  480. bits |= 1;
  481. BuildChars1( prev, node->children[1], bits, bitcount + 1 );
  482. }
  483. /*
  484. ==================
  485. BuildTree1
  486. ==================
  487. */
  488. void BuildTree1( int prev ){
  489. hnode_t *node, *nodebase;
  490. int numhnodes;
  491. // build the nodes
  492. numhnodes = HUF_TOKENS;
  493. nodebase = hnodes1[prev];
  494. while ( 1 )
  495. {
  496. node = &nodebase[numhnodes];
  497. // pick two lowest counts
  498. node->children[0] = SmallestNode1( nodebase, numhnodes );
  499. if ( node->children[0] == -1 ) {
  500. break; // no more
  501. }
  502. node->children[1] = SmallestNode1( nodebase, numhnodes );
  503. if ( node->children[1] == -1 ) {
  504. break;
  505. }
  506. node->count = nodebase[node->children[0]].count +
  507. nodebase[node->children[1]].count;
  508. numhnodes++;
  509. }
  510. numhnodes1[prev] = numhnodes - 1;
  511. BuildChars1( prev, numhnodes - 1, 0, 0 );
  512. }
  513. /*
  514. ==================
  515. Huffman1_Count
  516. ==================
  517. */
  518. void Huffman1_Count( cblock_t in ){
  519. int i;
  520. int prev;
  521. int v;
  522. int rept;
  523. prev = 0;
  524. for ( i = 0 ; i < in.count ; i++ )
  525. {
  526. v = in.data[i];
  527. order0counts[v]++;
  528. hnodes1[prev][v].count++;
  529. prev = v;
  530. #if 1
  531. for ( rept = 1 ; i + rept < in.count && rept < MAX_REPT ; rept++ )
  532. if ( in.data[i + rept] != v ) {
  533. break;
  534. }
  535. if ( rept > MIN_REPT ) {
  536. hnodes1[prev][255 + rept].count++;
  537. i += rept - 1;
  538. }
  539. #endif
  540. }
  541. }
  542. /*
  543. ==================
  544. Huffman1_Build
  545. ==================
  546. */
  547. byte scaled[256][HUF_TOKENS];
  548. void Huffman1_Build( FILE *f ){
  549. int i, j, v;
  550. int max;
  551. int total;
  552. for ( i = 0 ; i < 256 ; i++ )
  553. {
  554. // normalize and save the counts
  555. max = 0;
  556. for ( j = 0 ; j < HUF_TOKENS ; j++ )
  557. {
  558. if ( hnodes1[i][j].count > max ) {
  559. max = hnodes1[i][j].count;
  560. }
  561. }
  562. if ( max == 0 ) {
  563. max = 1;
  564. }
  565. total = 0;
  566. for ( j = 0 ; j < HUF_TOKENS ; j++ )
  567. { // easy to overflow 32 bits here!
  568. v = ( hnodes1[i][j].count * (double)255 + max - 1 ) / max;
  569. if ( v > 255 ) {
  570. Error( "v > 255" );
  571. }
  572. scaled[i][j] = hnodes1[i][j].count = v;
  573. if ( v ) {
  574. total++;
  575. }
  576. }
  577. if ( total == 1 ) { // must have two tokens
  578. if ( !scaled[i][0] ) {
  579. scaled[i][0] = hnodes1[i][0].count = 1;
  580. }
  581. else{
  582. scaled[i][1] = hnodes1[i][1].count = 1;
  583. }
  584. }
  585. BuildTree1( i );
  586. }
  587. #if 0
  588. // count up the total bits
  589. total = 0;
  590. for ( i = 0 ; i < 256 ; i++ )
  591. for ( j = 0 ; j < 256 ; j++ )
  592. total += charbitscount1[i][j] * hnodes1[i][j].count;
  593. total = ( total + 7 ) / 8;
  594. printf( "%i bytes huffman1 compressed\n", total );
  595. #endif
  596. fwrite( scaled, 1, sizeof( scaled ), f );
  597. }
  598. /*
  599. ==================
  600. Huffman1
  601. Order 1 compression with pre-built table
  602. ==================
  603. */
  604. cblock_t Huffman1( cblock_t in ){
  605. int i;
  606. int outbits, c;
  607. unsigned bits;
  608. byte *out_p;
  609. cblock_t out;
  610. int prev;
  611. int v;
  612. int rept;
  613. out_p = out.data = malloc( in.count * 2 + 1024 );
  614. memset( out_p, 0, in.count * 2 + 1024 );
  615. // write count
  616. *out_p++ = in.count & 255;
  617. *out_p++ = ( in.count >> 8 ) & 255;
  618. *out_p++ = ( in.count >> 16 ) & 255;
  619. *out_p++ = ( in.count >> 24 ) & 255;
  620. // write bits
  621. outbits = 0;
  622. prev = 0;
  623. for ( i = 0 ; i < in.count ; i++ )
  624. {
  625. v = in.data[i];
  626. c = charbitscount1[prev][v];
  627. bits = charbits1[prev][v];
  628. if ( !c ) {
  629. Error( "!bits" );
  630. }
  631. while ( c )
  632. {
  633. c--;
  634. if ( bits & ( 1 << c ) ) {
  635. out_p[outbits >> 3] |= 1 << ( outbits & 7 );
  636. }
  637. outbits++;
  638. }
  639. prev = v;
  640. #if 1
  641. // check for repeat encodes
  642. for ( rept = 1 ; i + rept < in.count && rept < MAX_REPT ; rept++ )
  643. if ( in.data[i + rept] != v ) {
  644. break;
  645. }
  646. if ( rept > MIN_REPT ) {
  647. c = charbitscount1[prev][255 + rept];
  648. bits = charbits1[prev][255 + rept];
  649. if ( !c ) {
  650. Error( "!bits" );
  651. }
  652. while ( c )
  653. {
  654. c--;
  655. if ( bits & ( 1 << c ) ) {
  656. out_p[outbits >> 3] |= 1 << ( outbits & 7 );
  657. }
  658. outbits++;
  659. }
  660. i += rept - 1;
  661. }
  662. #endif
  663. }
  664. out_p += ( outbits + 7 ) >> 3;
  665. out.count = out_p - out.data;
  666. return out;
  667. }
  668. #endif