DDSLoader.cpp 26 KB

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