EtcBlock4x4EncodingBits.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright 2015 The Etc2Comp Authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <assert.h>
  18. namespace Etc
  19. {
  20. // ################################################################################
  21. // Block4x4EncodingBits
  22. // Base class for Block4x4EncodingBits_XXXX
  23. // ################################################################################
  24. class Block4x4EncodingBits
  25. {
  26. public:
  27. enum class Format
  28. {
  29. UNKNOWN,
  30. //
  31. RGB8,
  32. RGBA8,
  33. R11,
  34. RG11,
  35. RGB8A1,
  36. //
  37. FORMATS
  38. };
  39. static unsigned int GetBytesPerBlock(Format a_format)
  40. {
  41. switch (a_format)
  42. {
  43. case Format::RGB8:
  44. case Format::R11:
  45. case Format::RGB8A1:
  46. return 8;
  47. break;
  48. case Format::RGBA8:
  49. case Format::RG11:
  50. return 16;
  51. break;
  52. default:
  53. return 0;
  54. break;
  55. }
  56. }
  57. };
  58. // ################################################################################
  59. // Block4x4EncodingBits_RGB8
  60. // Encoding bits for the RGB portion of ETC1, RGB8, RGB8A1 and RGBA8
  61. // ################################################################################
  62. class Block4x4EncodingBits_RGB8
  63. {
  64. public:
  65. static const unsigned int BYTES_PER_BLOCK = 8;
  66. inline Block4x4EncodingBits_RGB8(void)
  67. {
  68. assert(sizeof(Block4x4EncodingBits_RGB8) == BYTES_PER_BLOCK);
  69. for (unsigned int uiByte = 0; uiByte < BYTES_PER_BLOCK; uiByte++)
  70. {
  71. auc[uiByte] = 0;
  72. }
  73. }
  74. typedef struct
  75. {
  76. unsigned red2 : 4;
  77. unsigned red1 : 4;
  78. //
  79. unsigned green2 : 4;
  80. unsigned green1 : 4;
  81. //
  82. unsigned blue2 : 4;
  83. unsigned blue1 : 4;
  84. //
  85. unsigned flip : 1;
  86. unsigned diff : 1;
  87. unsigned cw2 : 3;
  88. unsigned cw1 : 3;
  89. //
  90. unsigned int selectors;
  91. } Individual;
  92. typedef struct
  93. {
  94. signed dred2 : 3;
  95. unsigned red1 : 5;
  96. //
  97. signed dgreen2 : 3;
  98. unsigned green1 : 5;
  99. //
  100. signed dblue2 : 3;
  101. unsigned blue1 : 5;
  102. //
  103. unsigned flip : 1;
  104. unsigned diff : 1;
  105. unsigned cw2 : 3;
  106. unsigned cw1 : 3;
  107. //
  108. unsigned int selectors;
  109. } Differential;
  110. typedef struct
  111. {
  112. unsigned red1b : 2;
  113. unsigned detect2 : 1;
  114. unsigned red1a : 2;
  115. unsigned detect1 : 3;
  116. //
  117. unsigned blue1 : 4;
  118. unsigned green1 : 4;
  119. //
  120. unsigned green2 : 4;
  121. unsigned red2 : 4;
  122. //
  123. unsigned db : 1;
  124. unsigned diff : 1;
  125. unsigned da : 2;
  126. unsigned blue2 : 4;
  127. //
  128. unsigned int selectors;
  129. } T;
  130. typedef struct
  131. {
  132. unsigned green1a : 3;
  133. unsigned red1 : 4;
  134. unsigned detect1 : 1;
  135. //
  136. unsigned blue1b : 2;
  137. unsigned detect3 : 1;
  138. unsigned blue1a : 1;
  139. unsigned green1b : 1;
  140. unsigned detect2 : 3;
  141. //
  142. unsigned green2a : 3;
  143. unsigned red2 : 4;
  144. unsigned blue1c : 1;
  145. //
  146. unsigned db : 1;
  147. unsigned diff : 1;
  148. unsigned da : 1;
  149. unsigned blue2 : 4;
  150. unsigned green2b : 1;
  151. //
  152. unsigned int selectors;
  153. } H;
  154. typedef struct
  155. {
  156. unsigned originGreen1 : 1;
  157. unsigned originRed : 6;
  158. unsigned detect1 : 1;
  159. //
  160. unsigned originBlue1 : 1;
  161. unsigned originGreen2 : 6;
  162. unsigned detect2 : 1;
  163. //
  164. unsigned originBlue3 : 2;
  165. unsigned detect4 : 1;
  166. unsigned originBlue2 : 2;
  167. unsigned detect3 : 3;
  168. //
  169. unsigned horizRed2 : 1;
  170. unsigned diff : 1;
  171. unsigned horizRed1 : 5;
  172. unsigned originBlue4 : 1;
  173. //
  174. unsigned horizBlue1: 1;
  175. unsigned horizGreen : 7;
  176. //
  177. unsigned vertRed1 : 3;
  178. unsigned horizBlue2 : 5;
  179. //
  180. unsigned vertGreen1 : 5;
  181. unsigned vertRed2 : 3;
  182. //
  183. unsigned vertBlue : 6;
  184. unsigned vertGreen2 : 2;
  185. } Planar;
  186. union
  187. {
  188. unsigned char auc[BYTES_PER_BLOCK];
  189. unsigned long int ul;
  190. Individual individual;
  191. Differential differential;
  192. T t;
  193. H h;
  194. Planar planar;
  195. };
  196. };
  197. // ################################################################################
  198. // Block4x4EncodingBits_A8
  199. // Encoding bits for the A portion of RGBA8
  200. // ################################################################################
  201. class Block4x4EncodingBits_A8
  202. {
  203. public:
  204. static const unsigned int BYTES_PER_BLOCK = 8;
  205. static const unsigned int SELECTOR_BYTES = 6;
  206. typedef struct
  207. {
  208. unsigned base : 8;
  209. unsigned table : 4;
  210. unsigned multiplier : 4;
  211. unsigned selectors0 : 8;
  212. unsigned selectors1 : 8;
  213. unsigned selectors2 : 8;
  214. unsigned selectors3 : 8;
  215. unsigned selectors4 : 8;
  216. unsigned selectors5 : 8;
  217. } Data;
  218. Data data;
  219. };
  220. // ################################################################################
  221. // Block4x4EncodingBits_R11
  222. // Encoding bits for the R portion of R11
  223. // ################################################################################
  224. class Block4x4EncodingBits_R11
  225. {
  226. public:
  227. static const unsigned int BYTES_PER_BLOCK = 8;
  228. static const unsigned int SELECTOR_BYTES = 6;
  229. typedef struct
  230. {
  231. unsigned base : 8;
  232. unsigned table : 4;
  233. unsigned multiplier : 4;
  234. unsigned selectors0 : 8;
  235. unsigned selectors1 : 8;
  236. unsigned selectors2 : 8;
  237. unsigned selectors3 : 8;
  238. unsigned selectors4 : 8;
  239. unsigned selectors5 : 8;
  240. } Data;
  241. Data data;
  242. };
  243. class Block4x4EncodingBits_RG11
  244. {
  245. public:
  246. static const unsigned int BYTES_PER_BLOCK = 16;
  247. static const unsigned int SELECTOR_BYTES = 12;
  248. typedef struct
  249. {
  250. //Red portion
  251. unsigned baseR : 8;
  252. unsigned tableIndexR : 4;
  253. unsigned multiplierR : 4;
  254. unsigned selectorsR0 : 8;
  255. unsigned selectorsR1 : 8;
  256. unsigned selectorsR2 : 8;
  257. unsigned selectorsR3 : 8;
  258. unsigned selectorsR4 : 8;
  259. unsigned selectorsR5 : 8;
  260. //Green portion
  261. unsigned baseG : 8;
  262. unsigned tableIndexG : 4;
  263. unsigned multiplierG : 4;
  264. unsigned selectorsG0 : 8;
  265. unsigned selectorsG1 : 8;
  266. unsigned selectorsG2 : 8;
  267. unsigned selectorsG3 : 8;
  268. unsigned selectorsG4 : 8;
  269. unsigned selectorsG5 : 8;
  270. } Data;
  271. Data data;
  272. };
  273. }