ETC2Decoder.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. using Ryujinx.Common;
  2. using System;
  3. using System.Buffers.Binary;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Graphics.Texture
  6. {
  7. public static class ETC2Decoder
  8. {
  9. private const uint AlphaMask = 0xff000000u;
  10. private const int BlockWidth = 4;
  11. private const int BlockHeight = 4;
  12. private static readonly int[][] _etc1Lut =
  13. {
  14. new int[] { 2, 8, -2, -8 },
  15. new int[] { 5, 17, -5, -17 },
  16. new int[] { 9, 29, -9, -29 },
  17. new int[] { 13, 42, -13, -42 },
  18. new int[] { 18, 60, -18, -60 },
  19. new int[] { 24, 80, -24, -80 },
  20. new int[] { 33, 106, -33, -106 },
  21. new int[] { 47, 183, -47, -183 }
  22. };
  23. private static readonly int[] _etc2Lut =
  24. {
  25. 3, 6, 11, 16, 23, 32, 41, 64
  26. };
  27. private static readonly int[][] _etc2AlphaLut =
  28. {
  29. new int[] { -3, -6, -9, -15, 2, 5, 8, 14 },
  30. new int[] { -3, -7, -10, -13, 2, 6, 9, 12 },
  31. new int[] { -2, -5, -8, -13, 1, 4, 7, 12 },
  32. new int[] { -2, -4, -6, -13, 1, 3, 5, 12 },
  33. new int[] { -3, -6, -8, -12, 2, 5, 7, 11 },
  34. new int[] { -3, -7, -9, -11, 2, 6, 8, 10 },
  35. new int[] { -4, -7, -8, -11, 3, 6, 7, 10 },
  36. new int[] { -3, -5, -8, -11, 2, 4, 7, 10 },
  37. new int[] { -2, -6, -8, -10, 1, 5, 7, 9 },
  38. new int[] { -2, -5, -8, -10, 1, 4, 7, 9 },
  39. new int[] { -2, -4, -8, -10, 1, 3, 7, 9 },
  40. new int[] { -2, -5, -7, -10, 1, 4, 6, 9 },
  41. new int[] { -3, -4, -7, -10, 2, 3, 6, 9 },
  42. new int[] { -1, -2, -3, -10, 0, 1, 2, 9 },
  43. new int[] { -4, -6, -8, -9, 3, 5, 7, 8 },
  44. new int[] { -3, -5, -7, -9, 2, 4, 6, 8 }
  45. };
  46. public static byte[] DecodeRgb(ReadOnlySpan<byte> data, int width, int height, int depth, int levels, int layers)
  47. {
  48. ReadOnlySpan<ulong> dataUlong = MemoryMarshal.Cast<byte, ulong>(data);
  49. int inputOffset = 0;
  50. byte[] output = new byte[CalculateOutputSize(width, height, depth, levels, layers)];
  51. Span<uint> outputUint = MemoryMarshal.Cast<byte, uint>(output);
  52. Span<uint> tile = stackalloc uint[BlockWidth * BlockHeight];
  53. int imageBaseOOffs = 0;
  54. for (int l = 0; l < levels; l++)
  55. {
  56. int wInBlocks = BitUtils.DivRoundUp(width, BlockWidth);
  57. int hInBlocks = BitUtils.DivRoundUp(height, BlockHeight);
  58. for (int l2 = 0; l2 < layers; l2++)
  59. {
  60. for (int z = 0; z < depth; z++)
  61. {
  62. for (int y = 0; y < hInBlocks; y++)
  63. {
  64. int ty = y * BlockHeight;
  65. int bh = Math.Min(BlockHeight, height - ty);
  66. for (int x = 0; x < wInBlocks; x++)
  67. {
  68. int tx = x * BlockWidth;
  69. int bw = Math.Min(BlockWidth, width - tx);
  70. ulong colorBlock = dataUlong[inputOffset++];
  71. DecodeBlock(tile, colorBlock);
  72. for (int py = 0; py < bh; py++)
  73. {
  74. int oOffsBase = imageBaseOOffs + ((ty + py) * width) + tx;
  75. for (int px = 0; px < bw; px++)
  76. {
  77. int oOffs = oOffsBase + px;
  78. outputUint[oOffs] = tile[py * BlockWidth + px] | AlphaMask;
  79. }
  80. }
  81. }
  82. }
  83. imageBaseOOffs += width * height;
  84. }
  85. }
  86. width = Math.Max(1, width >> 1);
  87. height = Math.Max(1, height >> 1);
  88. depth = Math.Max(1, depth >> 1);
  89. }
  90. return output;
  91. }
  92. public static byte[] DecodePta(ReadOnlySpan<byte> data, int width, int height, int depth, int levels, int layers)
  93. {
  94. ReadOnlySpan<ulong> dataUlong = MemoryMarshal.Cast<byte, ulong>(data);
  95. int inputOffset = 0;
  96. byte[] output = new byte[CalculateOutputSize(width, height, depth, levels, layers)];
  97. Span<uint> outputUint = MemoryMarshal.Cast<byte, uint>(output);
  98. Span<uint> tile = stackalloc uint[BlockWidth * BlockHeight];
  99. int imageBaseOOffs = 0;
  100. for (int l = 0; l < levels; l++)
  101. {
  102. int wInBlocks = BitUtils.DivRoundUp(width, BlockWidth);
  103. int hInBlocks = BitUtils.DivRoundUp(height, BlockHeight);
  104. for (int l2 = 0; l2 < layers; l2++)
  105. {
  106. for (int z = 0; z < depth; z++)
  107. {
  108. for (int y = 0; y < hInBlocks; y++)
  109. {
  110. int ty = y * BlockHeight;
  111. int bh = Math.Min(BlockHeight, height - ty);
  112. for (int x = 0; x < wInBlocks; x++)
  113. {
  114. int tx = x * BlockWidth;
  115. int bw = Math.Min(BlockWidth, width - tx);
  116. ulong colorBlock = dataUlong[inputOffset++];
  117. DecodeBlockPta(tile, colorBlock);
  118. for (int py = 0; py < bh; py++)
  119. {
  120. int oOffsBase = imageBaseOOffs + ((ty + py) * width) + tx;
  121. tile.Slice(py * BlockWidth, bw).CopyTo(outputUint.Slice(oOffsBase, bw));
  122. }
  123. }
  124. }
  125. imageBaseOOffs += width * height;
  126. }
  127. }
  128. width = Math.Max(1, width >> 1);
  129. height = Math.Max(1, height >> 1);
  130. depth = Math.Max(1, depth >> 1);
  131. }
  132. return output;
  133. }
  134. public static byte[] DecodeRgba(ReadOnlySpan<byte> data, int width, int height, int depth, int levels, int layers)
  135. {
  136. ReadOnlySpan<ulong> dataUlong = MemoryMarshal.Cast<byte, ulong>(data);
  137. int inputOffset = 0;
  138. byte[] output = new byte[CalculateOutputSize(width, height, depth, levels, layers)];
  139. Span<uint> outputUint = MemoryMarshal.Cast<byte, uint>(output);
  140. Span<uint> tile = stackalloc uint[BlockWidth * BlockHeight];
  141. int imageBaseOOffs = 0;
  142. for (int l = 0; l < levels; l++)
  143. {
  144. int wInBlocks = BitUtils.DivRoundUp(width, BlockWidth);
  145. int hInBlocks = BitUtils.DivRoundUp(height, BlockHeight);
  146. for (int l2 = 0; l2 < layers; l2++)
  147. {
  148. for (int z = 0; z < depth; z++)
  149. {
  150. for (int y = 0; y < hInBlocks; y++)
  151. {
  152. int ty = y * BlockHeight;
  153. int bh = Math.Min(BlockHeight, height - ty);
  154. for (int x = 0; x < wInBlocks; x++)
  155. {
  156. int tx = x * BlockWidth;
  157. int bw = Math.Min(BlockWidth, width - tx);
  158. ulong alphaBlock = dataUlong[inputOffset];
  159. ulong colorBlock = dataUlong[inputOffset + 1];
  160. inputOffset += 2;
  161. DecodeBlock(tile, colorBlock);
  162. byte alphaBase = (byte)alphaBlock;
  163. int[] alphaTable = _etc2AlphaLut[(alphaBlock >> 8) & 0xf];
  164. int alphaMultiplier = (int)(alphaBlock >> 12) & 0xf;
  165. ulong alphaIndices = BinaryPrimitives.ReverseEndianness(alphaBlock);
  166. if (alphaMultiplier != 0)
  167. {
  168. for (int py = 0; py < bh; py++)
  169. {
  170. int oOffsBase = imageBaseOOffs + ((ty + py) * width) + tx;
  171. for (int px = 0; px < bw; px++)
  172. {
  173. int oOffs = oOffsBase + px;
  174. int alphaIndex = (int)((alphaIndices >> (((px * BlockHeight + py) ^ 0xf) * 3)) & 7);
  175. byte a = Saturate(alphaBase + alphaTable[alphaIndex] * alphaMultiplier);
  176. outputUint[oOffs] = tile[py * BlockWidth + px] | ((uint)a << 24);
  177. }
  178. }
  179. }
  180. else
  181. {
  182. uint a = (uint)alphaBase << 24;
  183. for (int py = 0; py < bh; py++)
  184. {
  185. int oOffsBase = imageBaseOOffs + ((ty + py) * width) + tx;
  186. for (int px = 0; px < bw; px++)
  187. {
  188. int oOffs = oOffsBase + px;
  189. outputUint[oOffs] = tile[py * BlockWidth + px] | a;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. imageBaseOOffs += width * height;
  196. }
  197. }
  198. width = Math.Max(1, width >> 1);
  199. height = Math.Max(1, height >> 1);
  200. depth = Math.Max(1, depth >> 1);
  201. }
  202. return output;
  203. }
  204. private static void DecodeBlock(Span<uint> tile, ulong block)
  205. {
  206. uint blockLow = (uint)(block >> 0);
  207. uint blockHigh = (uint)(block >> 32);
  208. uint r1, g1, b1;
  209. uint r2, g2, b2;
  210. bool differentialMode = (blockLow & 0x2000000) != 0;
  211. if (differentialMode)
  212. {
  213. (r1, g1, b1, r2, g2, b2) = UnpackRgb555DiffEndPoints(blockLow);
  214. if (r2 > 31)
  215. {
  216. DecodeBlock59T(tile, blockLow, blockHigh);
  217. }
  218. else if (g2 > 31)
  219. {
  220. DecodeBlock58H(tile, blockLow, blockHigh);
  221. }
  222. else if (b2 > 31)
  223. {
  224. DecodeBlock57P(tile, block);
  225. }
  226. else
  227. {
  228. r1 |= r1 >> 5;
  229. g1 |= g1 >> 5;
  230. b1 |= b1 >> 5;
  231. r2 = (r2 << 3) | (r2 >> 2);
  232. g2 = (g2 << 3) | (g2 >> 2);
  233. b2 = (b2 << 3) | (b2 >> 2);
  234. DecodeBlockETC1(tile, blockLow, blockHigh, r1, g1, b1, r2, g2, b2);
  235. }
  236. }
  237. else
  238. {
  239. r1 = (blockLow & 0x0000f0) >> 0;
  240. g1 = (blockLow & 0x00f000) >> 8;
  241. b1 = (blockLow & 0xf00000) >> 16;
  242. r2 = (blockLow & 0x00000f) << 4;
  243. g2 = (blockLow & 0x000f00) >> 4;
  244. b2 = (blockLow & 0x0f0000) >> 12;
  245. r1 |= r1 >> 4;
  246. g1 |= g1 >> 4;
  247. b1 |= b1 >> 4;
  248. r2 |= r2 >> 4;
  249. g2 |= g2 >> 4;
  250. b2 |= b2 >> 4;
  251. DecodeBlockETC1(tile, blockLow, blockHigh, r1, g1, b1, r2, g2, b2);
  252. }
  253. }
  254. private static void DecodeBlockPta(Span<uint> tile, ulong block)
  255. {
  256. uint blockLow = (uint)(block >> 0);
  257. uint blockHigh = (uint)(block >> 32);
  258. (uint r1, uint g1, uint b1, uint r2, uint g2, uint b2) = UnpackRgb555DiffEndPoints(blockLow);
  259. bool fullyOpaque = (blockLow & 0x2000000) != 0;
  260. if (fullyOpaque)
  261. {
  262. if (r2 > 31)
  263. {
  264. DecodeBlock59T(tile, blockLow, blockHigh);
  265. }
  266. else if (g2 > 31)
  267. {
  268. DecodeBlock58H(tile, blockLow, blockHigh);
  269. }
  270. else if (b2 > 31)
  271. {
  272. DecodeBlock57P(tile, block);
  273. }
  274. else
  275. {
  276. r1 |= r1 >> 5;
  277. g1 |= g1 >> 5;
  278. b1 |= b1 >> 5;
  279. r2 = (r2 << 3) | (r2 >> 2);
  280. g2 = (g2 << 3) | (g2 >> 2);
  281. b2 = (b2 << 3) | (b2 >> 2);
  282. DecodeBlockETC1(tile, blockLow, blockHigh, r1, g1, b1, r2, g2, b2);
  283. }
  284. for (int i = 0; i < tile.Length; i++)
  285. {
  286. tile[i] |= AlphaMask;
  287. }
  288. }
  289. else
  290. {
  291. if (r2 > 31)
  292. {
  293. DecodeBlock59T(tile, blockLow, blockHigh, AlphaMask);
  294. }
  295. else if (g2 > 31)
  296. {
  297. DecodeBlock58H(tile, blockLow, blockHigh, AlphaMask);
  298. }
  299. else if (b2 > 31)
  300. {
  301. DecodeBlock57P(tile, block);
  302. for (int i = 0; i < tile.Length; i++)
  303. {
  304. tile[i] |= AlphaMask;
  305. }
  306. }
  307. else
  308. {
  309. r1 |= r1 >> 5;
  310. g1 |= g1 >> 5;
  311. b1 |= b1 >> 5;
  312. r2 = (r2 << 3) | (r2 >> 2);
  313. g2 = (g2 << 3) | (g2 >> 2);
  314. b2 = (b2 << 3) | (b2 >> 2);
  315. DecodeBlockETC1(tile, blockLow, blockHigh, r1, g1, b1, r2, g2, b2, AlphaMask);
  316. }
  317. }
  318. }
  319. private static (uint, uint, uint, uint, uint, uint) UnpackRgb555DiffEndPoints(uint blockLow)
  320. {
  321. uint r1 = (blockLow & 0x0000f8) >> 0;
  322. uint g1 = (blockLow & 0x00f800) >> 8;
  323. uint b1 = (blockLow & 0xf80000) >> 16;
  324. uint r2 = (uint)((sbyte)(r1 >> 3) + ((sbyte)((blockLow & 0x000007) << 5) >> 5));
  325. uint g2 = (uint)((sbyte)(g1 >> 3) + ((sbyte)((blockLow & 0x000700) >> 3) >> 5));
  326. uint b2 = (uint)((sbyte)(b1 >> 3) + ((sbyte)((blockLow & 0x070000) >> 11) >> 5));
  327. return (r1, g1, b1, r2, g2, b2);
  328. }
  329. private static void DecodeBlock59T(Span<uint> tile, uint blockLow, uint blockHigh, uint alphaMask = 0)
  330. {
  331. uint r1 = (blockLow & 3) | ((blockLow >> 1) & 0xc);
  332. uint g1 = (blockLow >> 12) & 0xf;
  333. uint b1 = (blockLow >> 8) & 0xf;
  334. uint r2 = (blockLow >> 20) & 0xf;
  335. uint g2 = (blockLow >> 16) & 0xf;
  336. uint b2 = (blockLow >> 28) & 0xf;
  337. r1 |= r1 << 4;
  338. g1 |= g1 << 4;
  339. b1 |= b1 << 4;
  340. r2 |= r2 << 4;
  341. g2 |= g2 << 4;
  342. b2 |= b2 << 4;
  343. int dist = _etc2Lut[((blockLow >> 24) & 1) | ((blockLow >> 25) & 6)];
  344. Span<uint> palette = stackalloc uint[4];
  345. palette[0] = Pack(r1, g1, b1);
  346. palette[1] = Pack(r2, g2, b2, dist);
  347. palette[2] = Pack(r2, g2, b2);
  348. palette[3] = Pack(r2, g2, b2, -dist);
  349. blockHigh = BinaryPrimitives.ReverseEndianness(blockHigh);
  350. for (int y = 0; y < BlockHeight; y++)
  351. {
  352. for (int x = 0; x < BlockWidth; x++)
  353. {
  354. int offset = (y * 4) + x;
  355. int index = (x * 4) + y;
  356. int paletteIndex = (int)((blockHigh >> index) & 1) | (int)((blockHigh >> (index + 15)) & 2);
  357. tile[offset] = palette[paletteIndex];
  358. if (alphaMask != 0)
  359. {
  360. if (paletteIndex == 2)
  361. {
  362. tile[offset] = 0;
  363. }
  364. else
  365. {
  366. tile[offset] |= alphaMask;
  367. }
  368. }
  369. }
  370. }
  371. }
  372. private static void DecodeBlock58H(Span<uint> tile, uint blockLow, uint blockHigh, uint alphaMask = 0)
  373. {
  374. uint r1 = (blockLow >> 3) & 0xf;
  375. uint g1 = ((blockLow << 1) & 0xe) | ((blockLow >> 12) & 1);
  376. uint b1 = ((blockLow >> 23) & 1) | ((blockLow >> 7) & 6) | ((blockLow >> 8) & 8);
  377. uint r2 = (blockLow >> 19) & 0xf;
  378. uint g2 = ((blockLow >> 31) & 1) | ((blockLow >> 15) & 0xe);
  379. uint b2 = (blockLow >> 27) & 0xf;
  380. uint rgb1 = Pack4Be(r1, g1, b1);
  381. uint rgb2 = Pack4Be(r2, g2, b2);
  382. r1 |= r1 << 4;
  383. g1 |= g1 << 4;
  384. b1 |= b1 << 4;
  385. r2 |= r2 << 4;
  386. g2 |= g2 << 4;
  387. b2 |= b2 << 4;
  388. int dist = _etc2Lut[(rgb1 >= rgb2 ? 1u : 0u) | ((blockLow >> 23) & 2) | ((blockLow >> 24) & 4)];
  389. Span<uint> palette = stackalloc uint[4];
  390. palette[0] = Pack(r1, g1, b1, dist);
  391. palette[1] = Pack(r1, g1, b1, -dist);
  392. palette[2] = Pack(r2, g2, b2, dist);
  393. palette[3] = Pack(r2, g2, b2, -dist);
  394. blockHigh = BinaryPrimitives.ReverseEndianness(blockHigh);
  395. for (int y = 0; y < BlockHeight; y++)
  396. {
  397. for (int x = 0; x < BlockWidth; x++)
  398. {
  399. int offset = (y * 4) + x;
  400. int index = (x * 4) + y;
  401. int paletteIndex = (int)((blockHigh >> index) & 1) | (int)((blockHigh >> (index + 15)) & 2);
  402. tile[offset] = palette[paletteIndex];
  403. if (alphaMask != 0)
  404. {
  405. if (paletteIndex == 2)
  406. {
  407. tile[offset] = 0;
  408. }
  409. else
  410. {
  411. tile[offset] |= alphaMask;
  412. }
  413. }
  414. }
  415. }
  416. }
  417. private static void DecodeBlock57P(Span<uint> tile, ulong block)
  418. {
  419. int r0 = (int)((block >> 1) & 0x3f);
  420. int g0 = (int)(((block >> 9) & 0x3f) | ((block & 1) << 6));
  421. int b0 = (int)(((block >> 31) & 1) | ((block >> 15) & 6) | ((block >> 16) & 0x18) | ((block >> 3) & 0x20));
  422. int rh = (int)(((block >> 24) & 1) | ((block >> 25) & 0x3e));
  423. int gh = (int)((block >> 33) & 0x7f);
  424. int bh = (int)(((block >> 43) & 0x1f) | ((block >> 27) & 0x20));
  425. int rv = (int)(((block >> 53) & 7) | ((block >> 37) & 0x38));
  426. int gv = (int)(((block >> 62) & 3) | ((block >> 46) & 0x7c));
  427. int bv = (int)((block >> 56) & 0x3f);
  428. r0 = (r0 << 2) | (r0 >> 4);
  429. g0 = (g0 << 1) | (g0 >> 6);
  430. b0 = (b0 << 2) | (b0 >> 4);
  431. rh = (rh << 2) | (rh >> 4);
  432. gh = (gh << 1) | (gh >> 6);
  433. bh = (bh << 2) | (bh >> 4);
  434. rv = (rv << 2) | (rv >> 4);
  435. gv = (gv << 1) | (gv >> 6);
  436. bv = (bv << 2) | (bv >> 4);
  437. for (int y = 0; y < BlockHeight; y++)
  438. {
  439. for (int x = 0; x < BlockWidth; x++)
  440. {
  441. int offset = y * BlockWidth + x;
  442. byte r = Saturate(((x * (rh - r0)) + (y * (rv - r0)) + (r0 * 4) + 2) >> 2);
  443. byte g = Saturate(((x * (gh - g0)) + (y * (gv - g0)) + (g0 * 4) + 2) >> 2);
  444. byte b = Saturate(((x * (bh - b0)) + (y * (bv - b0)) + (b0 * 4) + 2) >> 2);
  445. tile[offset] = Pack(r, g, b);
  446. }
  447. }
  448. }
  449. private static void DecodeBlockETC1(
  450. Span<uint> tile,
  451. uint blockLow,
  452. uint blockHigh,
  453. uint r1,
  454. uint g1,
  455. uint b1,
  456. uint r2,
  457. uint g2,
  458. uint b2,
  459. uint alphaMask = 0)
  460. {
  461. int[] table1 = _etc1Lut[(blockLow >> 29) & 7];
  462. int[] table2 = _etc1Lut[(blockLow >> 26) & 7];
  463. bool flip = (blockLow & 0x1000000) != 0;
  464. if (!flip)
  465. {
  466. for (int y = 0; y < BlockHeight; y++)
  467. {
  468. for (int x = 0; x < BlockWidth / 2; x++)
  469. {
  470. uint color1 = CalculatePixel(r1, g1, b1, x + 0, y, blockHigh, table1, alphaMask);
  471. uint color2 = CalculatePixel(r2, g2, b2, x + 2, y, blockHigh, table2, alphaMask);
  472. int offset1 = y * BlockWidth + x;
  473. int offset2 = y * BlockWidth + x + 2;
  474. tile[offset1] = color1;
  475. tile[offset2] = color2;
  476. }
  477. }
  478. }
  479. else
  480. {
  481. for (int y = 0; y < BlockHeight / 2; y++)
  482. {
  483. for (int x = 0; x < BlockWidth; x++)
  484. {
  485. uint color1 = CalculatePixel(r1, g1, b1, x, y + 0, blockHigh, table1, alphaMask);
  486. uint color2 = CalculatePixel(r2, g2, b2, x, y + 2, blockHigh, table2, alphaMask);
  487. int offset1 = (y * BlockWidth) + x;
  488. int offset2 = ((y + 2) * BlockWidth) + x;
  489. tile[offset1] = color1;
  490. tile[offset2] = color2;
  491. }
  492. }
  493. }
  494. }
  495. private static uint CalculatePixel(uint r, uint g, uint b, int x, int y, uint block, int[] table, uint alphaMask)
  496. {
  497. int index = x * BlockHeight + y;
  498. uint msb = block << 1;
  499. uint tableIndex = index < 8
  500. ? ((block >> (index + 24)) & 1) + ((msb >> (index + 8)) & 2)
  501. : ((block >> (index + 8)) & 1) + ((msb >> (index - 8)) & 2);
  502. if (alphaMask != 0)
  503. {
  504. if (tableIndex == 0)
  505. {
  506. return Pack(r, g, b) | alphaMask;
  507. }
  508. else if (tableIndex == 2)
  509. {
  510. return 0;
  511. }
  512. else
  513. {
  514. return Pack(r, g, b, table[tableIndex]) | alphaMask;
  515. }
  516. }
  517. return Pack(r, g, b, table[tableIndex]);
  518. }
  519. private static uint Pack(uint r, uint g, uint b, int offset)
  520. {
  521. r = Saturate((int)(r + offset));
  522. g = Saturate((int)(g + offset));
  523. b = Saturate((int)(b + offset));
  524. return Pack(r, g, b);
  525. }
  526. private static uint Pack(uint r, uint g, uint b)
  527. {
  528. return r | (g << 8) | (b << 16);
  529. }
  530. private static uint Pack4Be(uint r, uint g, uint b)
  531. {
  532. return (r << 8) | (g << 4) | b;
  533. }
  534. private static byte Saturate(int value)
  535. {
  536. return value > byte.MaxValue ? byte.MaxValue : value < byte.MinValue ? byte.MinValue : (byte)value;
  537. }
  538. private static int CalculateOutputSize(int width, int height, int depth, int levels, int layers)
  539. {
  540. int size = 0;
  541. for (int l = 0; l < levels; l++)
  542. {
  543. size += Math.Max(1, width >> l) * Math.Max(1, height >> l) * Math.Max(1, depth >> l) * layers * 4;
  544. }
  545. return size;
  546. }
  547. }
  548. }