etcdec.cxx 66 KB

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