Compressor.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. /*
  23. =================================================================================
  24. idCompressor_None
  25. =================================================================================
  26. */
  27. class idCompressor_None : public idCompressor {
  28. public:
  29. idCompressor_None( void );
  30. void Init( idFile *f, bool compress, int wordLength );
  31. void FinishCompress( void );
  32. float GetCompressionRatio( void ) const;
  33. const char * GetName( void );
  34. const char * GetFullPath( void );
  35. int Read( void *outData, int outLength );
  36. int Write( const void *inData, int inLength );
  37. int Length( void );
  38. ID_TIME_T Timestamp( void );
  39. int Tell( void );
  40. void ForceFlush( void );
  41. void Flush( void );
  42. int Seek( long offset, fsOrigin_t origin );
  43. protected:
  44. idFile * file;
  45. bool compress;
  46. };
  47. /*
  48. ================
  49. idCompressor_None::idCompressor_None
  50. ================
  51. */
  52. idCompressor_None::idCompressor_None( void ) {
  53. file = NULL;
  54. compress = true;
  55. }
  56. /*
  57. ================
  58. idCompressor_None::Init
  59. ================
  60. */
  61. void idCompressor_None::Init( idFile *f, bool compress, int wordLength ) {
  62. this->file = f;
  63. this->compress = compress;
  64. }
  65. /*
  66. ================
  67. idCompressor_None::FinishCompress
  68. ================
  69. */
  70. void idCompressor_None::FinishCompress( void ) {
  71. }
  72. /*
  73. ================
  74. idCompressor_None::GetCompressionRatio
  75. ================
  76. */
  77. float idCompressor_None::GetCompressionRatio( void ) const {
  78. return 0.0f;
  79. }
  80. /*
  81. ================
  82. idCompressor_None::GetName
  83. ================
  84. */
  85. const char *idCompressor_None::GetName( void ) {
  86. if ( file ) {
  87. return file->GetName();
  88. } else {
  89. return "";
  90. }
  91. }
  92. /*
  93. ================
  94. idCompressor_None::GetFullPath
  95. ================
  96. */
  97. const char *idCompressor_None::GetFullPath( void ) {
  98. if ( file ) {
  99. return file->GetFullPath();
  100. } else {
  101. return "";
  102. }
  103. }
  104. /*
  105. ================
  106. idCompressor_None::Write
  107. ================
  108. */
  109. int idCompressor_None::Write( const void *inData, int inLength ) {
  110. if ( compress == false || inLength <= 0 ) {
  111. return 0;
  112. }
  113. return file->Write( inData, inLength );
  114. }
  115. /*
  116. ================
  117. idCompressor_None::Read
  118. ================
  119. */
  120. int idCompressor_None::Read( void *outData, int outLength ) {
  121. if ( compress == true || outLength <= 0 ) {
  122. return 0;
  123. }
  124. return file->Read( outData, outLength );
  125. }
  126. /*
  127. ================
  128. idCompressor_None::Length
  129. ================
  130. */
  131. int idCompressor_None::Length( void ) {
  132. if ( file ) {
  133. return file->Length();
  134. } else {
  135. return 0;
  136. }
  137. }
  138. /*
  139. ================
  140. idCompressor_None::Timestamp
  141. ================
  142. */
  143. ID_TIME_T idCompressor_None::Timestamp( void ) {
  144. if ( file ) {
  145. return file->Timestamp();
  146. } else {
  147. return 0;
  148. }
  149. }
  150. /*
  151. ================
  152. idCompressor_None::Tell
  153. ================
  154. */
  155. int idCompressor_None::Tell( void ) {
  156. if ( file ) {
  157. return file->Tell();
  158. } else {
  159. return 0;
  160. }
  161. }
  162. /*
  163. ================
  164. idCompressor_None::ForceFlush
  165. ================
  166. */
  167. void idCompressor_None::ForceFlush( void ) {
  168. if ( file ) {
  169. file->ForceFlush();
  170. }
  171. }
  172. /*
  173. ================
  174. idCompressor_None::Flush
  175. ================
  176. */
  177. void idCompressor_None::Flush( void ) {
  178. if ( file ) {
  179. file->ForceFlush();
  180. }
  181. }
  182. /*
  183. ================
  184. idCompressor_None::Seek
  185. ================
  186. */
  187. int idCompressor_None::Seek( long offset, fsOrigin_t origin ) {
  188. common->Error( "cannot seek on idCompressor" );
  189. return -1;
  190. }
  191. /*
  192. =================================================================================
  193. idCompressor_BitStream
  194. Base class for bit stream compression.
  195. =================================================================================
  196. */
  197. class idCompressor_BitStream : public idCompressor_None {
  198. public:
  199. idCompressor_BitStream( void ) {}
  200. void Init( idFile *f, bool compress, int wordLength );
  201. void FinishCompress( void );
  202. float GetCompressionRatio( void ) const;
  203. int Write( const void *inData, int inLength );
  204. int Read( void *outData, int outLength );
  205. protected:
  206. byte buffer[65536];
  207. int wordLength;
  208. int readTotalBytes;
  209. int readLength;
  210. int readByte;
  211. int readBit;
  212. const byte * readData;
  213. int writeTotalBytes;
  214. int writeLength;
  215. int writeByte;
  216. int writeBit;
  217. byte * writeData;
  218. protected:
  219. void InitCompress( const void *inData, const int inLength );
  220. void InitDecompress( void *outData, int outLength );
  221. void WriteBits( int value, int numBits );
  222. int ReadBits( int numBits );
  223. void UnreadBits( int numBits );
  224. int Compare( const byte *src1, int bitPtr1, const byte *src2, int bitPtr2, int maxBits ) const;
  225. };
  226. /*
  227. ================
  228. idCompressor_BitStream::Init
  229. ================
  230. */
  231. void idCompressor_BitStream::Init( idFile *f, bool compress, int wordLength ) {
  232. assert( wordLength >= 1 && wordLength <= 32 );
  233. this->file = f;
  234. this->compress = compress;
  235. this->wordLength = wordLength;
  236. readTotalBytes = 0;
  237. readLength = 0;
  238. readByte = 0;
  239. readBit = 0;
  240. readData = NULL;
  241. writeTotalBytes = 0;
  242. writeLength = 0;
  243. writeByte = 0;
  244. writeBit = 0;
  245. writeData = NULL;
  246. }
  247. /*
  248. ================
  249. idCompressor_BitStream::InitCompress
  250. ================
  251. */
  252. ID_INLINE void idCompressor_BitStream::InitCompress( const void *inData, const int inLength ) {
  253. readLength = inLength;
  254. readByte = 0;
  255. readBit = 0;
  256. readData = (const byte *) inData;
  257. if ( !writeLength ) {
  258. writeLength = sizeof( buffer );
  259. writeByte = 0;
  260. writeBit = 0;
  261. writeData = buffer;
  262. }
  263. }
  264. /*
  265. ================
  266. idCompressor_BitStream::InitDecompress
  267. ================
  268. */
  269. ID_INLINE void idCompressor_BitStream::InitDecompress( void *outData, int outLength ) {
  270. if ( !readLength ) {
  271. readLength = file->Read( buffer, sizeof( buffer ) );
  272. readByte = 0;
  273. readBit = 0;
  274. readData = buffer;
  275. }
  276. writeLength = outLength;
  277. writeByte = 0;
  278. writeBit = 0;
  279. writeData = (byte *) outData;
  280. }
  281. /*
  282. ================
  283. idCompressor_BitStream::WriteBits
  284. ================
  285. */
  286. void idCompressor_BitStream::WriteBits( int value, int numBits ) {
  287. int put, fraction;
  288. // Short circuit for writing single bytes at a time
  289. if ( writeBit == 0 && numBits == 8 && writeByte < writeLength ) {
  290. writeByte++;
  291. writeTotalBytes++;
  292. writeData[writeByte - 1] = value;
  293. return;
  294. }
  295. while( numBits ) {
  296. if ( writeBit == 0 ) {
  297. if ( writeByte >= writeLength ) {
  298. if ( writeData == buffer ) {
  299. file->Write( buffer, writeByte );
  300. writeByte = 0;
  301. } else {
  302. put = numBits;
  303. writeBit = put & 7;
  304. writeByte += ( put >> 3 ) + ( writeBit != 0 );
  305. writeTotalBytes += ( put >> 3 ) + ( writeBit != 0 );
  306. return;
  307. }
  308. }
  309. writeData[writeByte] = 0;
  310. writeByte++;
  311. writeTotalBytes++;
  312. }
  313. put = 8 - writeBit;
  314. if ( put > numBits ) {
  315. put = numBits;
  316. }
  317. fraction = value & ( ( 1 << put ) - 1 );
  318. writeData[writeByte - 1] |= fraction << writeBit;
  319. numBits -= put;
  320. value >>= put;
  321. writeBit = ( writeBit + put ) & 7;
  322. }
  323. }
  324. /*
  325. ================
  326. idCompressor_BitStream::ReadBits
  327. ================
  328. */
  329. int idCompressor_BitStream::ReadBits( int numBits ) {
  330. int value, valueBits, get, fraction;
  331. value = 0;
  332. valueBits = 0;
  333. // Short circuit for reading single bytes at a time
  334. if ( readBit == 0 && numBits == 8 && readByte < readLength ) {
  335. readByte++;
  336. readTotalBytes++;
  337. return readData[readByte - 1];
  338. }
  339. while ( valueBits < numBits ) {
  340. if ( readBit == 0 ) {
  341. if ( readByte >= readLength ) {
  342. if ( readData == buffer ) {
  343. readLength = file->Read( buffer, sizeof( buffer ) );
  344. readByte = 0;
  345. } else {
  346. get = numBits - valueBits;
  347. readBit = get & 7;
  348. readByte += ( get >> 3 ) + ( readBit != 0 );
  349. readTotalBytes += ( get >> 3 ) + ( readBit != 0 );
  350. return value;
  351. }
  352. }
  353. readByte++;
  354. readTotalBytes++;
  355. }
  356. get = 8 - readBit;
  357. if ( get > (numBits - valueBits) ) {
  358. get = (numBits - valueBits);
  359. }
  360. fraction = readData[readByte - 1];
  361. fraction >>= readBit;
  362. fraction &= ( 1 << get ) - 1;
  363. value |= fraction << valueBits;
  364. valueBits += get;
  365. readBit = ( readBit + get ) & 7;
  366. }
  367. return value;
  368. }
  369. /*
  370. ================
  371. idCompressor_BitStream::UnreadBits
  372. ================
  373. */
  374. void idCompressor_BitStream::UnreadBits( int numBits ) {
  375. readByte -= ( numBits >> 3 );
  376. readTotalBytes -= ( numBits >> 3 );
  377. if ( readBit == 0 ) {
  378. readBit = 8 - ( numBits & 7 );
  379. } else {
  380. readBit -= numBits & 7;
  381. if ( readBit <= 0 ) {
  382. readByte--;
  383. readTotalBytes--;
  384. readBit = ( readBit + 8 ) & 7;
  385. }
  386. }
  387. if ( readByte < 0 ) {
  388. readByte = 0;
  389. readBit = 0;
  390. }
  391. }
  392. /*
  393. ================
  394. idCompressor_BitStream::Compare
  395. ================
  396. */
  397. int idCompressor_BitStream::Compare( const byte *src1, int bitPtr1, const byte *src2, int bitPtr2, int maxBits ) const {
  398. int i;
  399. // If the two bit pointers are aligned then we can use a faster comparison
  400. if ( ( bitPtr1 & 7 ) == (bitPtr2 & 7 ) && maxBits > 16 ) {
  401. const byte *p1 = &src1[bitPtr1 >> 3];
  402. const byte *p2 = &src2[bitPtr2 >> 3];
  403. int bits = 0;
  404. int bitsRemain = maxBits;
  405. // Compare the first couple bits (if any)
  406. if ( bitPtr1 & 7 ) {
  407. for ( i = (bitPtr1 & 7); i < 8; i++, bits++ ) {
  408. if ( ( ( *p1 >> i ) ^ ( *p2 >> i ) ) & 1 ) {
  409. return bits;
  410. }
  411. bitsRemain--;
  412. }
  413. p1++;
  414. p2++;
  415. }
  416. int remain = bitsRemain >> 3;
  417. // Compare the middle bytes as ints
  418. while ( remain >= 4 && (*(const int *)p1 == *(const int *)p2) ) {
  419. p1 += 4;
  420. p2 += 4;
  421. remain -= 4;
  422. bits += 32;
  423. }
  424. // Compare the remaining bytes
  425. while ( remain > 0 && (*p1 == *p2) ) {
  426. p1++;
  427. p2++;
  428. remain--;
  429. bits += 8;
  430. }
  431. // Compare the last couple of bits (if any)
  432. int finalBits = 8;
  433. if ( remain == 0 ) {
  434. finalBits = ( bitsRemain & 7 );
  435. }
  436. for ( i = 0; i < finalBits; i++, bits++ ) {
  437. if ( ( ( *p1 >> i ) ^ ( *p2 >> i ) ) & 1 ) {
  438. return bits;
  439. }
  440. }
  441. assert(bits == maxBits);
  442. return bits;
  443. } else {
  444. for ( i = 0; i < maxBits; i++ ) {
  445. if ( ( ( src1[bitPtr1 >> 3] >> ( bitPtr1 & 7 ) ) ^ ( src2[bitPtr2 >> 3] >> ( bitPtr2 & 7 ) ) ) & 1 ) {
  446. break;
  447. }
  448. bitPtr1++;
  449. bitPtr2++;
  450. }
  451. return i;
  452. }
  453. }
  454. /*
  455. ================
  456. idCompressor_BitStream::Write
  457. ================
  458. */
  459. int idCompressor_BitStream::Write( const void *inData, int inLength ) {
  460. int i;
  461. if ( compress == false || inLength <= 0 ) {
  462. return 0;
  463. }
  464. InitCompress( inData, inLength );
  465. for ( i = 0; i < inLength; i++ ) {
  466. WriteBits( ReadBits( 8 ), 8 );
  467. }
  468. return i;
  469. }
  470. /*
  471. ================
  472. idCompressor_BitStream::FinishCompress
  473. ================
  474. */
  475. void idCompressor_BitStream::FinishCompress( void ) {
  476. if ( compress == false ) {
  477. return;
  478. }
  479. if ( writeByte ) {
  480. file->Write( buffer, writeByte );
  481. }
  482. writeLength = 0;
  483. writeByte = 0;
  484. writeBit = 0;
  485. }
  486. /*
  487. ================
  488. idCompressor_BitStream::Read
  489. ================
  490. */
  491. int idCompressor_BitStream::Read( void *outData, int outLength ) {
  492. int i;
  493. if ( compress == true || outLength <= 0 ) {
  494. return 0;
  495. }
  496. InitDecompress( outData, outLength );
  497. for ( i = 0; i < outLength && readLength >= 0; i++ ) {
  498. WriteBits( ReadBits( 8 ), 8 );
  499. }
  500. return i;
  501. }
  502. /*
  503. ================
  504. idCompressor_BitStream::GetCompressionRatio
  505. ================
  506. */
  507. float idCompressor_BitStream::GetCompressionRatio( void ) const {
  508. if ( compress ) {
  509. return ( readTotalBytes - writeTotalBytes ) * 100.0f / readTotalBytes;
  510. } else {
  511. return ( writeTotalBytes - readTotalBytes ) * 100.0f / writeTotalBytes;
  512. }
  513. }
  514. /*
  515. =================================================================================
  516. idCompressor_RunLength
  517. The following algorithm implements run length compression with an arbitrary
  518. word size.
  519. =================================================================================
  520. */
  521. class idCompressor_RunLength : public idCompressor_BitStream {
  522. public:
  523. idCompressor_RunLength( void ) {}
  524. void Init( idFile *f, bool compress, int wordLength );
  525. int Write( const void *inData, int inLength );
  526. int Read( void *outData, int outLength );
  527. private:
  528. int runLengthCode;
  529. };
  530. /*
  531. ================
  532. idCompressor_RunLength::Init
  533. ================
  534. */
  535. void idCompressor_RunLength::Init( idFile *f, bool compress, int wordLength ) {
  536. idCompressor_BitStream::Init( f, compress, wordLength );
  537. runLengthCode = ( 1 << wordLength ) - 1;
  538. }
  539. /*
  540. ================
  541. idCompressor_RunLength::Write
  542. ================
  543. */
  544. int idCompressor_RunLength::Write( const void *inData, int inLength ) {
  545. int bits, nextBits, count;
  546. if ( compress == false || inLength <= 0 ) {
  547. return 0;
  548. }
  549. InitCompress( inData, inLength );
  550. while( readByte <= readLength ) {
  551. count = 1;
  552. bits = ReadBits( wordLength );
  553. for ( nextBits = ReadBits( wordLength ); nextBits == bits; nextBits = ReadBits( wordLength ) ) {
  554. count++;
  555. if ( count >= ( 1 << wordLength ) ) {
  556. if ( count >= ( 1 << wordLength ) + 3 || bits == runLengthCode ) {
  557. break;
  558. }
  559. }
  560. }
  561. if ( nextBits != bits ) {
  562. UnreadBits( wordLength );
  563. }
  564. if ( count > 3 || bits == runLengthCode ) {
  565. WriteBits( runLengthCode, wordLength );
  566. WriteBits( bits, wordLength );
  567. if ( bits != runLengthCode ) {
  568. count -= 3;
  569. }
  570. WriteBits( count - 1, wordLength );
  571. } else {
  572. while( count-- ) {
  573. WriteBits( bits, wordLength );
  574. }
  575. }
  576. }
  577. return inLength;
  578. }
  579. /*
  580. ================
  581. idCompressor_RunLength::Read
  582. ================
  583. */
  584. int idCompressor_RunLength::Read( void *outData, int outLength ) {
  585. int bits, count;
  586. if ( compress == true || outLength <= 0 ) {
  587. return 0;
  588. }
  589. InitDecompress( outData, outLength );
  590. while( writeByte <= writeLength && readLength >= 0 ) {
  591. bits = ReadBits( wordLength );
  592. if ( bits == runLengthCode ) {
  593. bits = ReadBits( wordLength );
  594. count = ReadBits( wordLength ) + 1;
  595. if ( bits != runLengthCode ) {
  596. count += 3;
  597. }
  598. while( count-- ) {
  599. WriteBits( bits, wordLength );
  600. }
  601. } else {
  602. WriteBits( bits, wordLength );
  603. }
  604. }
  605. return writeByte;
  606. }
  607. /*
  608. =================================================================================
  609. idCompressor_RunLength_ZeroBased
  610. The following algorithm implements run length compression with an arbitrary
  611. word size for data with a lot of zero bits.
  612. =================================================================================
  613. */
  614. class idCompressor_RunLength_ZeroBased : public idCompressor_BitStream {
  615. public:
  616. idCompressor_RunLength_ZeroBased( void ) {}
  617. int Write( const void *inData, int inLength );
  618. int Read( void *outData, int outLength );
  619. private:
  620. };
  621. /*
  622. ================
  623. idCompressor_RunLength_ZeroBased::Write
  624. ================
  625. */
  626. int idCompressor_RunLength_ZeroBased::Write( const void *inData, int inLength ) {
  627. int bits, count;
  628. if ( compress == false || inLength <= 0 ) {
  629. return 0;
  630. }
  631. InitCompress( inData, inLength );
  632. while( readByte <= readLength ) {
  633. count = 0;
  634. for ( bits = ReadBits( wordLength ); bits == 0 && count < ( 1 << wordLength ); bits = ReadBits( wordLength ) ) {
  635. count++;
  636. }
  637. if ( count ) {
  638. WriteBits( 0, wordLength );
  639. WriteBits( count - 1, wordLength );
  640. UnreadBits( wordLength );
  641. } else {
  642. WriteBits( bits, wordLength );
  643. }
  644. }
  645. return inLength;
  646. }
  647. /*
  648. ================
  649. idCompressor_RunLength_ZeroBased::Read
  650. ================
  651. */
  652. int idCompressor_RunLength_ZeroBased::Read( void *outData, int outLength ) {
  653. int bits, count;
  654. if ( compress == true || outLength <= 0 ) {
  655. return 0;
  656. }
  657. InitDecompress( outData, outLength );
  658. while( writeByte <= writeLength && readLength >= 0 ) {
  659. bits = ReadBits( wordLength );
  660. if ( bits == 0 ) {
  661. count = ReadBits( wordLength ) + 1;
  662. while( count-- > 0 ) {
  663. WriteBits( 0, wordLength );
  664. }
  665. } else {
  666. WriteBits( bits, wordLength );
  667. }
  668. }
  669. return writeByte;
  670. }
  671. /*
  672. =================================================================================
  673. idCompressor_Huffman
  674. The following algorithm is based on the adaptive Huffman algorithm described
  675. in Sayood's Data Compression book. The ranks are not actually stored, but
  676. implicitly defined by the location of a node within a doubly-linked list
  677. =================================================================================
  678. */
  679. const int HMAX = 256; // Maximum symbol
  680. const int NYT = HMAX; // NYT = Not Yet Transmitted
  681. const int INTERNAL_NODE = HMAX + 1; // internal node
  682. typedef struct nodetype {
  683. struct nodetype *left, *right, *parent; // tree structure
  684. struct nodetype *next, *prev; // doubly-linked list
  685. struct nodetype **head; // highest ranked node in block
  686. int weight;
  687. int symbol;
  688. } huffmanNode_t;
  689. class idCompressor_Huffman : public idCompressor_None {
  690. public:
  691. idCompressor_Huffman( void ) {}
  692. void Init( idFile *f, bool compress, int wordLength );
  693. void FinishCompress( void );
  694. float GetCompressionRatio( void ) const;
  695. int Write( const void *inData, int inLength );
  696. int Read( void *outData, int outLength );
  697. private:
  698. byte seq[65536];
  699. int bloc;
  700. int blocMax;
  701. int blocIn;
  702. int blocNode;
  703. int blocPtrs;
  704. int compressedSize;
  705. int unCompressedSize;
  706. huffmanNode_t * tree;
  707. huffmanNode_t * lhead;
  708. huffmanNode_t * ltail;
  709. huffmanNode_t * loc[HMAX+1];
  710. huffmanNode_t **freelist;
  711. huffmanNode_t nodeList[768];
  712. huffmanNode_t * nodePtrs[768];
  713. private:
  714. void AddRef( byte ch );
  715. int Receive( huffmanNode_t *node, int *ch );
  716. void Transmit( int ch, byte *fout );
  717. void PutBit( int bit, byte *fout, int *offset );
  718. int GetBit( byte *fout, int *offset );
  719. void Add_bit( char bit, byte *fout );
  720. int Get_bit();
  721. huffmanNode_t **Get_ppnode();
  722. void Free_ppnode( huffmanNode_t **ppnode );
  723. void Swap( huffmanNode_t *node1, huffmanNode_t *node2 );
  724. void Swaplist( huffmanNode_t *node1, huffmanNode_t *node2 );
  725. void Increment( huffmanNode_t *node );
  726. void Send( huffmanNode_t *node, huffmanNode_t *child, byte *fout );
  727. };
  728. /*
  729. ================
  730. idCompressor_Huffman::Init
  731. ================
  732. */
  733. void idCompressor_Huffman::Init( idFile *f, bool compress, int wordLength ) {
  734. int i;
  735. this->file = f;
  736. this->compress = compress;
  737. bloc = 0;
  738. blocMax = 0;
  739. blocIn = 0;
  740. blocNode = 0;
  741. blocPtrs = 0;
  742. compressedSize = 0;
  743. unCompressedSize = 0;
  744. tree = NULL;
  745. lhead = NULL;
  746. ltail = NULL;
  747. for( i = 0; i < (HMAX+1); i++ ) {
  748. loc[i] = NULL;
  749. }
  750. freelist = NULL;
  751. for( i = 0; i < 768; i++ ) {
  752. memset( &nodeList[i], 0, sizeof(huffmanNode_t) );
  753. nodePtrs[i] = NULL;
  754. }
  755. if ( compress ) {
  756. // Add the NYT (not yet transmitted) node into the tree/list
  757. tree = lhead = loc[NYT] = &nodeList[blocNode++];
  758. tree->symbol = NYT;
  759. tree->weight = 0;
  760. lhead->next = lhead->prev = NULL;
  761. tree->parent = tree->left = tree->right = NULL;
  762. loc[NYT] = tree;
  763. } else {
  764. // Initialize the tree & list with the NYT node
  765. tree = lhead = ltail = loc[NYT] = &nodeList[blocNode++];
  766. tree->symbol = NYT;
  767. tree->weight = 0;
  768. lhead->next = lhead->prev = NULL;
  769. tree->parent = tree->left = tree->right = NULL;
  770. }
  771. }
  772. /*
  773. ================
  774. idCompressor_Huffman::PutBit
  775. ================
  776. */
  777. void idCompressor_Huffman::PutBit( int bit, byte *fout, int *offset) {
  778. bloc = *offset;
  779. if ( (bloc&7) == 0 ) {
  780. fout[(bloc>>3)] = 0;
  781. }
  782. fout[(bloc>>3)] |= bit << (bloc&7);
  783. bloc++;
  784. *offset = bloc;
  785. }
  786. /*
  787. ================
  788. idCompressor_Huffman::GetBit
  789. ================
  790. */
  791. int idCompressor_Huffman::GetBit( byte *fin, int *offset) {
  792. int t;
  793. bloc = *offset;
  794. t = (fin[(bloc>>3)] >> (bloc&7)) & 0x1;
  795. bloc++;
  796. *offset = bloc;
  797. return t;
  798. }
  799. /*
  800. ================
  801. idCompressor_Huffman::Add_bit
  802. Add a bit to the output file (buffered)
  803. ================
  804. */
  805. void idCompressor_Huffman::Add_bit( char bit, byte *fout ) {
  806. if ( (bloc&7) == 0 ) {
  807. fout[(bloc>>3)] = 0;
  808. }
  809. fout[(bloc>>3)] |= bit << (bloc&7);
  810. bloc++;
  811. }
  812. /*
  813. ================
  814. idCompressor_Huffman::Get_bit
  815. Get one bit from the input file (buffered)
  816. ================
  817. */
  818. int idCompressor_Huffman::Get_bit() {
  819. int t;
  820. int wh = bloc >> 3;
  821. int whb = wh>> 16;
  822. if ( whb != blocIn ) {
  823. blocMax += file->Read( seq, sizeof( seq ) );
  824. blocIn++;
  825. }
  826. wh &= 0xffff;
  827. t = ( seq[wh] >> ( bloc & 7 ) ) & 0x1;
  828. bloc++;
  829. return t;
  830. }
  831. /*
  832. ================
  833. idCompressor_Huffman::Get_ppnode
  834. ================
  835. */
  836. huffmanNode_t **idCompressor_Huffman::Get_ppnode() {
  837. huffmanNode_t **tppnode;
  838. if ( !freelist ) {
  839. return &nodePtrs[blocPtrs++];
  840. } else {
  841. tppnode = freelist;
  842. freelist = (huffmanNode_t **)*tppnode;
  843. return tppnode;
  844. }
  845. }
  846. /*
  847. ================
  848. idCompressor_Huffman::Free_ppnode
  849. ================
  850. */
  851. void idCompressor_Huffman::Free_ppnode( huffmanNode_t **ppnode ) {
  852. *ppnode = (huffmanNode_t *)freelist;
  853. freelist = ppnode;
  854. }
  855. /*
  856. ================
  857. idCompressor_Huffman::Swap
  858. Swap the location of the given two nodes in the tree.
  859. ================
  860. */
  861. void idCompressor_Huffman::Swap( huffmanNode_t *node1, huffmanNode_t *node2 ) {
  862. huffmanNode_t *par1, *par2;
  863. par1 = node1->parent;
  864. par2 = node2->parent;
  865. if ( par1 ) {
  866. if ( par1->left == node1 ) {
  867. par1->left = node2;
  868. } else {
  869. par1->right = node2;
  870. }
  871. } else {
  872. tree = node2;
  873. }
  874. if ( par2 ) {
  875. if ( par2->left == node2 ) {
  876. par2->left = node1;
  877. } else {
  878. par2->right = node1;
  879. }
  880. } else {
  881. tree = node1;
  882. }
  883. node1->parent = par2;
  884. node2->parent = par1;
  885. }
  886. /*
  887. ================
  888. idCompressor_Huffman::Swaplist
  889. Swap the given two nodes in the linked list (update ranks)
  890. ================
  891. */
  892. void idCompressor_Huffman::Swaplist( huffmanNode_t *node1, huffmanNode_t *node2 ) {
  893. huffmanNode_t *par1;
  894. par1 = node1->next;
  895. node1->next = node2->next;
  896. node2->next = par1;
  897. par1 = node1->prev;
  898. node1->prev = node2->prev;
  899. node2->prev = par1;
  900. if ( node1->next == node1 ) {
  901. node1->next = node2;
  902. }
  903. if ( node2->next == node2 ) {
  904. node2->next = node1;
  905. }
  906. if ( node1->next ) {
  907. node1->next->prev = node1;
  908. }
  909. if ( node2->next ) {
  910. node2->next->prev = node2;
  911. }
  912. if ( node1->prev ) {
  913. node1->prev->next = node1;
  914. }
  915. if ( node2->prev ) {
  916. node2->prev->next = node2;
  917. }
  918. }
  919. /*
  920. ================
  921. idCompressor_Huffman::Increment
  922. ================
  923. */
  924. void idCompressor_Huffman::Increment( huffmanNode_t *node ) {
  925. huffmanNode_t *lnode;
  926. if ( !node ) {
  927. return;
  928. }
  929. if ( node->next != NULL && node->next->weight == node->weight ) {
  930. lnode = *node->head;
  931. if ( lnode != node->parent ) {
  932. Swap( lnode, node );
  933. }
  934. Swaplist( lnode, node );
  935. }
  936. if ( node->prev && node->prev->weight == node->weight ) {
  937. *node->head = node->prev;
  938. } else {
  939. *node->head = NULL;
  940. Free_ppnode( node->head );
  941. }
  942. node->weight++;
  943. if ( node->next && node->next->weight == node->weight ) {
  944. node->head = node->next->head;
  945. } else {
  946. node->head = Get_ppnode();
  947. *node->head = node;
  948. }
  949. if ( node->parent ) {
  950. Increment( node->parent );
  951. if ( node->prev == node->parent ) {
  952. Swaplist( node, node->parent );
  953. if ( *node->head == node ) {
  954. *node->head = node->parent;
  955. }
  956. }
  957. }
  958. }
  959. /*
  960. ================
  961. idCompressor_Huffman::AddRef
  962. ================
  963. */
  964. void idCompressor_Huffman::AddRef( byte ch ) {
  965. huffmanNode_t *tnode, *tnode2;
  966. if ( loc[ch] == NULL ) { /* if this is the first transmission of this node */
  967. tnode = &nodeList[blocNode++];
  968. tnode2 = &nodeList[blocNode++];
  969. tnode2->symbol = INTERNAL_NODE;
  970. tnode2->weight = 1;
  971. tnode2->next = lhead->next;
  972. if ( lhead->next ) {
  973. lhead->next->prev = tnode2;
  974. if ( lhead->next->weight == 1 ) {
  975. tnode2->head = lhead->next->head;
  976. } else {
  977. tnode2->head = Get_ppnode();
  978. *tnode2->head = tnode2;
  979. }
  980. } else {
  981. tnode2->head = Get_ppnode();
  982. *tnode2->head = tnode2;
  983. }
  984. lhead->next = tnode2;
  985. tnode2->prev = lhead;
  986. tnode->symbol = ch;
  987. tnode->weight = 1;
  988. tnode->next = lhead->next;
  989. if ( lhead->next ) {
  990. lhead->next->prev = tnode;
  991. if ( lhead->next->weight == 1 ) {
  992. tnode->head = lhead->next->head;
  993. } else {
  994. /* this should never happen */
  995. tnode->head = Get_ppnode();
  996. *tnode->head = tnode2;
  997. }
  998. } else {
  999. /* this should never happen */
  1000. tnode->head = Get_ppnode();
  1001. *tnode->head = tnode;
  1002. }
  1003. lhead->next = tnode;
  1004. tnode->prev = lhead;
  1005. tnode->left = tnode->right = NULL;
  1006. if ( lhead->parent ) {
  1007. if ( lhead->parent->left == lhead ) { /* lhead is guaranteed to by the NYT */
  1008. lhead->parent->left = tnode2;
  1009. } else {
  1010. lhead->parent->right = tnode2;
  1011. }
  1012. } else {
  1013. tree = tnode2;
  1014. }
  1015. tnode2->right = tnode;
  1016. tnode2->left = lhead;
  1017. tnode2->parent = lhead->parent;
  1018. lhead->parent = tnode->parent = tnode2;
  1019. loc[ch] = tnode;
  1020. Increment( tnode2->parent );
  1021. } else {
  1022. Increment( loc[ch] );
  1023. }
  1024. }
  1025. /*
  1026. ================
  1027. idCompressor_Huffman::Receive
  1028. Get a symbol.
  1029. ================
  1030. */
  1031. int idCompressor_Huffman::Receive( huffmanNode_t *node, int *ch ) {
  1032. while ( node && node->symbol == INTERNAL_NODE ) {
  1033. if ( Get_bit() ) {
  1034. node = node->right;
  1035. } else {
  1036. node = node->left;
  1037. }
  1038. }
  1039. if ( !node ) {
  1040. return 0;
  1041. }
  1042. return (*ch = node->symbol);
  1043. }
  1044. /*
  1045. ================
  1046. idCompressor_Huffman::Send
  1047. Send the prefix code for this node.
  1048. ================
  1049. */
  1050. void idCompressor_Huffman::Send( huffmanNode_t *node, huffmanNode_t *child, byte *fout ) {
  1051. if ( node->parent ) {
  1052. Send( node->parent, node, fout );
  1053. }
  1054. if ( child ) {
  1055. if ( node->right == child ) {
  1056. Add_bit( 1, fout );
  1057. } else {
  1058. Add_bit( 0, fout );
  1059. }
  1060. }
  1061. }
  1062. /*
  1063. ================
  1064. idCompressor_Huffman::Transmit
  1065. Send a symbol.
  1066. ================
  1067. */
  1068. void idCompressor_Huffman::Transmit( int ch, byte *fout ) {
  1069. int i;
  1070. if ( loc[ch] == NULL ) {
  1071. /* huffmanNode_t hasn't been transmitted, send a NYT, then the symbol */
  1072. Transmit( NYT, fout );
  1073. for ( i = 7; i >= 0; i-- ) {
  1074. Add_bit( (char)((ch >> i) & 0x1), fout );
  1075. }
  1076. } else {
  1077. Send( loc[ch], NULL, fout );
  1078. }
  1079. }
  1080. /*
  1081. ================
  1082. idCompressor_Huffman::Write
  1083. ================
  1084. */
  1085. int idCompressor_Huffman::Write( const void *inData, int inLength ) {
  1086. int i, ch;
  1087. if ( compress == false || inLength <= 0 ) {
  1088. return 0;
  1089. }
  1090. for ( i = 0; i < inLength; i++ ) {
  1091. ch = ((const byte *)inData)[i];
  1092. Transmit( ch, seq ); /* Transmit symbol */
  1093. AddRef( (byte)ch ); /* Do update */
  1094. int b = (bloc>>3);
  1095. if ( b > 32768 ) {
  1096. file->Write( seq, b );
  1097. seq[0] = seq[b];
  1098. bloc &= 7;
  1099. compressedSize += b;
  1100. }
  1101. }
  1102. unCompressedSize += i;
  1103. return i;
  1104. }
  1105. /*
  1106. ================
  1107. idCompressor_Huffman::FinishCompress
  1108. ================
  1109. */
  1110. void idCompressor_Huffman::FinishCompress( void ) {
  1111. if ( compress == false ) {
  1112. return;
  1113. }
  1114. bloc += 7;
  1115. int str = (bloc>>3);
  1116. if ( str ) {
  1117. file->Write( seq, str );
  1118. compressedSize += str;
  1119. }
  1120. }
  1121. /*
  1122. ================
  1123. idCompressor_Huffman::Read
  1124. ================
  1125. */
  1126. int idCompressor_Huffman::Read( void *outData, int outLength ) {
  1127. int i, j, ch;
  1128. if ( compress == true || outLength <= 0 ) {
  1129. return 0;
  1130. }
  1131. if ( bloc == 0 ) {
  1132. blocMax = file->Read( seq, sizeof( seq ) );
  1133. blocIn = 0;
  1134. }
  1135. for ( i = 0; i < outLength; i++ ) {
  1136. ch = 0;
  1137. // don't overflow reading from the file
  1138. if ( ( bloc >> 3 ) > blocMax ) {
  1139. break;
  1140. }
  1141. Receive( tree, &ch ); /* Get a character */
  1142. if ( ch == NYT ) { /* We got a NYT, get the symbol associated with it */
  1143. ch = 0;
  1144. for ( j = 0; j < 8; j++ ) {
  1145. ch = ( ch << 1 ) + Get_bit();
  1146. }
  1147. }
  1148. ((byte *)outData)[i] = ch; /* Write symbol */
  1149. AddRef( (byte) ch ); /* Increment node */
  1150. }
  1151. compressedSize = bloc >> 3;
  1152. unCompressedSize += i;
  1153. return i;
  1154. }
  1155. /*
  1156. ================
  1157. idCompressor_Huffman::GetCompressionRatio
  1158. ================
  1159. */
  1160. float idCompressor_Huffman::GetCompressionRatio( void ) const {
  1161. return ( unCompressedSize - compressedSize ) * 100.0f / unCompressedSize;
  1162. }
  1163. /*
  1164. =================================================================================
  1165. idCompressor_Arithmetic
  1166. The following algorithm is based on the Arithmetic Coding methods described
  1167. by Mark Nelson. The probability table is implicitly stored.
  1168. =================================================================================
  1169. */
  1170. const int AC_WORD_LENGTH = 8;
  1171. const int AC_NUM_BITS = 16;
  1172. const int AC_MSB_SHIFT = 15;
  1173. const int AC_MSB2_SHIFT = 14;
  1174. const int AC_MSB_MASK = 0x8000;
  1175. const int AC_MSB2_MASK = 0x4000;
  1176. const int AC_HIGH_INIT = 0xffff;
  1177. const int AC_LOW_INIT = 0x0000;
  1178. class idCompressor_Arithmetic : public idCompressor_BitStream {
  1179. public:
  1180. idCompressor_Arithmetic( void ) {}
  1181. void Init( idFile *f, bool compress, int wordLength );
  1182. void FinishCompress( void );
  1183. int Write( const void *inData, int inLength );
  1184. int Read( void *outData, int outLength );
  1185. private:
  1186. typedef struct acProbs_s {
  1187. unsigned int low;
  1188. unsigned int high;
  1189. } acProbs_t;
  1190. typedef struct acSymbol_s {
  1191. unsigned int low;
  1192. unsigned int high;
  1193. int position;
  1194. } acSymbol_t;
  1195. acProbs_t probabilities[1<<AC_WORD_LENGTH];
  1196. int symbolBuffer;
  1197. int symbolBit;
  1198. unsigned short low;
  1199. unsigned short high;
  1200. unsigned short code;
  1201. unsigned int underflowBits;
  1202. unsigned int scale;
  1203. private:
  1204. void InitProbabilities( void );
  1205. void UpdateProbabilities( acSymbol_t* symbol );
  1206. int ProbabilityForCount( unsigned int count );
  1207. void CharToSymbol( byte c, acSymbol_t* symbol );
  1208. void EncodeSymbol( acSymbol_t* symbol );
  1209. int SymbolFromCount( unsigned int count, acSymbol_t* symbol );
  1210. int GetCurrentCount( void );
  1211. void RemoveSymbolFromStream( acSymbol_t* symbol );
  1212. void PutBit( int bit );
  1213. int GetBit( void );
  1214. void WriteOverflowBits( void );
  1215. };
  1216. /*
  1217. ================
  1218. idCompressor_Arithmetic::Init
  1219. ================
  1220. */
  1221. void idCompressor_Arithmetic::Init( idFile *f, bool compress, int wordLength ) {
  1222. idCompressor_BitStream::Init( f, compress, wordLength );
  1223. symbolBuffer = 0;
  1224. symbolBit = 0;
  1225. }
  1226. /*
  1227. ================
  1228. idCompressor_Arithmetic::InitProbabilities
  1229. ================
  1230. */
  1231. void idCompressor_Arithmetic::InitProbabilities( void ) {
  1232. high = AC_HIGH_INIT;
  1233. low = AC_LOW_INIT;
  1234. underflowBits = 0;
  1235. code = 0;
  1236. for( int i = 0; i < (1<<AC_WORD_LENGTH); i++ ) {
  1237. probabilities[ i ].low = i;
  1238. probabilities[ i ].high = i + 1;
  1239. }
  1240. scale = (1<<AC_WORD_LENGTH);
  1241. }
  1242. /*
  1243. ================
  1244. idCompressor_Arithmetic::UpdateProbabilities
  1245. ================
  1246. */
  1247. void idCompressor_Arithmetic::UpdateProbabilities( acSymbol_t* symbol ) {
  1248. int i, x;
  1249. x = symbol->position;
  1250. probabilities[ x ].high++;
  1251. for( i = x + 1; i < (1<<AC_WORD_LENGTH); i++ ) {
  1252. probabilities[ i ].low++;
  1253. probabilities[ i ].high++;
  1254. }
  1255. scale++;
  1256. }
  1257. /*
  1258. ================
  1259. idCompressor_Arithmetic::GetCurrentCount
  1260. ================
  1261. */
  1262. int idCompressor_Arithmetic::GetCurrentCount( void ) {
  1263. return (unsigned int) ( ( ( ( (long) code - low ) + 1 ) * scale - 1 ) / ( ( (long) high - low ) + 1 ) );
  1264. }
  1265. /*
  1266. ================
  1267. idCompressor_Arithmetic::ProbabilityForCount
  1268. ================
  1269. */
  1270. int idCompressor_Arithmetic::ProbabilityForCount( unsigned int count ) {
  1271. #if 1
  1272. int len, mid, offset, res;
  1273. len = (1<<AC_WORD_LENGTH);
  1274. mid = len;
  1275. offset = 0;
  1276. res = 0;
  1277. while( mid > 0 ) {
  1278. mid = len >> 1;
  1279. if ( count >= probabilities[offset+mid].high ) {
  1280. offset += mid;
  1281. len -= mid;
  1282. res = 1;
  1283. }
  1284. else if ( count < probabilities[offset+mid].low ) {
  1285. len -= mid;
  1286. res = 0;
  1287. } else {
  1288. return offset+mid;
  1289. }
  1290. }
  1291. return offset+res;
  1292. #else
  1293. int j;
  1294. for( j = 0; j < (1<<AC_WORD_LENGTH); j++ ) {
  1295. if ( count >= probabilities[ j ].low && count < probabilities[ j ].high ) {
  1296. return j;
  1297. }
  1298. }
  1299. assert( false );
  1300. return 0;
  1301. #endif
  1302. }
  1303. /*
  1304. ================
  1305. idCompressor_Arithmetic::SymbolFromCount
  1306. ================
  1307. */
  1308. int idCompressor_Arithmetic::SymbolFromCount( unsigned int count, acSymbol_t* symbol ) {
  1309. int p = ProbabilityForCount( count );
  1310. symbol->low = probabilities[ p ].low;
  1311. symbol->high = probabilities[ p ].high;
  1312. symbol->position = p;
  1313. return p;
  1314. }
  1315. /*
  1316. ================
  1317. idCompressor_Arithmetic::RemoveSymbolFromStream
  1318. ================
  1319. */
  1320. void idCompressor_Arithmetic::RemoveSymbolFromStream( acSymbol_t* symbol ) {
  1321. long range;
  1322. range = ( long )( high - low ) + 1;
  1323. high = low + ( unsigned short )( ( range * symbol->high ) / scale - 1 );
  1324. low = low + ( unsigned short )( ( range * symbol->low ) / scale );
  1325. while( true ) {
  1326. if ( ( high & AC_MSB_MASK ) == ( low & AC_MSB_MASK ) ) {
  1327. } else if( ( low & AC_MSB2_MASK ) == AC_MSB2_MASK && ( high & AC_MSB2_MASK ) == 0 ) {
  1328. code ^= AC_MSB2_MASK;
  1329. low &= AC_MSB2_MASK - 1;
  1330. high |= AC_MSB2_MASK;
  1331. } else {
  1332. UpdateProbabilities( symbol );
  1333. return;
  1334. }
  1335. low <<= 1;
  1336. high <<= 1;
  1337. high |= 1;
  1338. code <<= 1;
  1339. code |= ReadBits( 1 );
  1340. }
  1341. }
  1342. /*
  1343. ================
  1344. idCompressor_Arithmetic::GetBit
  1345. ================
  1346. */
  1347. int idCompressor_Arithmetic::GetBit( void ) {
  1348. int getbit;
  1349. if( symbolBit <= 0 ) {
  1350. // read a new symbol out
  1351. acSymbol_t symbol;
  1352. symbolBuffer = SymbolFromCount( GetCurrentCount(), &symbol );
  1353. RemoveSymbolFromStream( &symbol );
  1354. symbolBit = AC_WORD_LENGTH;
  1355. }
  1356. getbit = ( symbolBuffer >> ( AC_WORD_LENGTH - symbolBit ) ) & 1;
  1357. symbolBit--;
  1358. return getbit;
  1359. }
  1360. /*
  1361. ================
  1362. idCompressor_Arithmetic::EncodeSymbol
  1363. ================
  1364. */
  1365. void idCompressor_Arithmetic::EncodeSymbol( acSymbol_t* symbol ) {
  1366. unsigned int range;
  1367. // rescale high and low for the new symbol.
  1368. range = ( high - low ) + 1;
  1369. high = low + ( unsigned short )(( range * symbol->high ) / scale - 1 );
  1370. low = low + ( unsigned short )(( range * symbol->low ) / scale );
  1371. while( true ) {
  1372. if ( ( high & AC_MSB_MASK ) == ( low & AC_MSB_MASK ) ) {
  1373. // the high digits of low and high have converged, and can be written to the stream
  1374. WriteBits( high >> AC_MSB_SHIFT, 1 );
  1375. while( underflowBits > 0 ) {
  1376. WriteBits( ~high >> AC_MSB_SHIFT, 1 );
  1377. underflowBits--;
  1378. }
  1379. } else if ( ( low & AC_MSB2_MASK ) && !( high & AC_MSB2_MASK ) ) {
  1380. // underflow is in danger of happening, 2nd digits are converging but 1st digits don't match
  1381. underflowBits += 1;
  1382. low &= AC_MSB2_MASK - 1;
  1383. high |= AC_MSB2_MASK;
  1384. } else {
  1385. UpdateProbabilities( symbol );
  1386. return;
  1387. }
  1388. low <<= 1;
  1389. high <<= 1;
  1390. high |= 1;
  1391. }
  1392. }
  1393. /*
  1394. ================
  1395. idCompressor_Arithmetic::CharToSymbol
  1396. ================
  1397. */
  1398. void idCompressor_Arithmetic::CharToSymbol( byte c, acSymbol_t* symbol ) {
  1399. symbol->low = probabilities[ c ].low;
  1400. symbol->high = probabilities[ c ].high;
  1401. symbol->position = c;
  1402. }
  1403. /*
  1404. ================
  1405. idCompressor_Arithmetic::PutBit
  1406. ================
  1407. */
  1408. void idCompressor_Arithmetic::PutBit( int putbit ) {
  1409. symbolBuffer |= ( putbit & 1 ) << symbolBit;
  1410. symbolBit++;
  1411. if( symbolBit >= AC_WORD_LENGTH ) {
  1412. acSymbol_t symbol;
  1413. CharToSymbol( symbolBuffer, &symbol );
  1414. EncodeSymbol( &symbol );
  1415. symbolBit = 0;
  1416. symbolBuffer = 0;
  1417. }
  1418. }
  1419. /*
  1420. ================
  1421. idCompressor_Arithmetic::WriteOverflowBits
  1422. ================
  1423. */
  1424. void idCompressor_Arithmetic::WriteOverflowBits( void ) {
  1425. WriteBits( low >> AC_MSB2_SHIFT, 1 );
  1426. underflowBits++;
  1427. while( underflowBits-- > 0 ) {
  1428. WriteBits( ~low >> AC_MSB2_SHIFT, 1 );
  1429. }
  1430. }
  1431. /*
  1432. ================
  1433. idCompressor_Arithmetic::Write
  1434. ================
  1435. */
  1436. int idCompressor_Arithmetic::Write( const void *inData, int inLength ) {
  1437. int i, j;
  1438. if ( compress == false || inLength <= 0 ) {
  1439. return 0;
  1440. }
  1441. InitCompress( inData, inLength );
  1442. for( i = 0; i < inLength; i++ ) {
  1443. if ( ( readTotalBytes & ( ( 1 << 14 ) - 1 ) ) == 0 ) {
  1444. if ( readTotalBytes ) {
  1445. WriteOverflowBits();
  1446. WriteBits( 0, 15 );
  1447. while( writeBit ) {
  1448. WriteBits( 0, 1 );
  1449. }
  1450. WriteBits( 255, 8 );
  1451. }
  1452. InitProbabilities();
  1453. }
  1454. for ( j = 0; j < 8; j++ ) {
  1455. PutBit( ReadBits( 1 ) );
  1456. }
  1457. }
  1458. return inLength;
  1459. }
  1460. /*
  1461. ================
  1462. idCompressor_Arithmetic::FinishCompress
  1463. ================
  1464. */
  1465. void idCompressor_Arithmetic::FinishCompress( void ) {
  1466. if ( compress == false ) {
  1467. return;
  1468. }
  1469. WriteOverflowBits();
  1470. idCompressor_BitStream::FinishCompress();
  1471. }
  1472. /*
  1473. ================
  1474. idCompressor_Arithmetic::Read
  1475. ================
  1476. */
  1477. int idCompressor_Arithmetic::Read( void *outData, int outLength ) {
  1478. int i, j;
  1479. if ( compress == true || outLength <= 0 ) {
  1480. return 0;
  1481. }
  1482. InitDecompress( outData, outLength );
  1483. for( i = 0; i < outLength && readLength >= 0; i++ ) {
  1484. if ( ( writeTotalBytes & ( ( 1 << 14 ) - 1 ) ) == 0 ) {
  1485. if ( writeTotalBytes ) {
  1486. while( readBit ) {
  1487. ReadBits( 1 );
  1488. }
  1489. while( ReadBits( 8 ) == 0 && readLength > 0 ) {
  1490. }
  1491. }
  1492. InitProbabilities();
  1493. for ( j = 0; j < AC_NUM_BITS; j++ ) {
  1494. code <<= 1;
  1495. code |= ReadBits( 1 );
  1496. }
  1497. }
  1498. for ( j = 0; j < 8; j++ ) {
  1499. WriteBits( GetBit(), 1 );
  1500. }
  1501. }
  1502. return i;
  1503. }
  1504. /*
  1505. =================================================================================
  1506. idCompressor_LZSS
  1507. In 1977 Abraham Lempel and Jacob Ziv presented a dictionary based scheme for
  1508. text compression called LZ77. For any new text LZ77 outputs an offset/length
  1509. pair to previously seen text and the next new byte after the previously seen
  1510. text.
  1511. In 1982 James Storer and Thomas Szymanski presented a modification on the work
  1512. of Lempel and Ziv called LZSS. LZ77 always outputs an offset/length pair, even
  1513. if a match is only one byte long. An offset/length pair usually takes more than
  1514. a single byte to store and the compression is not optimal for small match sizes.
  1515. LZSS uses a bit flag which tells whether the following data is a literal (byte)
  1516. or an offset/length pair.
  1517. The following algorithm is an implementation of LZSS with arbitrary word size.
  1518. =================================================================================
  1519. */
  1520. const int LZSS_BLOCK_SIZE = 65535;
  1521. const int LZSS_HASH_BITS = 10;
  1522. const int LZSS_HASH_SIZE = ( 1 << LZSS_HASH_BITS );
  1523. const int LZSS_HASH_MASK = ( 1 << LZSS_HASH_BITS ) - 1;
  1524. const int LZSS_OFFSET_BITS = 11;
  1525. const int LZSS_LENGTH_BITS = 5;
  1526. class idCompressor_LZSS : public idCompressor_BitStream {
  1527. public:
  1528. idCompressor_LZSS( void ) {}
  1529. void Init( idFile *f, bool compress, int wordLength );
  1530. void FinishCompress( void );
  1531. int Write( const void *inData, int inLength );
  1532. int Read( void *outData, int outLength );
  1533. protected:
  1534. int offsetBits;
  1535. int lengthBits;
  1536. int minMatchWords;
  1537. byte block[LZSS_BLOCK_SIZE];
  1538. int blockSize;
  1539. int blockIndex;
  1540. int hashTable[LZSS_HASH_SIZE];
  1541. int hashNext[LZSS_BLOCK_SIZE * 8];
  1542. protected:
  1543. bool FindMatch( int startWord, int startValue, int &wordOffset, int &numWords );
  1544. void AddToHash( int index, int hash );
  1545. int GetWordFromBlock( int wordOffset ) const;
  1546. virtual void CompressBlock( void );
  1547. virtual void DecompressBlock( void );
  1548. };
  1549. /*
  1550. ================
  1551. idCompressor_LZSS::Init
  1552. ================
  1553. */
  1554. void idCompressor_LZSS::Init( idFile *f, bool compress, int wordLength ) {
  1555. idCompressor_BitStream::Init( f, compress, wordLength );
  1556. offsetBits = LZSS_OFFSET_BITS;
  1557. lengthBits = LZSS_LENGTH_BITS;
  1558. minMatchWords = ( offsetBits + lengthBits + wordLength ) / wordLength;
  1559. blockSize = 0;
  1560. blockIndex = 0;
  1561. }
  1562. /*
  1563. ================
  1564. idCompressor_LZSS::FindMatch
  1565. ================
  1566. */
  1567. bool idCompressor_LZSS::FindMatch( int startWord, int startValue, int &wordOffset, int &numWords ) {
  1568. int i, n, hash, bottom, maxBits;
  1569. wordOffset = startWord;
  1570. numWords = minMatchWords - 1;
  1571. bottom = Max( 0, startWord - ( ( 1 << offsetBits ) - 1 ) );
  1572. maxBits = ( blockSize << 3 ) - startWord * wordLength;
  1573. hash = startValue & LZSS_HASH_MASK;
  1574. for ( i = hashTable[hash]; i >= bottom; i = hashNext[i] ) {
  1575. n = Compare( block, i * wordLength, block, startWord * wordLength, Min( maxBits, ( startWord - i ) * wordLength ) );
  1576. if ( n > numWords * wordLength ) {
  1577. numWords = n / wordLength;
  1578. wordOffset = i;
  1579. if ( numWords > ( ( 1 << lengthBits ) - 1 + minMatchWords ) - 1 ) {
  1580. numWords = ( ( 1 << lengthBits ) - 1 + minMatchWords ) - 1;
  1581. break;
  1582. }
  1583. }
  1584. }
  1585. return ( numWords >= minMatchWords );
  1586. }
  1587. /*
  1588. ================
  1589. idCompressor_LZSS::AddToHash
  1590. ================
  1591. */
  1592. void idCompressor_LZSS::AddToHash( int index, int hash ) {
  1593. hashNext[index] = hashTable[hash];
  1594. hashTable[hash] = index;
  1595. }
  1596. /*
  1597. ================
  1598. idCompressor_LZSS::GetWordFromBlock
  1599. ================
  1600. */
  1601. int idCompressor_LZSS::GetWordFromBlock( int wordOffset ) const {
  1602. int blockBit, blockByte, value, valueBits, get, fraction;
  1603. blockBit = ( wordOffset * wordLength ) & 7;
  1604. blockByte = ( wordOffset * wordLength ) >> 3;
  1605. if ( blockBit != 0 ) {
  1606. blockByte++;
  1607. }
  1608. value = 0;
  1609. valueBits = 0;
  1610. while ( valueBits < wordLength ) {
  1611. if ( blockBit == 0 ) {
  1612. if ( blockByte >= LZSS_BLOCK_SIZE ) {
  1613. return value;
  1614. }
  1615. blockByte++;
  1616. }
  1617. get = 8 - blockBit;
  1618. if ( get > (wordLength - valueBits) ) {
  1619. get = (wordLength - valueBits);
  1620. }
  1621. fraction = block[blockByte - 1];
  1622. fraction >>= blockBit;
  1623. fraction &= ( 1 << get ) - 1;
  1624. value |= fraction << valueBits;
  1625. valueBits += get;
  1626. blockBit = ( blockBit + get ) & 7;
  1627. }
  1628. return value;
  1629. }
  1630. /*
  1631. ================
  1632. idCompressor_LZSS::CompressBlock
  1633. ================
  1634. */
  1635. void idCompressor_LZSS::CompressBlock( void ) {
  1636. int i, startWord, startValue, wordOffset, numWords;
  1637. InitCompress( block, blockSize );
  1638. memset( hashTable, -1, sizeof( hashTable ) );
  1639. memset( hashNext, -1, sizeof( hashNext ) );
  1640. startWord = 0;
  1641. while( readByte < readLength ) {
  1642. startValue = ReadBits( wordLength );
  1643. if ( FindMatch( startWord, startValue, wordOffset, numWords ) ) {
  1644. WriteBits( 1, 1 );
  1645. WriteBits( startWord - wordOffset, offsetBits );
  1646. WriteBits( numWords - minMatchWords, lengthBits );
  1647. UnreadBits( wordLength );
  1648. for ( i = 0; i < numWords; i++ ) {
  1649. startValue = ReadBits( wordLength );
  1650. AddToHash( startWord, startValue & LZSS_HASH_MASK );
  1651. startWord++;
  1652. }
  1653. } else {
  1654. WriteBits( 0, 1 );
  1655. WriteBits( startValue, wordLength );
  1656. AddToHash( startWord, startValue & LZSS_HASH_MASK );
  1657. startWord++;
  1658. }
  1659. }
  1660. blockSize = 0;
  1661. }
  1662. /*
  1663. ================
  1664. idCompressor_LZSS::DecompressBlock
  1665. ================
  1666. */
  1667. void idCompressor_LZSS::DecompressBlock( void ) {
  1668. int i, offset, startWord, numWords;
  1669. InitDecompress( block, LZSS_BLOCK_SIZE );
  1670. startWord = 0;
  1671. while( writeByte < writeLength && readLength >= 0 ) {
  1672. if ( ReadBits( 1 ) ) {
  1673. offset = startWord - ReadBits( offsetBits );
  1674. numWords = ReadBits( lengthBits ) + minMatchWords;
  1675. for ( i = 0; i < numWords; i++ ) {
  1676. WriteBits( GetWordFromBlock( offset + i ), wordLength );
  1677. startWord++;
  1678. }
  1679. } else {
  1680. WriteBits( ReadBits( wordLength ), wordLength );
  1681. startWord++;
  1682. }
  1683. }
  1684. blockSize = Min( writeByte, LZSS_BLOCK_SIZE );
  1685. }
  1686. /*
  1687. ================
  1688. idCompressor_LZSS::Write
  1689. ================
  1690. */
  1691. int idCompressor_LZSS::Write( const void *inData, int inLength ) {
  1692. int i, n;
  1693. if ( compress == false || inLength <= 0 ) {
  1694. return 0;
  1695. }
  1696. for ( n = i = 0; i < inLength; i += n ) {
  1697. n = LZSS_BLOCK_SIZE - blockSize;
  1698. if ( inLength - i >= n ) {
  1699. memcpy( block + blockSize, ((const byte *)inData) + i, n );
  1700. blockSize = LZSS_BLOCK_SIZE;
  1701. CompressBlock();
  1702. blockSize = 0;
  1703. } else {
  1704. memcpy( block + blockSize, ((const byte *)inData) + i, inLength - i );
  1705. n = inLength - i;
  1706. blockSize += n;
  1707. }
  1708. }
  1709. return inLength;
  1710. }
  1711. /*
  1712. ================
  1713. idCompressor_LZSS::FinishCompress
  1714. ================
  1715. */
  1716. void idCompressor_LZSS::FinishCompress( void ) {
  1717. if ( compress == false ) {
  1718. return;
  1719. }
  1720. if ( blockSize ) {
  1721. CompressBlock();
  1722. }
  1723. idCompressor_BitStream::FinishCompress();
  1724. }
  1725. /*
  1726. ================
  1727. idCompressor_LZSS::Read
  1728. ================
  1729. */
  1730. int idCompressor_LZSS::Read( void *outData, int outLength ) {
  1731. int i, n;
  1732. if ( compress == true || outLength <= 0 ) {
  1733. return 0;
  1734. }
  1735. if ( !blockSize ) {
  1736. DecompressBlock();
  1737. }
  1738. for ( n = i = 0; i < outLength; i += n ) {
  1739. if ( !blockSize ) {
  1740. return i;
  1741. }
  1742. n = blockSize - blockIndex;
  1743. if ( outLength - i >= n ) {
  1744. memcpy( ((byte *)outData) + i, block + blockIndex, n );
  1745. DecompressBlock();
  1746. blockIndex = 0;
  1747. } else {
  1748. memcpy( ((byte *)outData) + i, block + blockIndex, outLength - i );
  1749. n = outLength - i;
  1750. blockIndex += n;
  1751. }
  1752. }
  1753. return outLength;
  1754. }
  1755. /*
  1756. =================================================================================
  1757. idCompressor_LZSS_WordAligned
  1758. Outputs word aligned compressed data.
  1759. =================================================================================
  1760. */
  1761. class idCompressor_LZSS_WordAligned : public idCompressor_LZSS {
  1762. public:
  1763. idCompressor_LZSS_WordAligned( void ) {}
  1764. void Init( idFile *f, bool compress, int wordLength );
  1765. private:
  1766. virtual void CompressBlock( void );
  1767. virtual void DecompressBlock( void );
  1768. };
  1769. /*
  1770. ================
  1771. idCompressor_LZSS_WordAligned::Init
  1772. ================
  1773. */
  1774. void idCompressor_LZSS_WordAligned::Init( idFile *f, bool compress, int wordLength ) {
  1775. idCompressor_LZSS::Init( f, compress, wordLength );
  1776. offsetBits = 2 * wordLength;
  1777. lengthBits = wordLength;
  1778. minMatchWords = ( offsetBits + lengthBits + wordLength ) / wordLength;
  1779. blockSize = 0;
  1780. blockIndex = 0;
  1781. }
  1782. /*
  1783. ================
  1784. idCompressor_LZSS_WordAligned::CompressBlock
  1785. ================
  1786. */
  1787. void idCompressor_LZSS_WordAligned::CompressBlock( void ) {
  1788. int i, startWord, startValue, wordOffset, numWords;
  1789. InitCompress( block, blockSize );
  1790. memset( hashTable, -1, sizeof( hashTable ) );
  1791. memset( hashNext, -1, sizeof( hashNext ) );
  1792. startWord = 0;
  1793. while( readByte < readLength ) {
  1794. startValue = ReadBits( wordLength );
  1795. if ( FindMatch( startWord, startValue, wordOffset, numWords ) ) {
  1796. WriteBits( numWords - ( minMatchWords - 1 ), lengthBits );
  1797. WriteBits( startWord - wordOffset, offsetBits );
  1798. UnreadBits( wordLength );
  1799. for ( i = 0; i < numWords; i++ ) {
  1800. startValue = ReadBits( wordLength );
  1801. AddToHash( startWord, startValue & LZSS_HASH_MASK );
  1802. startWord++;
  1803. }
  1804. } else {
  1805. WriteBits( 0, lengthBits );
  1806. WriteBits( startValue, wordLength );
  1807. AddToHash( startWord, startValue & LZSS_HASH_MASK );
  1808. startWord++;
  1809. }
  1810. }
  1811. blockSize = 0;
  1812. }
  1813. /*
  1814. ================
  1815. idCompressor_LZSS_WordAligned::DecompressBlock
  1816. ================
  1817. */
  1818. void idCompressor_LZSS_WordAligned::DecompressBlock( void ) {
  1819. int i, offset, startWord, numWords;
  1820. InitDecompress( block, LZSS_BLOCK_SIZE );
  1821. startWord = 0;
  1822. while( writeByte < writeLength && readLength >= 0 ) {
  1823. numWords = ReadBits( lengthBits );
  1824. if ( numWords ) {
  1825. numWords += ( minMatchWords - 1 );
  1826. offset = startWord - ReadBits( offsetBits );
  1827. for ( i = 0; i < numWords; i++ ) {
  1828. WriteBits( GetWordFromBlock( offset + i ), wordLength );
  1829. startWord++;
  1830. }
  1831. } else {
  1832. WriteBits( ReadBits( wordLength ), wordLength );
  1833. startWord++;
  1834. }
  1835. }
  1836. blockSize = Min( writeByte, LZSS_BLOCK_SIZE );
  1837. }
  1838. /*
  1839. =================================================================================
  1840. idCompressor_LZW
  1841. http://www.unisys.com/about__unisys/lzw
  1842. http://www.dogma.net/markn/articles/lzw/lzw.htm
  1843. http://www.cs.cf.ac.uk/Dave/Multimedia/node214.html
  1844. http://www.cs.duke.edu/csed/curious/compression/lzw.html
  1845. http://oldwww.rasip.fer.hr/research/compress/algorithms/fund/lz/lzw.html
  1846. This is the same compression scheme used by GIF with the exception that
  1847. the EOI and clear codes are not explicitly stored. Instead EOI happens
  1848. when the input stream runs dry and CC happens when the table gets to big.
  1849. This is a derivation of LZ78, but the dictionary starts with all single
  1850. character values so only code words are output. It is similar in theory
  1851. to LZ77, but instead of using the previous X bytes as a lookup table, a table
  1852. is built as the stream is read. The compressor and decompressor use the
  1853. same formula, so the tables should be exactly alike. The only catch is the
  1854. decompressor is always one step behind the compressor and may get a code not
  1855. yet in the table. In this case, it is easy to determine what the next code
  1856. is going to be (it will be the previous string plus the first byte of the
  1857. previous string).
  1858. The dictionary can be any size, but 12 bits seems to produce best results for
  1859. most sample data. The code size is variable. It starts with the minimum
  1860. number of bits required to store the dictionary and automatically increases
  1861. as the dictionary gets bigger (it starts at 9 bits and grows to 10 bits when
  1862. item 512 is added, 11 bits when 1024 is added, etc...) once the the dictionary
  1863. is filled (4096 items for a 12 bit dictionary), the whole thing is cleared and
  1864. the process starts over again.
  1865. The compressor increases the bit size after it adds the item, while the
  1866. decompressor does before it adds the item. The difference is subtle, but
  1867. it's because decompressor being one step behind. Otherwise, the decompressor
  1868. would read 512 with only 9 bits.
  1869. If "Hello" is in the dictionary, then "Hell", "Hel", "He" and "H" will be too.
  1870. We use this to our advantage by storing the index of the previous code, and
  1871. the value of the last character. This means when we traverse through the
  1872. dictionary, we get the characters in reverse.
  1873. Dictionary entries 0-255 are always going to have the values 0-255
  1874. =================================================================================
  1875. */
  1876. class idCompressor_LZW : public idCompressor_BitStream {
  1877. public:
  1878. idCompressor_LZW( void ) {}
  1879. void Init( idFile *f, bool compress, int wordLength );
  1880. void FinishCompress( void );
  1881. int Write( const void *inData, int inLength );
  1882. int Read( void *outData, int outLength );
  1883. protected:
  1884. int AddToDict( int w, int k );
  1885. int Lookup( int w, int k );
  1886. bool BumpBits();
  1887. int WriteChain( int code );
  1888. void DecompressBlock();
  1889. static const int LZW_BLOCK_SIZE = 32767;
  1890. static const int LZW_START_BITS = 9;
  1891. static const int LZW_FIRST_CODE = (1 << (LZW_START_BITS-1));
  1892. static const int LZW_DICT_BITS = 12;
  1893. static const int LZW_DICT_SIZE = 1 << LZW_DICT_BITS;
  1894. // Dictionary data
  1895. struct {
  1896. int k;
  1897. int w;
  1898. } dictionary[LZW_DICT_SIZE];
  1899. idHashIndex index;
  1900. int nextCode;
  1901. int codeBits;
  1902. // Block data
  1903. byte block[LZW_BLOCK_SIZE];
  1904. int blockSize;
  1905. int blockIndex;
  1906. // Used by the compressor
  1907. int w;
  1908. // Used by the decompressor
  1909. int oldCode;
  1910. };
  1911. /*
  1912. ================
  1913. idCompressor_LZW::Init
  1914. ================
  1915. */
  1916. void idCompressor_LZW::Init( idFile *f, bool compress, int wordLength ) {
  1917. idCompressor_BitStream::Init( f, compress, wordLength );
  1918. for ( int i=0; i<LZW_FIRST_CODE; i++ ) {
  1919. dictionary[i].k = i;
  1920. dictionary[i].w = -1;
  1921. }
  1922. index.Clear();
  1923. nextCode = LZW_FIRST_CODE;
  1924. codeBits = LZW_START_BITS;
  1925. blockSize = 0;
  1926. blockIndex = 0;
  1927. w = -1;
  1928. oldCode = -1;
  1929. }
  1930. /*
  1931. ================
  1932. idCompressor_LZW::Read
  1933. ================
  1934. */
  1935. int idCompressor_LZW::Read( void *outData, int outLength ) {
  1936. int i, n;
  1937. if ( compress == true || outLength <= 0 ) {
  1938. return 0;
  1939. }
  1940. if ( !blockSize ) {
  1941. DecompressBlock();
  1942. }
  1943. for ( n = i = 0; i < outLength; i += n ) {
  1944. if ( !blockSize ) {
  1945. return i;
  1946. }
  1947. n = blockSize - blockIndex;
  1948. if ( outLength - i >= n ) {
  1949. memcpy( ((byte *)outData) + i, block + blockIndex, n );
  1950. DecompressBlock();
  1951. blockIndex = 0;
  1952. } else {
  1953. memcpy( ((byte *)outData) + i, block + blockIndex, outLength - i );
  1954. n = outLength - i;
  1955. blockIndex += n;
  1956. }
  1957. }
  1958. return outLength;
  1959. }
  1960. /*
  1961. ================
  1962. idCompressor_LZW::Lookup
  1963. ================
  1964. */
  1965. int idCompressor_LZW::Lookup( int w, int k ) {
  1966. int j;
  1967. if ( w == -1 ) {
  1968. return k;
  1969. } else {
  1970. for ( j = index.First( w ^ k ); j >= 0 ; j = index.Next( j ) ) {
  1971. if ( dictionary[ j ].k == k && dictionary[ j ].w == w ) {
  1972. return j;
  1973. }
  1974. }
  1975. }
  1976. return -1;
  1977. }
  1978. /*
  1979. ================
  1980. idCompressor_LZW::AddToDict
  1981. ================
  1982. */
  1983. int idCompressor_LZW::AddToDict( int w, int k ) {
  1984. dictionary[ nextCode ].k = k;
  1985. dictionary[ nextCode ].w = w;
  1986. index.Add( w ^ k, nextCode );
  1987. return nextCode++;
  1988. }
  1989. /*
  1990. ================
  1991. idCompressor_LZW::BumpBits
  1992. Possibly increments codeBits
  1993. Returns true if the dictionary was cleared
  1994. ================
  1995. */
  1996. bool idCompressor_LZW::BumpBits() {
  1997. if ( nextCode == ( 1 << codeBits ) ) {
  1998. codeBits ++;
  1999. if ( codeBits > LZW_DICT_BITS ) {
  2000. nextCode = LZW_FIRST_CODE;
  2001. codeBits = LZW_START_BITS;
  2002. index.Clear();
  2003. return true;
  2004. }
  2005. }
  2006. return false;
  2007. }
  2008. /*
  2009. ================
  2010. idCompressor_LZW::FinishCompress
  2011. ================
  2012. */
  2013. void idCompressor_LZW::FinishCompress( void ) {
  2014. WriteBits( w, codeBits );
  2015. idCompressor_BitStream::FinishCompress();
  2016. }
  2017. /*
  2018. ================
  2019. idCompressor_LZW::Write
  2020. ================
  2021. */
  2022. int idCompressor_LZW::Write( const void *inData, int inLength ) {
  2023. int i;
  2024. InitCompress( inData, inLength );
  2025. for ( i = 0; i < inLength; i++ ) {
  2026. int k = ReadBits( 8 );
  2027. int code = Lookup(w, k);
  2028. if ( code >= 0 ) {
  2029. w = code;
  2030. } else {
  2031. WriteBits( w, codeBits );
  2032. if ( !BumpBits() ) {
  2033. AddToDict( w, k );
  2034. }
  2035. w = k;
  2036. }
  2037. }
  2038. return inLength;
  2039. }
  2040. /*
  2041. ================
  2042. idCompressor_LZW::WriteChain
  2043. The chain is stored backwards, so we have to write it to a buffer then output the buffer in reverse
  2044. ================
  2045. */
  2046. int idCompressor_LZW::WriteChain( int code ) {
  2047. byte chain[LZW_DICT_SIZE];
  2048. int firstChar = 0;
  2049. int i = 0;
  2050. do {
  2051. assert( i < LZW_DICT_SIZE-1 && code >= 0 );
  2052. chain[i++] = dictionary[code].k;
  2053. code = dictionary[code].w;
  2054. } while ( code >= 0 );
  2055. firstChar = chain[--i];
  2056. for ( ; i >= 0; i-- ) {
  2057. WriteBits( chain[i], 8 );
  2058. }
  2059. return firstChar;
  2060. }
  2061. /*
  2062. ================
  2063. idCompressor_LZW::DecompressBlock
  2064. ================
  2065. */
  2066. void idCompressor_LZW::DecompressBlock() {
  2067. int code, firstChar;
  2068. InitDecompress( block, LZW_BLOCK_SIZE );
  2069. while( writeByte < writeLength - LZW_DICT_SIZE && readLength > 0 ) {
  2070. assert( codeBits <= LZW_DICT_BITS );
  2071. code = ReadBits( codeBits );
  2072. if ( readLength == 0 ) {
  2073. break;
  2074. }
  2075. if ( oldCode == -1 ) {
  2076. assert( code < 256 );
  2077. WriteBits( code, 8 );
  2078. oldCode = code;
  2079. firstChar = code;
  2080. continue;
  2081. }
  2082. if ( code >= nextCode ) {
  2083. assert( code == nextCode );
  2084. firstChar = WriteChain( oldCode );
  2085. WriteBits( firstChar, 8 );
  2086. } else {
  2087. firstChar = WriteChain( code );
  2088. }
  2089. AddToDict( oldCode, firstChar );
  2090. if ( BumpBits() ) {
  2091. oldCode = -1;
  2092. } else {
  2093. oldCode = code;
  2094. }
  2095. }
  2096. blockSize = Min( writeByte, LZW_BLOCK_SIZE );
  2097. }
  2098. /*
  2099. =================================================================================
  2100. idCompressor
  2101. =================================================================================
  2102. */
  2103. /*
  2104. ================
  2105. idCompressor::AllocNoCompression
  2106. ================
  2107. */
  2108. idCompressor * idCompressor::AllocNoCompression( void ) {
  2109. return new idCompressor_None();
  2110. }
  2111. /*
  2112. ================
  2113. idCompressor::AllocBitStream
  2114. ================
  2115. */
  2116. idCompressor * idCompressor::AllocBitStream( void ) {
  2117. return new idCompressor_BitStream();
  2118. }
  2119. /*
  2120. ================
  2121. idCompressor::AllocRunLength
  2122. ================
  2123. */
  2124. idCompressor * idCompressor::AllocRunLength( void ) {
  2125. return new idCompressor_RunLength();
  2126. }
  2127. /*
  2128. ================
  2129. idCompressor::AllocRunLength_ZeroBased
  2130. ================
  2131. */
  2132. idCompressor * idCompressor::AllocRunLength_ZeroBased( void ) {
  2133. return new idCompressor_RunLength_ZeroBased();
  2134. }
  2135. /*
  2136. ================
  2137. idCompressor::AllocHuffman
  2138. ================
  2139. */
  2140. idCompressor * idCompressor::AllocHuffman( void ) {
  2141. return new idCompressor_Huffman();
  2142. }
  2143. /*
  2144. ================
  2145. idCompressor::AllocArithmetic
  2146. ================
  2147. */
  2148. idCompressor * idCompressor::AllocArithmetic( void ) {
  2149. return new idCompressor_Arithmetic();
  2150. }
  2151. /*
  2152. ================
  2153. idCompressor::AllocLZSS
  2154. ================
  2155. */
  2156. idCompressor * idCompressor::AllocLZSS( void ) {
  2157. return new idCompressor_LZSS();
  2158. }
  2159. /*
  2160. ================
  2161. idCompressor::AllocLZSS_WordAligned
  2162. ================
  2163. */
  2164. idCompressor * idCompressor::AllocLZSS_WordAligned( void ) {
  2165. return new idCompressor_LZSS_WordAligned();
  2166. }
  2167. /*
  2168. ================
  2169. idCompressor::AllocLZW
  2170. ================
  2171. */
  2172. idCompressor * idCompressor::AllocLZW( void ) {
  2173. return new idCompressor_LZW();
  2174. }