image_saver_dds.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /**************************************************************************/
  2. /* image_saver_dds.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "image_saver_dds.h"
  31. #include "dds_enums.h"
  32. #include "core/io/file_access.h"
  33. #include "core/io/stream_peer.h"
  34. Error save_dds(const String &p_path, const Ref<Image> &p_img) {
  35. Vector<uint8_t> buffer = save_dds_buffer(p_img);
  36. Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE);
  37. if (file.is_null()) {
  38. return ERR_CANT_CREATE;
  39. }
  40. file->store_buffer(buffer.ptr(), buffer.size());
  41. return OK;
  42. }
  43. enum DDSFormatType {
  44. DDFT_BITMASK,
  45. DDFT_FOURCC,
  46. DDFT_DXGI,
  47. };
  48. DDSFormatType _dds_format_get_type(DDSFormat p_format) {
  49. switch (p_format) {
  50. case DDS_DXT1:
  51. case DDS_DXT3:
  52. case DDS_DXT5:
  53. case DDS_ATI1:
  54. case DDS_ATI2:
  55. case DDS_R16F:
  56. case DDS_RG16F:
  57. case DDS_RGBA16F:
  58. case DDS_R32F:
  59. case DDS_RG32F:
  60. case DDS_RGBA32F:
  61. return DDFT_FOURCC;
  62. case DDS_BC6S:
  63. case DDS_BC6U:
  64. case DDS_BC7:
  65. case DDS_RGB9E5:
  66. case DDS_RGB32F:
  67. return DDFT_DXGI;
  68. default:
  69. return DDFT_BITMASK;
  70. }
  71. }
  72. DDSFormat _image_format_to_dds_format(Image::Format p_image_format) {
  73. switch (p_image_format) {
  74. case Image::FORMAT_RGBAF: {
  75. return DDS_RGBA32F;
  76. }
  77. case Image::FORMAT_RGBF: {
  78. return DDS_RGB32F;
  79. }
  80. case Image::FORMAT_RGBAH: {
  81. return DDS_RGBA16F;
  82. }
  83. case Image::FORMAT_RGF: {
  84. return DDS_RG32F;
  85. }
  86. case Image::FORMAT_RGBA8: {
  87. return DDS_RGBA8;
  88. }
  89. case Image::FORMAT_RGH: {
  90. return DDS_RG16F;
  91. }
  92. case Image::FORMAT_RF: {
  93. return DDS_R32F;
  94. }
  95. case Image::FORMAT_L8:
  96. case Image::FORMAT_R8: {
  97. return DDS_LUMINANCE;
  98. }
  99. case Image::FORMAT_RH: {
  100. return DDS_R16F;
  101. }
  102. case Image::FORMAT_LA8:
  103. case Image::FORMAT_RG8: {
  104. return DDS_LUMINANCE_ALPHA;
  105. }
  106. case Image::FORMAT_RGBA4444: {
  107. return DDS_BGRA4;
  108. }
  109. case Image::FORMAT_RGB565: {
  110. return DDS_BGR565;
  111. }
  112. case Image::FORMAT_RGBE9995: {
  113. return DDS_RGB9E5;
  114. }
  115. case Image::FORMAT_DXT1: {
  116. return DDS_DXT1;
  117. }
  118. case Image::FORMAT_DXT3: {
  119. return DDS_DXT3;
  120. }
  121. case Image::FORMAT_DXT5: {
  122. return DDS_DXT5;
  123. }
  124. case Image::FORMAT_RGTC_R: {
  125. return DDS_ATI1;
  126. }
  127. case Image::FORMAT_RGTC_RG: {
  128. return DDS_ATI2;
  129. }
  130. case Image::FORMAT_RGB8: {
  131. return DDS_RGB8;
  132. }
  133. case Image::FORMAT_BPTC_RGBFU: {
  134. return DDS_BC6U;
  135. }
  136. case Image::FORMAT_BPTC_RGBF: {
  137. return DDS_BC6S;
  138. }
  139. case Image::FORMAT_BPTC_RGBA: {
  140. return DDS_BC7;
  141. }
  142. default: {
  143. return DDS_MAX;
  144. }
  145. }
  146. }
  147. uint32_t _image_format_to_fourcc_format(Image::Format p_format) {
  148. switch (p_format) {
  149. case Image::FORMAT_DXT1:
  150. return DDFCC_DXT1;
  151. case Image::FORMAT_DXT3:
  152. return DDFCC_DXT3;
  153. case Image::FORMAT_DXT5:
  154. return DDFCC_DXT5;
  155. case Image::FORMAT_RGTC_R:
  156. return DDFCC_ATI1;
  157. case Image::FORMAT_RGTC_RG:
  158. return DDFCC_ATI2;
  159. case Image::FORMAT_RF:
  160. return DDFCC_R32F;
  161. case Image::FORMAT_RGF:
  162. return DDFCC_RG32F;
  163. case Image::FORMAT_RGBAF:
  164. return DDFCC_RGBA32F;
  165. case Image::FORMAT_RH:
  166. return DDFCC_R16F;
  167. case Image::FORMAT_RGH:
  168. return DDFCC_RG16F;
  169. case Image::FORMAT_RGBAH:
  170. return DDFCC_RGBA16F;
  171. default:
  172. return 0;
  173. }
  174. }
  175. uint32_t _image_format_to_dxgi_format(Image::Format p_format) {
  176. switch (p_format) {
  177. case Image::FORMAT_DXT1:
  178. return DXGI_BC1_UNORM;
  179. case Image::FORMAT_DXT3:
  180. return DXGI_BC2_UNORM;
  181. case Image::FORMAT_DXT5:
  182. return DXGI_BC3_UNORM;
  183. case Image::FORMAT_RGTC_R:
  184. return DXGI_BC4_UNORM;
  185. case Image::FORMAT_RGTC_RG:
  186. return DXGI_BC5_UNORM;
  187. case Image::FORMAT_BPTC_RGBFU:
  188. return DXGI_BC6H_UF16;
  189. case Image::FORMAT_BPTC_RGBF:
  190. return DXGI_BC6H_SF16;
  191. case Image::FORMAT_BPTC_RGBA:
  192. return DXGI_BC7_UNORM;
  193. case Image::FORMAT_RF:
  194. return DXGI_R32_FLOAT;
  195. case Image::FORMAT_RGF:
  196. return DXGI_R32G32_FLOAT;
  197. case Image::FORMAT_RGBF:
  198. return DXGI_R32G32B32_FLOAT;
  199. case Image::FORMAT_RGBAF:
  200. return DXGI_R32G32B32A32_FLOAT;
  201. case Image::FORMAT_RH:
  202. return DXGI_R16_FLOAT;
  203. case Image::FORMAT_RGH:
  204. return DXGI_R16G16_FLOAT;
  205. case Image::FORMAT_RGBAH:
  206. return DXGI_R16G16B16A16_FLOAT;
  207. case Image::FORMAT_RGBE9995:
  208. return DXGI_R9G9B9E5;
  209. default:
  210. return 0;
  211. }
  212. }
  213. void _get_dds_pixel_bitmask(Image::Format p_format, uint32_t &r_bit_count, uint32_t &r_red_mask, uint32_t &r_green_mask, uint32_t &r_blue_mask, uint32_t &r_alpha_mask) {
  214. switch (p_format) {
  215. case Image::FORMAT_R8:
  216. case Image::FORMAT_L8: {
  217. r_bit_count = 8;
  218. r_red_mask = 0xff;
  219. r_green_mask = 0;
  220. r_blue_mask = 0;
  221. r_alpha_mask = 0;
  222. } break;
  223. case Image::FORMAT_RG8:
  224. case Image::FORMAT_LA8: {
  225. r_bit_count = 16;
  226. r_red_mask = 0xff;
  227. r_green_mask = 0;
  228. r_blue_mask = 0;
  229. r_alpha_mask = 0xff00;
  230. } break;
  231. case Image::FORMAT_RGB8: {
  232. // BGR8
  233. r_bit_count = 24;
  234. r_red_mask = 0xff0000;
  235. r_green_mask = 0xff00;
  236. r_blue_mask = 0xff;
  237. r_alpha_mask = 0;
  238. } break;
  239. case Image::FORMAT_RGBA8: {
  240. r_bit_count = 32;
  241. r_red_mask = 0xff;
  242. r_green_mask = 0xff00;
  243. r_blue_mask = 0xff0000;
  244. r_alpha_mask = 0xff000000;
  245. } break;
  246. case Image::FORMAT_RGBA4444: {
  247. // BGRA4444
  248. r_bit_count = 16;
  249. r_red_mask = 0xf00;
  250. r_green_mask = 0xf0;
  251. r_blue_mask = 0xf;
  252. r_alpha_mask = 0xf000;
  253. } break;
  254. case Image::FORMAT_RGB565: {
  255. // BGR565
  256. r_bit_count = 16;
  257. r_red_mask = 0xf800;
  258. r_green_mask = 0x7e0;
  259. r_blue_mask = 0x1f;
  260. r_alpha_mask = 0;
  261. } break;
  262. default: {
  263. r_bit_count = 0;
  264. r_red_mask = 0;
  265. r_green_mask = 0;
  266. r_blue_mask = 0;
  267. r_alpha_mask = 0;
  268. } break;
  269. }
  270. }
  271. Vector<uint8_t> save_dds_buffer(const Ref<Image> &p_img) {
  272. Ref<StreamPeerBuffer> stream_buffer;
  273. stream_buffer.instantiate();
  274. Ref<Image> image = p_img;
  275. stream_buffer->put_32(DDS_MAGIC);
  276. stream_buffer->put_32(DDS_HEADER_SIZE);
  277. uint32_t flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_LINEARSIZE;
  278. if (image->has_mipmaps()) {
  279. flags |= DDSD_MIPMAPCOUNT;
  280. }
  281. stream_buffer->put_32(flags);
  282. uint32_t height = image->get_height();
  283. stream_buffer->put_32(height);
  284. uint32_t width = image->get_width();
  285. stream_buffer->put_32(width);
  286. DDSFormat dds_format = _image_format_to_dds_format(image->get_format());
  287. const DDSFormatInfo &info = dds_format_info[dds_format];
  288. uint32_t depth = 1; // Default depth for 2D textures
  289. uint32_t pitch;
  290. if (info.compressed) {
  291. pitch = ((MAX(info.divisor, width) + info.divisor - 1) / info.divisor) * ((MAX(info.divisor, height) + info.divisor - 1) / info.divisor) * info.block_size;
  292. } else {
  293. pitch = width * info.block_size;
  294. }
  295. stream_buffer->put_32(pitch);
  296. stream_buffer->put_32(depth);
  297. uint32_t mipmaps = image->get_mipmap_count() + 1;
  298. stream_buffer->put_32(mipmaps);
  299. uint32_t reserved = 0;
  300. for (int i = 0; i < 11; i++) {
  301. stream_buffer->put_32(reserved);
  302. }
  303. stream_buffer->put_32(DDS_PIXELFORMAT_SIZE);
  304. uint32_t pf_flags = 0;
  305. DDSFormatType format_type = _dds_format_get_type(dds_format);
  306. if (format_type == DDFT_BITMASK) {
  307. pf_flags = DDPF_RGB;
  308. if (image->get_format() == Image::FORMAT_LA8 || image->get_format() == Image::FORMAT_RG8 || image->get_format() == Image::FORMAT_RGBA8 || image->get_format() == Image::FORMAT_RGBA4444) {
  309. pf_flags |= DDPF_ALPHAPIXELS;
  310. }
  311. } else {
  312. pf_flags = DDPF_FOURCC;
  313. }
  314. stream_buffer->put_32(pf_flags);
  315. bool needs_pixeldata_swap = false;
  316. if (format_type == DDFT_BITMASK) {
  317. // Uncompressed bitmasked.
  318. stream_buffer->put_32(0); // FourCC
  319. uint32_t bit_count, r_mask, g_mask, b_mask, a_mask;
  320. _get_dds_pixel_bitmask(image->get_format(), bit_count, r_mask, g_mask, b_mask, a_mask);
  321. stream_buffer->put_32(bit_count);
  322. stream_buffer->put_32(r_mask);
  323. stream_buffer->put_32(g_mask);
  324. stream_buffer->put_32(b_mask);
  325. stream_buffer->put_32(a_mask);
  326. if (image->get_format() == Image::FORMAT_RGBA4444 || image->get_format() == Image::FORMAT_RGB565 || image->get_format() == Image::FORMAT_RGB8) {
  327. needs_pixeldata_swap = true;
  328. }
  329. } else if (format_type == DDFT_FOURCC) {
  330. // FourCC.
  331. uint32_t fourcc = _image_format_to_fourcc_format(image->get_format());
  332. stream_buffer->put_32(fourcc);
  333. stream_buffer->put_32(0); // Bit count
  334. stream_buffer->put_32(0); // R Bitmask
  335. stream_buffer->put_32(0); // G Bitmask
  336. stream_buffer->put_32(0); // B Bitmask
  337. stream_buffer->put_32(0); // A Bitmask
  338. } else {
  339. // DXGI format and DX10 header.
  340. stream_buffer->put_32(DDFCC_DX10);
  341. stream_buffer->put_32(0); // Bit count
  342. stream_buffer->put_32(0); // R Bitmask
  343. stream_buffer->put_32(0); // G Bitmask
  344. stream_buffer->put_32(0); // B Bitmask
  345. stream_buffer->put_32(0); // A Bitmask
  346. }
  347. uint32_t caps1 = info.compressed ? DDSD_LINEARSIZE : DDSD_PITCH;
  348. stream_buffer->put_32(caps1);
  349. stream_buffer->put_32(0); // Caps2
  350. stream_buffer->put_32(0); // Caps3
  351. stream_buffer->put_32(0); // Caps4
  352. stream_buffer->put_32(0); // Reserved 2
  353. if (format_type == DDFT_DXGI) {
  354. // DX10 header.
  355. uint32_t dxgi_format = _image_format_to_dxgi_format(image->get_format());
  356. stream_buffer->put_32(dxgi_format);
  357. stream_buffer->put_32(DX10D_2D);
  358. stream_buffer->put_32(0); // Misc flags 1
  359. stream_buffer->put_32(1); // Array size
  360. stream_buffer->put_32(0); // Misc flags 2
  361. }
  362. for (uint32_t mip_i = 0; mip_i < mipmaps; mip_i++) {
  363. uint32_t mip_width = MAX(1u, width >> mip_i);
  364. uint32_t mip_height = MAX(1u, height >> mip_i);
  365. uint32_t expected_size = 0;
  366. if (info.compressed) {
  367. uint32_t blocks_x = (mip_width + info.divisor - 1) / info.divisor;
  368. uint32_t blocks_y = (mip_height + info.divisor - 1) / info.divisor;
  369. expected_size = blocks_x * blocks_y * info.block_size;
  370. } else {
  371. expected_size = mip_width * mip_height * info.block_size;
  372. }
  373. if (needs_pixeldata_swap) {
  374. // The image's channels need to be swapped.
  375. Ref<Image> mip_image = image->get_image_from_mipmap(mip_i);
  376. Vector<uint8_t> data = mip_image->get_data();
  377. ERR_FAIL_COND_V_MSG(data.size() != expected_size, Vector<uint8_t>(),
  378. "Image data size mismatch for mipmap level " + itos(mip_i) +
  379. ". Expected size: " + itos(expected_size) + ", actual size: " + itos(data.size()) + ".");
  380. if (mip_image->get_format() == Image::FORMAT_RGBA4444) {
  381. // RGBA4 to BGRA4
  382. const int64_t data_size = data.size();
  383. uint8_t *wb = data.ptrw();
  384. for (int64_t data_i = 0; data_i < data_size; data_i += 2) {
  385. uint8_t ar = wb[data_i + 0];
  386. uint8_t gb = wb[data_i + 1];
  387. wb[data_i + 1] = ((ar & 0x0F) << 4) | ((gb & 0xF0) >> 4);
  388. wb[data_i + 0] = ((ar & 0xF0) >> 4) | ((gb & 0x0F) << 4);
  389. }
  390. } else if (mip_image->get_format() == Image::FORMAT_RGB565) {
  391. // RGB565 to BGR565
  392. const int64_t data_size = data.size();
  393. uint8_t *wb = data.ptrw();
  394. for (int64_t data_i = 0; data_i < data_size; data_i += 2) {
  395. uint16_t px = wb[data_i] | (wb[data_i + 1] << 8);
  396. uint8_t r = (px >> 11) & 0x1F;
  397. uint8_t g = (px >> 5) & 0x3F;
  398. uint8_t b = px & 0x1F;
  399. uint16_t out_px = (b << 11) | (g << 5) | r;
  400. wb[data_i + 0] = out_px & 0xFF;
  401. wb[data_i + 1] = (out_px >> 8) & 0xFF;
  402. }
  403. } else if (mip_image->get_format() == Image::FORMAT_RGB8) {
  404. // RGB8 to BGR8
  405. const int64_t data_size = data.size();
  406. uint8_t *wb = data.ptrw();
  407. for (int64_t data_i = 0; data_i < data_size; data_i += 3) {
  408. SWAP(wb[data_i], wb[data_i + 2]);
  409. }
  410. }
  411. stream_buffer->put_data(data.ptr(), data.size());
  412. } else {
  413. int64_t ofs, size;
  414. image->get_mipmap_offset_and_size(mip_i, ofs, size);
  415. ERR_FAIL_COND_V_MSG(size != expected_size, Vector<uint8_t>(),
  416. "Image data size mismatch for mipmap level " + itos(mip_i) +
  417. ". Expected size: " + itos(expected_size) + ", actual size: " + itos(size) + ".");
  418. stream_buffer->put_data(image->ptr() + ofs, size);
  419. }
  420. }
  421. return stream_buffer->get_data_array();
  422. }