Advertisement
Conmanx360

Untitled

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