DDSLoader.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/DeprecatedString.h>
  8. #include <AK/Endian.h>
  9. #include <AK/Error.h>
  10. #include <AK/MemoryStream.h>
  11. #include <AK/StringBuilder.h>
  12. #include <AK/Try.h>
  13. #include <AK/Vector.h>
  14. #include <LibGfx/ImageFormats/DDSLoader.h>
  15. #include <fcntl.h>
  16. #include <math.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/mman.h>
  20. #include <sys/stat.h>
  21. #include <unistd.h>
  22. namespace Gfx {
  23. struct DDSLoadingContext {
  24. enum State {
  25. NotDecoded = 0,
  26. Error,
  27. BitmapDecoded,
  28. };
  29. State state { State::NotDecoded };
  30. u8 const* data { nullptr };
  31. size_t data_size { 0 };
  32. DDSHeader header;
  33. DDSHeaderDXT10 header10;
  34. RefPtr<Gfx::Bitmap> bitmap;
  35. void dump_debug();
  36. };
  37. static constexpr u32 create_four_cc(char c0, char c1, char c2, char c3)
  38. {
  39. return c0 | c1 << 8 | c2 << 16 | c3 << 24;
  40. }
  41. static u64 get_width(DDSHeader header, size_t mipmap_level)
  42. {
  43. if (mipmap_level >= header.mip_map_count) {
  44. return header.width;
  45. }
  46. return header.width >> mipmap_level;
  47. }
  48. static u64 get_height(DDSHeader header, size_t mipmap_level)
  49. {
  50. if (mipmap_level >= header.mip_map_count) {
  51. return header.height;
  52. }
  53. return header.height >> mipmap_level;
  54. }
  55. static constexpr bool has_bitmask(DDSPixelFormat format, u32 r, u32 g, u32 b, u32 a)
  56. {
  57. return format.r_bit_mask == r && format.g_bit_mask == g && format.b_bit_mask == b && format.a_bit_mask == a;
  58. }
  59. static DXGIFormat get_format(DDSPixelFormat format)
  60. {
  61. if ((format.flags & PixelFormatFlags::DDPF_RGB) == PixelFormatFlags::DDPF_RGB) {
  62. switch (format.rgb_bit_count) {
  63. case 32: {
  64. if (has_bitmask(format, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000))
  65. return DXGI_FORMAT_R8G8B8A8_UNORM;
  66. if (has_bitmask(format, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000))
  67. return DXGI_FORMAT_B8G8R8A8_UNORM;
  68. if (has_bitmask(format, 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000))
  69. return DXGI_FORMAT_B8G8R8X8_UNORM;
  70. if (has_bitmask(format, 0x3FF00000, 0x000FFC00, 0x000003FF, 0xC0000000))
  71. return DXGI_FORMAT_R10G10B10A2_UNORM;
  72. if (has_bitmask(format, 0x0000FFFF, 0xFFFF0000, 0x00000000, 0x00000000))
  73. return DXGI_FORMAT_R16G16_UNORM;
  74. if (has_bitmask(format, 0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000))
  75. return DXGI_FORMAT_R32_FLOAT;
  76. break;
  77. }
  78. case 24:
  79. break;
  80. case 16: {
  81. if (has_bitmask(format, 0x7C00, 0x03E0, 0x001F, 0x8000))
  82. return DXGI_FORMAT_B5G5R5A1_UNORM;
  83. if (has_bitmask(format, 0xF800, 0x07E0, 0x001F, 0x0000))
  84. return DXGI_FORMAT_B5G6R5_UNORM;
  85. if (has_bitmask(format, 0xF800, 0x07E0, 0x001F, 0x0000))
  86. return DXGI_FORMAT_B5G6R5_UNORM;
  87. if (has_bitmask(format, 0x0F00, 0x00F0, 0x000F, 0xF000))
  88. return DXGI_FORMAT_B4G4R4A4_UNORM;
  89. if (has_bitmask(format, 0x00FF, 0x0000, 0x0000, 0xFF00))
  90. return DXGI_FORMAT_R8G8_UNORM;
  91. if (has_bitmask(format, 0xFFFF, 0x0000, 0x0000, 0x0000))
  92. return DXGI_FORMAT_R16_UNORM;
  93. break;
  94. }
  95. case 8: {
  96. if (has_bitmask(format, 0xFF, 0x00, 0x00, 0x00))
  97. return DXGI_FORMAT_R8_UNORM;
  98. break;
  99. }
  100. }
  101. } else if ((format.flags & PixelFormatFlags::DDPF_LUMINANCE) == PixelFormatFlags::DDPF_LUMINANCE) {
  102. switch (format.rgb_bit_count) {
  103. case 16: {
  104. if (has_bitmask(format, 0xFFFF, 0x0000, 0x0000, 0x0000))
  105. return DXGI_FORMAT_R16_UNORM;
  106. if (has_bitmask(format, 0x00FF, 0x0000, 0x0000, 0xFF00))
  107. return DXGI_FORMAT_R8G8_UNORM;
  108. break;
  109. }
  110. case 8: {
  111. if (has_bitmask(format, 0xFF, 0x00, 0x00, 0x00))
  112. return DXGI_FORMAT_R8_UNORM;
  113. // Some writers mistakenly write this as 8 bpp.
  114. if (has_bitmask(format, 0x00FF, 0x0000, 0x0000, 0xFF00))
  115. return DXGI_FORMAT_R8G8_UNORM;
  116. break;
  117. }
  118. }
  119. } else if ((format.flags & PixelFormatFlags::DDPF_ALPHA) == PixelFormatFlags::DDPF_ALPHA) {
  120. if (format.rgb_bit_count == 8)
  121. return DXGI_FORMAT_A8_UNORM;
  122. } else if ((format.flags & PixelFormatFlags::DDPF_BUMPDUDV) == PixelFormatFlags::DDPF_BUMPDUDV) {
  123. switch (format.rgb_bit_count) {
  124. case 32: {
  125. if (has_bitmask(format, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000))
  126. return DXGI_FORMAT_R8G8B8A8_SNORM;
  127. if (has_bitmask(format, 0x0000FFFF, 0xFFFF0000, 0x00000000, 0x00000000))
  128. return DXGI_FORMAT_R16G16_SNORM;
  129. break;
  130. }
  131. case 16: {
  132. if (has_bitmask(format, 0x00FF, 0xFF00, 0x0000, 0x0000))
  133. return DXGI_FORMAT_R8G8_SNORM;
  134. break;
  135. }
  136. }
  137. } else if ((format.flags & PixelFormatFlags::DDPF_FOURCC) == PixelFormatFlags::DDPF_FOURCC) {
  138. if (format.four_cc == create_four_cc('D', 'X', 'T', '1'))
  139. return DXGI_FORMAT_BC1_UNORM;
  140. if (format.four_cc == create_four_cc('D', 'X', 'T', '2'))
  141. return DXGI_FORMAT_BC2_UNORM;
  142. if (format.four_cc == create_four_cc('D', 'X', 'T', '3'))
  143. return DXGI_FORMAT_BC2_UNORM;
  144. if (format.four_cc == create_four_cc('D', 'X', 'T', '4'))
  145. return DXGI_FORMAT_BC3_UNORM;
  146. if (format.four_cc == create_four_cc('D', 'X', 'T', '5'))
  147. return DXGI_FORMAT_BC3_UNORM;
  148. if (format.four_cc == create_four_cc('A', 'T', 'I', '1'))
  149. return DXGI_FORMAT_BC4_UNORM;
  150. if (format.four_cc == create_four_cc('B', 'C', '4', 'U'))
  151. return DXGI_FORMAT_BC4_UNORM;
  152. if (format.four_cc == create_four_cc('B', 'C', '4', 'S'))
  153. return DXGI_FORMAT_BC4_SNORM;
  154. if (format.four_cc == create_four_cc('A', 'T', 'I', '2'))
  155. return DXGI_FORMAT_BC5_UNORM;
  156. if (format.four_cc == create_four_cc('B', 'C', '5', 'U'))
  157. return DXGI_FORMAT_BC5_UNORM;
  158. if (format.four_cc == create_four_cc('B', 'C', '5', 'S'))
  159. return DXGI_FORMAT_BC5_SNORM;
  160. if (format.four_cc == create_four_cc('R', 'G', 'B', 'G'))
  161. return DXGI_FORMAT_R8G8_B8G8_UNORM;
  162. if (format.four_cc == create_four_cc('G', 'R', 'G', 'B'))
  163. return DXGI_FORMAT_G8R8_G8B8_UNORM;
  164. if (format.four_cc == create_four_cc('Y', 'U', 'Y', '2'))
  165. return DXGI_FORMAT_YUY2;
  166. switch (format.four_cc) {
  167. case 36:
  168. return DXGI_FORMAT_R16G16B16A16_UNORM;
  169. case 110:
  170. return DXGI_FORMAT_R16G16B16A16_SNORM;
  171. case 111:
  172. return DXGI_FORMAT_R16_FLOAT;
  173. case 112:
  174. return DXGI_FORMAT_R16G16_FLOAT;
  175. case 113:
  176. return DXGI_FORMAT_R16G16B16A16_FLOAT;
  177. case 114:
  178. return DXGI_FORMAT_R32_FLOAT;
  179. case 115:
  180. return DXGI_FORMAT_R32G32_FLOAT;
  181. case 116:
  182. return DXGI_FORMAT_R32G32B32A32_FLOAT;
  183. }
  184. }
  185. return DXGI_FORMAT_UNKNOWN;
  186. }
  187. static ErrorOr<void> decode_dx5_alpha_block(Stream& stream, DDSLoadingContext& context, u64 bitmap_x, u64 bitmap_y)
  188. {
  189. auto color0 = TRY(stream.read_value<LittleEndian<u8>>());
  190. auto color1 = TRY(stream.read_value<LittleEndian<u8>>());
  191. auto code0 = TRY(stream.read_value<LittleEndian<u8>>());
  192. auto code1 = TRY(stream.read_value<LittleEndian<u8>>());
  193. auto code2 = TRY(stream.read_value<LittleEndian<u8>>());
  194. auto code3 = TRY(stream.read_value<LittleEndian<u8>>());
  195. auto code4 = TRY(stream.read_value<LittleEndian<u8>>());
  196. auto code5 = TRY(stream.read_value<LittleEndian<u8>>());
  197. u32 codes[6] = { 0 };
  198. codes[0] = code0 + 256 * (code1 + 256);
  199. codes[1] = code1 + 256 * (code2 + 256);
  200. codes[2] = code2 + 256 * (code3 + 256);
  201. codes[3] = code3 + 256 * (code4 + 256);
  202. codes[4] = code4 + 256 * code5;
  203. codes[5] = code5;
  204. u32 color[8] = { 0 };
  205. if (color0 > 128) {
  206. color[0] = color0;
  207. }
  208. if (color1 > 128) {
  209. color[1] = color1;
  210. }
  211. if (color0 > color1) {
  212. color[2] = (6 * color[0] + 1 * color[1]) / 7;
  213. color[3] = (5 * color[0] + 2 * color[1]) / 7;
  214. color[4] = (4 * color[0] + 3 * color[1]) / 7;
  215. color[5] = (3 * color[0] + 4 * color[1]) / 7;
  216. color[6] = (2 * color[0] + 5 * color[1]) / 7;
  217. color[7] = (1 * color[0] + 6 * color[1]) / 7;
  218. } else {
  219. color[2] = (4 * color[0] + 1 * color[1]) / 5;
  220. color[3] = (3 * color[0] + 2 * color[1]) / 5;
  221. color[4] = (2 * color[0] + 3 * color[1]) / 5;
  222. color[5] = (1 * color[0] + 4 * color[1]) / 5;
  223. color[6] = 0;
  224. color[7] = 255;
  225. }
  226. for (size_t y = 0; y < 4; y++) {
  227. for (size_t x = 0; x < 4; x++) {
  228. u8 index = 3 * (4 * y + x);
  229. u8 bit_location = floor(index / 8.0);
  230. u8 adjusted_index = index - (bit_location * 8);
  231. u8 code = (codes[bit_location] >> adjusted_index) & 7;
  232. u8 alpha = color[code];
  233. Color color = Color(0, 0, 0, alpha);
  234. context.bitmap->set_pixel(bitmap_x + x, bitmap_y + y, color);
  235. }
  236. }
  237. return {};
  238. }
  239. static ErrorOr<void> decode_dx3_alpha_block(Stream& stream, DDSLoadingContext& context, u64 bitmap_x, u64 bitmap_y)
  240. {
  241. auto a0 = TRY(stream.read_value<LittleEndian<u8>>());
  242. auto a1 = TRY(stream.read_value<LittleEndian<u8>>());
  243. auto a2 = TRY(stream.read_value<LittleEndian<u8>>());
  244. auto a3 = TRY(stream.read_value<LittleEndian<u8>>());
  245. auto a4 = TRY(stream.read_value<LittleEndian<u8>>());
  246. auto a5 = TRY(stream.read_value<LittleEndian<u8>>());
  247. auto a6 = TRY(stream.read_value<LittleEndian<u8>>());
  248. auto a7 = TRY(stream.read_value<LittleEndian<u8>>());
  249. u64 alpha_0 = a0 + 256u * (a1 + 256u * (a2 + 256u * (a3 + 256u)));
  250. u64 alpha_1 = a4 + 256u * (a5 + 256u * (a6 + 256u * a7));
  251. for (size_t y = 0; y < 4; y++) {
  252. for (size_t x = 0; x < 4; x++) {
  253. u8 code = 4 * (4 * y + x);
  254. if (code >= 32) {
  255. code = code - 32;
  256. u8 alpha = ((alpha_1 >> code) & 0x0F) * 17;
  257. Color color = Color(0, 0, 0, alpha);
  258. context.bitmap->set_pixel(bitmap_x + x, bitmap_y + y, color);
  259. } else {
  260. u8 alpha = ((alpha_0 >> code) & 0x0F) * 17;
  261. Color color = Color(0, 0, 0, alpha);
  262. context.bitmap->set_pixel(bitmap_x + x, bitmap_y + y, color);
  263. }
  264. }
  265. }
  266. return {};
  267. }
  268. static void unpack_rbg_565(u32 rgb, u8* output)
  269. {
  270. u8 r = (rgb >> 11) & 0x1F;
  271. u8 g = (rgb >> 5) & 0x3F;
  272. u8 b = rgb & 0x1F;
  273. output[0] = (r << 3) | (r >> 2);
  274. output[1] = (g << 2) | (g >> 4);
  275. output[2] = (b << 3) | (b >> 2);
  276. output[3] = 255;
  277. }
  278. static ErrorOr<void> decode_color_block(Stream& stream, DDSLoadingContext& context, bool dxt1, u64 bitmap_x, u64 bitmap_y)
  279. {
  280. auto c0_low = TRY(stream.read_value<LittleEndian<u8>>());
  281. auto c0_high = TRY(stream.read_value<LittleEndian<u8>>());
  282. auto c1_low = TRY(stream.read_value<LittleEndian<u8>>());
  283. auto c1_high = TRY(stream.read_value<LittleEndian<u8>>());
  284. auto codes_0 = TRY(stream.read_value<LittleEndian<u8>>());
  285. auto codes_1 = TRY(stream.read_value<LittleEndian<u8>>());
  286. auto codes_2 = TRY(stream.read_value<LittleEndian<u8>>());
  287. auto codes_3 = TRY(stream.read_value<LittleEndian<u8>>());
  288. u64 code = codes_0 + 256 * (codes_1 + 256 * (codes_2 + 256 * codes_3));
  289. u32 color_0 = c0_low + (c0_high * 256);
  290. u32 color_1 = c1_low + (c1_high * 256);
  291. u8 rgba[4][4];
  292. unpack_rbg_565(color_0, rgba[0]);
  293. unpack_rbg_565(color_1, rgba[1]);
  294. if (color_0 > color_1) {
  295. for (size_t i = 0; i < 3; i++) {
  296. rgba[2][i] = (2 * rgba[0][i] + rgba[1][i]) / 3;
  297. rgba[3][i] = (rgba[0][i] + 2 * rgba[1][i]) / 3;
  298. }
  299. rgba[2][3] = 255;
  300. rgba[3][3] = 255;
  301. } else {
  302. for (size_t i = 0; i < 3; i++) {
  303. rgba[2][i] = (rgba[0][i] + rgba[1][i]) / 2;
  304. rgba[3][i] = 0;
  305. }
  306. rgba[2][3] = 255;
  307. rgba[3][3] = dxt1 ? 0 : 255;
  308. }
  309. size_t i = 0;
  310. for (size_t y = 0; y < 4; y++) {
  311. for (size_t x = 0; x < 4; x++) {
  312. u8 code_byte = (code >> (i * 2)) & 3;
  313. u8 r = rgba[code_byte][0];
  314. u8 g = rgba[code_byte][1];
  315. u8 b = rgba[code_byte][2];
  316. u8 a = dxt1 ? rgba[code_byte][3] : context.bitmap->get_pixel(bitmap_x + x, bitmap_y + y).alpha();
  317. Color color = Color(r, g, b, a);
  318. context.bitmap->set_pixel(bitmap_x + x, bitmap_y + y, color);
  319. i++;
  320. }
  321. }
  322. return {};
  323. }
  324. static ErrorOr<void> decode_dxt(Stream& stream, DDSLoadingContext& context, DXGIFormat format, u64 width, u64 y)
  325. {
  326. if (format == DXGI_FORMAT_BC1_UNORM) {
  327. for (size_t x = 0; x < width; x += 4) {
  328. TRY(decode_color_block(stream, context, true, x, y));
  329. }
  330. }
  331. if (format == DXGI_FORMAT_BC2_UNORM) {
  332. for (size_t x = 0; x < width; x += 4) {
  333. TRY(decode_dx3_alpha_block(stream, context, x, y));
  334. TRY(decode_color_block(stream, context, false, x, y));
  335. }
  336. }
  337. if (format == DXGI_FORMAT_BC3_UNORM) {
  338. for (size_t x = 0; x < width; x += 4) {
  339. TRY(decode_dx5_alpha_block(stream, context, x, y));
  340. TRY(decode_color_block(stream, context, false, x, y));
  341. }
  342. }
  343. return {};
  344. }
  345. static ErrorOr<void> decode_bitmap(Stream& stream, DDSLoadingContext& context, DXGIFormat format, u64 width, u64 height)
  346. {
  347. Vector<u32> dxt_formats = { DXGI_FORMAT_BC1_UNORM, DXGI_FORMAT_BC2_UNORM, DXGI_FORMAT_BC3_UNORM };
  348. if (dxt_formats.contains_slow(format)) {
  349. for (u64 y = 0; y < height; y += 4) {
  350. TRY(decode_dxt(stream, context, format, width, y));
  351. }
  352. }
  353. // FIXME: Support more encodings (ATI, YUV, RAW, etc...).
  354. return {};
  355. }
  356. static ErrorOr<void> decode_dds(DDSLoadingContext& context)
  357. {
  358. // All valid DDS files are at least 128 bytes long.
  359. if (context.data_size < 128) {
  360. dbgln_if(DDS_DEBUG, "File is too short for DDS");
  361. context.state = DDSLoadingContext::State::Error;
  362. return Error::from_string_literal("File is too short for DDS");
  363. }
  364. FixedMemoryStream stream { ReadonlyBytes { context.data, context.data_size } };
  365. auto magic = TRY(stream.read_value<u32>());
  366. if (magic != create_four_cc('D', 'D', 'S', ' ')) {
  367. dbgln_if(DDS_DEBUG, "Missing magic number");
  368. context.state = DDSLoadingContext::State::Error;
  369. return Error::from_string_literal("Missing magic number");
  370. }
  371. context.header = TRY(stream.read_value<DDSHeader>());
  372. if (context.header.size != 124) {
  373. dbgln_if(DDS_DEBUG, "Header size is malformed");
  374. context.state = DDSLoadingContext::State::Error;
  375. return Error::from_string_literal("Header size is malformed");
  376. }
  377. if (context.header.pixel_format.size != 32) {
  378. dbgln_if(DDS_DEBUG, "Pixel format size is malformed");
  379. context.state = DDSLoadingContext::State::Error;
  380. return Error::from_string_literal("Pixel format size is malformed");
  381. }
  382. if ((context.header.pixel_format.flags & PixelFormatFlags::DDPF_FOURCC) == PixelFormatFlags::DDPF_FOURCC) {
  383. if (context.header.pixel_format.four_cc == create_four_cc('D', 'X', '1', '0')) {
  384. if (context.data_size < 148) {
  385. dbgln_if(DDS_DEBUG, "DX10 header is too short");
  386. context.state = DDSLoadingContext::State::Error;
  387. return Error::from_string_literal("DX10 header is too short");
  388. }
  389. context.header10 = TRY(stream.read_value<DDSHeaderDXT10>());
  390. }
  391. }
  392. if constexpr (DDS_DEBUG) {
  393. context.dump_debug();
  394. }
  395. DXGIFormat format = get_format(context.header.pixel_format);
  396. Vector<u32> supported_formats = { DXGI_FORMAT_BC1_UNORM, DXGI_FORMAT_BC2_UNORM, DXGI_FORMAT_BC3_UNORM };
  397. if (!supported_formats.contains_slow(format)) {
  398. dbgln_if(DDS_DEBUG, "Format of type {} is not supported at the moment", static_cast<u32>(format));
  399. context.state = DDSLoadingContext::State::Error;
  400. return Error::from_string_literal("Format type is not supported at the moment");
  401. }
  402. // We support parsing mipmaps, but we only care about the largest one :^) (At least for now)
  403. if (size_t mipmap_level = 0; mipmap_level < max(context.header.mip_map_count, 1u)) {
  404. u64 width = get_width(context.header, mipmap_level);
  405. u64 height = get_height(context.header, mipmap_level);
  406. context.bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, { width, height }));
  407. TRY(decode_bitmap(stream, context, format, width, height));
  408. }
  409. context.state = DDSLoadingContext::State::BitmapDecoded;
  410. return {};
  411. }
  412. void DDSLoadingContext::dump_debug()
  413. {
  414. StringBuilder builder;
  415. builder.append("\nDDS:\n"sv);
  416. builder.appendff("\tHeader Size: {}\n", header.size);
  417. builder.append("\tFlags:"sv);
  418. if ((header.flags & DDSFlags::DDSD_CAPS) == DDSFlags::DDSD_CAPS)
  419. builder.append(" DDSD_CAPS"sv);
  420. if ((header.flags & DDSFlags::DDSD_HEIGHT) == DDSFlags::DDSD_HEIGHT)
  421. builder.append(" DDSD_HEIGHT"sv);
  422. if ((header.flags & DDSFlags::DDSD_WIDTH) == DDSFlags::DDSD_WIDTH)
  423. builder.append(" DDSD_WIDTH"sv);
  424. if ((header.flags & DDSFlags::DDSD_PITCH) == DDSFlags::DDSD_PITCH)
  425. builder.append(" DDSD_PITCH"sv);
  426. if ((header.flags & DDSFlags::DDSD_PIXELFORMAT) == DDSFlags::DDSD_PIXELFORMAT)
  427. builder.append(" DDSD_PIXELFORMAT"sv);
  428. if ((header.flags & DDSFlags::DDSD_MIPMAPCOUNT) == DDSFlags::DDSD_MIPMAPCOUNT)
  429. builder.append(" DDSD_MIPMAPCOUNT"sv);
  430. if ((header.flags & DDSFlags::DDSD_LINEARSIZE) == DDSFlags::DDSD_LINEARSIZE)
  431. builder.append(" DDSD_LINEARSIZE"sv);
  432. if ((header.flags & DDSFlags::DDSD_DEPTH) == DDSFlags::DDSD_DEPTH)
  433. builder.append(" DDSD_DEPTH"sv);
  434. builder.append("\n"sv);
  435. builder.appendff("\tHeight: {}\n", header.height);
  436. builder.appendff("\tWidth: {}\n", header.width);
  437. builder.appendff("\tPitch: {}\n", header.pitch);
  438. builder.appendff("\tDepth: {}\n", header.depth);
  439. builder.appendff("\tMipmap Count: {}\n", header.mip_map_count);
  440. builder.append("\tCaps:"sv);
  441. if ((header.caps1 & Caps1Flags::DDSCAPS_COMPLEX) == Caps1Flags::DDSCAPS_COMPLEX)
  442. builder.append(" DDSCAPS_COMPLEX"sv);
  443. if ((header.caps1 & Caps1Flags::DDSCAPS_MIPMAP) == Caps1Flags::DDSCAPS_MIPMAP)
  444. builder.append(" DDSCAPS_MIPMAP"sv);
  445. if ((header.caps1 & Caps1Flags::DDSCAPS_TEXTURE) == Caps1Flags::DDSCAPS_TEXTURE)
  446. builder.append(" DDSCAPS_TEXTURE"sv);
  447. builder.append("\n"sv);
  448. builder.append("\tCaps2:"sv);
  449. if ((header.caps2 & Caps2Flags::DDSCAPS2_CUBEMAP) == Caps2Flags::DDSCAPS2_CUBEMAP)
  450. builder.append(" DDSCAPS2_CUBEMAP"sv);
  451. if ((header.caps2 & Caps2Flags::DDSCAPS2_CUBEMAP_POSITIVEX) == Caps2Flags::DDSCAPS2_CUBEMAP_POSITIVEX)
  452. builder.append(" DDSCAPS2_CUBEMAP_POSITIVEX"sv);
  453. if ((header.caps2 & Caps2Flags::DDSCAPS2_CUBEMAP_NEGATIVEX) == Caps2Flags::DDSCAPS2_CUBEMAP_NEGATIVEX)
  454. builder.append(" DDSCAPS2_CUBEMAP_NEGATIVEX"sv);
  455. if ((header.caps2 & Caps2Flags::DDSCAPS2_CUBEMAP_POSITIVEY) == Caps2Flags::DDSCAPS2_CUBEMAP_POSITIVEY)
  456. builder.append(" DDSCAPS2_CUBEMAP_POSITIVEY"sv);
  457. if ((header.caps2 & Caps2Flags::DDSCAPS2_CUBEMAP_NEGATIVEY) == Caps2Flags::DDSCAPS2_CUBEMAP_NEGATIVEY)
  458. builder.append(" DDSCAPS2_CUBEMAP_NEGATIVEY"sv);
  459. if ((header.caps2 & Caps2Flags::DDSCAPS2_CUBEMAP_POSITIVEZ) == Caps2Flags::DDSCAPS2_CUBEMAP_POSITIVEZ)
  460. builder.append(" DDSCAPS2_CUBEMAP_POSITIVEZ"sv);
  461. if ((header.caps2 & Caps2Flags::DDSCAPS2_CUBEMAP_NEGATIVEZ) == Caps2Flags::DDSCAPS2_CUBEMAP_NEGATIVEZ)
  462. builder.append(" DDSCAPS2_CUBEMAP_NEGATIVEZ"sv);
  463. if ((header.caps2 & Caps2Flags::DDSCAPS2_VOLUME) == Caps2Flags::DDSCAPS2_VOLUME)
  464. builder.append(" DDSCAPS2_VOLUME"sv);
  465. builder.append("\n"sv);
  466. builder.append("Pixel Format:\n"sv);
  467. builder.appendff("\tStruct Size: {}\n", header.pixel_format.size);
  468. builder.append("\tFlags:"sv);
  469. if ((header.pixel_format.flags & PixelFormatFlags::DDPF_ALPHAPIXELS) == PixelFormatFlags::DDPF_ALPHAPIXELS)
  470. builder.append(" DDPF_ALPHAPIXELS"sv);
  471. if ((header.pixel_format.flags & PixelFormatFlags::DDPF_ALPHA) == PixelFormatFlags::DDPF_ALPHA)
  472. builder.append(" DDPF_ALPHA"sv);
  473. if ((header.pixel_format.flags & PixelFormatFlags::DDPF_FOURCC) == PixelFormatFlags::DDPF_FOURCC)
  474. builder.append(" DDPF_FOURCC"sv);
  475. if ((header.pixel_format.flags & PixelFormatFlags::DDPF_PALETTEINDEXED8) == PixelFormatFlags::DDPF_PALETTEINDEXED8)
  476. builder.append(" DDPF_PALETTEINDEXED8"sv);
  477. if ((header.pixel_format.flags & PixelFormatFlags::DDPF_RGB) == PixelFormatFlags::DDPF_RGB)
  478. builder.append(" DDPF_RGB"sv);
  479. if ((header.pixel_format.flags & PixelFormatFlags::DDPF_YUV) == PixelFormatFlags::DDPF_YUV)
  480. builder.append(" DDPF_YUV"sv);
  481. if ((header.pixel_format.flags & PixelFormatFlags::DDPF_LUMINANCE) == PixelFormatFlags::DDPF_LUMINANCE)
  482. builder.append(" DDPF_LUMINANCE"sv);
  483. if ((header.pixel_format.flags & PixelFormatFlags::DDPF_BUMPDUDV) == PixelFormatFlags::DDPF_BUMPDUDV)
  484. builder.append(" DDPF_BUMPDUDV"sv);
  485. if ((header.pixel_format.flags & PixelFormatFlags::DDPF_NORMAL) == PixelFormatFlags::DDPF_NORMAL)
  486. builder.append(" DDPF_NORMAL"sv);
  487. builder.append("\n"sv);
  488. builder.append("\tFour CC: "sv);
  489. builder.appendff("{:c}", (header.pixel_format.four_cc >> (8 * 0)) & 0xFF);
  490. builder.appendff("{:c}", (header.pixel_format.four_cc >> (8 * 1)) & 0xFF);
  491. builder.appendff("{:c}", (header.pixel_format.four_cc >> (8 * 2)) & 0xFF);
  492. builder.appendff("{:c}", (header.pixel_format.four_cc >> (8 * 3)) & 0xFF);
  493. builder.append("\n"sv);
  494. builder.appendff("\tRGB Bit Count: {}\n", header.pixel_format.rgb_bit_count);
  495. builder.appendff("\tR Bit Mask: {}\n", header.pixel_format.r_bit_mask);
  496. builder.appendff("\tG Bit Mask: {}\n", header.pixel_format.g_bit_mask);
  497. builder.appendff("\tB Bit Mask: {}\n", header.pixel_format.b_bit_mask);
  498. builder.appendff("\tA Bit Mask: {}\n", header.pixel_format.a_bit_mask);
  499. builder.append("DDS10:\n"sv);
  500. builder.appendff("\tFormat: {}\n", static_cast<u32>(header10.format));
  501. builder.append("\tResource Dimension:"sv);
  502. if ((header10.resource_dimension & ResourceDimensions::DDS_DIMENSION_UNKNOWN) == ResourceDimensions::DDS_DIMENSION_UNKNOWN)
  503. builder.append(" DDS_DIMENSION_UNKNOWN"sv);
  504. if ((header10.resource_dimension & ResourceDimensions::DDS_DIMENSION_BUFFER) == ResourceDimensions::DDS_DIMENSION_BUFFER)
  505. builder.append(" DDS_DIMENSION_BUFFER"sv);
  506. if ((header10.resource_dimension & ResourceDimensions::DDS_DIMENSION_TEXTURE1D) == ResourceDimensions::DDS_DIMENSION_TEXTURE1D)
  507. builder.append(" DDS_DIMENSION_TEXTURE1D"sv);
  508. if ((header10.resource_dimension & ResourceDimensions::DDS_DIMENSION_TEXTURE2D) == ResourceDimensions::DDS_DIMENSION_TEXTURE2D)
  509. builder.append(" DDS_DIMENSION_TEXTURE2D"sv);
  510. if ((header10.resource_dimension & ResourceDimensions::DDS_DIMENSION_TEXTURE3D) == ResourceDimensions::DDS_DIMENSION_TEXTURE3D)
  511. builder.append(" DDS_DIMENSION_TEXTURE3D"sv);
  512. builder.append("\n"sv);
  513. builder.appendff("\tArray Size: {}\n", header10.array_size);
  514. builder.append("\tMisc Flags:"sv);
  515. if ((header10.misc_flag & MiscFlags::DDS_RESOURCE_MISC_TEXTURECUBE) == MiscFlags::DDS_RESOURCE_MISC_TEXTURECUBE)
  516. builder.append(" DDS_RESOURCE_MISC_TEXTURECUBE"sv);
  517. builder.append("\n"sv);
  518. builder.append("\tMisc Flags 2:"sv);
  519. if ((header10.misc_flag2 & Misc2Flags::DDS_ALPHA_MODE_UNKNOWN) == Misc2Flags::DDS_ALPHA_MODE_UNKNOWN)
  520. builder.append(" DDS_ALPHA_MODE_UNKNOWN"sv);
  521. if ((header10.misc_flag2 & Misc2Flags::DDS_ALPHA_MODE_STRAIGHT) == Misc2Flags::DDS_ALPHA_MODE_STRAIGHT)
  522. builder.append(" DDS_ALPHA_MODE_STRAIGHT"sv);
  523. if ((header10.misc_flag2 & Misc2Flags::DDS_ALPHA_MODE_PREMULTIPLIED) == Misc2Flags::DDS_ALPHA_MODE_PREMULTIPLIED)
  524. builder.append(" DDS_ALPHA_MODE_PREMULTIPLIED"sv);
  525. if ((header10.misc_flag2 & Misc2Flags::DDS_ALPHA_MODE_OPAQUE) == Misc2Flags::DDS_ALPHA_MODE_OPAQUE)
  526. builder.append(" DDS_ALPHA_MODE_OPAQUE"sv);
  527. if ((header10.misc_flag2 & Misc2Flags::DDS_ALPHA_MODE_CUSTOM) == Misc2Flags::DDS_ALPHA_MODE_CUSTOM)
  528. builder.append(" DDS_ALPHA_MODE_CUSTOM"sv);
  529. builder.append("\n"sv);
  530. dbgln("{}", builder.to_deprecated_string());
  531. }
  532. DDSImageDecoderPlugin::DDSImageDecoderPlugin(u8 const* data, size_t size)
  533. {
  534. m_context = make<DDSLoadingContext>();
  535. m_context->data = data;
  536. m_context->data_size = size;
  537. }
  538. DDSImageDecoderPlugin::~DDSImageDecoderPlugin() = default;
  539. IntSize DDSImageDecoderPlugin::size()
  540. {
  541. if (m_context->state == DDSLoadingContext::State::Error)
  542. return {};
  543. if (m_context->state == DDSLoadingContext::State::BitmapDecoded)
  544. return { m_context->header.width, m_context->header.height };
  545. return {};
  546. }
  547. void DDSImageDecoderPlugin::set_volatile()
  548. {
  549. if (m_context->bitmap)
  550. m_context->bitmap->set_volatile();
  551. }
  552. bool DDSImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  553. {
  554. if (!m_context->bitmap)
  555. return false;
  556. return m_context->bitmap->set_nonvolatile(was_purged);
  557. }
  558. bool DDSImageDecoderPlugin::initialize()
  559. {
  560. // The header is always at least 128 bytes, so if the file is smaller, it can't be a DDS.
  561. return m_context->data_size > 128
  562. && m_context->data[0] == 0x44
  563. && m_context->data[1] == 0x44
  564. && m_context->data[2] == 0x53
  565. && m_context->data[3] == 0x20;
  566. }
  567. bool DDSImageDecoderPlugin::sniff(ReadonlyBytes data)
  568. {
  569. // The header is always at least 128 bytes, so if the file is smaller, it can't be a DDS.
  570. return data.size() > 128
  571. && data.data()[0] == 0x44
  572. && data.data()[1] == 0x44
  573. && data.data()[2] == 0x53
  574. && data.data()[3] == 0x20;
  575. }
  576. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> DDSImageDecoderPlugin::create(ReadonlyBytes data)
  577. {
  578. return adopt_nonnull_own_or_enomem(new (nothrow) DDSImageDecoderPlugin(data.data(), data.size()));
  579. }
  580. bool DDSImageDecoderPlugin::is_animated()
  581. {
  582. return false;
  583. }
  584. size_t DDSImageDecoderPlugin::loop_count()
  585. {
  586. return 0;
  587. }
  588. size_t DDSImageDecoderPlugin::frame_count()
  589. {
  590. return 1;
  591. }
  592. ErrorOr<ImageFrameDescriptor> DDSImageDecoderPlugin::frame(size_t index)
  593. {
  594. if (index > 0)
  595. return Error::from_string_literal("DDSImageDecoderPlugin: Invalid frame index");
  596. if (m_context->state == DDSLoadingContext::State::Error)
  597. return Error::from_string_literal("DDSImageDecoderPlugin: Decoding failed");
  598. if (m_context->state < DDSLoadingContext::State::BitmapDecoded) {
  599. TRY(decode_dds(*m_context));
  600. }
  601. VERIFY(m_context->bitmap);
  602. return ImageFrameDescriptor { m_context->bitmap, 0 };
  603. }
  604. ErrorOr<Optional<ReadonlyBytes>> DDSImageDecoderPlugin::icc_data()
  605. {
  606. return OptionalNone {};
  607. }
  608. }