Compressor.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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();
  30. void Init( idFile *f, bool compress, int wordLength );
  31. void FinishCompress();
  32. float GetCompressionRatio() const;
  33. const char * GetName();
  34. const char * GetFullPath();
  35. int Read( void *outData, int outLength );
  36. int Write( const void *inData, int inLength );
  37. int Length();
  38. ID_TIME_T Timestamp();
  39. int Tell();
  40. void ForceFlush();
  41. void Flush();
  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() {
  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() {
  71. }
  72. /*
  73. ================
  74. idCompressor_None::GetCompressionRatio
  75. ================
  76. */
  77. float idCompressor_None::GetCompressionRatio() const {
  78. return 0.0f;
  79. }
  80. /*
  81. ================
  82. idCompressor_None::GetName
  83. ================
  84. */
  85. const char *idCompressor_None::GetName() {
  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() {
  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() {
  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() {
  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() {
  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() {
  168. if ( file ) {
  169. file->ForceFlush();
  170. }
  171. }
  172. /*
  173. ================
  174. idCompressor_None::Flush
  175. ================
  176. */
  177. void idCompressor_None::Flush() {
  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() {}
  200. void Init( idFile *f, bool compress, int wordLength );
  201. void FinishCompress();
  202. float GetCompressionRatio() 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() {
  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() 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() {}
  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() {}
  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() {}
  692. void Init( idFile *f, bool compress, int wordLength );
  693. void FinishCompress();
  694. float GetCompressionRatio() 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. } else {
  763. // Initialize the tree & list with the NYT node
  764. tree = lhead = ltail = loc[NYT] = &nodeList[blocNode++];
  765. tree->symbol = NYT;
  766. tree->weight = 0;
  767. lhead->next = lhead->prev = NULL;
  768. tree->parent = tree->left = tree->right = NULL;
  769. }
  770. }
  771. /*
  772. ================
  773. idCompressor_Huffman::PutBit
  774. ================
  775. */
  776. void idCompressor_Huffman::PutBit( int bit, byte *fout, int *offset) {
  777. bloc = *offset;
  778. if ( (bloc&7) == 0 ) {
  779. fout[(bloc>>3)] = 0;
  780. }
  781. fout[(bloc>>3)] |= bit << (bloc&7);
  782. bloc++;
  783. *offset = bloc;
  784. }
  785. /*
  786. ================
  787. idCompressor_Huffman::GetBit
  788. ================
  789. */
  790. int idCompressor_Huffman::GetBit( byte *fin, int *offset) {
  791. int t;
  792. bloc = *offset;
  793. t = (fin[(bloc>>3)] >> (bloc&7)) & 0x1;
  794. bloc++;
  795. *offset = bloc;
  796. return t;
  797. }
  798. /*
  799. ================
  800. idCompressor_Huffman::Add_bit
  801. Add a bit to the output file (buffered)
  802. ================
  803. */
  804. void idCompressor_Huffman::Add_bit( char bit, byte *fout ) {
  805. if ( (bloc&7) == 0 ) {
  806. fout[(bloc>>3)] = 0;
  807. }
  808. fout[(bloc>>3)] |= bit << (bloc&7);
  809. bloc++;
  810. }
  811. /*
  812. ================
  813. idCompressor_Huffman::Get_bit
  814. Get one bit from the input file (buffered)
  815. ================
  816. */
  817. int idCompressor_Huffman::Get_bit() {
  818. int t;
  819. int wh = bloc >> 3;
  820. int whb = wh>> 16;
  821. if ( whb != blocIn ) {
  822. blocMax += file->Read( seq, sizeof( seq ) );
  823. blocIn++;
  824. }
  825. wh &= 0xffff;
  826. t = ( seq[wh] >> ( bloc & 7 ) ) & 0x1;
  827. bloc++;
  828. return t;
  829. }
  830. /*
  831. ================
  832. idCompressor_Huffman::Get_ppnode
  833. ================
  834. */
  835. huffmanNode_t **idCompressor_Huffman::Get_ppnode() {
  836. huffmanNode_t **tppnode;
  837. if ( !freelist ) {
  838. return &nodePtrs[blocPtrs++];
  839. } else {
  840. tppnode = freelist;
  841. freelist = (huffmanNode_t **)*tppnode;
  842. return tppnode;
  843. }
  844. }
  845. /*
  846. ================
  847. idCompressor_Huffman::Free_ppnode
  848. ================
  849. */
  850. void idCompressor_Huffman::Free_ppnode( huffmanNode_t **ppnode ) {
  851. *ppnode = (huffmanNode_t *)freelist;
  852. freelist = ppnode;
  853. }
  854. /*
  855. ================
  856. idCompressor_Huffman::Swap
  857. Swap the location of the given two nodes in the tree.
  858. ================
  859. */
  860. void idCompressor_Huffman::Swap( huffmanNode_t *node1, huffmanNode_t *node2 ) {
  861. huffmanNode_t *par1, *par2;
  862. par1 = node1->parent;
  863. par2 = node2->parent;
  864. if ( par1 ) {
  865. if ( par1->left == node1 ) {
  866. par1->left = node2;
  867. } else {
  868. par1->right = node2;
  869. }
  870. } else {
  871. tree = node2;
  872. }
  873. if ( par2 ) {
  874. if ( par2->left == node2 ) {
  875. par2->left = node1;
  876. } else {
  877. par2->right = node1;
  878. }
  879. } else {
  880. tree = node1;
  881. }
  882. node1->parent = par2;
  883. node2->parent = par1;
  884. }
  885. /*
  886. ================
  887. idCompressor_Huffman::Swaplist
  888. Swap the given two nodes in the linked list (update ranks)
  889. ================
  890. */
  891. void idCompressor_Huffman::Swaplist( huffmanNode_t *node1, huffmanNode_t *node2 ) {
  892. huffmanNode_t *par1;
  893. par1 = node1->next;
  894. node1->next = node2->next;
  895. node2->next = par1;
  896. par1 = node1->prev;
  897. node1->prev = node2->prev;
  898. node2->prev = par1;
  899. if ( node1->next == node1 ) {
  900. node1->next = node2;
  901. }
  902. if ( node2->next == node2 ) {
  903. node2->next = node1;
  904. }
  905. if ( node1->next ) {
  906. node1->next->prev = node1;
  907. }
  908. if ( node2->next ) {
  909. node2->next->prev = node2;
  910. }
  911. if ( node1->prev ) {
  912. node1->prev->next = node1;
  913. }
  914. if ( node2->prev ) {
  915. node2->prev->next = node2;
  916. }
  917. }
  918. /*
  919. ================
  920. idCompressor_Huffman::Increment
  921. ================
  922. */
  923. void idCompressor_Huffman::Increment( huffmanNode_t *node ) {
  924. huffmanNode_t *lnode;
  925. if ( !node ) {
  926. return;
  927. }
  928. if ( node->next != NULL && node->next->weight == node->weight ) {
  929. lnode = *node->head;
  930. if ( lnode != node->parent ) {
  931. Swap( lnode, node );
  932. }
  933. Swaplist( lnode, node );
  934. }
  935. if ( node->prev && node->prev->weight == node->weight ) {
  936. *node->head = node->prev;
  937. } else {
  938. *node->head = NULL;
  939. Free_ppnode( node->head );
  940. }
  941. node->weight++;
  942. if ( node->next && node->next->weight == node->weight ) {
  943. node->head = node->next->head;
  944. } else {
  945. node->head = Get_ppnode();
  946. *node->head = node;
  947. }
  948. if ( node->parent ) {
  949. Increment( node->parent );
  950. if ( node->prev == node->parent ) {
  951. Swaplist( node, node->parent );
  952. if ( *node->head == node ) {
  953. *node->head = node->parent;
  954. }
  955. }
  956. }
  957. }
  958. /*
  959. ================
  960. idCompressor_Huffman::AddRef
  961. ================
  962. */
  963. void idCompressor_Huffman::AddRef( byte ch ) {
  964. huffmanNode_t *tnode, *tnode2;
  965. if ( loc[ch] == NULL ) { /* if this is the first transmission of this node */
  966. tnode = &nodeList[blocNode++];
  967. tnode2 = &nodeList[blocNode++];
  968. tnode2->symbol = INTERNAL_NODE;
  969. tnode2->weight = 1;
  970. tnode2->next = lhead->next;
  971. if ( lhead->next ) {
  972. lhead->next->prev = tnode2;
  973. if ( lhead->next->weight == 1 ) {
  974. tnode2->head = lhead->next->head;
  975. } else {
  976. tnode2->head = Get_ppnode();
  977. *tnode2->head = tnode2;
  978. }
  979. } else {
  980. tnode2->head = Get_ppnode();
  981. *tnode2->head = tnode2;
  982. }
  983. lhead->next = tnode2;
  984. tnode2->prev = lhead;
  985. tnode->symbol = ch;
  986. tnode->weight = 1;
  987. tnode->next = lhead->next;
  988. if ( lhead->next ) {
  989. lhead->next->prev = tnode;
  990. if ( lhead->next->weight == 1 ) {
  991. tnode->head = lhead->next->head;
  992. } else {
  993. /* this should never happen */
  994. tnode->head = Get_ppnode();
  995. *tnode->head = tnode2;
  996. }
  997. } else {
  998. /* this should never happen */
  999. tnode->head = Get_ppnode();
  1000. *tnode->head = tnode;
  1001. }
  1002. lhead->next = tnode;
  1003. tnode->prev = lhead;
  1004. tnode->left = tnode->right = NULL;
  1005. if ( lhead->parent ) {
  1006. if ( lhead->parent->left == lhead ) { /* lhead is guaranteed to by the NYT */
  1007. lhead->parent->left = tnode2;
  1008. } else {
  1009. lhead->parent->right = tnode2;
  1010. }
  1011. } else {
  1012. tree = tnode2;
  1013. }
  1014. tnode2->right = tnode;
  1015. tnode2->left = lhead;
  1016. tnode2->parent = lhead->parent;
  1017. lhead->parent = tnode->parent = tnode2;
  1018. loc[ch] = tnode;
  1019. Increment( tnode2->parent );
  1020. } else {
  1021. Increment( loc[ch] );
  1022. }
  1023. }
  1024. /*
  1025. ================
  1026. idCompressor_Huffman::Receive
  1027. Get a symbol.
  1028. ================
  1029. */
  1030. int idCompressor_Huffman::Receive( huffmanNode_t *node, int *ch ) {
  1031. while ( node && node->symbol == INTERNAL_NODE ) {
  1032. if ( Get_bit() ) {
  1033. node = node->right;
  1034. } else {
  1035. node = node->left;
  1036. }
  1037. }
  1038. if ( !node ) {
  1039. return 0;
  1040. }
  1041. return (*ch = node->symbol);
  1042. }
  1043. /*
  1044. ================
  1045. idCompressor_Huffman::Send
  1046. Send the prefix code for this node.
  1047. ================
  1048. */
  1049. void idCompressor_Huffman::Send( huffmanNode_t *node, huffmanNode_t *child, byte *fout ) {
  1050. if ( node->parent ) {
  1051. Send( node->parent, node, fout );
  1052. }
  1053. if ( child ) {
  1054. if ( node->right == child ) {
  1055. Add_bit( 1, fout );
  1056. } else {
  1057. Add_bit( 0, fout );
  1058. }
  1059. }
  1060. }
  1061. /*
  1062. ================
  1063. idCompressor_Huffman::Transmit
  1064. Send a symbol.
  1065. ================
  1066. */
  1067. void idCompressor_Huffman::Transmit( int ch, byte *fout ) {
  1068. int i;
  1069. if ( loc[ch] == NULL ) {
  1070. /* huffmanNode_t hasn't been transmitted, send a NYT, then the symbol */
  1071. Transmit( NYT, fout );
  1072. for ( i = 7; i >= 0; i-- ) {
  1073. Add_bit( (char)((ch >> i) & 0x1), fout );
  1074. }
  1075. } else {
  1076. Send( loc[ch], NULL, fout );
  1077. }
  1078. }
  1079. /*
  1080. ================
  1081. idCompressor_Huffman::Write
  1082. ================
  1083. */
  1084. int idCompressor_Huffman::Write( const void *inData, int inLength ) {
  1085. int i, ch;
  1086. if ( compress == false || inLength <= 0 ) {
  1087. return 0;
  1088. }
  1089. for ( i = 0; i < inLength; i++ ) {
  1090. ch = ((const byte *)inData)[i];
  1091. Transmit( ch, seq ); /* Transmit symbol */
  1092. AddRef( (byte)ch ); /* Do update */
  1093. int b = (bloc>>3);
  1094. if ( b > 32768 ) {
  1095. file->Write( seq, b );
  1096. seq[0] = seq[b];
  1097. bloc &= 7;
  1098. compressedSize += b;
  1099. }
  1100. }
  1101. unCompressedSize += i;
  1102. return i;
  1103. }
  1104. /*
  1105. ================
  1106. idCompressor_Huffman::FinishCompress
  1107. ================
  1108. */
  1109. void idCompressor_Huffman::FinishCompress() {
  1110. if ( compress == false ) {
  1111. return;
  1112. }
  1113. bloc += 7;
  1114. int str = (bloc>>3);
  1115. if ( str ) {
  1116. file->Write( seq, str );
  1117. compressedSize += str;
  1118. }
  1119. }
  1120. /*
  1121. ================
  1122. idCompressor_Huffman::Read
  1123. ================
  1124. */
  1125. int idCompressor_Huffman::Read( void *outData, int outLength ) {
  1126. int i, j, ch;
  1127. if ( compress == true || outLength <= 0 ) {
  1128. return 0;
  1129. }
  1130. if ( bloc == 0 ) {
  1131. blocMax = file->Read( seq, sizeof( seq ) );
  1132. blocIn = 0;
  1133. }
  1134. for ( i = 0; i < outLength; i++ ) {
  1135. ch = 0;
  1136. // don't overflow reading from the file
  1137. if ( ( bloc >> 3 ) > blocMax ) {
  1138. break;
  1139. }
  1140. Receive( tree, &ch ); /* Get a character */
  1141. if ( ch == NYT ) { /* We got a NYT, get the symbol associated with it */
  1142. ch = 0;
  1143. for ( j = 0; j < 8; j++ ) {
  1144. ch = ( ch << 1 ) + Get_bit();
  1145. }
  1146. }
  1147. ((byte *)outData)[i] = ch; /* Write symbol */
  1148. AddRef( (byte) ch ); /* Increment node */
  1149. }
  1150. compressedSize = bloc >> 3;
  1151. unCompressedSize += i;
  1152. return i;
  1153. }
  1154. /*
  1155. ================
  1156. idCompressor_Huffman::GetCompressionRatio
  1157. ================
  1158. */
  1159. float idCompressor_Huffman::GetCompressionRatio() const {
  1160. return ( unCompressedSize - compressedSize ) * 100.0f / unCompressedSize;
  1161. }
  1162. /*
  1163. =================================================================================
  1164. idCompressor_Arithmetic
  1165. The following algorithm is based on the Arithmetic Coding methods described
  1166. by Mark Nelson. The probability table is implicitly stored.
  1167. =================================================================================
  1168. */
  1169. const int AC_WORD_LENGTH = 8;
  1170. const int AC_NUM_BITS = 16;
  1171. const int AC_MSB_SHIFT = 15;
  1172. const int AC_MSB2_SHIFT = 14;
  1173. const int AC_MSB_MASK = 0x8000;
  1174. const int AC_MSB2_MASK = 0x4000;
  1175. const int AC_HIGH_INIT = 0xffff;
  1176. const int AC_LOW_INIT = 0x0000;
  1177. class idCompressor_Arithmetic : public idCompressor_BitStream {
  1178. public:
  1179. idCompressor_Arithmetic() {}
  1180. void Init( idFile *f, bool compress, int wordLength );
  1181. void FinishCompress();
  1182. int Write( const void *inData, int inLength );
  1183. int Read( void *outData, int outLength );
  1184. private:
  1185. typedef struct acProbs_s {
  1186. unsigned int low;
  1187. unsigned int high;
  1188. } acProbs_t;
  1189. typedef struct acSymbol_s {
  1190. unsigned int low;
  1191. unsigned int high;
  1192. int position;
  1193. } acSymbol_t;
  1194. acProbs_t probabilities[1<<AC_WORD_LENGTH];
  1195. int symbolBuffer;
  1196. int symbolBit;
  1197. unsigned short low;
  1198. unsigned short high;
  1199. unsigned short code;
  1200. unsigned int underflowBits;
  1201. unsigned int scale;
  1202. private:
  1203. void InitProbabilities();
  1204. void UpdateProbabilities( acSymbol_t* symbol );
  1205. int ProbabilityForCount( unsigned int count );
  1206. void CharToSymbol( byte c, acSymbol_t* symbol );
  1207. void EncodeSymbol( acSymbol_t* symbol );
  1208. int SymbolFromCount( unsigned int count, acSymbol_t* symbol );
  1209. int GetCurrentCount();
  1210. void RemoveSymbolFromStream( acSymbol_t* symbol );
  1211. void PutBit( int bit );
  1212. int GetBit();
  1213. void WriteOverflowBits();
  1214. };
  1215. /*
  1216. ================
  1217. idCompressor_Arithmetic::Init
  1218. ================
  1219. */
  1220. void idCompressor_Arithmetic::Init( idFile *f, bool compress, int wordLength ) {
  1221. idCompressor_BitStream::Init( f, compress, wordLength );
  1222. symbolBuffer = 0;
  1223. symbolBit = 0;
  1224. }
  1225. /*
  1226. ================
  1227. idCompressor_Arithmetic::InitProbabilities
  1228. ================
  1229. */
  1230. void idCompressor_Arithmetic::InitProbabilities() {
  1231. high = AC_HIGH_INIT;
  1232. low = AC_LOW_INIT;
  1233. underflowBits = 0;
  1234. code = 0;
  1235. for( int i = 0; i < (1<<AC_WORD_LENGTH); i++ ) {
  1236. probabilities[ i ].low = i;
  1237. probabilities[ i ].high = i + 1;
  1238. }
  1239. scale = (1<<AC_WORD_LENGTH);
  1240. }
  1241. /*
  1242. ================
  1243. idCompressor_Arithmetic::UpdateProbabilities
  1244. ================
  1245. */
  1246. void idCompressor_Arithmetic::UpdateProbabilities( acSymbol_t* symbol ) {
  1247. int i, x;
  1248. x = symbol->position;
  1249. probabilities[ x ].high++;
  1250. for( i = x + 1; i < (1<<AC_WORD_LENGTH); i++ ) {
  1251. probabilities[ i ].low++;
  1252. probabilities[ i ].high++;
  1253. }
  1254. scale++;
  1255. }
  1256. /*
  1257. ================
  1258. idCompressor_Arithmetic::GetCurrentCount
  1259. ================
  1260. */
  1261. int idCompressor_Arithmetic::GetCurrentCount() {
  1262. return (unsigned int) ( ( ( ( (long) code - low ) + 1 ) * scale - 1 ) / ( ( (long) high - low ) + 1 ) );
  1263. }
  1264. /*
  1265. ================
  1266. idCompressor_Arithmetic::ProbabilityForCount
  1267. ================
  1268. */
  1269. int idCompressor_Arithmetic::ProbabilityForCount( unsigned int count ) {
  1270. #if 1
  1271. int len, mid, offset, res;
  1272. len = (1<<AC_WORD_LENGTH);
  1273. mid = len;
  1274. offset = 0;
  1275. res = 0;
  1276. while( mid > 0 ) {
  1277. mid = len >> 1;
  1278. if ( count >= probabilities[offset+mid].high ) {
  1279. offset += mid;
  1280. len -= mid;
  1281. res = 1;
  1282. }
  1283. else if ( count < probabilities[offset+mid].low ) {
  1284. len -= mid;
  1285. res = 0;
  1286. } else {
  1287. return offset+mid;
  1288. }
  1289. }
  1290. return offset+res;
  1291. #else
  1292. int j;
  1293. for( j = 0; j < (1<<AC_WORD_LENGTH); j++ ) {
  1294. if ( count >= probabilities[ j ].low && count < probabilities[ j ].high ) {
  1295. return j;
  1296. }
  1297. }
  1298. assert( false );
  1299. return 0;
  1300. #endif
  1301. }
  1302. /*
  1303. ================
  1304. idCompressor_Arithmetic::SymbolFromCount
  1305. ================
  1306. */
  1307. int idCompressor_Arithmetic::SymbolFromCount( unsigned int count, acSymbol_t* symbol ) {
  1308. int p = ProbabilityForCount( count );
  1309. symbol->low = probabilities[ p ].low;
  1310. symbol->high = probabilities[ p ].high;
  1311. symbol->position = p;
  1312. return p;
  1313. }
  1314. /*
  1315. ================
  1316. idCompressor_Arithmetic::RemoveSymbolFromStream
  1317. ================
  1318. */
  1319. void idCompressor_Arithmetic::RemoveSymbolFromStream( acSymbol_t* symbol ) {
  1320. long range;
  1321. range = ( long )( high - low ) + 1;
  1322. high = low + ( unsigned short )( ( range * symbol->high ) / scale - 1 );
  1323. low = low + ( unsigned short )( ( range * symbol->low ) / scale );
  1324. while( true ) {
  1325. if ( ( high & AC_MSB_MASK ) == ( low & AC_MSB_MASK ) ) {
  1326. } else if( ( low & AC_MSB2_MASK ) == AC_MSB2_MASK && ( high & AC_MSB2_MASK ) == 0 ) {
  1327. code ^= AC_MSB2_MASK;
  1328. low &= AC_MSB2_MASK - 1;
  1329. high |= AC_MSB2_MASK;
  1330. } else {
  1331. UpdateProbabilities( symbol );
  1332. return;
  1333. }
  1334. low <<= 1;
  1335. high <<= 1;
  1336. high |= 1;
  1337. code <<= 1;
  1338. code |= ReadBits( 1 );
  1339. }
  1340. }
  1341. /*
  1342. ================
  1343. idCompressor_Arithmetic::GetBit
  1344. ================
  1345. */
  1346. int idCompressor_Arithmetic::GetBit() {
  1347. int getbit;
  1348. if( symbolBit <= 0 ) {
  1349. // read a new symbol out
  1350. acSymbol_t symbol;
  1351. symbolBuffer = SymbolFromCount( GetCurrentCount(), &symbol );
  1352. RemoveSymbolFromStream( &symbol );
  1353. symbolBit = AC_WORD_LENGTH;
  1354. }
  1355. getbit = ( symbolBuffer >> ( AC_WORD_LENGTH - symbolBit ) ) & 1;
  1356. symbolBit--;
  1357. return getbit;
  1358. }
  1359. /*
  1360. ================
  1361. idCompressor_Arithmetic::EncodeSymbol
  1362. ================
  1363. */
  1364. void idCompressor_Arithmetic::EncodeSymbol( acSymbol_t* symbol ) {
  1365. unsigned int range;
  1366. // rescale high and low for the new symbol.
  1367. range = ( high - low ) + 1;
  1368. high = low + ( unsigned short )(( range * symbol->high ) / scale - 1 );
  1369. low = low + ( unsigned short )(( range * symbol->low ) / scale );
  1370. while( true ) {
  1371. if ( ( high & AC_MSB_MASK ) == ( low & AC_MSB_MASK ) ) {
  1372. // the high digits of low and high have converged, and can be written to the stream
  1373. WriteBits( high >> AC_MSB_SHIFT, 1 );
  1374. while( underflowBits > 0 ) {
  1375. WriteBits( ~high >> AC_MSB_SHIFT, 1 );
  1376. underflowBits--;
  1377. }
  1378. } else if ( ( low & AC_MSB2_MASK ) && !( high & AC_MSB2_MASK ) ) {
  1379. // underflow is in danger of happening, 2nd digits are converging but 1st digits don't match
  1380. underflowBits += 1;
  1381. low &= AC_MSB2_MASK - 1;
  1382. high |= AC_MSB2_MASK;
  1383. } else {
  1384. UpdateProbabilities( symbol );
  1385. return;
  1386. }
  1387. low <<= 1;
  1388. high <<= 1;
  1389. high |= 1;
  1390. }
  1391. }
  1392. /*
  1393. ================
  1394. idCompressor_Arithmetic::CharToSymbol
  1395. ================
  1396. */
  1397. void idCompressor_Arithmetic::CharToSymbol( byte c, acSymbol_t* symbol ) {
  1398. symbol->low = probabilities[ c ].low;
  1399. symbol->high = probabilities[ c ].high;
  1400. symbol->position = c;
  1401. }
  1402. /*
  1403. ================
  1404. idCompressor_Arithmetic::PutBit
  1405. ================
  1406. */
  1407. void idCompressor_Arithmetic::PutBit( int putbit ) {
  1408. symbolBuffer |= ( putbit & 1 ) << symbolBit;
  1409. symbolBit++;
  1410. if( symbolBit >= AC_WORD_LENGTH ) {
  1411. acSymbol_t symbol;
  1412. CharToSymbol( symbolBuffer, &symbol );
  1413. EncodeSymbol( &symbol );
  1414. symbolBit = 0;
  1415. symbolBuffer = 0;
  1416. }
  1417. }
  1418. /*
  1419. ================
  1420. idCompressor_Arithmetic::WriteOverflowBits
  1421. ================
  1422. */
  1423. void idCompressor_Arithmetic::WriteOverflowBits() {
  1424. WriteBits( low >> AC_MSB2_SHIFT, 1 );
  1425. underflowBits++;
  1426. while( underflowBits-- > 0 ) {
  1427. WriteBits( ~low >> AC_MSB2_SHIFT, 1 );
  1428. }
  1429. }
  1430. /*
  1431. ================
  1432. idCompressor_Arithmetic::Write
  1433. ================
  1434. */
  1435. int idCompressor_Arithmetic::Write( const void *inData, int inLength ) {
  1436. int i, j;
  1437. if ( compress == false || inLength <= 0 ) {
  1438. return 0;
  1439. }
  1440. InitCompress( inData, inLength );
  1441. for( i = 0; i < inLength; i++ ) {
  1442. if ( ( readTotalBytes & ( ( 1 << 14 ) - 1 ) ) == 0 ) {
  1443. if ( readTotalBytes ) {
  1444. WriteOverflowBits();
  1445. WriteBits( 0, 15 );
  1446. while( writeBit ) {
  1447. WriteBits( 0, 1 );
  1448. }
  1449. WriteBits( 255, 8 );
  1450. }
  1451. InitProbabilities();
  1452. }
  1453. for ( j = 0; j < 8; j++ ) {
  1454. PutBit( ReadBits( 1 ) );
  1455. }
  1456. }
  1457. return inLength;
  1458. }
  1459. /*
  1460. ================
  1461. idCompressor_Arithmetic::FinishCompress
  1462. ================
  1463. */
  1464. void idCompressor_Arithmetic::FinishCompress() {
  1465. if ( compress == false ) {
  1466. return;
  1467. }
  1468. WriteOverflowBits();
  1469. idCompressor_BitStream::FinishCompress();
  1470. }
  1471. /*
  1472. ================
  1473. idCompressor_Arithmetic::Read
  1474. ================
  1475. */
  1476. int idCompressor_Arithmetic::Read( void *outData, int outLength ) {
  1477. int i, j;
  1478. if ( compress == true || outLength <= 0 ) {
  1479. return 0;
  1480. }
  1481. InitDecompress( outData, outLength );
  1482. for( i = 0; i < outLength && readLength >= 0; i++ ) {
  1483. if ( ( writeTotalBytes & ( ( 1 << 14 ) - 1 ) ) == 0 ) {
  1484. if ( writeTotalBytes ) {
  1485. while( readBit ) {
  1486. ReadBits( 1 );
  1487. }
  1488. while( ReadBits( 8 ) == 0 && readLength > 0 ) {
  1489. }
  1490. }
  1491. InitProbabilities();
  1492. for ( j = 0; j < AC_NUM_BITS; j++ ) {
  1493. code <<= 1;
  1494. code |= ReadBits( 1 );
  1495. }
  1496. }
  1497. for ( j = 0; j < 8; j++ ) {
  1498. WriteBits( GetBit(), 1 );
  1499. }
  1500. }
  1501. return i;
  1502. }
  1503. /*
  1504. =================================================================================
  1505. idCompressor_LZSS
  1506. In 1977 Abraham Lempel and Jacob Ziv presented a dictionary based scheme for
  1507. text compression called LZ77. For any new text LZ77 outputs an offset/length
  1508. pair to previously seen text and the next new byte after the previously seen
  1509. text.
  1510. In 1982 James Storer and Thomas Szymanski presented a modification on the work
  1511. of Lempel and Ziv called LZSS. LZ77 always outputs an offset/length pair, even
  1512. if a match is only one byte long. An offset/length pair usually takes more than
  1513. a single byte to store and the compression is not optimal for small match sizes.
  1514. LZSS uses a bit flag which tells whether the following data is a literal (byte)
  1515. or an offset/length pair.
  1516. The following algorithm is an implementation of LZSS with arbitrary word size.
  1517. =================================================================================
  1518. */
  1519. const int LZSS_BLOCK_SIZE = 65535;
  1520. const int LZSS_HASH_BITS = 10;
  1521. const int LZSS_HASH_SIZE = ( 1 << LZSS_HASH_BITS );
  1522. const int LZSS_HASH_MASK = ( 1 << LZSS_HASH_BITS ) - 1;
  1523. const int LZSS_OFFSET_BITS = 11;
  1524. const int LZSS_LENGTH_BITS = 5;
  1525. class idCompressor_LZSS : public idCompressor_BitStream {
  1526. public:
  1527. idCompressor_LZSS() {}
  1528. void Init( idFile *f, bool compress, int wordLength );
  1529. void FinishCompress();
  1530. int Write( const void *inData, int inLength );
  1531. int Read( void *outData, int outLength );
  1532. protected:
  1533. int offsetBits;
  1534. int lengthBits;
  1535. int minMatchWords;
  1536. byte block[LZSS_BLOCK_SIZE];
  1537. int blockSize;
  1538. int blockIndex;
  1539. int hashTable[LZSS_HASH_SIZE];
  1540. int hashNext[LZSS_BLOCK_SIZE * 8];
  1541. protected:
  1542. bool FindMatch( int startWord, int startValue, int &wordOffset, int &numWords );
  1543. void AddToHash( int index, int hash );
  1544. int GetWordFromBlock( int wordOffset ) const;
  1545. virtual void CompressBlock();
  1546. virtual void DecompressBlock();
  1547. };
  1548. /*
  1549. ================
  1550. idCompressor_LZSS::Init
  1551. ================
  1552. */
  1553. void idCompressor_LZSS::Init( idFile *f, bool compress, int wordLength ) {
  1554. idCompressor_BitStream::Init( f, compress, wordLength );
  1555. offsetBits = LZSS_OFFSET_BITS;
  1556. lengthBits = LZSS_LENGTH_BITS;
  1557. minMatchWords = ( offsetBits + lengthBits + wordLength ) / wordLength;
  1558. blockSize = 0;
  1559. blockIndex = 0;
  1560. }
  1561. /*
  1562. ================
  1563. idCompressor_LZSS::FindMatch
  1564. ================
  1565. */
  1566. bool idCompressor_LZSS::FindMatch( int startWord, int startValue, int &wordOffset, int &numWords ) {
  1567. int i, n, hash, bottom, maxBits;
  1568. wordOffset = startWord;
  1569. numWords = minMatchWords - 1;
  1570. bottom = Max( 0, startWord - ( ( 1 << offsetBits ) - 1 ) );
  1571. maxBits = ( blockSize << 3 ) - startWord * wordLength;
  1572. hash = startValue & LZSS_HASH_MASK;
  1573. for ( i = hashTable[hash]; i >= bottom; i = hashNext[i] ) {
  1574. n = Compare( block, i * wordLength, block, startWord * wordLength, Min( maxBits, ( startWord - i ) * wordLength ) );
  1575. if ( n > numWords * wordLength ) {
  1576. numWords = n / wordLength;
  1577. wordOffset = i;
  1578. if ( numWords > ( ( 1 << lengthBits ) - 1 + minMatchWords ) - 1 ) {
  1579. numWords = ( ( 1 << lengthBits ) - 1 + minMatchWords ) - 1;
  1580. break;
  1581. }
  1582. }
  1583. }
  1584. return ( numWords >= minMatchWords );
  1585. }
  1586. /*
  1587. ================
  1588. idCompressor_LZSS::AddToHash
  1589. ================
  1590. */
  1591. void idCompressor_LZSS::AddToHash( int index, int hash ) {
  1592. hashNext[index] = hashTable[hash];
  1593. hashTable[hash] = index;
  1594. }
  1595. /*
  1596. ================
  1597. idCompressor_LZSS::GetWordFromBlock
  1598. ================
  1599. */
  1600. int idCompressor_LZSS::GetWordFromBlock( int wordOffset ) const {
  1601. int blockBit, blockByte, value, valueBits, get, fraction;
  1602. blockBit = ( wordOffset * wordLength ) & 7;
  1603. blockByte = ( wordOffset * wordLength ) >> 3;
  1604. if ( blockBit != 0 ) {
  1605. blockByte++;
  1606. }
  1607. value = 0;
  1608. valueBits = 0;
  1609. while ( valueBits < wordLength ) {
  1610. if ( blockBit == 0 ) {
  1611. if ( blockByte >= LZSS_BLOCK_SIZE ) {
  1612. return value;
  1613. }
  1614. blockByte++;
  1615. }
  1616. get = 8 - blockBit;
  1617. if ( get > (wordLength - valueBits) ) {
  1618. get = (wordLength - valueBits);
  1619. }
  1620. fraction = block[blockByte - 1];
  1621. fraction >>= blockBit;
  1622. fraction &= ( 1 << get ) - 1;
  1623. value |= fraction << valueBits;
  1624. valueBits += get;
  1625. blockBit = ( blockBit + get ) & 7;
  1626. }
  1627. return value;
  1628. }
  1629. /*
  1630. ================
  1631. idCompressor_LZSS::CompressBlock
  1632. ================
  1633. */
  1634. void idCompressor_LZSS::CompressBlock() {
  1635. int i, startWord, startValue, wordOffset, numWords;
  1636. InitCompress( block, blockSize );
  1637. memset( hashTable, -1, sizeof( hashTable ) );
  1638. memset( hashNext, -1, sizeof( hashNext ) );
  1639. startWord = 0;
  1640. while( readByte < readLength ) {
  1641. startValue = ReadBits( wordLength );
  1642. if ( FindMatch( startWord, startValue, wordOffset, numWords ) ) {
  1643. WriteBits( 1, 1 );
  1644. WriteBits( startWord - wordOffset, offsetBits );
  1645. WriteBits( numWords - minMatchWords, lengthBits );
  1646. UnreadBits( wordLength );
  1647. for ( i = 0; i < numWords; i++ ) {
  1648. startValue = ReadBits( wordLength );
  1649. AddToHash( startWord, startValue & LZSS_HASH_MASK );
  1650. startWord++;
  1651. }
  1652. } else {
  1653. WriteBits( 0, 1 );
  1654. WriteBits( startValue, wordLength );
  1655. AddToHash( startWord, startValue & LZSS_HASH_MASK );
  1656. startWord++;
  1657. }
  1658. }
  1659. blockSize = 0;
  1660. }
  1661. /*
  1662. ================
  1663. idCompressor_LZSS::DecompressBlock
  1664. ================
  1665. */
  1666. void idCompressor_LZSS::DecompressBlock() {
  1667. int i, offset, startWord, numWords;
  1668. InitDecompress( block, LZSS_BLOCK_SIZE );
  1669. startWord = 0;
  1670. while( writeByte < writeLength && readLength >= 0 ) {
  1671. if ( ReadBits( 1 ) ) {
  1672. offset = startWord - ReadBits( offsetBits );
  1673. numWords = ReadBits( lengthBits ) + minMatchWords;
  1674. for ( i = 0; i < numWords; i++ ) {
  1675. WriteBits( GetWordFromBlock( offset + i ), wordLength );
  1676. startWord++;
  1677. }
  1678. } else {
  1679. WriteBits( ReadBits( wordLength ), wordLength );
  1680. startWord++;
  1681. }
  1682. }
  1683. blockSize = Min( writeByte, LZSS_BLOCK_SIZE );
  1684. }
  1685. /*
  1686. ================
  1687. idCompressor_LZSS::Write
  1688. ================
  1689. */
  1690. int idCompressor_LZSS::Write( const void *inData, int inLength ) {
  1691. int i, n;
  1692. if ( compress == false || inLength <= 0 ) {
  1693. return 0;
  1694. }
  1695. for ( n = i = 0; i < inLength; i += n ) {
  1696. n = LZSS_BLOCK_SIZE - blockSize;
  1697. if ( inLength - i >= n ) {
  1698. memcpy( block + blockSize, ((const byte *)inData) + i, n );
  1699. blockSize = LZSS_BLOCK_SIZE;
  1700. CompressBlock();
  1701. blockSize = 0;
  1702. } else {
  1703. memcpy( block + blockSize, ((const byte *)inData) + i, inLength - i );
  1704. n = inLength - i;
  1705. blockSize += n;
  1706. }
  1707. }
  1708. return inLength;
  1709. }
  1710. /*
  1711. ================
  1712. idCompressor_LZSS::FinishCompress
  1713. ================
  1714. */
  1715. void idCompressor_LZSS::FinishCompress() {
  1716. if ( compress == false ) {
  1717. return;
  1718. }
  1719. if ( blockSize ) {
  1720. CompressBlock();
  1721. }
  1722. idCompressor_BitStream::FinishCompress();
  1723. }
  1724. /*
  1725. ================
  1726. idCompressor_LZSS::Read
  1727. ================
  1728. */
  1729. int idCompressor_LZSS::Read( void *outData, int outLength ) {
  1730. int i, n;
  1731. if ( compress == true || outLength <= 0 ) {
  1732. return 0;
  1733. }
  1734. if ( !blockSize ) {
  1735. DecompressBlock();
  1736. }
  1737. for ( n = i = 0; i < outLength; i += n ) {
  1738. if ( !blockSize ) {
  1739. return i;
  1740. }
  1741. n = blockSize - blockIndex;
  1742. if ( outLength - i >= n ) {
  1743. memcpy( ((byte *)outData) + i, block + blockIndex, n );
  1744. DecompressBlock();
  1745. blockIndex = 0;
  1746. } else {
  1747. memcpy( ((byte *)outData) + i, block + blockIndex, outLength - i );
  1748. n = outLength - i;
  1749. blockIndex += n;
  1750. }
  1751. }
  1752. return outLength;
  1753. }
  1754. /*
  1755. =================================================================================
  1756. idCompressor_LZSS_WordAligned
  1757. Outputs word aligned compressed data.
  1758. =================================================================================
  1759. */
  1760. class idCompressor_LZSS_WordAligned : public idCompressor_LZSS {
  1761. public:
  1762. idCompressor_LZSS_WordAligned() {}
  1763. void Init( idFile *f, bool compress, int wordLength );
  1764. private:
  1765. virtual void CompressBlock();
  1766. virtual void DecompressBlock();
  1767. };
  1768. /*
  1769. ================
  1770. idCompressor_LZSS_WordAligned::Init
  1771. ================
  1772. */
  1773. void idCompressor_LZSS_WordAligned::Init( idFile *f, bool compress, int wordLength ) {
  1774. idCompressor_LZSS::Init( f, compress, wordLength );
  1775. offsetBits = 2 * wordLength;
  1776. lengthBits = wordLength;
  1777. minMatchWords = ( offsetBits + lengthBits + wordLength ) / wordLength;
  1778. blockSize = 0;
  1779. blockIndex = 0;
  1780. }
  1781. /*
  1782. ================
  1783. idCompressor_LZSS_WordAligned::CompressBlock
  1784. ================
  1785. */
  1786. void idCompressor_LZSS_WordAligned::CompressBlock() {
  1787. int i, startWord, startValue, wordOffset, numWords;
  1788. InitCompress( block, blockSize );
  1789. memset( hashTable, -1, sizeof( hashTable ) );
  1790. memset( hashNext, -1, sizeof( hashNext ) );
  1791. startWord = 0;
  1792. while( readByte < readLength ) {
  1793. startValue = ReadBits( wordLength );
  1794. if ( FindMatch( startWord, startValue, wordOffset, numWords ) ) {
  1795. WriteBits( numWords - ( minMatchWords - 1 ), lengthBits );
  1796. WriteBits( startWord - wordOffset, offsetBits );
  1797. UnreadBits( wordLength );
  1798. for ( i = 0; i < numWords; i++ ) {
  1799. startValue = ReadBits( wordLength );
  1800. AddToHash( startWord, startValue & LZSS_HASH_MASK );
  1801. startWord++;
  1802. }
  1803. } else {
  1804. WriteBits( 0, lengthBits );
  1805. WriteBits( startValue, wordLength );
  1806. AddToHash( startWord, startValue & LZSS_HASH_MASK );
  1807. startWord++;
  1808. }
  1809. }
  1810. blockSize = 0;
  1811. }
  1812. /*
  1813. ================
  1814. idCompressor_LZSS_WordAligned::DecompressBlock
  1815. ================
  1816. */
  1817. void idCompressor_LZSS_WordAligned::DecompressBlock() {
  1818. int i, offset, startWord, numWords;
  1819. InitDecompress( block, LZSS_BLOCK_SIZE );
  1820. startWord = 0;
  1821. while( writeByte < writeLength && readLength >= 0 ) {
  1822. numWords = ReadBits( lengthBits );
  1823. if ( numWords ) {
  1824. numWords += ( minMatchWords - 1 );
  1825. offset = startWord - ReadBits( offsetBits );
  1826. for ( i = 0; i < numWords; i++ ) {
  1827. WriteBits( GetWordFromBlock( offset + i ), wordLength );
  1828. startWord++;
  1829. }
  1830. } else {
  1831. WriteBits( ReadBits( wordLength ), wordLength );
  1832. startWord++;
  1833. }
  1834. }
  1835. blockSize = Min( writeByte, LZSS_BLOCK_SIZE );
  1836. }
  1837. /*
  1838. =================================================================================
  1839. idCompressor_LZW
  1840. http://www.unisys.com/about__unisys/lzw
  1841. http://www.dogma.net/markn/articles/lzw/lzw.htm
  1842. http://www.cs.cf.ac.uk/Dave/Multimedia/node214.html
  1843. http://www.cs.duke.edu/csed/curious/compression/lzw.html
  1844. http://oldwww.rasip.fer.hr/research/compress/algorithms/fund/lz/lzw.html
  1845. This is the same compression scheme used by GIF with the exception that
  1846. the EOI and clear codes are not explicitly stored. Instead EOI happens
  1847. when the input stream runs dry and CC happens when the table gets to big.
  1848. This is a derivation of LZ78, but the dictionary starts with all single
  1849. character values so only code words are output. It is similar in theory
  1850. to LZ77, but instead of using the previous X bytes as a lookup table, a table
  1851. is built as the stream is read. The compressor and decompressor use the
  1852. same formula, so the tables should be exactly alike. The only catch is the
  1853. decompressor is always one step behind the compressor and may get a code not
  1854. yet in the table. In this case, it is easy to determine what the next code
  1855. is going to be (it will be the previous string plus the first byte of the
  1856. previous string).
  1857. The dictionary can be any size, but 12 bits seems to produce best results for
  1858. most sample data. The code size is variable. It starts with the minimum
  1859. number of bits required to store the dictionary and automatically increases
  1860. as the dictionary gets bigger (it starts at 9 bits and grows to 10 bits when
  1861. item 512 is added, 11 bits when 1024 is added, etc...) once the the dictionary
  1862. is filled (4096 items for a 12 bit dictionary), the whole thing is cleared and
  1863. the process starts over again.
  1864. The compressor increases the bit size after it adds the item, while the
  1865. decompressor does before it adds the item. The difference is subtle, but
  1866. it's because decompressor being one step behind. Otherwise, the decompressor
  1867. would read 512 with only 9 bits.
  1868. If "Hello" is in the dictionary, then "Hell", "Hel", "He" and "H" will be too.
  1869. We use this to our advantage by storing the index of the previous code, and
  1870. the value of the last character. This means when we traverse through the
  1871. dictionary, we get the characters in reverse.
  1872. Dictionary entries 0-255 are always going to have the values 0-255
  1873. =================================================================================
  1874. */
  1875. class idCompressor_LZW : public idCompressor_BitStream {
  1876. public:
  1877. idCompressor_LZW() {}
  1878. void Init( idFile *f, bool compress, int wordLength );
  1879. void FinishCompress();
  1880. int Write( const void *inData, int inLength );
  1881. int Read( void *outData, int outLength );
  1882. protected:
  1883. int AddToDict( int w, int k );
  1884. int Lookup( int w, int k );
  1885. bool BumpBits();
  1886. int WriteChain( int code );
  1887. void DecompressBlock();
  1888. static const int LZW_BLOCK_SIZE = 32767;
  1889. static const int LZW_START_BITS = 9;
  1890. static const int LZW_FIRST_CODE = (1 << (LZW_START_BITS-1));
  1891. static const int LZW_DICT_BITS = 12;
  1892. static const int LZW_DICT_SIZE = 1 << LZW_DICT_BITS;
  1893. // Dictionary data
  1894. struct {
  1895. int k;
  1896. int w;
  1897. } dictionary[LZW_DICT_SIZE];
  1898. idHashIndex index;
  1899. int nextCode;
  1900. int codeBits;
  1901. // Block data
  1902. byte block[LZW_BLOCK_SIZE];
  1903. int blockSize;
  1904. int blockIndex;
  1905. // Used by the compressor
  1906. int w;
  1907. // Used by the decompressor
  1908. int oldCode;
  1909. };
  1910. /*
  1911. ================
  1912. idCompressor_LZW::Init
  1913. ================
  1914. */
  1915. void idCompressor_LZW::Init( idFile *f, bool compress, int wordLength ) {
  1916. idCompressor_BitStream::Init( f, compress, wordLength );
  1917. for ( int i=0; i<LZW_FIRST_CODE; i++ ) {
  1918. dictionary[i].k = i;
  1919. dictionary[i].w = -1;
  1920. }
  1921. index.Clear();
  1922. nextCode = LZW_FIRST_CODE;
  1923. codeBits = LZW_START_BITS;
  1924. blockSize = 0;
  1925. blockIndex = 0;
  1926. w = -1;
  1927. oldCode = -1;
  1928. }
  1929. /*
  1930. ================
  1931. idCompressor_LZW::Read
  1932. ================
  1933. */
  1934. int idCompressor_LZW::Read( void *outData, int outLength ) {
  1935. int i, n;
  1936. if ( compress == true || outLength <= 0 ) {
  1937. return 0;
  1938. }
  1939. if ( !blockSize ) {
  1940. DecompressBlock();
  1941. }
  1942. for ( n = i = 0; i < outLength; i += n ) {
  1943. if ( !blockSize ) {
  1944. return i;
  1945. }
  1946. n = blockSize - blockIndex;
  1947. if ( outLength - i >= n ) {
  1948. memcpy( ((byte *)outData) + i, block + blockIndex, n );
  1949. DecompressBlock();
  1950. blockIndex = 0;
  1951. } else {
  1952. memcpy( ((byte *)outData) + i, block + blockIndex, outLength - i );
  1953. n = outLength - i;
  1954. blockIndex += n;
  1955. }
  1956. }
  1957. return outLength;
  1958. }
  1959. /*
  1960. ================
  1961. idCompressor_LZW::Lookup
  1962. ================
  1963. */
  1964. int idCompressor_LZW::Lookup( int w, int k ) {
  1965. int j;
  1966. if ( w == -1 ) {
  1967. return k;
  1968. } else {
  1969. for ( j = index.First( w ^ k ); j >= 0 ; j = index.Next( j ) ) {
  1970. if ( dictionary[ j ].k == k && dictionary[ j ].w == w ) {
  1971. return j;
  1972. }
  1973. }
  1974. }
  1975. return -1;
  1976. }
  1977. /*
  1978. ================
  1979. idCompressor_LZW::AddToDict
  1980. ================
  1981. */
  1982. int idCompressor_LZW::AddToDict( int w, int k ) {
  1983. dictionary[ nextCode ].k = k;
  1984. dictionary[ nextCode ].w = w;
  1985. index.Add( w ^ k, nextCode );
  1986. return nextCode++;
  1987. }
  1988. /*
  1989. ================
  1990. idCompressor_LZW::BumpBits
  1991. Possibly increments codeBits
  1992. Returns true if the dictionary was cleared
  1993. ================
  1994. */
  1995. bool idCompressor_LZW::BumpBits() {
  1996. if ( nextCode == ( 1 << codeBits ) ) {
  1997. codeBits ++;
  1998. if ( codeBits > LZW_DICT_BITS ) {
  1999. nextCode = LZW_FIRST_CODE;
  2000. codeBits = LZW_START_BITS;
  2001. index.Clear();
  2002. return true;
  2003. }
  2004. }
  2005. return false;
  2006. }
  2007. /*
  2008. ================
  2009. idCompressor_LZW::FinishCompress
  2010. ================
  2011. */
  2012. void idCompressor_LZW::FinishCompress() {
  2013. WriteBits( w, codeBits );
  2014. idCompressor_BitStream::FinishCompress();
  2015. }
  2016. /*
  2017. ================
  2018. idCompressor_LZW::Write
  2019. ================
  2020. */
  2021. int idCompressor_LZW::Write( const void *inData, int inLength ) {
  2022. int i;
  2023. InitCompress( inData, inLength );
  2024. for ( i = 0; i < inLength; i++ ) {
  2025. int k = ReadBits( 8 );
  2026. int code = Lookup(w, k);
  2027. if ( code >= 0 ) {
  2028. w = code;
  2029. } else {
  2030. WriteBits( w, codeBits );
  2031. if ( !BumpBits() ) {
  2032. AddToDict( w, k );
  2033. }
  2034. w = k;
  2035. }
  2036. }
  2037. return inLength;
  2038. }
  2039. /*
  2040. ================
  2041. idCompressor_LZW::WriteChain
  2042. The chain is stored backwards, so we have to write it to a buffer then output the buffer in reverse
  2043. ================
  2044. */
  2045. int idCompressor_LZW::WriteChain( int code ) {
  2046. byte chain[LZW_DICT_SIZE];
  2047. int firstChar = 0;
  2048. int i = 0;
  2049. do {
  2050. assert( i < LZW_DICT_SIZE-1 && code >= 0 );
  2051. chain[i++] = dictionary[code].k;
  2052. code = dictionary[code].w;
  2053. } while ( code >= 0 );
  2054. firstChar = chain[--i];
  2055. for ( ; i >= 0; i-- ) {
  2056. WriteBits( chain[i], 8 );
  2057. }
  2058. return firstChar;
  2059. }
  2060. /*
  2061. ================
  2062. idCompressor_LZW::DecompressBlock
  2063. ================
  2064. */
  2065. void idCompressor_LZW::DecompressBlock() {
  2066. int code, firstChar;
  2067. InitDecompress( block, LZW_BLOCK_SIZE );
  2068. while( writeByte < writeLength - LZW_DICT_SIZE && readLength > 0 ) {
  2069. assert( codeBits <= LZW_DICT_BITS );
  2070. code = ReadBits( codeBits );
  2071. if ( readLength == 0 ) {
  2072. break;
  2073. }
  2074. if ( oldCode == -1 ) {
  2075. assert( code < 256 );
  2076. WriteBits( code, 8 );
  2077. oldCode = code;
  2078. firstChar = code;
  2079. continue;
  2080. }
  2081. if ( code >= nextCode ) {
  2082. assert( code == nextCode );
  2083. firstChar = WriteChain( oldCode );
  2084. WriteBits( firstChar, 8 );
  2085. } else {
  2086. firstChar = WriteChain( code );
  2087. }
  2088. AddToDict( oldCode, firstChar );
  2089. if ( BumpBits() ) {
  2090. oldCode = -1;
  2091. } else {
  2092. oldCode = code;
  2093. }
  2094. }
  2095. blockSize = Min( writeByte, LZW_BLOCK_SIZE );
  2096. }
  2097. /*
  2098. =================================================================================
  2099. idCompressor
  2100. =================================================================================
  2101. */
  2102. /*
  2103. ================
  2104. idCompressor::AllocNoCompression
  2105. ================
  2106. */
  2107. idCompressor * idCompressor::AllocNoCompression() {
  2108. return new (TAG_IDFILE) idCompressor_None();
  2109. }
  2110. /*
  2111. ================
  2112. idCompressor::AllocBitStream
  2113. ================
  2114. */
  2115. idCompressor * idCompressor::AllocBitStream() {
  2116. return new (TAG_IDFILE) idCompressor_BitStream();
  2117. }
  2118. /*
  2119. ================
  2120. idCompressor::AllocRunLength
  2121. ================
  2122. */
  2123. idCompressor * idCompressor::AllocRunLength() {
  2124. return new (TAG_IDFILE) idCompressor_RunLength();
  2125. }
  2126. /*
  2127. ================
  2128. idCompressor::AllocRunLength_ZeroBased
  2129. ================
  2130. */
  2131. idCompressor * idCompressor::AllocRunLength_ZeroBased() {
  2132. return new (TAG_IDFILE) idCompressor_RunLength_ZeroBased();
  2133. }
  2134. /*
  2135. ================
  2136. idCompressor::AllocHuffman
  2137. ================
  2138. */
  2139. idCompressor * idCompressor::AllocHuffman() {
  2140. return new (TAG_IDFILE) idCompressor_Huffman();
  2141. }
  2142. /*
  2143. ================
  2144. idCompressor::AllocArithmetic
  2145. ================
  2146. */
  2147. idCompressor * idCompressor::AllocArithmetic() {
  2148. return new (TAG_IDFILE) idCompressor_Arithmetic();
  2149. }
  2150. /*
  2151. ================
  2152. idCompressor::AllocLZSS
  2153. ================
  2154. */
  2155. idCompressor * idCompressor::AllocLZSS() {
  2156. return new (TAG_IDFILE) idCompressor_LZSS();
  2157. }
  2158. /*
  2159. ================
  2160. idCompressor::AllocLZSS_WordAligned
  2161. ================
  2162. */
  2163. idCompressor * idCompressor::AllocLZSS_WordAligned() {
  2164. return new (TAG_IDFILE) idCompressor_LZSS_WordAligned();
  2165. }
  2166. /*
  2167. ================
  2168. idCompressor::AllocLZW
  2169. ================
  2170. */
  2171. idCompressor * idCompressor::AllocLZW() {
  2172. return new (TAG_IDFILE) idCompressor_LZW();
  2173. }