dds.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * Copyright 2011-2012 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "bgfx_p.h"
  6. #include "dds.h"
  7. namespace bgfx
  8. {
  9. #define DDS_MAGIC MAKEFOURCC('D','D','S',' ')
  10. #define DDS_HEADER_SIZE 124
  11. #define DDS_IMAGE_DATA_OFFSET (DDS_HEADER_SIZE + 4)
  12. #define DDS_DXT1 MAKEFOURCC('D', 'X', 'T', '1')
  13. #define DDS_DXT2 MAKEFOURCC('D', 'X', 'T', '2')
  14. #define DDS_DXT3 MAKEFOURCC('D', 'X', 'T', '3')
  15. #define DDS_DXT4 MAKEFOURCC('D', 'X', 'T', '4')
  16. #define DDS_DXT5 MAKEFOURCC('D', 'X', 'T', '5')
  17. #define DDSD_CAPS 0x00000001
  18. #define DDSD_HEIGHT 0x00000002
  19. #define DDSD_WIDTH 0x00000004
  20. #define DDSD_PITCH 0x00000008
  21. #define DDSD_PIXELFORMAT 0x00001000
  22. #define DDSD_MIPMAPCOUNT 0x00020000
  23. #define DDSD_LINEARSIZE 0x00080000
  24. #define DDSD_DEPTH 0x00800000
  25. #define DDPF_ALPHAPIXELS 0x00000001
  26. #define DDPF_ALPHA 0x00000002
  27. #define DDPF_FOURCC 0x00000004
  28. #define DDPF_INDEXED 0x00000020
  29. #define DDPF_RGB 0x00000040
  30. #define DDPF_YUV 0x00000200
  31. #define DDPF_LUMINANCE 0x00020000
  32. #define DDSCAPS_COMPLEX 0x00000008
  33. #define DDSCAPS_TEXTURE 0x00001000
  34. #define DDSCAPS_MIPMAP 0x00400000
  35. #define DDSCAPS2_CUBEMAP 0x00000200
  36. #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
  37. #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
  38. #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
  39. #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
  40. #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
  41. #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
  42. #define DDSCAPS2_VOLUME 0x00200000
  43. bool isDds(const Memory* _mem)
  44. {
  45. StreamRead stream(_mem->data, _mem->size);
  46. uint32_t magic;
  47. stream.read(magic);
  48. return DDS_MAGIC == magic;
  49. }
  50. uint32_t bitRangeConvert(uint32_t _in, uint32_t _from, uint32_t _to)
  51. {
  52. uint32_t tmp0 = uint32_sll(1, _to);
  53. uint32_t tmp1 = uint32_sll(1, _from);
  54. uint32_t tmp2 = uint32_dec(tmp0);
  55. uint32_t tmp3 = uint32_dec(tmp1);
  56. uint32_t tmp4 = uint32_mul(_in, tmp2);
  57. uint32_t tmp5 = uint32_add(tmp3, tmp4);
  58. uint32_t tmp6 = uint32_srl(tmp5, _from);
  59. uint32_t tmp7 = uint32_add(tmp5, tmp6);
  60. uint32_t result = uint32_srl(tmp7, _from);
  61. return result;
  62. }
  63. void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8])
  64. {
  65. uint8_t colors[4*3];
  66. uint32_t c0 = _src[0] | (_src[1] << 8);
  67. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  68. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  69. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  70. uint32_t c1 = _src[2] | (_src[3] << 8);
  71. colors[3] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  72. colors[4] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  73. colors[5] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  74. colors[6] = (2*colors[0] + colors[3]) / 3;
  75. colors[7] = (2*colors[1] + colors[4]) / 3;
  76. colors[8] = (2*colors[2] + colors[5]) / 3;
  77. colors[ 9] = (colors[0] + 2*colors[3]) / 3;
  78. colors[10] = (colors[1] + 2*colors[4]) / 3;
  79. colors[11] = (colors[2] + 2*colors[5]) / 3;
  80. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  81. {
  82. int idx = ( (_src[next>>3] >> (next & 7)) & 3) * 3;
  83. _dst[ii+0] = colors[idx+0];
  84. _dst[ii+1] = colors[idx+1];
  85. _dst[ii+2] = colors[idx+2];
  86. }
  87. }
  88. void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8])
  89. {
  90. uint8_t colors[4*4];
  91. uint32_t c0 = _src[0] | (_src[1] << 8);
  92. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  93. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  94. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  95. colors[3] = 255;
  96. uint32_t c1 = _src[2] | (_src[3] << 8);
  97. colors[4] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  98. colors[5] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  99. colors[6] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  100. colors[7] = 255;
  101. if (c0 > c1)
  102. {
  103. colors[ 8] = (2*colors[0] + colors[4]) / 3;
  104. colors[ 9] = (2*colors[1] + colors[5]) / 3;
  105. colors[10] = (2*colors[2] + colors[6]) / 3;
  106. colors[11] = 255;
  107. colors[12] = (colors[0] + 2*colors[4]) / 3;
  108. colors[13] = (colors[1] + 2*colors[5]) / 3;
  109. colors[14] = (colors[2] + 2*colors[6]) / 3;
  110. colors[15] = 255;
  111. }
  112. else
  113. {
  114. colors[ 8] = (colors[0] + colors[4]) / 2;
  115. colors[ 9] = (colors[1] + colors[5]) / 2;
  116. colors[10] = (colors[2] + colors[6]) / 2;
  117. colors[11] = 255;
  118. colors[12] = 0;
  119. colors[13] = 0;
  120. colors[14] = 0;
  121. colors[15] = 0;
  122. }
  123. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  124. {
  125. int idx = ( (_src[next>>3] >> (next & 7)) & 3) * 4;
  126. _dst[ii+0] = colors[idx+0];
  127. _dst[ii+1] = colors[idx+1];
  128. _dst[ii+2] = colors[idx+2];
  129. _dst[ii+3] = colors[idx+3];
  130. }
  131. }
  132. void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8])
  133. {
  134. for (uint32_t ii = 3, next = 0; ii < 16*4; ii += 4, next += 4)
  135. {
  136. uint32_t c0 = (_src[next>>3] >> (next&7) ) & 0xf;
  137. _dst[ii] = bitRangeConvert(c0, 4, 8);
  138. }
  139. }
  140. void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8])
  141. {
  142. uint8_t alpha[8];
  143. alpha[0] = _src[0];
  144. alpha[1] = _src[1];
  145. if (alpha[0] > alpha[1])
  146. {
  147. alpha[2] = (6*alpha[0] + 1*alpha[1]) / 7;
  148. alpha[3] = (5*alpha[0] + 2*alpha[1]) / 7;
  149. alpha[4] = (4*alpha[0] + 3*alpha[1]) / 7;
  150. alpha[5] = (3*alpha[0] + 4*alpha[1]) / 7;
  151. alpha[6] = (2*alpha[0] + 5*alpha[1]) / 7;
  152. alpha[7] = (1*alpha[0] + 6*alpha[1]) / 7;
  153. }
  154. else
  155. {
  156. alpha[2] = (4*alpha[0] + 1*alpha[1]) / 5;
  157. alpha[3] = (3*alpha[0] + 2*alpha[1]) / 5;
  158. alpha[4] = (2*alpha[0] + 3*alpha[1]) / 5;
  159. alpha[5] = (1*alpha[0] + 4*alpha[1]) / 5;
  160. alpha[6] = 0;
  161. alpha[7] = 255;
  162. }
  163. for (uint32_t ii = 3, next = 8*2; ii < 16*4; ii += 4, ++next)
  164. {
  165. uint32_t bit = (_src[next>>3] >> (next&7) ) & 1;
  166. uint32_t idx = bit;
  167. ++next;
  168. bit = (_src[next>>3] >> (next&7) ) & 1;
  169. idx += bit << 1;
  170. ++next;
  171. bit = (_src[next>>3] >> (next&7) ) & 1;
  172. idx += bit << 2;
  173. _dst[ii] = alpha[idx & 7];
  174. }
  175. }
  176. uint32_t Mip::getDecodedSize() const
  177. {
  178. return m_width*m_height*4;
  179. }
  180. void Mip::decode(uint8_t* _dst)
  181. {
  182. const uint8_t* src = m_data;
  183. if (0 != m_type)
  184. {
  185. uint32_t width = m_width/4;
  186. uint32_t height = m_height/4;
  187. uint32_t pitch = m_width*4;
  188. uint8_t temp[16*4];
  189. switch (m_type)
  190. {
  191. case 1:
  192. for (uint32_t yy = 0; yy < height; ++yy)
  193. {
  194. for (uint32_t xx = 0; xx < width; ++xx)
  195. {
  196. decodeBlockDxt1(temp, src);
  197. src += 8;
  198. uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
  199. memcpy(dst, temp, 16);
  200. memcpy(&dst[pitch], &temp[16], 16);
  201. memcpy(&dst[2*pitch], &temp[32], 16);
  202. memcpy(&dst[3*pitch], &temp[48], 16);
  203. }
  204. }
  205. break;
  206. case 2:
  207. for (uint32_t yy = 0; yy < height; ++yy)
  208. {
  209. for (uint32_t xx = 0; xx < width; ++xx)
  210. {
  211. decodeBlockDxt23A(temp, src);
  212. src += 8;
  213. decodeBlockDxt(temp, src);
  214. src += 8;
  215. uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
  216. memcpy(dst, temp, 16);
  217. memcpy(&dst[pitch], &temp[16], 16);
  218. memcpy(&dst[2*pitch], &temp[32], 16);
  219. memcpy(&dst[3*pitch], &temp[48], 16);
  220. }
  221. }
  222. break;
  223. case 3:
  224. for (uint32_t yy = 0; yy < height; ++yy)
  225. {
  226. for (uint32_t xx = 0; xx < width; ++xx)
  227. {
  228. decodeBlockDxt45A(temp, src);
  229. src += 8;
  230. decodeBlockDxt(temp, src);
  231. src += 8;
  232. uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
  233. memcpy(dst, temp, 16);
  234. memcpy(&dst[pitch], &temp[16], 16);
  235. memcpy(&dst[2*pitch], &temp[32], 16);
  236. memcpy(&dst[3*pitch], &temp[48], 16);
  237. }
  238. }
  239. break;
  240. }
  241. }
  242. else
  243. {
  244. uint32_t width = m_width;
  245. uint32_t height = m_height;
  246. if (m_bpp == 1
  247. || m_bpp == 4)
  248. {
  249. uint32_t pitch = m_width*m_bpp;
  250. memcpy(_dst, src, pitch*height);
  251. }
  252. else
  253. {
  254. uint32_t pitch = m_width*4;
  255. for (uint32_t yy = 0; yy < height; ++yy)
  256. {
  257. uint8_t* dst = &_dst[yy*pitch];
  258. for (uint32_t xx = 0; xx < width; ++xx)
  259. {
  260. memcpy(dst, src, 3);
  261. dst[3] = 255;
  262. dst += 4;
  263. src += 3;
  264. }
  265. }
  266. }
  267. }
  268. }
  269. bool parseDds(Dds& _dds, const Memory* _mem)
  270. {
  271. StreamRead stream(_mem->data, _mem->size);
  272. uint32_t magic;
  273. stream.read(magic);
  274. if (DDS_MAGIC != magic)
  275. {
  276. return false;
  277. }
  278. uint32_t headerSize;
  279. stream.read(headerSize);
  280. if (headerSize < DDS_HEADER_SIZE)
  281. {
  282. return false;
  283. }
  284. uint32_t flags;
  285. stream.read(flags);
  286. if ( (flags & (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) ) != (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) )
  287. {
  288. return false;
  289. }
  290. uint32_t height;
  291. stream.read(height);
  292. uint32_t width;
  293. stream.read(width);
  294. uint32_t pitch;
  295. stream.read(pitch);
  296. uint32_t depth;
  297. stream.read(depth);
  298. uint32_t mips;
  299. stream.read(mips);
  300. stream.skip(44); // reserved
  301. stream.skip(4); // pixel format size
  302. uint32_t pixelFlags;
  303. stream.read(pixelFlags);
  304. uint32_t fourcc;
  305. stream.read(fourcc);
  306. uint32_t rgbCount;
  307. stream.read(rgbCount);
  308. uint32_t rbitmask;
  309. stream.read(rbitmask);
  310. uint32_t gbitmask;
  311. stream.read(gbitmask);
  312. uint32_t bbitmask;
  313. stream.read(bbitmask);
  314. uint32_t abitmask;
  315. stream.read(abitmask);
  316. uint32_t caps[4];
  317. stream.read(caps);
  318. if ( (caps[0] & DDSCAPS_TEXTURE) == 0)
  319. {
  320. return false;
  321. }
  322. stream.skip(4); // reserved
  323. uint8_t bpp = 1;
  324. uint8_t blockSize = 1;
  325. uint8_t type = 0;
  326. bool hasAlpha = pixelFlags & DDPF_ALPHAPIXELS;
  327. if (pixelFlags & DDPF_FOURCC)
  328. {
  329. switch (fourcc)
  330. {
  331. case DDS_DXT1:
  332. type = 1;
  333. blockSize = 8;
  334. break;
  335. case DDS_DXT2:
  336. case DDS_DXT3:
  337. type = 2;
  338. blockSize = 16;
  339. break;
  340. case DDS_DXT4:
  341. case DDS_DXT5:
  342. type = 3;
  343. blockSize = 16;
  344. break;
  345. }
  346. }
  347. else
  348. {
  349. switch (pixelFlags)
  350. {
  351. case DDPF_RGB:
  352. blockSize *= 3;
  353. break;
  354. case DDPF_RGB|DDPF_ALPHAPIXELS:
  355. blockSize *= 4;
  356. break;
  357. case DDPF_LUMINANCE:
  358. case DDPF_INDEXED:
  359. case DDPF_ALPHA:
  360. bpp = 1;
  361. break;
  362. default:
  363. bpp = 0;
  364. break;
  365. }
  366. }
  367. _dds.m_width = width;
  368. _dds.m_height = height;
  369. _dds.m_depth = depth;
  370. _dds.m_blockSize = blockSize;
  371. _dds.m_numMips = (caps[0] & DDSCAPS_MIPMAP) ? mips : 1;
  372. _dds.m_bpp = bpp;
  373. _dds.m_type = type;
  374. _dds.m_hasAlpha = hasAlpha;
  375. return true;
  376. }
  377. bool getRawImageData(const Dds& _dds, uint8_t _index, const Memory* _mem, Mip& _mip)
  378. {
  379. uint32_t width = _dds.m_width;
  380. uint32_t height = _dds.m_height;
  381. uint32_t blockSize = _dds.m_blockSize;
  382. uint32_t offset = DDS_IMAGE_DATA_OFFSET;
  383. uint8_t bpp = _dds.m_bpp;
  384. uint8_t type = _dds.m_type;
  385. bool hasAlpha = _dds.m_hasAlpha;
  386. for (uint8_t ii = 0, num = _dds.m_numMips; ii < num; ++ii)
  387. {
  388. width = uint32_max(1, width);
  389. height = uint32_max(1, height);
  390. uint32_t size = width*height*blockSize;
  391. if (0 != type)
  392. {
  393. width = uint32_max(1, (width + 3)>>2);
  394. height = uint32_max(1, (height + 3)>>2);
  395. size = width*height*blockSize;
  396. width <<= 2;
  397. height <<= 2;
  398. }
  399. if (ii == _index)
  400. {
  401. _mip.m_width = width;
  402. _mip.m_height = height;
  403. _mip.m_blockSize = blockSize;
  404. _mip.m_size = size;
  405. _mip.m_data = _mem->data + offset;
  406. _mip.m_bpp = bpp;
  407. _mip.m_type = type;
  408. _mip.m_hasAlpha = hasAlpha;
  409. return true;
  410. }
  411. offset += size;
  412. width >>= 1;
  413. height >>= 1;
  414. }
  415. return false;
  416. }
  417. } // namespace bgfx