etcdec.cxx 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  1. /**
  2. ESENTHEL CHANGED: char -> signed char
  3. @~English
  4. @page licensing Licensing
  5. @section etcdec etcdec.cxx License
  6. etcdec.cxx is made available under the terms and conditions of the following
  7. License Agreement.
  8. Software License Agreement
  9. PLEASE REVIEW THE FOLLOWING TERMS AND CONDITIONS PRIOR TO USING THE
  10. ERICSSON TEXTURE COMPRESSION CODEC SOFTWARE (THE "SOFTWARE"). THE USE
  11. OF THE SOFTWARE IS SUBJECT TO THE TERMS AND CONDITIONS OF THE
  12. FOLLOWING SOFTWARE LICENSE AGREEMENT (THE "SLA"). IF YOU DO NOT ACCEPT
  13. SUCH TERMS AND CONDITIONS YOU MAY NOT USE THE SOFTWARE.
  14. Subject to the terms and conditions of the SLA, the licensee of the
  15. Software (the "Licensee") hereby, receives a non-exclusive,
  16. non-transferable, limited, free-of-charge, perpetual and worldwide
  17. license, to copy, use, distribute and modify the Software, but only
  18. for the purpose of developing, manufacturing, selling, using and
  19. distributing products including the Software in binary form, which
  20. products are used for compression and/or decompression according to
  21. the Khronos standard specifications OpenGL, OpenGL ES and
  22. WebGL. Notwithstanding anything of the above, Licensee may distribute
  23. [etcdec.cxx] in source code form provided (i) it is in unmodified
  24. form; and (ii) it is included in software owned by Licensee.
  25. If Licensee institutes, or threatens to institute, patent litigation
  26. against Ericsson or Ericsson's affiliates for using the Software for
  27. developing, having developed, manufacturing, having manufactured,
  28. selling, offer for sale, importing, using, leasing, operating,
  29. repairing and/or distributing products (i) within the scope of the
  30. Khronos framework; or (ii) using software or other intellectual
  31. property rights owned by Ericsson or its affiliates and provided under
  32. the Khronos framework, Ericsson shall have the right to terminate this
  33. SLA with immediate effect. Moreover, if Licensee institutes, or
  34. threatens to institute, patent litigation against any other licensee
  35. of the Software for using the Software in products within the scope of
  36. the Khronos framework, Ericsson shall have the right to terminate this
  37. SLA with immediate effect. However, should Licensee institute, or
  38. threaten to institute, patent litigation against any other licensee of
  39. the Software based on such other licensee's use of any other software
  40. together with the Software, then Ericsson shall have no right to
  41. terminate this SLA.
  42. This SLA does not transfer to Licensee any ownership to any Ericsson
  43. or third party intellectual property rights. All rights not expressly
  44. granted by Ericsson under this SLA are hereby expressly
  45. reserved. Furthermore, nothing in this SLA shall be construed as a
  46. right to use or sell products in a manner which conveys or purports to
  47. convey whether explicitly, by principles of implied license, or
  48. otherwise, any rights to any third party, under any patent of Ericsson
  49. or of Ericsson's affiliates covering or relating to any combination of
  50. the Software with any other software or product (not licensed
  51. hereunder) where the right applies specifically to the combination and
  52. not to the software or product itself.
  53. THE SOFTWARE IS PROVIDED "AS IS". ERICSSON MAKES NO REPRESENTATIONS OF
  54. ANY KIND, EXTENDS NO WARRANTIES OR CONDITIONS OF ANY KIND, EITHER
  55. EXPRESS, IMPLIED OR STATUTORY; INCLUDING, BUT NOT LIMITED TO, EXPRESS,
  56. IMPLIED OR STATUTORY WARRANTIES OR CONDITIONS OF TITLE,
  57. MERCHANTABILITY, SATISFACTORY QUALITY, SUITABILITY, AND FITNESS FOR A
  58. PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
  59. OF THE SOFTWARE IS WITH THE LICENSEE. SHOULD THE SOFTWARE PROVE
  60. DEFECTIVE, THE LICENSEE ASSUMES THE COST OF ALL NECESSARY SERVICING,
  61. REPAIR OR CORRECTION. ERICSSON MAKES NO WARRANTY THAT THE MANUFACTURE,
  62. SALE, OFFERING FOR SALE, DISTRIBUTION, LEASE, USE OR IMPORTATION UNDER
  63. THE SLA WILL BE FREE FROM INFRINGEMENT OF PATENTS, COPYRIGHTS OR OTHER
  64. INTELLECTUAL PROPERTY RIGHTS OF OTHERS, AND THE VALIDITY OF THE
  65. LICENSE AND THE SLA ARE SUBJECT TO LICENSEE'S SOLE RESPONSIBILITY TO
  66. MAKE SUCH DETERMINATION AND ACQUIRE SUCH LICENSES AS MAY BE NECESSARY
  67. WITH RESPECT TO PATENTS, COPYRIGHT AND OTHER INTELLECTUAL PROPERTY OF
  68. THIRD PARTIES.
  69. THE LICENSEE ACKNOWLEDGES AND ACCEPTS THAT THE SOFTWARE (I) IS NOT
  70. LICENSED FOR; (II) IS NOT DESIGNED FOR OR INTENDED FOR; AND (III) MAY
  71. NOT BE USED FOR; ANY MISSION CRITICAL APPLICATIONS SUCH AS, BUT NOT
  72. LIMITED TO OPERATION OF NUCLEAR OR HEALTHCARE COMPUTER SYSTEMS AND/OR
  73. NETWORKS, AIRCRAFT OR TRAIN CONTROL AND/OR COMMUNICATION SYSTEMS OR
  74. ANY OTHER COMPUTER SYSTEMS AND/OR NETWORKS OR CONTROL AND/OR
  75. COMMUNICATION SYSTEMS ALL IN WHICH CASE THE FAILURE OF THE SOFTWARE
  76. COULD LEAD TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL, MATERIAL OR
  77. ENVIRONMENTAL DAMAGE. LICENSEE'S RIGHTS UNDER THIS LICENSE WILL
  78. TERMINATE AUTOMATICALLY AND IMMEDIATELY WITHOUT NOTICE IF LICENSEE
  79. FAILS TO COMPLY WITH THIS PARAGRAPH.
  80. IN NO EVENT SHALL ERICSSON BE LIABLE FOR ANY DAMAGES WHATSOEVER,
  81. INCLUDING BUT NOT LIMITED TO PERSONAL INJURY, ANY GENERAL, SPECIAL,
  82. INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF OR IN
  83. CONNECTION WITH THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING
  84. BUT NOT LIMITED TO LOSS OF PROFITS, BUSINESS INTERUPTIONS, OR ANY
  85. OTHER COMMERCIAL DAMAGES OR LOSSES, LOSS OF DATA OR DATA BEING
  86. RENDERED INACCURATE OR LOSSES SUSTAINED BY THE LICENSEE OR THIRD
  87. PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER
  88. SOFTWARE) REGARDLESS OF THE THEORY OF LIABILITY (CONTRACT, TORT, OR
  89. OTHERWISE), EVEN IF THE LICENSEE OR ANY OTHER PARTY HAS BEEN ADVISED
  90. OF THE POSSIBILITY OF SUCH DAMAGES.
  91. Licensee acknowledges that "ERICSSON ///" is the corporate trademark
  92. of Telefonaktiebolaget LM Ericsson and that both "Ericsson" and the
  93. figure "///" are important features of the trade names of
  94. Telefonaktiebolaget LM Ericsson. Nothing contained in these terms and
  95. conditions shall be deemed to grant Licensee any right, title or
  96. interest in the word "Ericsson" or the figure "///". No delay or
  97. omission by Ericsson to exercise any right or power shall impair any
  98. such right or power to be construed to be a waiver thereof. Consent by
  99. Ericsson to, or waiver of, a breach by the Licensee shall not
  100. constitute consent to, waiver of, or excuse for any other different or
  101. subsequent breach.
  102. This SLA shall be governed by the substantive law of Sweden. Any
  103. dispute, controversy or claim arising out of or in connection with
  104. this SLA, or the breach, termination or invalidity thereof, shall be
  105. submitted to the exclusive jurisdiction of the Swedish Courts.
  106. */
  107. //// etcpack v2.74
  108. ////
  109. //// NO WARRANTY
  110. ////
  111. //// BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE THE PROGRAM IS PROVIDED
  112. //// "AS IS". ERICSSON MAKES NO REPRESENTATIONS OF ANY KIND, EXTENDS NO
  113. //// WARRANTIES OR CONDITIONS OF ANY KIND; EITHER EXPRESS, IMPLIED OR
  114. //// STATUTORY; INCLUDING, BUT NOT LIMITED TO, EXPRESS, IMPLIED OR
  115. //// STATUTORY WARRANTIES OR CONDITIONS OF TITLE, MERCHANTABILITY,
  116. //// SATISFACTORY QUALITY, SUITABILITY AND FITNESS FOR A PARTICULAR
  117. //// PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
  118. //// PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
  119. //// THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. ERICSSON
  120. //// MAKES NO WARRANTY THAT THE MANUFACTURE, SALE, OFFERING FOR SALE,
  121. //// DISTRIBUTION, LEASE, USE OR IMPORTATION UNDER THE LICENSE WILL BE FREE
  122. //// FROM INFRINGEMENT OF PATENTS, COPYRIGHTS OR OTHER INTELLECTUAL
  123. //// PROPERTY RIGHTS OF OTHERS, AND THE VALIDITY OF THE LICENSE IS SUBJECT
  124. //// TO YOUR SOLE RESPONSIBILITY TO MAKE SUCH DETERMINATION AND ACQUIRE
  125. //// SUCH LICENSES AS MAY BE NECESSARY WITH RESPECT TO PATENTS, COPYRIGHT
  126. //// AND OTHER INTELLECTUAL PROPERTY OF THIRD PARTIES.
  127. ////
  128. //// FOR THE AVOIDANCE OF DOUBT THE PROGRAM (I) IS NOT LICENSED FOR; (II)
  129. //// IS NOT DESIGNED FOR OR INTENDED FOR; AND (III) MAY NOT BE USED FOR;
  130. //// ANY MISSION CRITICAL APPLICATIONS SUCH AS, BUT NOT LIMITED TO
  131. //// OPERATION OF NUCLEAR OR HEALTHCARE COMPUTER SYSTEMS AND/OR NETWORKS,
  132. //// AIRCRAFT OR TRAIN CONTROL AND/OR COMMUNICATION SYSTEMS OR ANY OTHER
  133. //// COMPUTER SYSTEMS AND/OR NETWORKS OR CONTROL AND/OR COMMUNICATION
  134. //// SYSTEMS ALL IN WHICH CASE THE FAILURE OF THE PROGRAM COULD LEAD TO
  135. //// DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL, MATERIAL OR ENVIRONMENTAL
  136. //// DAMAGE. YOUR RIGHTS UNDER THIS LICENSE WILL TERMINATE AUTOMATICALLY
  137. //// AND IMMEDIATELY WITHOUT NOTICE IF YOU FAIL TO COMPLY WITH THIS
  138. //// PARAGRAPH.
  139. ////
  140. //// IN NO EVENT WILL ERICSSON, BE LIABLE FOR ANY DAMAGES WHATSOEVER,
  141. //// INCLUDING BUT NOT LIMITED TO PERSONAL INJURY, ANY GENERAL, SPECIAL,
  142. //// INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR IN
  143. //// CONNECTION WITH THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
  144. //// NOT LIMITED TO LOSS OF PROFITS, BUSINESS INTERUPTIONS, OR ANY OTHER
  145. //// COMMERCIAL DAMAGES OR LOSSES, LOSS OF DATA OR DATA BEING RENDERED
  146. //// INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF
  147. //// THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) REGARDLESS OF THE
  148. //// THEORY OF LIABILITY (CONTRACT, TORT OR OTHERWISE), EVEN IF SUCH HOLDER
  149. //// OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  150. ////
  151. //// (C) Ericsson AB 2005-2013. All Rights Reserved.
  152. ////
  153. /*#include <stdio.h>
  154. #include <stdlib.h>*/
  155. // Typedefs
  156. typedef unsigned char uint8;
  157. typedef unsigned short uint16;
  158. typedef short int16;
  159. // Macros to help with bit extraction/insertion
  160. #define SHIFT(size,startpos) ((startpos)-(size)+1)
  161. #define MASK(size, startpos) (((2u<<(size-1u))-1u) << SHIFT(size,startpos)) // ESENTHEL CHANGED: converted to unsigned constants to silent compiler warnings on Android
  162. #define PUTBITS( dest, data, size, startpos) dest = ((dest & ~MASK(size, startpos)) | ((data << SHIFT(size, startpos)) & MASK(size,startpos)))
  163. #define SHIFTHIGH(size, startpos) (((startpos)-32)-(size)+1)
  164. #define MASKHIGH(size, startpos) (((1<<(size))-1) << SHIFTHIGH(size,startpos))
  165. #define PUTBITSHIGH(dest, data, size, startpos) dest = ((dest & ~MASKHIGH(size, startpos)) | ((data << SHIFTHIGH(size, startpos)) & MASKHIGH(size,startpos)))
  166. #define GETBITS(source, size, startpos) (( (source) >> ((startpos)-(size)+1) ) & ((1<<(size)) -1))
  167. #define GETBITSHIGH(source, size, startpos) (( (source) >> (((startpos)-32)-(size)+1) ) & ((1<<(size)) -1))
  168. #ifndef PGMOUT
  169. #define PGMOUT 1
  170. #endif
  171. // Thumb macros and definitions
  172. #define R_BITS59T 4
  173. #define G_BITS59T 4
  174. #define B_BITS59T 4
  175. #define R_BITS58H 4
  176. #define G_BITS58H 4
  177. #define B_BITS58H 4
  178. #define MAXIMUM_ERROR (255*255*16*1000)
  179. #define R 0
  180. #define G 1
  181. #define B 2
  182. #define BLOCKHEIGHT 4
  183. #define BLOCKWIDTH 4
  184. #define BINPOW(power) (1<<(power))
  185. #define TABLE_BITS_59T 3
  186. #define TABLE_BITS_58H 3
  187. // Helper Macros
  188. #define CLAMP(ll,x,ul) (((x)<(ll)) ? (ll) : (((x)>(ul)) ? (ul) : (x)))
  189. #define JAS_ROUND(x) (((x) < 0.0 ) ? ((int)((x)-0.5)) : ((int)((x)+0.5)))
  190. #define RED_CHANNEL(img,width,x,y,channels) img[channels*(y*width+x)+0]
  191. #define GREEN_CHANNEL(img,width,x,y,channels) img[channels*(y*width+x)+1]
  192. #define BLUE_CHANNEL(img,width,x,y,channels) img[channels*(y*width+x)+2]
  193. #define ALPHA_CHANNEL(img,width,x,y,channels) img[channels*(y*width+x)+3]
  194. // Global tables
  195. static uint8 table59T[8] = {3,6,11,16,23,32,41,64}; // 3-bit table for the 59 bit T-mode
  196. static uint8 table58H[8] = {3,6,11,16,23,32,41,64}; // 3-bit table for the 58 bit H-mode
  197. static int compressParams[16][4] = {{-8, -2, 2, 8}, {-8, -2, 2, 8}, {-17, -5, 5, 17}, {-17, -5, 5, 17}, {-29, -9, 9, 29}, {-29, -9, 9, 29}, {-42, -13, 13, 42}, {-42, -13, 13, 42}, {-60, -18, 18, 60}, {-60, -18, 18, 60}, {-80, -24, 24, 80}, {-80, -24, 24, 80}, {-106, -33, 33, 106}, {-106, -33, 33, 106}, {-183, -47, 47, 183}, {-183, -47, 47, 183}};
  198. static int unscramble[4] = {2, 3, 1, 0};
  199. int alphaTableInitialized = 0;
  200. int alphaTable[256][8];
  201. int alphaBase[16][4] = {
  202. {-15,-9,-6,-3},
  203. {-13,-10,-7,-3},
  204. {-13,-8,-5,-2},
  205. {-13,-6,-4,-2},
  206. {-12,-8,-6,-3},
  207. {-11,-9,-7,-3},
  208. {-11,-8,-7,-4},
  209. {-11,-8,-5,-3},
  210. { -10,-8,-6,-2},
  211. { -10,-8,-5,-2},
  212. { -10,-8,-4,-2},
  213. { -10,-7,-5,-2},
  214. { -10,-7,-4,-3},
  215. { -10,-3,-2, -1},
  216. { -9,-8,-6,-4},
  217. { -9,-7,-5,-3}
  218. };
  219. // Global variables
  220. int formatSigned = 0;
  221. // Enums
  222. enum{PATTERN_H = 0,
  223. PATTERN_T = 1};
  224. // Code used to create the valtab
  225. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  226. void setupAlphaTable()
  227. {
  228. if(alphaTableInitialized)
  229. return;
  230. //read table used for alpha compression
  231. int buf;
  232. for(int i = 16; i<32; i++)
  233. {
  234. for(int j=0; j<8; j++)
  235. {
  236. buf=alphaBase[i-16][3-j%4];
  237. if(j<4)
  238. alphaTable[i][j]=buf;
  239. else
  240. alphaTable[i][j]=(-buf-1);
  241. }
  242. }
  243. //beyond the first 16 values, the rest of the table is implicit.. so calculate that!
  244. for(int i=0; i<256; i++)
  245. {
  246. //fill remaining slots in table with multiples of the first ones.
  247. int mul = i/16;
  248. int old = 16+i%16;
  249. for(int j = 0; j<8; j++)
  250. {
  251. alphaTable[i][j]=alphaTable[old][j]*mul;
  252. //note: we don't do clamping here, though we could, because we'll be clamped afterwards anyway.
  253. }
  254. }
  255. alphaTableInitialized = 1; // ESENTHEL CHANGED
  256. }
  257. // Read a word in big endian style
  258. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  259. void read_big_endian_2byte_word(unsigned short *blockadr, FILE *f)
  260. {
  261. uint8 bytes[2];
  262. unsigned short block;
  263. fread(&bytes[0], 1, 1, f);
  264. fread(&bytes[1], 1, 1, f);
  265. block = 0;
  266. block |= bytes[0];
  267. block = block << 8;
  268. block |= bytes[1];
  269. blockadr[0] = block;
  270. }
  271. // Read a word in big endian style
  272. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  273. void read_big_endian_4byte_word(unsigned int *blockadr, FILE *f)
  274. {
  275. uint8 bytes[4];
  276. unsigned int block;
  277. fread(&bytes[0], 1, 1, f);
  278. fread(&bytes[1], 1, 1, f);
  279. fread(&bytes[2], 1, 1, f);
  280. fread(&bytes[3], 1, 1, f);
  281. block = 0;
  282. block |= bytes[0];
  283. block = block << 8;
  284. block |= bytes[1];
  285. block = block << 8;
  286. block |= bytes[2];
  287. block = block << 8;
  288. block |= bytes[3];
  289. blockadr[0] = block;
  290. }
  291. // The format stores the bits for the three extra modes in a roundabout way to be able to
  292. // fit them without increasing the bit rate. This function converts them into something
  293. // that is easier to work with.
  294. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  295. void unstuff57bits(unsigned int planar_word1, unsigned int planar_word2, unsigned int &planar57_word1, unsigned int &planar57_word2)
  296. {
  297. // Get bits from twotimer configuration for 57 bits
  298. //
  299. // Go to this bit layout:
  300. //
  301. // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
  302. // -----------------------------------------------------------------------------------------------
  303. // |R0 |G01G02 |B01B02 ;B03 |RH1 |RH2|GH |
  304. // -----------------------------------------------------------------------------------------------
  305. //
  306. // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  307. // -----------------------------------------------------------------------------------------------
  308. // |BH |RV |GV |BV | not used |
  309. // -----------------------------------------------------------------------------------------------
  310. //
  311. // From this:
  312. //
  313. // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
  314. // ------------------------------------------------------------------------------------------------
  315. // |//|R0 |G01|/|G02 |B01|/ // //|B02 |//|B03 |RH1 |df|RH2|
  316. // ------------------------------------------------------------------------------------------------
  317. //
  318. // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  319. // -----------------------------------------------------------------------------------------------
  320. // |GH |BH |RV |GV |BV |
  321. // -----------------------------------------------------------------------------------------------
  322. //
  323. // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
  324. // ---------------------------------------------------------------------------------------------------
  325. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  326. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  327. // ---------------------------------------------------------------------------------------------------
  328. uint8 RO, GO1, GO2, BO1, BO2, BO3, RH1, RH2, GH, BH, RV, GV, BV;
  329. RO = GETBITSHIGH( planar_word1, 6, 62);
  330. GO1 = GETBITSHIGH( planar_word1, 1, 56);
  331. GO2 = GETBITSHIGH( planar_word1, 6, 54);
  332. BO1 = GETBITSHIGH( planar_word1, 1, 48);
  333. BO2 = GETBITSHIGH( planar_word1, 2, 44);
  334. BO3 = GETBITSHIGH( planar_word1, 3, 41);
  335. RH1 = GETBITSHIGH( planar_word1, 5, 38);
  336. RH2 = GETBITSHIGH( planar_word1, 1, 32);
  337. GH = GETBITS( planar_word2, 7, 31);
  338. BH = GETBITS( planar_word2, 6, 24);
  339. RV = GETBITS( planar_word2, 6, 18);
  340. GV = GETBITS( planar_word2, 7, 12);
  341. BV = GETBITS( planar_word2, 6, 5);
  342. planar57_word1 = 0; planar57_word2 = 0;
  343. PUTBITSHIGH( planar57_word1, RO, 6, 63);
  344. PUTBITSHIGH( planar57_word1, GO1, 1, 57);
  345. PUTBITSHIGH( planar57_word1, GO2, 6, 56);
  346. PUTBITSHIGH( planar57_word1, BO1, 1, 50);
  347. PUTBITSHIGH( planar57_word1, BO2, 2, 49);
  348. PUTBITSHIGH( planar57_word1, BO3, 3, 47);
  349. PUTBITSHIGH( planar57_word1, RH1, 5, 44);
  350. PUTBITSHIGH( planar57_word1, RH2, 1, 39);
  351. PUTBITSHIGH( planar57_word1, GH, 7, 38);
  352. PUTBITS( planar57_word2, BH, 6, 31);
  353. PUTBITS( planar57_word2, RV, 6, 25);
  354. PUTBITS( planar57_word2, GV, 7, 19);
  355. PUTBITS( planar57_word2, BV, 6, 12);
  356. }
  357. // The format stores the bits for the three extra modes in a roundabout way to be able to
  358. // fit them without increasing the bit rate. This function converts them into something
  359. // that is easier to work with.
  360. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  361. void unstuff58bits(unsigned int thumbH_word1, unsigned int thumbH_word2, unsigned int &thumbH58_word1, unsigned int &thumbH58_word2)
  362. {
  363. // Go to this layout:
  364. //
  365. // |63 62 61 60 59 58|57 56 55 54 53 52 51|50 49|48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33|32 |
  366. // |-------empty-----|part0---------------|part1|part2------------------------------------------|part3|
  367. //
  368. // from this:
  369. //
  370. // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
  371. // --------------------------------------------------------------------------------------------------|
  372. // |//|part0 |// // //|part1|//|part2 |df|part3|
  373. // --------------------------------------------------------------------------------------------------|
  374. unsigned int part0, part1, part2, part3;
  375. // move parts
  376. part0 = GETBITSHIGH( thumbH_word1, 7, 62);
  377. part1 = GETBITSHIGH( thumbH_word1, 2, 52);
  378. part2 = GETBITSHIGH( thumbH_word1,16, 49);
  379. part3 = GETBITSHIGH( thumbH_word1, 1, 32);
  380. thumbH58_word1 = 0;
  381. PUTBITSHIGH( thumbH58_word1, part0, 7, 57);
  382. PUTBITSHIGH( thumbH58_word1, part1, 2, 50);
  383. PUTBITSHIGH( thumbH58_word1, part2, 16, 48);
  384. PUTBITSHIGH( thumbH58_word1, part3, 1, 32);
  385. thumbH58_word2 = thumbH_word2;
  386. }
  387. // The format stores the bits for the three extra modes in a roundabout way to be able to
  388. // fit them without increasing the bit rate. This function converts them into something
  389. // that is easier to work with.
  390. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  391. void unstuff59bits(unsigned int thumbT_word1, unsigned int thumbT_word2, unsigned int &thumbT59_word1, unsigned int &thumbT59_word2)
  392. {
  393. // Get bits from twotimer configuration 59 bits.
  394. //
  395. // Go to this bit layout:
  396. //
  397. // |63 62 61 60 59|58 57 56 55|54 53 52 51|50 49 48 47|46 45 44 43|42 41 40 39|38 37 36 35|34 33 32|
  398. // |----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
  399. //
  400. // |31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
  401. // |----------------------------------------index bits---------------------------------------------|
  402. //
  403. //
  404. // From this:
  405. //
  406. // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
  407. // -----------------------------------------------------------------------------------------------
  408. // |// // //|R0a |//|R0b |G0 |B0 |R1 |G1 |B1 |da |df|db|
  409. // -----------------------------------------------------------------------------------------------
  410. //
  411. // |31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
  412. // |----------------------------------------index bits---------------------------------------------|
  413. //
  414. // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
  415. // -----------------------------------------------------------------------------------------------
  416. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |df|fp|
  417. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bt|bt|
  418. // ------------------------------------------------------------------------------------------------
  419. uint8 R0a;
  420. // Fix middle part
  421. thumbT59_word1 = thumbT_word1 >> 1;
  422. // Fix db (lowest bit of d)
  423. PUTBITSHIGH( thumbT59_word1, thumbT_word1, 1, 32);
  424. // Fix R0a (top two bits of R0)
  425. R0a = GETBITSHIGH( thumbT_word1, 2, 60);
  426. PUTBITSHIGH( thumbT59_word1, R0a, 2, 58);
  427. // Zero top part (not needed)
  428. PUTBITSHIGH( thumbT59_word1, 0, 5, 63);
  429. thumbT59_word2 = thumbT_word2;
  430. }
  431. // The color bits are expanded to the full color
  432. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  433. void decompressColor(int R_B, int G_B, int B_B, uint8 (colors_RGB444)[2][3], uint8 (colors)[2][3])
  434. {
  435. // The color should be retrieved as:
  436. //
  437. // c = round(255/(r_bits^2-1))*comp_color
  438. //
  439. // This is similar to bit replication
  440. //
  441. // Note -- this code only work for bit replication from 4 bits and up --- 3 bits needs
  442. // two copy operations.
  443. colors[0][R] = (colors_RGB444[0][R] << (8 - R_B)) | (colors_RGB444[0][R] >> (R_B - (8-R_B)) );
  444. colors[0][G] = (colors_RGB444[0][G] << (8 - G_B)) | (colors_RGB444[0][G] >> (G_B - (8-G_B)) );
  445. colors[0][B] = (colors_RGB444[0][B] << (8 - B_B)) | (colors_RGB444[0][B] >> (B_B - (8-B_B)) );
  446. colors[1][R] = (colors_RGB444[1][R] << (8 - R_B)) | (colors_RGB444[1][R] >> (R_B - (8-R_B)) );
  447. colors[1][G] = (colors_RGB444[1][G] << (8 - G_B)) | (colors_RGB444[1][G] >> (G_B - (8-G_B)) );
  448. colors[1][B] = (colors_RGB444[1][B] << (8 - B_B)) | (colors_RGB444[1][B] >> (B_B - (8-B_B)) );
  449. }
  450. void calculatePaintColors59T(uint8 d, uint8 p, uint8 (colors)[2][3], uint8 (possible_colors)[4][3])
  451. {
  452. //////////////////////////////////////////////
  453. //
  454. // C3 C1 C4----C1---C2
  455. // | | |
  456. // | | |
  457. // |-------| |
  458. // | | |
  459. // | | |
  460. // C4 C2 C3
  461. //
  462. //////////////////////////////////////////////
  463. // C4
  464. possible_colors[3][R] = CLAMP(0,colors[1][R] - table59T[d],255);
  465. possible_colors[3][G] = CLAMP(0,colors[1][G] - table59T[d],255);
  466. possible_colors[3][B] = CLAMP(0,colors[1][B] - table59T[d],255);
  467. if (p == PATTERN_T)
  468. {
  469. // C3
  470. possible_colors[0][R] = colors[0][R];
  471. possible_colors[0][G] = colors[0][G];
  472. possible_colors[0][B] = colors[0][B];
  473. // C2
  474. possible_colors[1][R] = CLAMP(0,colors[1][R] + table59T[d],255);
  475. possible_colors[1][G] = CLAMP(0,colors[1][G] + table59T[d],255);
  476. possible_colors[1][B] = CLAMP(0,colors[1][B] + table59T[d],255);
  477. // C1
  478. possible_colors[2][R] = colors[1][R];
  479. possible_colors[2][G] = colors[1][G];
  480. possible_colors[2][B] = colors[1][B];
  481. }
  482. else
  483. {
  484. printf("Invalid pattern. Terminating");
  485. exit(1);
  486. }
  487. }
  488. // Decompress a T-mode block (simple packing)
  489. // Simple 59T packing:
  490. //|63 62 61 60 59|58 57 56 55|54 53 52 51|50 49 48 47|46 45 44 43|42 41 40 39|38 37 36 35|34 33 32|
  491. //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
  492. //
  493. //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
  494. //|----------------------------------------index bits---------------------------------------------|
  495. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  496. void decompressBlockTHUMB59Tc(unsigned int block_part1, unsigned int block_part2, uint8 *img,int width,int height,int startx,int starty, int channels)
  497. {
  498. uint8 colorsRGB444[2][3];
  499. uint8 colors[2][3];
  500. uint8 paint_colors[4][3];
  501. uint8 distance;
  502. uint8 block_mask[4][4];
  503. // First decode left part of block.
  504. colorsRGB444[0][R]= GETBITSHIGH(block_part1, 4, 58);
  505. colorsRGB444[0][G]= GETBITSHIGH(block_part1, 4, 54);
  506. colorsRGB444[0][B]= GETBITSHIGH(block_part1, 4, 50);
  507. colorsRGB444[1][R]= GETBITSHIGH(block_part1, 4, 46);
  508. colorsRGB444[1][G]= GETBITSHIGH(block_part1, 4, 42);
  509. colorsRGB444[1][B]= GETBITSHIGH(block_part1, 4, 38);
  510. distance = GETBITSHIGH(block_part1, TABLE_BITS_59T, 34);
  511. // Extend the two colors to RGB888
  512. decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
  513. calculatePaintColors59T(distance, PATTERN_T, colors, paint_colors);
  514. // Choose one of the four paint colors for each texel
  515. for (uint8 x = 0; x < BLOCKWIDTH; ++x)
  516. {
  517. for (uint8 y = 0; y < BLOCKHEIGHT; ++y)
  518. {
  519. //block_mask[x][y] = GETBITS(block_part2,2,31-(y*4+x)*2);
  520. block_mask[x][y] = GETBITS(block_part2,1,(y+x*4)+16)<<1;
  521. block_mask[x][y] |= GETBITS(block_part2,1,(y+x*4));
  522. img[channels*((starty+y)*width+startx+x)+R] =
  523. CLAMP(0,paint_colors[block_mask[x][y]][R],255); // RED
  524. img[channels*((starty+y)*width+startx+x)+G] =
  525. CLAMP(0,paint_colors[block_mask[x][y]][G],255); // GREEN
  526. img[channels*((starty+y)*width+startx+x)+B] =
  527. CLAMP(0,paint_colors[block_mask[x][y]][B],255); // BLUE
  528. }
  529. }
  530. }
  531. void decompressBlockTHUMB59T(unsigned int block_part1, unsigned int block_part2, uint8 *img, int width, int height, int startx, int starty)
  532. {
  533. decompressBlockTHUMB59Tc(block_part1, block_part2, img, width, height, startx, starty, 3);
  534. }
  535. // Calculate the paint colors from the block colors
  536. // using a distance d and one of the H- or T-patterns.
  537. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  538. void calculatePaintColors58H(uint8 d, uint8 p, uint8 (colors)[2][3], uint8 (possible_colors)[4][3])
  539. {
  540. //////////////////////////////////////////////
  541. //
  542. // C3 C1 C4----C1---C2
  543. // | | |
  544. // | | |
  545. // |-------| |
  546. // | | |
  547. // | | |
  548. // C4 C2 C3
  549. //
  550. //////////////////////////////////////////////
  551. // C4
  552. possible_colors[3][R] = CLAMP(0,colors[1][R] - table58H[d],255);
  553. possible_colors[3][G] = CLAMP(0,colors[1][G] - table58H[d],255);
  554. possible_colors[3][B] = CLAMP(0,colors[1][B] - table58H[d],255);
  555. if (p == PATTERN_H)
  556. {
  557. // C1
  558. possible_colors[0][R] = CLAMP(0,colors[0][R] + table58H[d],255);
  559. possible_colors[0][G] = CLAMP(0,colors[0][G] + table58H[d],255);
  560. possible_colors[0][B] = CLAMP(0,colors[0][B] + table58H[d],255);
  561. // C2
  562. possible_colors[1][R] = CLAMP(0,colors[0][R] - table58H[d],255);
  563. possible_colors[1][G] = CLAMP(0,colors[0][G] - table58H[d],255);
  564. possible_colors[1][B] = CLAMP(0,colors[0][B] - table58H[d],255);
  565. // C3
  566. possible_colors[2][R] = CLAMP(0,colors[1][R] + table58H[d],255);
  567. possible_colors[2][G] = CLAMP(0,colors[1][G] + table58H[d],255);
  568. possible_colors[2][B] = CLAMP(0,colors[1][B] + table58H[d],255);
  569. }
  570. else
  571. {
  572. printf("Invalid pattern. Terminating");
  573. exit(1);
  574. }
  575. }
  576. // Decompress an H-mode block
  577. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  578. void decompressBlockTHUMB58Hc(unsigned int block_part1, unsigned int block_part2, uint8 *img, int width, int height, int startx, int starty, int channels)
  579. {
  580. unsigned int col0, col1;
  581. uint8 colors[2][3];
  582. uint8 colorsRGB444[2][3];
  583. uint8 paint_colors[4][3];
  584. uint8 distance;
  585. uint8 block_mask[4][4];
  586. // First decode left part of block.
  587. colorsRGB444[0][R]= GETBITSHIGH(block_part1, 4, 57);
  588. colorsRGB444[0][G]= GETBITSHIGH(block_part1, 4, 53);
  589. colorsRGB444[0][B]= GETBITSHIGH(block_part1, 4, 49);
  590. colorsRGB444[1][R]= GETBITSHIGH(block_part1, 4, 45);
  591. colorsRGB444[1][G]= GETBITSHIGH(block_part1, 4, 41);
  592. colorsRGB444[1][B]= GETBITSHIGH(block_part1, 4, 37);
  593. distance = 0;
  594. distance = (GETBITSHIGH(block_part1, 2, 33)) << 1;
  595. col0 = GETBITSHIGH(block_part1, 12, 57);
  596. col1 = GETBITSHIGH(block_part1, 12, 45);
  597. if(col0 >= col1)
  598. {
  599. distance |= 1;
  600. }
  601. // Extend the two colors to RGB888
  602. decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
  603. calculatePaintColors58H(distance, PATTERN_H, colors, paint_colors);
  604. // Choose one of the four paint colors for each texel
  605. for (uint8 x = 0; x < BLOCKWIDTH; ++x)
  606. {
  607. for (uint8 y = 0; y < BLOCKHEIGHT; ++y)
  608. {
  609. //block_mask[x][y] = GETBITS(block_part2,2,31-(y*4+x)*2);
  610. block_mask[x][y] = GETBITS(block_part2,1,(y+x*4)+16)<<1;
  611. block_mask[x][y] |= GETBITS(block_part2,1,(y+x*4));
  612. img[channels*((starty+y)*width+startx+x)+R] =
  613. CLAMP(0,paint_colors[block_mask[x][y]][R],255); // RED
  614. img[channels*((starty+y)*width+startx+x)+G] =
  615. CLAMP(0,paint_colors[block_mask[x][y]][G],255); // GREEN
  616. img[channels*((starty+y)*width+startx+x)+B] =
  617. CLAMP(0,paint_colors[block_mask[x][y]][B],255); // BLUE
  618. }
  619. }
  620. }
  621. void decompressBlockTHUMB58H(unsigned int block_part1, unsigned int block_part2, uint8 *img, int width, int height, int startx, int starty)
  622. {
  623. decompressBlockTHUMB58Hc(block_part1, block_part2, img, width, height, startx, starty, 3);
  624. }
  625. // Decompress the planar mode.
  626. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  627. void decompressBlockPlanar57c(unsigned int compressed57_1, unsigned int compressed57_2, uint8 *img, int width, int height, int startx, int starty, int channels)
  628. {
  629. uint8 colorO[3], colorH[3], colorV[3];
  630. colorO[0] = GETBITSHIGH( compressed57_1, 6, 63);
  631. colorO[1] = GETBITSHIGH( compressed57_1, 7, 57);
  632. colorO[2] = GETBITSHIGH( compressed57_1, 6, 50);
  633. colorH[0] = GETBITSHIGH( compressed57_1, 6, 44);
  634. colorH[1] = GETBITSHIGH( compressed57_1, 7, 38);
  635. colorH[2] = GETBITS( compressed57_2, 6, 31);
  636. colorV[0] = GETBITS( compressed57_2, 6, 25);
  637. colorV[1] = GETBITS( compressed57_2, 7, 19);
  638. colorV[2] = GETBITS( compressed57_2, 6, 12);
  639. colorO[0] = (colorO[0] << 2) | (colorO[0] >> 4);
  640. colorO[1] = (colorO[1] << 1) | (colorO[1] >> 6);
  641. colorO[2] = (colorO[2] << 2) | (colorO[2] >> 4);
  642. colorH[0] = (colorH[0] << 2) | (colorH[0] >> 4);
  643. colorH[1] = (colorH[1] << 1) | (colorH[1] >> 6);
  644. colorH[2] = (colorH[2] << 2) | (colorH[2] >> 4);
  645. colorV[0] = (colorV[0] << 2) | (colorV[0] >> 4);
  646. colorV[1] = (colorV[1] << 1) | (colorV[1] >> 6);
  647. colorV[2] = (colorV[2] << 2) | (colorV[2] >> 4);
  648. int xx, yy;
  649. for( xx=0; xx<4; xx++)
  650. {
  651. for( yy=0; yy<4; yy++)
  652. {
  653. img[channels*width*(starty+yy) + channels*(startx+xx) + 0] = CLAMP(0, ((xx*(colorH[0]-colorO[0]) + yy*(colorV[0]-colorO[0]) + 4*colorO[0] + 2) >> 2),255);
  654. img[channels*width*(starty+yy) + channels*(startx+xx) + 1] = CLAMP(0, ((xx*(colorH[1]-colorO[1]) + yy*(colorV[1]-colorO[1]) + 4*colorO[1] + 2) >> 2),255);
  655. img[channels*width*(starty+yy) + channels*(startx+xx) + 2] = CLAMP(0, ((xx*(colorH[2]-colorO[2]) + yy*(colorV[2]-colorO[2]) + 4*colorO[2] + 2) >> 2),255);
  656. //Equivalent method
  657. /*img[channels*width*(starty+yy) + channels*(startx+xx) + 0] = (int)CLAMP(0, JAS_ROUND((xx*(colorH[0]-colorO[0])/4.0 + yy*(colorV[0]-colorO[0])/4.0 + colorO[0])), 255);
  658. img[channels*width*(starty+yy) + channels*(startx+xx) + 1] = (int)CLAMP(0, JAS_ROUND((xx*(colorH[1]-colorO[1])/4.0 + yy*(colorV[1]-colorO[1])/4.0 + colorO[1])), 255);
  659. img[channels*width*(starty+yy) + channels*(startx+xx) + 2] = (int)CLAMP(0, JAS_ROUND((xx*(colorH[2]-colorO[2])/4.0 + yy*(colorV[2]-colorO[2])/4.0 + colorO[2])), 255);*/
  660. }
  661. }
  662. }
  663. void decompressBlockPlanar57(unsigned int compressed57_1, unsigned int compressed57_2, uint8 *img, int width, int height, int startx, int starty)
  664. {
  665. decompressBlockPlanar57c(compressed57_1, compressed57_2, img, width, height, startx, starty, 3);
  666. }
  667. // Decompress an ETC1 block (or ETC2 using individual or differential mode).
  668. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  669. void decompressBlockDiffFlipC(unsigned int block_part1, unsigned int block_part2, uint8 *img, int width, int height, int startx, int starty, int channels)
  670. {
  671. uint8 avg_color[3], enc_color1[3], enc_color2[3];
  672. signed char diff[3];
  673. int table;
  674. int index,shift;
  675. int r,g,b;
  676. int diffbit;
  677. int flipbit;
  678. diffbit = (GETBITSHIGH(block_part1, 1, 33));
  679. flipbit = (GETBITSHIGH(block_part1, 1, 32));
  680. if( !diffbit )
  681. {
  682. // We have diffbit = 0.
  683. // First decode left part of block.
  684. avg_color[0]= GETBITSHIGH(block_part1, 4, 63);
  685. avg_color[1]= GETBITSHIGH(block_part1, 4, 55);
  686. avg_color[2]= GETBITSHIGH(block_part1, 4, 47);
  687. // Here, we should really multiply by 17 instead of 16. This can
  688. // be done by just copying the four lower bits to the upper ones
  689. // while keeping the lower bits.
  690. avg_color[0] |= (avg_color[0] <<4);
  691. avg_color[1] |= (avg_color[1] <<4);
  692. avg_color[2] |= (avg_color[2] <<4);
  693. table = GETBITSHIGH(block_part1, 3, 39) << 1;
  694. unsigned int pixel_indices_MSB, pixel_indices_LSB;
  695. pixel_indices_MSB = GETBITS(block_part2, 16, 31);
  696. pixel_indices_LSB = GETBITS(block_part2, 16, 15);
  697. if( (flipbit) == 0 )
  698. {
  699. // We should not flip
  700. shift = 0;
  701. for(int x=startx; x<startx+2; x++)
  702. {
  703. for(int y=starty; y<starty+4; y++)
  704. {
  705. index = ((pixel_indices_MSB >> shift) & 1) << 1;
  706. index |= ((pixel_indices_LSB >> shift) & 1);
  707. shift++;
  708. index=unscramble[index];
  709. r=RED_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
  710. g=GREEN_CHANNEL(img,width,x,y,channels)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
  711. b=BLUE_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
  712. }
  713. }
  714. }
  715. else
  716. {
  717. // We should flip
  718. shift = 0;
  719. for(int x=startx; x<startx+4; x++)
  720. {
  721. for(int y=starty; y<starty+2; y++)
  722. {
  723. index = ((pixel_indices_MSB >> shift) & 1) << 1;
  724. index |= ((pixel_indices_LSB >> shift) & 1);
  725. shift++;
  726. index=unscramble[index];
  727. r=RED_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
  728. g=GREEN_CHANNEL(img,width,x,y,channels)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
  729. b=BLUE_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
  730. }
  731. shift+=2;
  732. }
  733. }
  734. // Now decode other part of block.
  735. avg_color[0]= GETBITSHIGH(block_part1, 4, 59);
  736. avg_color[1]= GETBITSHIGH(block_part1, 4, 51);
  737. avg_color[2]= GETBITSHIGH(block_part1, 4, 43);
  738. // Here, we should really multiply by 17 instead of 16. This can
  739. // be done by just copying the four lower bits to the upper ones
  740. // while keeping the lower bits.
  741. avg_color[0] |= (avg_color[0] <<4);
  742. avg_color[1] |= (avg_color[1] <<4);
  743. avg_color[2] |= (avg_color[2] <<4);
  744. table = GETBITSHIGH(block_part1, 3, 36) << 1;
  745. pixel_indices_MSB = GETBITS(block_part2, 16, 31);
  746. pixel_indices_LSB = GETBITS(block_part2, 16, 15);
  747. if( (flipbit) == 0 )
  748. {
  749. // We should not flip
  750. shift=8;
  751. for(int x=startx+2; x<startx+4; x++)
  752. {
  753. for(int y=starty; y<starty+4; y++)
  754. {
  755. index = ((pixel_indices_MSB >> shift) & 1) << 1;
  756. index |= ((pixel_indices_LSB >> shift) & 1);
  757. shift++;
  758. index=unscramble[index];
  759. r=RED_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
  760. g=GREEN_CHANNEL(img,width,x,y,channels)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
  761. b=BLUE_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
  762. }
  763. }
  764. }
  765. else
  766. {
  767. // We should flip
  768. shift=2;
  769. for(int x=startx; x<startx+4; x++)
  770. {
  771. for(int y=starty+2; y<starty+4; y++)
  772. {
  773. index = ((pixel_indices_MSB >> shift) & 1) << 1;
  774. index |= ((pixel_indices_LSB >> shift) & 1);
  775. shift++;
  776. index=unscramble[index];
  777. r=RED_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
  778. g=GREEN_CHANNEL(img,width,x,y,channels)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
  779. b=BLUE_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
  780. }
  781. shift += 2;
  782. }
  783. }
  784. }
  785. else
  786. {
  787. // We have diffbit = 1.
  788. // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
  789. // ---------------------------------------------------------------------------------------------------
  790. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  791. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  792. // ---------------------------------------------------------------------------------------------------
  793. //
  794. //
  795. // c) bit layout in bits 31 through 0 (in both cases)
  796. //
  797. // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  798. // --------------------------------------------------------------------------------------------------
  799. // | most significant pixel index bits | least significant pixel index bits |
  800. // | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
  801. // --------------------------------------------------------------------------------------------------
  802. // First decode left part of block.
  803. enc_color1[0]= GETBITSHIGH(block_part1, 5, 63);
  804. enc_color1[1]= GETBITSHIGH(block_part1, 5, 55);
  805. enc_color1[2]= GETBITSHIGH(block_part1, 5, 47);
  806. // Expand from 5 to 8 bits
  807. avg_color[0] = (enc_color1[0] <<3) | (enc_color1[0] >> 2);
  808. avg_color[1] = (enc_color1[1] <<3) | (enc_color1[1] >> 2);
  809. avg_color[2] = (enc_color1[2] <<3) | (enc_color1[2] >> 2);
  810. table = GETBITSHIGH(block_part1, 3, 39) << 1;
  811. unsigned int pixel_indices_MSB, pixel_indices_LSB;
  812. pixel_indices_MSB = GETBITS(block_part2, 16, 31);
  813. pixel_indices_LSB = GETBITS(block_part2, 16, 15);
  814. if( (flipbit) == 0 )
  815. {
  816. // We should not flip
  817. shift = 0;
  818. for(int x=startx; x<startx+2; x++)
  819. {
  820. for(int y=starty; y<starty+4; y++)
  821. {
  822. index = ((pixel_indices_MSB >> shift) & 1) << 1;
  823. index |= ((pixel_indices_LSB >> shift) & 1);
  824. shift++;
  825. index=unscramble[index];
  826. r=RED_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
  827. g=GREEN_CHANNEL(img,width,x,y,channels)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
  828. b=BLUE_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
  829. }
  830. }
  831. }
  832. else
  833. {
  834. // We should flip
  835. shift = 0;
  836. for(int x=startx; x<startx+4; x++)
  837. {
  838. for(int y=starty; y<starty+2; y++)
  839. {
  840. index = ((pixel_indices_MSB >> shift) & 1) << 1;
  841. index |= ((pixel_indices_LSB >> shift) & 1);
  842. shift++;
  843. index=unscramble[index];
  844. r=RED_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
  845. g=GREEN_CHANNEL(img,width,x,y,channels)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
  846. b=BLUE_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
  847. }
  848. shift+=2;
  849. }
  850. }
  851. // Now decode right part of block.
  852. diff[0]= GETBITSHIGH(block_part1, 3, 58);
  853. diff[1]= GETBITSHIGH(block_part1, 3, 50);
  854. diff[2]= GETBITSHIGH(block_part1, 3, 42);
  855. // Extend sign bit to entire byte.
  856. diff[0] = (diff[0] << 5);
  857. diff[1] = (diff[1] << 5);
  858. diff[2] = (diff[2] << 5);
  859. diff[0] = diff[0] >> 5;
  860. diff[1] = diff[1] >> 5;
  861. diff[2] = diff[2] >> 5;
  862. // Calculale second color
  863. enc_color2[0]= enc_color1[0] + diff[0];
  864. enc_color2[1]= enc_color1[1] + diff[1];
  865. enc_color2[2]= enc_color1[2] + diff[2];
  866. // Expand from 5 to 8 bits
  867. avg_color[0] = (enc_color2[0] <<3) | (enc_color2[0] >> 2);
  868. avg_color[1] = (enc_color2[1] <<3) | (enc_color2[1] >> 2);
  869. avg_color[2] = (enc_color2[2] <<3) | (enc_color2[2] >> 2);
  870. table = GETBITSHIGH(block_part1, 3, 36) << 1;
  871. pixel_indices_MSB = GETBITS(block_part2, 16, 31);
  872. pixel_indices_LSB = GETBITS(block_part2, 16, 15);
  873. if( (flipbit) == 0 )
  874. {
  875. // We should not flip
  876. shift=8;
  877. for(int x=startx+2; x<startx+4; x++)
  878. {
  879. for(int y=starty; y<starty+4; y++)
  880. {
  881. index = ((pixel_indices_MSB >> shift) & 1) << 1;
  882. index |= ((pixel_indices_LSB >> shift) & 1);
  883. shift++;
  884. index=unscramble[index];
  885. r=RED_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
  886. g=GREEN_CHANNEL(img,width,x,y,channels)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
  887. b=BLUE_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
  888. }
  889. }
  890. }
  891. else
  892. {
  893. // We should flip
  894. shift=2;
  895. for(int x=startx; x<startx+4; x++)
  896. {
  897. for(int y=starty+2; y<starty+4; y++)
  898. {
  899. index = ((pixel_indices_MSB >> shift) & 1) << 1;
  900. index |= ((pixel_indices_LSB >> shift) & 1);
  901. shift++;
  902. index=unscramble[index];
  903. r=RED_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
  904. g=GREEN_CHANNEL(img,width,x,y,channels)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
  905. b=BLUE_CHANNEL(img,width,x,y,channels) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
  906. }
  907. shift += 2;
  908. }
  909. }
  910. }
  911. }
  912. void decompressBlockDiffFlip(unsigned int block_part1, unsigned int block_part2, uint8 *img, int width, int height, int startx, int starty)
  913. {
  914. decompressBlockDiffFlipC(block_part1, block_part2, img, width, height, startx, starty, 3);
  915. }
  916. // Decompress an ETC2 RGB block
  917. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  918. void decompressBlockETC2c(unsigned int block_part1, unsigned int block_part2, uint8 *img, int width, int height, int startx, int starty, int channels)
  919. {
  920. int diffbit;
  921. signed char color1[3];
  922. signed char diff[3];
  923. signed char red, green, blue;
  924. diffbit = (GETBITSHIGH(block_part1, 1, 33));
  925. if( diffbit )
  926. {
  927. // We have diffbit = 1;
  928. // Base color
  929. color1[0]= GETBITSHIGH(block_part1, 5, 63);
  930. color1[1]= GETBITSHIGH(block_part1, 5, 55);
  931. color1[2]= GETBITSHIGH(block_part1, 5, 47);
  932. // Diff color
  933. diff[0]= GETBITSHIGH(block_part1, 3, 58);
  934. diff[1]= GETBITSHIGH(block_part1, 3, 50);
  935. diff[2]= GETBITSHIGH(block_part1, 3, 42);
  936. // Extend sign bit to entire byte.
  937. diff[0] = (diff[0] << 5);
  938. diff[1] = (diff[1] << 5);
  939. diff[2] = (diff[2] << 5);
  940. diff[0] = diff[0] >> 5;
  941. diff[1] = diff[1] >> 5;
  942. diff[2] = diff[2] >> 5;
  943. red = color1[0] + diff[0];
  944. green = color1[1] + diff[1];
  945. blue = color1[2] + diff[2];
  946. if(red < 0 || red > 31)
  947. {
  948. unsigned int block59_part1, block59_part2;
  949. unstuff59bits(block_part1, block_part2, block59_part1, block59_part2);
  950. decompressBlockTHUMB59Tc(block59_part1, block59_part2, img, width, height, startx, starty, channels);
  951. }
  952. else if (green < 0 || green > 31)
  953. {
  954. unsigned int block58_part1, block58_part2;
  955. unstuff58bits(block_part1, block_part2, block58_part1, block58_part2);
  956. decompressBlockTHUMB58Hc(block58_part1, block58_part2, img, width, height, startx, starty, channels);
  957. }
  958. else if(blue < 0 || blue > 31)
  959. {
  960. unsigned int block57_part1, block57_part2;
  961. unstuff57bits(block_part1, block_part2, block57_part1, block57_part2);
  962. decompressBlockPlanar57c(block57_part1, block57_part2, img, width, height, startx, starty, channels);
  963. }
  964. else
  965. {
  966. decompressBlockDiffFlipC(block_part1, block_part2, img, width, height, startx, starty, channels);
  967. }
  968. }
  969. else
  970. {
  971. // We have diffbit = 0;
  972. decompressBlockDiffFlipC(block_part1, block_part2, img, width, height, startx, starty, channels);
  973. }
  974. }
  975. void decompressBlockETC2(unsigned int block_part1, unsigned int block_part2, uint8 *img, int width, int height, int startx, int starty)
  976. {
  977. decompressBlockETC2c(block_part1, block_part2, img, width, height, startx, starty, 3);
  978. }
  979. // Decompress an ETC2 block with punchthrough alpha
  980. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  981. void decompressBlockDifferentialWithAlphaC(unsigned int block_part1, unsigned int block_part2, uint8* img, uint8* alpha, int width, int height, int startx, int starty, int channelsRGB)
  982. {
  983. uint8 avg_color[3], enc_color1[3], enc_color2[3];
  984. signed char diff[3];
  985. int table;
  986. int index,shift;
  987. int r,g,b;
  988. int diffbit;
  989. int flipbit;
  990. int channelsA;
  991. if(channelsRGB == 3)
  992. {
  993. // We will decode the alpha data to a separate memory area.
  994. channelsA = 1;
  995. }
  996. else
  997. {
  998. // We will decode the RGB data and the alpha data to the same memory area,
  999. // interleaved as RGBA.
  1000. channelsA = 4;
  1001. alpha = &img[0+3];
  1002. }
  1003. //the diffbit now encodes whether or not the entire alpha channel is 255.
  1004. diffbit = (GETBITSHIGH(block_part1, 1, 33));
  1005. flipbit = (GETBITSHIGH(block_part1, 1, 32));
  1006. // First decode left part of block.
  1007. enc_color1[0]= GETBITSHIGH(block_part1, 5, 63);
  1008. enc_color1[1]= GETBITSHIGH(block_part1, 5, 55);
  1009. enc_color1[2]= GETBITSHIGH(block_part1, 5, 47);
  1010. // Expand from 5 to 8 bits
  1011. avg_color[0] = (enc_color1[0] <<3) | (enc_color1[0] >> 2);
  1012. avg_color[1] = (enc_color1[1] <<3) | (enc_color1[1] >> 2);
  1013. avg_color[2] = (enc_color1[2] <<3) | (enc_color1[2] >> 2);
  1014. table = GETBITSHIGH(block_part1, 3, 39) << 1;
  1015. unsigned int pixel_indices_MSB, pixel_indices_LSB;
  1016. pixel_indices_MSB = GETBITS(block_part2, 16, 31);
  1017. pixel_indices_LSB = GETBITS(block_part2, 16, 15);
  1018. if( (flipbit) == 0 )
  1019. {
  1020. // We should not flip
  1021. shift = 0;
  1022. for(int x=startx; x<startx+2; x++)
  1023. {
  1024. for(int y=starty; y<starty+4; y++)
  1025. {
  1026. index = ((pixel_indices_MSB >> shift) & 1) << 1;
  1027. index |= ((pixel_indices_LSB >> shift) & 1);
  1028. shift++;
  1029. index=unscramble[index];
  1030. int mod = compressParams[table][index];
  1031. if(diffbit==0&&(index==1||index==2))
  1032. {
  1033. mod=0;
  1034. }
  1035. r=RED_CHANNEL(img,width,x,y,channelsRGB) =CLAMP(0,avg_color[0]+mod,255);
  1036. g=GREEN_CHANNEL(img,width,x,y,channelsRGB)=CLAMP(0,avg_color[1]+mod,255);
  1037. b=BLUE_CHANNEL(img,width,x,y,channelsRGB) =CLAMP(0,avg_color[2]+mod,255);
  1038. if(diffbit==0&&index==1)
  1039. {
  1040. alpha[(y*width+x)*channelsA]=0;
  1041. r=RED_CHANNEL(img,width,x,y,channelsRGB)=0;
  1042. g=GREEN_CHANNEL(img,width,x,y,channelsRGB)=0;
  1043. b=BLUE_CHANNEL(img,width,x,y,channelsRGB)=0;
  1044. }
  1045. else
  1046. {
  1047. alpha[(y*width+x)*channelsA]=255;
  1048. }
  1049. }
  1050. }
  1051. }
  1052. else
  1053. {
  1054. // We should flip
  1055. shift = 0;
  1056. for(int x=startx; x<startx+4; x++)
  1057. {
  1058. for(int y=starty; y<starty+2; y++)
  1059. {
  1060. index = ((pixel_indices_MSB >> shift) & 1) << 1;
  1061. index |= ((pixel_indices_LSB >> shift) & 1);
  1062. shift++;
  1063. index=unscramble[index];
  1064. int mod = compressParams[table][index];
  1065. if(diffbit==0&&(index==1||index==2))
  1066. {
  1067. mod=0;
  1068. }
  1069. r=RED_CHANNEL(img,width,x,y,channelsRGB) =CLAMP(0,avg_color[0]+mod,255);
  1070. g=GREEN_CHANNEL(img,width,x,y,channelsRGB)=CLAMP(0,avg_color[1]+mod,255);
  1071. b=BLUE_CHANNEL(img,width,x,y,channelsRGB) =CLAMP(0,avg_color[2]+mod,255);
  1072. if(diffbit==0&&index==1)
  1073. {
  1074. alpha[(y*width+x)*channelsA]=0;
  1075. r=RED_CHANNEL(img,width,x,y,channelsRGB)=0;
  1076. g=GREEN_CHANNEL(img,width,x,y,channelsRGB)=0;
  1077. b=BLUE_CHANNEL(img,width,x,y,channelsRGB)=0;
  1078. }
  1079. else
  1080. {
  1081. alpha[(y*width+x)*channelsA]=255;
  1082. }
  1083. }
  1084. shift+=2;
  1085. }
  1086. }
  1087. // Now decode right part of block.
  1088. diff[0]= GETBITSHIGH(block_part1, 3, 58);
  1089. diff[1]= GETBITSHIGH(block_part1, 3, 50);
  1090. diff[2]= GETBITSHIGH(block_part1, 3, 42);
  1091. // Extend sign bit to entire byte.
  1092. diff[0] = (diff[0] << 5);
  1093. diff[1] = (diff[1] << 5);
  1094. diff[2] = (diff[2] << 5);
  1095. diff[0] = diff[0] >> 5;
  1096. diff[1] = diff[1] >> 5;
  1097. diff[2] = diff[2] >> 5;
  1098. // Calculate second color
  1099. enc_color2[0]= enc_color1[0] + diff[0];
  1100. enc_color2[1]= enc_color1[1] + diff[1];
  1101. enc_color2[2]= enc_color1[2] + diff[2];
  1102. // Expand from 5 to 8 bits
  1103. avg_color[0] = (enc_color2[0] <<3) | (enc_color2[0] >> 2);
  1104. avg_color[1] = (enc_color2[1] <<3) | (enc_color2[1] >> 2);
  1105. avg_color[2] = (enc_color2[2] <<3) | (enc_color2[2] >> 2);
  1106. table = GETBITSHIGH(block_part1, 3, 36) << 1;
  1107. pixel_indices_MSB = GETBITS(block_part2, 16, 31);
  1108. pixel_indices_LSB = GETBITS(block_part2, 16, 15);
  1109. if( (flipbit) == 0 )
  1110. {
  1111. // We should not flip
  1112. shift=8;
  1113. for(int x=startx+2; x<startx+4; x++)
  1114. {
  1115. for(int y=starty; y<starty+4; y++)
  1116. {
  1117. index = ((pixel_indices_MSB >> shift) & 1) << 1;
  1118. index |= ((pixel_indices_LSB >> shift) & 1);
  1119. shift++;
  1120. index=unscramble[index];
  1121. int mod = compressParams[table][index];
  1122. if(diffbit==0&&(index==1||index==2))
  1123. {
  1124. mod=0;
  1125. }
  1126. r=RED_CHANNEL(img,width,x,y,channelsRGB) =CLAMP(0,avg_color[0]+mod,255);
  1127. g=GREEN_CHANNEL(img,width,x,y,channelsRGB)=CLAMP(0,avg_color[1]+mod,255);
  1128. b=BLUE_CHANNEL(img,width,x,y,channelsRGB) =CLAMP(0,avg_color[2]+mod,255);
  1129. if(diffbit==0&&index==1)
  1130. {
  1131. alpha[(y*width+x)*channelsA]=0;
  1132. r=RED_CHANNEL(img,width,x,y,channelsRGB)=0;
  1133. g=GREEN_CHANNEL(img,width,x,y,channelsRGB)=0;
  1134. b=BLUE_CHANNEL(img,width,x,y,channelsRGB)=0;
  1135. }
  1136. else
  1137. {
  1138. alpha[(y*width+x)*channelsA]=255;
  1139. }
  1140. }
  1141. }
  1142. }
  1143. else
  1144. {
  1145. // We should flip
  1146. shift=2;
  1147. for(int x=startx; x<startx+4; x++)
  1148. {
  1149. for(int y=starty+2; y<starty+4; y++)
  1150. {
  1151. index = ((pixel_indices_MSB >> shift) & 1) << 1;
  1152. index |= ((pixel_indices_LSB >> shift) & 1);
  1153. shift++;
  1154. index=unscramble[index];
  1155. int mod = compressParams[table][index];
  1156. if(diffbit==0&&(index==1||index==2))
  1157. {
  1158. mod=0;
  1159. }
  1160. r=RED_CHANNEL(img,width,x,y,channelsRGB) =CLAMP(0,avg_color[0]+mod,255);
  1161. g=GREEN_CHANNEL(img,width,x,y,channelsRGB)=CLAMP(0,avg_color[1]+mod,255);
  1162. b=BLUE_CHANNEL(img,width,x,y,channelsRGB) =CLAMP(0,avg_color[2]+mod,255);
  1163. if(diffbit==0&&index==1)
  1164. {
  1165. alpha[(y*width+x)*channelsA]=0;
  1166. r=RED_CHANNEL(img,width,x,y,channelsRGB)=0;
  1167. g=GREEN_CHANNEL(img,width,x,y,channelsRGB)=0;
  1168. b=BLUE_CHANNEL(img,width,x,y,channelsRGB)=0;
  1169. }
  1170. else
  1171. {
  1172. alpha[(y*width+x)*channelsA]=255;
  1173. }
  1174. }
  1175. shift += 2;
  1176. }
  1177. }
  1178. }
  1179. void decompressBlockDifferentialWithAlpha(unsigned int block_part1, unsigned int block_part2, uint8* img, uint8* alpha, int width, int height, int startx, int starty)
  1180. {
  1181. decompressBlockDifferentialWithAlphaC(block_part1, block_part2, img, alpha, width, height, startx, starty, 3);
  1182. }
  1183. // similar to regular decompression, but alpha channel is set to 0 if pixel index is 2, otherwise 255.
  1184. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1185. void decompressBlockTHUMB59TAlphaC(unsigned int block_part1, unsigned int block_part2, uint8 *img, uint8* alpha, int width, int height, int startx, int starty, int channelsRGB)
  1186. {
  1187. uint8 colorsRGB444[2][3];
  1188. uint8 colors[2][3];
  1189. uint8 paint_colors[4][3];
  1190. uint8 distance;
  1191. uint8 block_mask[4][4];
  1192. int channelsA;
  1193. if(channelsRGB == 3)
  1194. {
  1195. // We will decode the alpha data to a separate memory area.
  1196. channelsA = 1;
  1197. }
  1198. else
  1199. {
  1200. // We will decode the RGB data and the alpha data to the same memory area,
  1201. // interleaved as RGBA.
  1202. channelsA = 4;
  1203. alpha = &img[0+3];
  1204. }
  1205. // First decode left part of block.
  1206. colorsRGB444[0][R]= GETBITSHIGH(block_part1, 4, 58);
  1207. colorsRGB444[0][G]= GETBITSHIGH(block_part1, 4, 54);
  1208. colorsRGB444[0][B]= GETBITSHIGH(block_part1, 4, 50);
  1209. colorsRGB444[1][R]= GETBITSHIGH(block_part1, 4, 46);
  1210. colorsRGB444[1][G]= GETBITSHIGH(block_part1, 4, 42);
  1211. colorsRGB444[1][B]= GETBITSHIGH(block_part1, 4, 38);
  1212. distance = GETBITSHIGH(block_part1, TABLE_BITS_59T, 34);
  1213. // Extend the two colors to RGB888
  1214. decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
  1215. calculatePaintColors59T(distance, PATTERN_T, colors, paint_colors);
  1216. // Choose one of the four paint colors for each texel
  1217. for (uint8 x = 0; x < BLOCKWIDTH; ++x)
  1218. {
  1219. for (uint8 y = 0; y < BLOCKHEIGHT; ++y)
  1220. {
  1221. //block_mask[x][y] = GETBITS(block_part2,2,31-(y*4+x)*2);
  1222. block_mask[x][y] = GETBITS(block_part2,1,(y+x*4)+16)<<1;
  1223. block_mask[x][y] |= GETBITS(block_part2,1,(y+x*4));
  1224. img[channelsRGB*((starty+y)*width+startx+x)+R] =
  1225. CLAMP(0,paint_colors[block_mask[x][y]][R],255); // RED
  1226. img[channelsRGB*((starty+y)*width+startx+x)+G] =
  1227. CLAMP(0,paint_colors[block_mask[x][y]][G],255); // GREEN
  1228. img[channelsRGB*((starty+y)*width+startx+x)+B] =
  1229. CLAMP(0,paint_colors[block_mask[x][y]][B],255); // BLUE
  1230. if(block_mask[x][y]==2)
  1231. {
  1232. alpha[channelsA*(x+startx+(y+starty)*width)]=0;
  1233. img[channelsRGB*((starty+y)*width+startx+x)+R] =0;
  1234. img[channelsRGB*((starty+y)*width+startx+x)+G] =0;
  1235. img[channelsRGB*((starty+y)*width+startx+x)+B] =0;
  1236. }
  1237. else
  1238. alpha[channelsA*(x+startx+(y+starty)*width)]=255;
  1239. }
  1240. }
  1241. }
  1242. void decompressBlockTHUMB59TAlpha(unsigned int block_part1, unsigned int block_part2, uint8 *img, uint8* alpha, int width, int height, int startx, int starty)
  1243. {
  1244. decompressBlockTHUMB59TAlphaC(block_part1, block_part2, img, alpha, width, height, startx, starty, 3);
  1245. }
  1246. // Decompress an H-mode block with alpha
  1247. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1248. void decompressBlockTHUMB58HAlphaC(unsigned int block_part1, unsigned int block_part2, uint8 *img, uint8* alpha, int width, int height, int startx, int starty, int channelsRGB)
  1249. {
  1250. unsigned int col0, col1;
  1251. uint8 colors[2][3];
  1252. uint8 colorsRGB444[2][3];
  1253. uint8 paint_colors[4][3];
  1254. uint8 distance;
  1255. uint8 block_mask[4][4];
  1256. int channelsA;
  1257. if(channelsRGB == 3)
  1258. {
  1259. // We will decode the alpha data to a separate memory area.
  1260. channelsA = 1;
  1261. }
  1262. else
  1263. {
  1264. // We will decode the RGB data and the alpha data to the same memory area,
  1265. // interleaved as RGBA.
  1266. channelsA = 4;
  1267. alpha = &img[0+3];
  1268. }
  1269. // First decode left part of block.
  1270. colorsRGB444[0][R]= GETBITSHIGH(block_part1, 4, 57);
  1271. colorsRGB444[0][G]= GETBITSHIGH(block_part1, 4, 53);
  1272. colorsRGB444[0][B]= GETBITSHIGH(block_part1, 4, 49);
  1273. colorsRGB444[1][R]= GETBITSHIGH(block_part1, 4, 45);
  1274. colorsRGB444[1][G]= GETBITSHIGH(block_part1, 4, 41);
  1275. colorsRGB444[1][B]= GETBITSHIGH(block_part1, 4, 37);
  1276. distance = 0;
  1277. distance = (GETBITSHIGH(block_part1, 2, 33)) << 1;
  1278. col0 = GETBITSHIGH(block_part1, 12, 57);
  1279. col1 = GETBITSHIGH(block_part1, 12, 45);
  1280. if(col0 >= col1)
  1281. {
  1282. distance |= 1;
  1283. }
  1284. // Extend the two colors to RGB888
  1285. decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
  1286. calculatePaintColors58H(distance, PATTERN_H, colors, paint_colors);
  1287. // Choose one of the four paint colors for each texel
  1288. for (uint8 x = 0; x < BLOCKWIDTH; ++x)
  1289. {
  1290. for (uint8 y = 0; y < BLOCKHEIGHT; ++y)
  1291. {
  1292. //block_mask[x][y] = GETBITS(block_part2,2,31-(y*4+x)*2);
  1293. block_mask[x][y] = GETBITS(block_part2,1,(y+x*4)+16)<<1;
  1294. block_mask[x][y] |= GETBITS(block_part2,1,(y+x*4));
  1295. img[channelsRGB*((starty+y)*width+startx+x)+R] =
  1296. CLAMP(0,paint_colors[block_mask[x][y]][R],255); // RED
  1297. img[channelsRGB*((starty+y)*width+startx+x)+G] =
  1298. CLAMP(0,paint_colors[block_mask[x][y]][G],255); // GREEN
  1299. img[channelsRGB*((starty+y)*width+startx+x)+B] =
  1300. CLAMP(0,paint_colors[block_mask[x][y]][B],255); // BLUE
  1301. if(block_mask[x][y]==2)
  1302. {
  1303. alpha[channelsA*(x+startx+(y+starty)*width)]=0;
  1304. img[channelsRGB*((starty+y)*width+startx+x)+R] =0;
  1305. img[channelsRGB*((starty+y)*width+startx+x)+G] =0;
  1306. img[channelsRGB*((starty+y)*width+startx+x)+B] =0;
  1307. }
  1308. else
  1309. alpha[channelsA*(x+startx+(y+starty)*width)]=255;
  1310. }
  1311. }
  1312. }
  1313. void decompressBlockTHUMB58HAlpha(unsigned int block_part1, unsigned int block_part2, uint8 *img, uint8* alpha, int width, int height, int startx, int starty)
  1314. {
  1315. decompressBlockTHUMB58HAlphaC(block_part1, block_part2, img, alpha, width, height, startx, starty, 3);
  1316. }
  1317. // Decompression function for ETC2_RGBA1 format.
  1318. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1319. void decompressBlockETC21BitAlphaC(unsigned int block_part1, unsigned int block_part2, uint8 *img, uint8* alphaimg, int width, int height, int startx, int starty, int channelsRGB)
  1320. {
  1321. int diffbit;
  1322. signed char color1[3];
  1323. signed char diff[3];
  1324. signed char red, green, blue;
  1325. int channelsA;
  1326. if(channelsRGB == 3)
  1327. {
  1328. // We will decode the alpha data to a separate memory area.
  1329. channelsA = 1;
  1330. }
  1331. else
  1332. {
  1333. // We will decode the RGB data and the alpha data to the same memory area,
  1334. // interleaved as RGBA.
  1335. channelsA = 4;
  1336. alphaimg = &img[0+3];
  1337. }
  1338. diffbit = (GETBITSHIGH(block_part1, 1, 33));
  1339. if( diffbit )
  1340. {
  1341. // We have diffbit = 1, meaning no transparent pixels. regular decompression.
  1342. // Base color
  1343. color1[0]= GETBITSHIGH(block_part1, 5, 63);
  1344. color1[1]= GETBITSHIGH(block_part1, 5, 55);
  1345. color1[2]= GETBITSHIGH(block_part1, 5, 47);
  1346. // Diff color
  1347. diff[0]= GETBITSHIGH(block_part1, 3, 58);
  1348. diff[1]= GETBITSHIGH(block_part1, 3, 50);
  1349. diff[2]= GETBITSHIGH(block_part1, 3, 42);
  1350. // Extend sign bit to entire byte.
  1351. diff[0] = (diff[0] << 5);
  1352. diff[1] = (diff[1] << 5);
  1353. diff[2] = (diff[2] << 5);
  1354. diff[0] = diff[0] >> 5;
  1355. diff[1] = diff[1] >> 5;
  1356. diff[2] = diff[2] >> 5;
  1357. red = color1[0] + diff[0];
  1358. green = color1[1] + diff[1];
  1359. blue = color1[2] + diff[2];
  1360. if(red < 0 || red > 31)
  1361. {
  1362. unsigned int block59_part1, block59_part2;
  1363. unstuff59bits(block_part1, block_part2, block59_part1, block59_part2);
  1364. decompressBlockTHUMB59Tc(block59_part1, block59_part2, img, width, height, startx, starty, channelsRGB);
  1365. }
  1366. else if (green < 0 || green > 31)
  1367. {
  1368. unsigned int block58_part1, block58_part2;
  1369. unstuff58bits(block_part1, block_part2, block58_part1, block58_part2);
  1370. decompressBlockTHUMB58Hc(block58_part1, block58_part2, img, width, height, startx, starty, channelsRGB);
  1371. }
  1372. else if(blue < 0 || blue > 31)
  1373. {
  1374. unsigned int block57_part1, block57_part2;
  1375. unstuff57bits(block_part1, block_part2, block57_part1, block57_part2);
  1376. decompressBlockPlanar57c(block57_part1, block57_part2, img, width, height, startx, starty, channelsRGB);
  1377. }
  1378. else
  1379. {
  1380. decompressBlockDifferentialWithAlphaC(block_part1, block_part2, img, alphaimg, width, height, startx, starty, channelsRGB);
  1381. }
  1382. for(int x=startx; x<startx+4; x++)
  1383. {
  1384. for(int y=starty; y<starty+4; y++)
  1385. {
  1386. alphaimg[channelsA*(x+y*width)]=255;
  1387. }
  1388. }
  1389. }
  1390. else
  1391. {
  1392. // We have diffbit = 0, transparent pixels. Only T-, H- or regular diff-mode possible.
  1393. // Base color
  1394. color1[0]= GETBITSHIGH(block_part1, 5, 63);
  1395. color1[1]= GETBITSHIGH(block_part1, 5, 55);
  1396. color1[2]= GETBITSHIGH(block_part1, 5, 47);
  1397. // Diff color
  1398. diff[0]= GETBITSHIGH(block_part1, 3, 58);
  1399. diff[1]= GETBITSHIGH(block_part1, 3, 50);
  1400. diff[2]= GETBITSHIGH(block_part1, 3, 42);
  1401. // Extend sign bit to entire byte.
  1402. diff[0] = (diff[0] << 5);
  1403. diff[1] = (diff[1] << 5);
  1404. diff[2] = (diff[2] << 5);
  1405. diff[0] = diff[0] >> 5;
  1406. diff[1] = diff[1] >> 5;
  1407. diff[2] = diff[2] >> 5;
  1408. red = color1[0] + diff[0];
  1409. green = color1[1] + diff[1];
  1410. blue = color1[2] + diff[2];
  1411. if(red < 0 || red > 31)
  1412. {
  1413. unsigned int block59_part1, block59_part2;
  1414. unstuff59bits(block_part1, block_part2, block59_part1, block59_part2);
  1415. decompressBlockTHUMB59TAlphaC(block59_part1, block59_part2, img, alphaimg, width, height, startx, starty, channelsRGB);
  1416. }
  1417. else if(green < 0 || green > 31)
  1418. {
  1419. unsigned int block58_part1, block58_part2;
  1420. unstuff58bits(block_part1, block_part2, block58_part1, block58_part2);
  1421. decompressBlockTHUMB58HAlphaC(block58_part1, block58_part2, img, alphaimg, width, height, startx, starty, channelsRGB);
  1422. }
  1423. else if(blue < 0 || blue > 31)
  1424. {
  1425. unsigned int block57_part1, block57_part2;
  1426. unstuff57bits(block_part1, block_part2, block57_part1, block57_part2);
  1427. decompressBlockPlanar57c(block57_part1, block57_part2, img, width, height, startx, starty, channelsRGB);
  1428. for(int x=startx; x<startx+4; x++)
  1429. {
  1430. for(int y=starty; y<starty+4; y++)
  1431. {
  1432. alphaimg[channelsA*(x+y*width)]=255;
  1433. }
  1434. }
  1435. }
  1436. else
  1437. decompressBlockDifferentialWithAlphaC(block_part1, block_part2, img,alphaimg, width, height, startx, starty, channelsRGB);
  1438. }
  1439. }
  1440. void decompressBlockETC21BitAlpha(unsigned int block_part1, unsigned int block_part2, uint8 *img, uint8* alphaimg, int width, int height, int startx, int starty)
  1441. {
  1442. decompressBlockETC21BitAlphaC(block_part1, block_part2, img, alphaimg, width, height, startx, starty, 3);
  1443. }
  1444. //
  1445. // Utility functions used for alpha compression
  1446. //
  1447. // bit number frompos is extracted from input, and moved to bit number topos in the return value.
  1448. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1449. uint8 getbit(uint8 input, int frompos, int topos)
  1450. {
  1451. uint8 output=0;
  1452. if(frompos>topos)
  1453. return ((1<<frompos)&input)>>(frompos-topos);
  1454. return ((1<<frompos)&input)<<(topos-frompos);
  1455. }
  1456. // takes as input a value, returns the value clamped to the interval [0,255].
  1457. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1458. int clamp(int val)
  1459. {
  1460. if(val<0)
  1461. val=0;
  1462. if(val>255)
  1463. val=255;
  1464. return val;
  1465. }
  1466. // Decodes tha alpha component in a block coded with GL_COMPRESSED_RGBA8_ETC2_EAC.
  1467. // Note that this decoding is slightly different from that of GL_COMPRESSED_R11_EAC.
  1468. // However, a hardware decoder can share gates between the two formats as explained
  1469. // in the specification under GL_COMPRESSED_R11_EAC.
  1470. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1471. void decompressBlockAlphaC(uint8* data, uint8* img, int width, int height, int ix, int iy, int channels)
  1472. {
  1473. int alpha = data[0];
  1474. int table = data[1];
  1475. int bit=0;
  1476. int byte=2;
  1477. //extract an alpha value for each pixel.
  1478. for(int x=0; x<4; x++)
  1479. {
  1480. for(int y=0; y<4; y++)
  1481. {
  1482. //Extract table index
  1483. int index=0;
  1484. for(int bitpos=0; bitpos<3; bitpos++)
  1485. {
  1486. index|=getbit(data[byte],7-bit,2-bitpos);
  1487. bit++;
  1488. if(bit>7)
  1489. {
  1490. bit=0;
  1491. byte++;
  1492. }
  1493. }
  1494. img[(ix+x+(iy+y)*width)*channels]=clamp(alpha +alphaTable[table][index]);
  1495. }
  1496. }
  1497. }
  1498. void decompressBlockAlpha(uint8* data, uint8* img, int width, int height, int ix, int iy)
  1499. {
  1500. decompressBlockAlphaC(data, img, width, height, ix, iy, 1);
  1501. }
  1502. // Does decompression and then immediately converts from 11 bit signed to a 16-bit format.
  1503. //
  1504. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1505. int16 get16bits11signed(int base, int table, int mul, int index)
  1506. {
  1507. int elevenbase = base-128;
  1508. if(elevenbase==-128)
  1509. elevenbase=-127;
  1510. elevenbase*=8;
  1511. //i want the positive value here
  1512. int tabVal = -alphaBase[table][3-index%4]-1;
  1513. //and the sign, please
  1514. int sign = 1-(index/4);
  1515. if(sign)
  1516. tabVal=tabVal+1;
  1517. int elevenTabVal = tabVal*8;
  1518. if(mul!=0)
  1519. elevenTabVal*=mul;
  1520. else
  1521. elevenTabVal/=8;
  1522. if(sign)
  1523. elevenTabVal=-elevenTabVal;
  1524. //calculate sum
  1525. int elevenbits = elevenbase+elevenTabVal;
  1526. //clamp..
  1527. if(elevenbits>=1024)
  1528. elevenbits=1023;
  1529. else if(elevenbits<-1023)
  1530. elevenbits=-1023;
  1531. //this is the value we would actually output..
  1532. //but there aren't any good 11-bit file or uncompressed GL formats
  1533. //so we extend to 15 bits signed.
  1534. sign = elevenbits<0;
  1535. elevenbits=abs(elevenbits);
  1536. int16 fifteenbits = (elevenbits<<5)+(elevenbits>>5);
  1537. int16 sixteenbits=fifteenbits;
  1538. if(sign)
  1539. sixteenbits=-sixteenbits;
  1540. return sixteenbits;
  1541. }
  1542. // Does decompression and then immediately converts from 11 bit signed to a 16-bit format
  1543. // Calculates the 11 bit value represented by base, table, mul and index, and extends it to 16 bits.
  1544. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1545. uint16 get16bits11bits(int base, int table, int mul, int index)
  1546. {
  1547. int elevenbase = base*8+4;
  1548. //i want the positive value here
  1549. int tabVal = -alphaBase[table][3-index%4]-1;
  1550. //and the sign, please
  1551. int sign = 1-(index/4);
  1552. if(sign)
  1553. tabVal=tabVal+1;
  1554. int elevenTabVal = tabVal*8;
  1555. if(mul!=0)
  1556. elevenTabVal*=mul;
  1557. else
  1558. elevenTabVal/=8;
  1559. if(sign)
  1560. elevenTabVal=-elevenTabVal;
  1561. //calculate sum
  1562. int elevenbits = elevenbase+elevenTabVal;
  1563. //clamp..
  1564. if(elevenbits>=256*8)
  1565. elevenbits=256*8-1;
  1566. else if(elevenbits<0)
  1567. elevenbits=0;
  1568. //elevenbits now contains the 11 bit alpha value as defined in the spec.
  1569. //extend to 16 bits before returning, since we don't have any good 11-bit file formats.
  1570. uint16 sixteenbits = (elevenbits<<5)+(elevenbits>>6);
  1571. return sixteenbits;
  1572. }
  1573. // Decompresses a block using one of the GL_COMPRESSED_R11_EAC or GL_COMPRESSED_SIGNED_R11_EAC-formats
  1574. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1575. void decompressBlockAlpha16bitC(uint8* data, uint8* img, int width, int height, int ix, int iy, int channels)
  1576. {
  1577. int alpha = data[0];
  1578. int table = data[1];
  1579. if(formatSigned)
  1580. {
  1581. //if we have a signed format, the base value is given as a signed byte. We convert it to (0-255) here,
  1582. //so more code can be shared with the unsigned mode.
  1583. alpha = *((signed char*)(&data[0]));
  1584. alpha = alpha+128;
  1585. }
  1586. int bit=0;
  1587. int byte=2;
  1588. //extract an alpha value for each pixel.
  1589. for(int x=0; x<4; x++)
  1590. {
  1591. for(int y=0; y<4; y++)
  1592. {
  1593. //Extract table index
  1594. int index=0;
  1595. for(int bitpos=0; bitpos<3; bitpos++)
  1596. {
  1597. index|=getbit(data[byte],7-bit,2-bitpos);
  1598. bit++;
  1599. if(bit>7)
  1600. {
  1601. bit=0;
  1602. byte++;
  1603. }
  1604. }
  1605. int windex = channels*(2*(ix+x+(iy+y)*width));
  1606. #if !PGMOUT
  1607. if(formatSigned)
  1608. {
  1609. *(int16 *)&img[windex] = get16bits11signed(alpha,(table%16),(table/16),index);
  1610. }
  1611. else
  1612. {
  1613. *(uint16 *)&img[windex] = get16bits11bits(alpha,(table%16),(table/16),index);
  1614. }
  1615. #else
  1616. //make data compatible with the .pgm format. See the comment in compressBlockAlpha16() for details.
  1617. uint16 uSixteen;
  1618. if (formatSigned)
  1619. {
  1620. //the pgm-format only allows unsigned images,
  1621. //so we add 2^15 to get a 16-bit value.
  1622. uSixteen = get16bits11signed(alpha,(table%16),(table/16),index) + 256*128;
  1623. }
  1624. else
  1625. {
  1626. uSixteen = get16bits11bits(alpha,(table%16),(table/16),index);
  1627. }
  1628. //byte swap for pgm
  1629. img[windex] = uSixteen/256;
  1630. img[windex+1] = uSixteen%256;
  1631. #endif
  1632. }
  1633. }
  1634. }
  1635. void decompressBlockAlpha16bit(uint8* data, uint8* img, int width, int height, int ix, int iy)
  1636. {
  1637. decompressBlockAlpha16bitC(data, img, width, height, ix, iy, 1);
  1638. }