Advertisement
Conmanx360

Untitled

Jun 22nd, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. early test of all functions put together.
  2.  
  3. static void dxtn_decompress_block(const BYTE *src, BYTE *dst, UINT width, UINT height, UINT depth,
  4. UINT x_pos, UINT y_pos, UINT z_pos, UINT dst_slice_pitch, UINT64 cur_block, UINT fmt)
  5. {
  6. UINT64 alpha_block, alpha_index, color_block, color_index;
  7. const UINT64 *source;
  8. DWORD *dest;
  9. DWORD alpha_lookup;
  10. DWORD bgra;
  11. DWORD temp;
  12. DWORD i, x, y;
  13. WORD color[2];
  14. BYTE alpha_val, color_val;
  15. BYTE alpha[8];
  16. BYTE r[4];
  17. BYTE g[4];
  18. BYTE b[4];
  19. BYTE block_offset;
  20.  
  21. /* Block selection section. */
  22. if (fmt == WINED3DFMT_DXT1)
  23. block_offset = 8;
  24. else
  25. block_offset = 16;
  26.  
  27. source = (const UINT64 *)(src + cur_block * block_offset);
  28. if (fmt == WINED3DFMT_DXT1)
  29. color_block = source[0];
  30. else
  31. {
  32. alpha_block = source[0];
  33. color_block = source[1];
  34. }
  35.  
  36. /* Color handling section. */
  37. color[0] = color_block & 0xffff;
  38. color[1] = (color_block >> 16) & 0xffff;
  39. for (i = 0; i < 2; ++i)
  40. {
  41. temp = (color[i] >> 11) * 255 + 16;
  42. r[i] = (temp / 32 + temp) / 32;
  43. temp = ((color[i] >> 5) & 0x3f) * 255 + 32;
  44. g[i] = (temp / 64 + temp) / 64;
  45. temp = (color[i] & 0x1f) * 255 + 16;
  46. b[i] = (temp / 32 + temp) / 32;
  47. }
  48.  
  49. if (fmt == WINED3DFMT_DXT3 || fmt == WINED3DFMT_DXT5 || color[0] > color[1])
  50. {
  51. for (i = 0; i < 2; ++i)
  52. {
  53. r[2 + i] = (2 * r[0 + i] + r[1 - i]) / 3;
  54. g[2 + i] = (2 * g[0 + i] + g[1 - i]) / 3;
  55. b[2 + i] = (2 * b[0 + i] + b[1 - i]) / 3;
  56. }
  57. if (fmt == WINED3DFMT_DXT1)
  58. {
  59. for (i = 0; i < 8; ++i)
  60. alpha[i] = 255;
  61. }
  62. }
  63. else if (fmt == WINED3DFMT_DXT1 && color[0] <= color[1])
  64. {
  65. r[2] = (r[0] + r[1]) / 2;
  66. g[2] = (g[0] + g[1]) / 2;
  67. b[2] = (b[0] + b[1]) / 2;
  68.  
  69. r[3] = 0;
  70. g[3] = 0;
  71. b[3] = 0;
  72.  
  73. for (i = 0; i < 8; ++i)
  74. alpha[i] = 255;
  75. alpha[3] = 0;
  76. }
  77.  
  78. /* Alpha handling section. */
  79. switch (fmt)
  80. {
  81. case WINED3DFMT_DXT1:
  82. alpha_index = 0;
  83. for (i = 0; i < 8; ++i)
  84. alpha[i] = 255;
  85. if (color[0] <= color[1])
  86. alpha[3] = 0;
  87. break;
  88. case WINED3DFMT_DXT3:
  89. alpha_index = alpha_block;
  90. for (i = 0; i < 8; ++i)
  91. alpha[i] = 0;
  92. break;
  93. case WINED3DFMT_DXT5:
  94. alpha_index = (alpha_block >> 16);
  95. alpha[0] = alpha_block & 0xff;
  96. alpha[1] = (alpha_block >> 8) & 0xff;
  97. if (alpha[0] > alpha[1])
  98. {
  99. for (i = 0; i < 6; ++i)
  100. alpha[2 + i] = (((6 - i) * alpha[0]) + ((1 + i) * alpha[1])) / 7;
  101. }
  102. else if (alpha[0] <= alpha[1])
  103. {
  104. for (i = 0; i < 4; ++i)
  105. alpha[2 + i] = (((4 - i) * alpha[0]) + ((1 + i) * alpha[1])) / 5;
  106. alpha[6] = 0;
  107. alpha[7] = 255;
  108. }
  109. break;
  110. default:
  111. alpha_index = 0;
  112. break;
  113. }
  114.  
  115. /* This is the data writing section. */
  116. color_index = (color_block >> 32) & 0xffffffff;
  117. dest = (DWORD *)(dst + z_pos * dst_slice_pitch);
  118. for (y = 0; y < 4; ++y)
  119. {
  120. if (y_pos + y >= height)
  121. break;
  122. for (x = 0; x < 4; ++x)
  123. {
  124. if (x_pos + x >= width)
  125. break;
  126.  
  127. color_val = 0;
  128. alpha_val = 0;
  129. bgra = 0;
  130.  
  131. color_val = (color_index >> (y * 8));
  132. color_val = (color_val >> (x * 2)) & 0x3;
  133. switch (fmt)
  134. {
  135. case WINED3DFMT_DXT1:
  136. alpha_val = color_val;
  137. break;
  138. case WINED3DFMT_DXT3:
  139. alpha_lookup = (alpha_index >> (y * 16)) & 0xffff;
  140. alpha_val = (alpha_lookup >> (x * 4)) & 0xf;
  141. temp = alpha_val * 255 + 8;
  142. alpha[0] = (temp / 16 + temp) / 16;
  143. break;
  144. case WINED3DFMT_DXT5:
  145. alpha_lookup = (alpha_index >> (y * 12)) & 0xfff;
  146. alpha_val = (alpha_lookup >> (x * 3)) & 0x7;
  147. break;
  148. }
  149. bgra = ((alpha[alpha_val] << 24) | (r[color_val] << 16) | (g[color_val] << 8) | b[color_val]);
  150. dest[(y_pos + y) * width + (x_pos + x)] = bgra;
  151. }
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement