DDSLoader.cpp 27 KB

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