BMPLoader.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/Function.h>
  8. #include <AK/String.h>
  9. #include <AK/Vector.h>
  10. #include <LibGfx/BMPLoader.h>
  11. namespace Gfx {
  12. const u8 bmp_header_size = 14;
  13. const u32 color_palette_limit = 1024;
  14. // Compression flags
  15. struct Compression {
  16. enum : u32 {
  17. RGB = 0,
  18. RLE8,
  19. RLE4,
  20. BITFIELDS,
  21. RLE24, // doubles as JPEG for V4+, but that is unsupported
  22. PNG,
  23. ALPHABITFIELDS,
  24. CMYK = 11,
  25. CMYKRLE8,
  26. CMYKRLE4,
  27. };
  28. };
  29. struct DIBCore {
  30. // u16 for BITMAPHEADERCORE, but i32 for everything else. If the dib type is
  31. // BITMAPHEADERCORE, this is range checked.
  32. i32 width;
  33. i32 height;
  34. u16 bpp;
  35. };
  36. struct DIBInfo {
  37. u32 compression { Compression::RGB };
  38. u32 image_size { 0 };
  39. i32 horizontal_resolution { 0 };
  40. i32 vertical_resolution { 0 };
  41. u32 number_of_palette_colors { 0 };
  42. u32 number_of_important_palette_colors { number_of_palette_colors };
  43. // Introduced in the BITMAPV2INFOHEADER and would ideally be stored in the
  44. // DIBV2 struct, however with a compression value of BI_BITFIELDS or
  45. // BI_ALPHABITFIELDS, these can be specified with the Info header.
  46. Vector<u32> masks;
  47. Vector<i8> mask_shifts;
  48. Vector<u8> mask_sizes;
  49. };
  50. struct DIBOSV2 {
  51. u16 recording;
  52. u16 halftoning;
  53. u16 size1;
  54. u16 size2;
  55. };
  56. template<typename T>
  57. struct Endpoint {
  58. T x;
  59. T y;
  60. T z;
  61. };
  62. }
  63. namespace AK {
  64. template<typename T>
  65. struct Formatter<Gfx::Endpoint<T>> : Formatter<StringView> {
  66. void format(FormatBuilder& builder, const Gfx::Endpoint<T>& value)
  67. {
  68. Formatter<StringView>::format(builder, String::formatted("({}, {}, {})", value.x, value.y, value.z));
  69. }
  70. };
  71. }
  72. namespace Gfx {
  73. struct DIBV4 {
  74. u32 color_space { 0 };
  75. Endpoint<i32> red_endpoint { 0, 0, 0 };
  76. Endpoint<i32> green_endpoint { 0, 0, 0 };
  77. Endpoint<i32> blue_endpoint { 0, 0, 0 };
  78. Endpoint<u32> gamma_endpoint { 0, 0, 0 };
  79. };
  80. struct DIBV5 {
  81. u32 intent { 0 };
  82. u32 profile_data { 0 };
  83. u32 profile_size { 0 };
  84. };
  85. struct DIB {
  86. DIBCore core;
  87. DIBInfo info;
  88. DIBOSV2 osv2;
  89. DIBV4 v4;
  90. DIBV5 v5;
  91. };
  92. enum class DIBType {
  93. Core = 0,
  94. OSV2Short,
  95. OSV2,
  96. Info,
  97. V2,
  98. V3,
  99. V4,
  100. V5
  101. };
  102. struct BMPLoadingContext {
  103. enum class State {
  104. NotDecoded = 0,
  105. HeaderDecoded,
  106. DIBDecoded,
  107. ColorTableDecoded,
  108. PixelDataDecoded,
  109. Error,
  110. };
  111. State state { State::NotDecoded };
  112. const u8* file_bytes { nullptr };
  113. size_t file_size { 0 };
  114. u32 data_offset { 0 };
  115. DIB dib;
  116. DIBType dib_type;
  117. Vector<u32> color_table;
  118. RefPtr<Gfx::Bitmap> bitmap;
  119. u32 dib_size() const
  120. {
  121. switch (dib_type) {
  122. case DIBType::Core:
  123. return 12;
  124. case DIBType::OSV2Short:
  125. return 16;
  126. case DIBType::OSV2:
  127. return 64;
  128. case DIBType::Info:
  129. return 40;
  130. case DIBType::V2:
  131. return 52;
  132. case DIBType::V3:
  133. return 56;
  134. case DIBType::V4:
  135. return 108;
  136. case DIBType::V5:
  137. return 124;
  138. }
  139. VERIFY_NOT_REACHED();
  140. }
  141. };
  142. class InputStreamer {
  143. public:
  144. InputStreamer(const u8* data, size_t size)
  145. : m_data_ptr(data)
  146. , m_size_remaining(size)
  147. {
  148. }
  149. u8 read_u8()
  150. {
  151. VERIFY(m_size_remaining >= 1);
  152. m_size_remaining--;
  153. return *(m_data_ptr++);
  154. }
  155. u16 read_u16()
  156. {
  157. return read_u8() | (read_u8() << 8);
  158. }
  159. u32 read_u24()
  160. {
  161. return read_u8() | (read_u8() << 8) | (read_u8() << 16);
  162. }
  163. i32 read_i32()
  164. {
  165. return static_cast<i32>(read_u16() | (read_u16() << 16));
  166. }
  167. u32 read_u32()
  168. {
  169. return read_u16() | (read_u16() << 16);
  170. }
  171. void drop_bytes(u8 num_bytes)
  172. {
  173. VERIFY(m_size_remaining >= num_bytes);
  174. m_size_remaining -= num_bytes;
  175. m_data_ptr += num_bytes;
  176. }
  177. bool at_end() const { return !m_size_remaining; }
  178. bool has_u8() const { return m_size_remaining >= 1; }
  179. bool has_u16() const { return m_size_remaining >= 2; }
  180. bool has_u24() const { return m_size_remaining >= 3; }
  181. bool has_u32() const { return m_size_remaining >= 4; }
  182. size_t remaining() const { return m_size_remaining; }
  183. private:
  184. const u8* m_data_ptr { nullptr };
  185. size_t m_size_remaining { 0 };
  186. };
  187. // Lookup table for distributing all possible 2-bit numbers evenly into 8-bit numbers
  188. static u8 scaling_factors_2bit[4] = {
  189. 0x00,
  190. 0x55,
  191. 0xaa,
  192. 0xff,
  193. };
  194. // Lookup table for distributing all possible 3-bit numbers evenly into 8-bit numbers
  195. static u8 scaling_factors_3bit[8] = {
  196. 0x00,
  197. 0x24,
  198. 0x48,
  199. 0x6d,
  200. 0x91,
  201. 0xb6,
  202. 0xdb,
  203. 0xff,
  204. };
  205. static u8 scale_masked_8bit_number(u8 number, u8 bits_set)
  206. {
  207. // If there are more than 4 bit set, an easy way to scale the number is to
  208. // just copy the most significant bits into the least significant bits
  209. if (bits_set >= 4)
  210. return number | (number >> bits_set);
  211. if (!bits_set)
  212. return 0;
  213. if (bits_set == 1)
  214. return number ? 0xff : 0;
  215. if (bits_set == 2)
  216. return scaling_factors_2bit[number >> 6];
  217. return scaling_factors_3bit[number >> 5];
  218. }
  219. static u8 get_scaled_color(u32 data, u8 mask_size, i8 mask_shift)
  220. {
  221. // A negative mask_shift indicates we actually need to left shift
  222. // the result in order to get out a valid 8-bit color (for example, the blue
  223. // value in an RGB555 encoding is XXXBBBBB, which needs to be shifted to the
  224. // left by 3, hence it would have a "mask_shift" value of -3).
  225. if (mask_shift < 0)
  226. return scale_masked_8bit_number(data << -mask_shift, mask_size);
  227. return scale_masked_8bit_number(data >> mask_shift, mask_size);
  228. }
  229. // Scales an 8-bit number with "mask_size" bits set (and "8 - mask_size" bits
  230. // ignored). This function scales the number appropriately over the entire
  231. // 256 value color spectrum.
  232. // Note that a much simpler scaling can be done by simple bit shifting. If you
  233. // just ignore the bottom 8-mask_size bits, then you get *close*. However,
  234. // consider, as an example, a 5 bit number (so the bottom 3 bits are ignored).
  235. // The purest white you could get is 0xf8, which is 248 in RGB-land. We need
  236. // to scale the values in order to reach the proper value of 255.
  237. static u32 int_to_scaled_rgb(BMPLoadingContext& context, u32 data)
  238. {
  239. dbgln_if(BMP_DEBUG, "DIB info sizes before access: #masks={}, #mask_sizes={}, #mask_shifts={}",
  240. context.dib.info.masks.size(),
  241. context.dib.info.mask_sizes.size(),
  242. context.dib.info.mask_shifts.size());
  243. u8 r = get_scaled_color(data & context.dib.info.masks[0], context.dib.info.mask_sizes[0], context.dib.info.mask_shifts[0]);
  244. u8 g = get_scaled_color(data & context.dib.info.masks[1], context.dib.info.mask_sizes[1], context.dib.info.mask_shifts[1]);
  245. u8 b = get_scaled_color(data & context.dib.info.masks[2], context.dib.info.mask_sizes[2], context.dib.info.mask_shifts[2]);
  246. u32 color = (r << 16) | (g << 8) | b;
  247. if (context.dib.info.masks.size() == 4) {
  248. // The bitmap has an alpha mask
  249. u8 a = get_scaled_color(data & context.dib.info.masks[3], context.dib.info.mask_sizes[3], context.dib.info.mask_shifts[3]);
  250. color |= (a << 24);
  251. } else {
  252. color |= 0xff000000;
  253. }
  254. return color;
  255. }
  256. static void populate_dib_mask_info_if_needed(BMPLoadingContext& context)
  257. {
  258. if (context.dib.info.masks.is_empty())
  259. return;
  260. // Mask shift is the number of right shifts needed to align the MSb of the
  261. // mask to the MSb of the LSB. Note that this can be a negative number.
  262. // Mask size is the number of set bits in the mask. This is required for
  263. // color scaling (for example, ensuring that a 4-bit color value spans the
  264. // entire 256 value color spectrum.
  265. auto& masks = context.dib.info.masks;
  266. auto& mask_shifts = context.dib.info.mask_shifts;
  267. auto& mask_sizes = context.dib.info.mask_sizes;
  268. if (!mask_shifts.is_empty() && !mask_sizes.is_empty())
  269. return;
  270. VERIFY(mask_shifts.is_empty() && mask_sizes.is_empty());
  271. mask_shifts.ensure_capacity(masks.size());
  272. mask_sizes.ensure_capacity(masks.size());
  273. for (size_t i = 0; i < masks.size(); ++i) {
  274. u32 mask = masks[i];
  275. if (!mask) {
  276. mask_shifts.append(0);
  277. mask_sizes.append(0);
  278. continue;
  279. }
  280. int trailing_zeros = count_trailing_zeroes_32(mask);
  281. // If mask is exactly `0xFFFFFFFF`, then we might try to count the trailing zeros of 0x00000000 here, so we need the safe version:
  282. int size = count_trailing_zeroes_32_safe(~(mask >> trailing_zeros));
  283. if (size > 8) {
  284. // Drop lowest bits if mask is longer than 8 bits.
  285. trailing_zeros += size - 8;
  286. size = 8;
  287. }
  288. mask_shifts.append(size + trailing_zeros - 8);
  289. mask_sizes.append(size);
  290. }
  291. }
  292. static bool check_for_invalid_bitmask_combinations(BMPLoadingContext& context)
  293. {
  294. auto& bpp = context.dib.core.bpp;
  295. auto& compression = context.dib.info.compression;
  296. if (compression == Compression::ALPHABITFIELDS && context.dib_type != DIBType::Info)
  297. return false;
  298. switch (context.dib_type) {
  299. case DIBType::Core:
  300. if (bpp == 2 || bpp == 16 || bpp == 32)
  301. return false;
  302. break;
  303. case DIBType::Info:
  304. switch (compression) {
  305. case Compression::BITFIELDS:
  306. case Compression::ALPHABITFIELDS:
  307. if (bpp != 16 && bpp != 32)
  308. return false;
  309. break;
  310. case Compression::RGB:
  311. break;
  312. case Compression::RLE8:
  313. if (bpp > 8)
  314. return false;
  315. break;
  316. case Compression::RLE4:
  317. // TODO: This is a guess
  318. if (bpp > 4)
  319. return false;
  320. break;
  321. default:
  322. // Other compressions are not officially supported.
  323. // Technically, we could even drop ALPHABITFIELDS.
  324. return false;
  325. }
  326. break;
  327. case DIBType::OSV2Short:
  328. case DIBType::OSV2:
  329. case DIBType::V2:
  330. case DIBType::V3:
  331. case DIBType::V4:
  332. case DIBType::V5:
  333. if (compression == Compression::BITFIELDS && bpp != 16 && bpp != 32)
  334. return false;
  335. break;
  336. }
  337. return true;
  338. }
  339. static bool set_dib_bitmasks(BMPLoadingContext& context, InputStreamer& streamer)
  340. {
  341. if (!check_for_invalid_bitmask_combinations(context))
  342. return false;
  343. auto& bpp = context.dib.core.bpp;
  344. if (bpp <= 8 || bpp == 24)
  345. return true;
  346. auto& compression = context.dib.info.compression;
  347. auto& type = context.dib_type;
  348. if (type > DIBType::OSV2 && bpp == 16 && compression == Compression::RGB) {
  349. context.dib.info.masks.extend({ 0x7c00, 0x03e0, 0x001f });
  350. context.dib.info.mask_shifts.extend({ 7, 2, -3 });
  351. context.dib.info.mask_sizes.extend({ 5, 5, 5 });
  352. } else if (type == DIBType::Info && (compression == Compression::BITFIELDS || compression == Compression::ALPHABITFIELDS)) {
  353. // Consume the extra BITFIELDS bytes
  354. auto number_of_mask_fields = compression == Compression::ALPHABITFIELDS ? 4 : 3;
  355. for (auto i = 0; i < number_of_mask_fields; i++) {
  356. if (!streamer.has_u32())
  357. return false;
  358. context.dib.info.masks.append(streamer.read_u32());
  359. }
  360. }
  361. populate_dib_mask_info_if_needed(context);
  362. return true;
  363. }
  364. static bool decode_bmp_header(BMPLoadingContext& context)
  365. {
  366. if (context.state == BMPLoadingContext::State::Error)
  367. return false;
  368. if (context.state >= BMPLoadingContext::State::HeaderDecoded)
  369. return true;
  370. if (!context.file_bytes || context.file_size < bmp_header_size) {
  371. dbgln_if(BMP_DEBUG, "Missing BMP header");
  372. context.state = BMPLoadingContext::State::Error;
  373. return false;
  374. }
  375. InputStreamer streamer(context.file_bytes, bmp_header_size);
  376. u16 header = streamer.read_u16();
  377. if (header != 0x4d42) {
  378. dbgln_if(BMP_DEBUG, "BMP has invalid magic header number: {:#04x}", header);
  379. context.state = BMPLoadingContext::State::Error;
  380. return false;
  381. }
  382. // The reported size of the file in the header is actually not important
  383. // for decoding the file. Some specifications say that this value should
  384. // be the size of the header instead, so we just rely on the known file
  385. // size, instead of a possibly-correct-but-also-possibly-incorrect reported
  386. // value of the file size.
  387. streamer.drop_bytes(4);
  388. // Ignore reserved bytes
  389. streamer.drop_bytes(4);
  390. context.data_offset = streamer.read_u32();
  391. if constexpr (BMP_DEBUG) {
  392. dbgln("BMP file size: {}", context.file_size);
  393. dbgln("BMP data offset: {}", context.data_offset);
  394. }
  395. if (context.data_offset >= context.file_size) {
  396. dbgln_if(BMP_DEBUG, "BMP data offset is beyond file end?!");
  397. return false;
  398. }
  399. context.state = BMPLoadingContext::State::HeaderDecoded;
  400. return true;
  401. }
  402. static bool decode_bmp_core_dib(BMPLoadingContext& context, InputStreamer& streamer)
  403. {
  404. auto& core = context.dib.core;
  405. // The width and height are u16 fields in the actual BITMAPCOREHEADER format.
  406. if (context.dib_type == DIBType::Core) {
  407. core.width = streamer.read_u16();
  408. core.height = streamer.read_u16();
  409. } else {
  410. core.width = streamer.read_i32();
  411. core.height = streamer.read_i32();
  412. }
  413. if (core.width < 0) {
  414. dbgln("BMP has a negative width: {}", core.width);
  415. return false;
  416. }
  417. if (static_cast<size_t>(core.width) > maximum_width_for_decoded_images || static_cast<size_t>(abs(core.height)) > maximum_height_for_decoded_images) {
  418. dbgln("This BMP is too large for comfort: {}x{}", core.width, abs(core.height));
  419. return false;
  420. }
  421. auto color_planes = streamer.read_u16();
  422. if (color_planes != 1) {
  423. dbgln("BMP has an invalid number of color planes: {}", color_planes);
  424. return false;
  425. }
  426. core.bpp = streamer.read_u16();
  427. switch (core.bpp) {
  428. case 1:
  429. case 2:
  430. case 4:
  431. case 8:
  432. case 16:
  433. case 24:
  434. case 32:
  435. break;
  436. default:
  437. dbgln("BMP has an invalid bpp: {}", core.bpp);
  438. context.state = BMPLoadingContext::State::Error;
  439. return false;
  440. }
  441. if constexpr (BMP_DEBUG) {
  442. dbgln("BMP width: {}", core.width);
  443. dbgln("BMP height: {}", core.height);
  444. dbgln("BMP bits_per_pixel: {}", core.bpp);
  445. }
  446. return true;
  447. }
  448. ALWAYS_INLINE static bool is_supported_compression_format(BMPLoadingContext& context, u32 compression)
  449. {
  450. return compression == Compression::RGB || compression == Compression::BITFIELDS
  451. || compression == Compression::ALPHABITFIELDS || compression == Compression::RLE8
  452. || compression == Compression::RLE4 || (compression == Compression::RLE24 && context.dib_type <= DIBType::OSV2);
  453. }
  454. static bool decode_bmp_osv2_dib(BMPLoadingContext& context, InputStreamer& streamer, bool short_variant = false)
  455. {
  456. auto& core = context.dib.core;
  457. core.width = streamer.read_u32();
  458. core.height = streamer.read_u32();
  459. if (core.width < 0) {
  460. dbgln("BMP has a negative width: {}", core.width);
  461. return false;
  462. }
  463. auto color_planes = streamer.read_u16();
  464. if (color_planes != 1) {
  465. dbgln("BMP has an invalid number of color planes: {}", color_planes);
  466. return false;
  467. }
  468. core.bpp = streamer.read_u16();
  469. switch (core.bpp) {
  470. case 1:
  471. case 2:
  472. case 4:
  473. case 8:
  474. case 24:
  475. break;
  476. default:
  477. // OS/2 didn't expect 16- or 32-bpp to be popular.
  478. dbgln("BMP has an invalid bpp: {}", core.bpp);
  479. context.state = BMPLoadingContext::State::Error;
  480. return false;
  481. }
  482. if constexpr (BMP_DEBUG) {
  483. dbgln("BMP width: {}", core.width);
  484. dbgln("BMP height: {}", core.height);
  485. dbgln("BMP bits_per_pixel: {}", core.bpp);
  486. }
  487. if (short_variant)
  488. return true;
  489. auto& info = context.dib.info;
  490. auto& osv2 = context.dib.osv2;
  491. info.compression = streamer.read_u32();
  492. info.image_size = streamer.read_u32();
  493. info.horizontal_resolution = streamer.read_u32();
  494. info.vertical_resolution = streamer.read_u32();
  495. info.number_of_palette_colors = streamer.read_u32();
  496. info.number_of_important_palette_colors = streamer.read_u32();
  497. if (!is_supported_compression_format(context, info.compression)) {
  498. dbgln("BMP has unsupported compression value: {}", info.compression);
  499. return false;
  500. }
  501. if (info.number_of_palette_colors > color_palette_limit || info.number_of_important_palette_colors > color_palette_limit) {
  502. dbgln("BMP header indicates too many palette colors: {}", info.number_of_palette_colors);
  503. return false;
  504. }
  505. // Units (2) + reserved (2)
  506. streamer.drop_bytes(4);
  507. osv2.recording = streamer.read_u16();
  508. osv2.halftoning = streamer.read_u16();
  509. osv2.size1 = streamer.read_u32();
  510. osv2.size2 = streamer.read_u32();
  511. // ColorEncoding (4) + Identifier (4)
  512. streamer.drop_bytes(8);
  513. if constexpr (BMP_DEBUG) {
  514. dbgln("BMP compression: {}", info.compression);
  515. dbgln("BMP image size: {}", info.image_size);
  516. dbgln("BMP horizontal res: {}", info.horizontal_resolution);
  517. dbgln("BMP vertical res: {}", info.vertical_resolution);
  518. dbgln("BMP colors: {}", info.number_of_palette_colors);
  519. dbgln("BMP important colors: {}", info.number_of_important_palette_colors);
  520. }
  521. return true;
  522. }
  523. static bool decode_bmp_info_dib(BMPLoadingContext& context, InputStreamer& streamer)
  524. {
  525. if (!decode_bmp_core_dib(context, streamer))
  526. return false;
  527. auto& info = context.dib.info;
  528. auto compression = streamer.read_u32();
  529. info.compression = compression;
  530. if (!is_supported_compression_format(context, compression)) {
  531. dbgln("BMP has unsupported compression value: {}", compression);
  532. return false;
  533. }
  534. info.image_size = streamer.read_u32();
  535. info.horizontal_resolution = streamer.read_i32();
  536. info.vertical_resolution = streamer.read_i32();
  537. info.number_of_palette_colors = streamer.read_u32();
  538. info.number_of_important_palette_colors = streamer.read_u32();
  539. if (info.number_of_palette_colors > color_palette_limit || info.number_of_important_palette_colors > color_palette_limit) {
  540. dbgln("BMP header indicates too many palette colors: {}", info.number_of_palette_colors);
  541. return false;
  542. }
  543. if (info.number_of_important_palette_colors == 0)
  544. info.number_of_important_palette_colors = info.number_of_palette_colors;
  545. if constexpr (BMP_DEBUG) {
  546. dbgln("BMP compression: {}", info.compression);
  547. dbgln("BMP image size: {}", info.image_size);
  548. dbgln("BMP horizontal res: {}", info.horizontal_resolution);
  549. dbgln("BMP vertical res: {}", info.vertical_resolution);
  550. dbgln("BMP colors: {}", info.number_of_palette_colors);
  551. dbgln("BMP important colors: {}", info.number_of_important_palette_colors);
  552. }
  553. return true;
  554. }
  555. static bool decode_bmp_v2_dib(BMPLoadingContext& context, InputStreamer& streamer)
  556. {
  557. if (!decode_bmp_info_dib(context, streamer))
  558. return false;
  559. context.dib.info.masks.append(streamer.read_u32());
  560. context.dib.info.masks.append(streamer.read_u32());
  561. context.dib.info.masks.append(streamer.read_u32());
  562. if constexpr (BMP_DEBUG) {
  563. dbgln("BMP red mask: {:#08x}", context.dib.info.masks[0]);
  564. dbgln("BMP green mask: {:#08x}", context.dib.info.masks[1]);
  565. dbgln("BMP blue mask: {:#08x}", context.dib.info.masks[2]);
  566. }
  567. return true;
  568. }
  569. static bool decode_bmp_v3_dib(BMPLoadingContext& context, InputStreamer& streamer)
  570. {
  571. if (!decode_bmp_v2_dib(context, streamer))
  572. return false;
  573. // There is zero documentation about when alpha masks actually get applied.
  574. // Well, there's some, but it's not even close to comprehensive. So, this is
  575. // in no way based off of any spec, it's simply based off of the BMP test
  576. // suite results.
  577. if (context.dib.info.compression == Compression::ALPHABITFIELDS) {
  578. context.dib.info.masks.append(streamer.read_u32());
  579. dbgln_if(BMP_DEBUG, "BMP alpha mask: {:#08x}", context.dib.info.masks[3]);
  580. } else if (context.dib_size() >= 56 && context.dib.core.bpp >= 16) {
  581. auto mask = streamer.read_u32();
  582. if ((context.dib.core.bpp == 32 && mask != 0) || context.dib.core.bpp == 16) {
  583. context.dib.info.masks.append(mask);
  584. dbgln_if(BMP_DEBUG, "BMP alpha mask: {:#08x}", mask);
  585. }
  586. } else {
  587. streamer.drop_bytes(4);
  588. }
  589. return true;
  590. }
  591. static bool decode_bmp_v4_dib(BMPLoadingContext& context, InputStreamer& streamer)
  592. {
  593. if (!decode_bmp_v3_dib(context, streamer))
  594. return false;
  595. auto& v4 = context.dib.v4;
  596. v4.color_space = streamer.read_u32();
  597. v4.red_endpoint = { streamer.read_i32(), streamer.read_i32(), streamer.read_i32() };
  598. v4.green_endpoint = { streamer.read_i32(), streamer.read_i32(), streamer.read_i32() };
  599. v4.blue_endpoint = { streamer.read_i32(), streamer.read_i32(), streamer.read_i32() };
  600. v4.gamma_endpoint = { streamer.read_u32(), streamer.read_u32(), streamer.read_u32() };
  601. if constexpr (BMP_DEBUG) {
  602. dbgln("BMP color space: {}", v4.color_space);
  603. dbgln("BMP red endpoint: {}", v4.red_endpoint);
  604. dbgln("BMP green endpoint: {}", v4.green_endpoint);
  605. dbgln("BMP blue endpoint: {}", v4.blue_endpoint);
  606. dbgln("BMP gamma endpoint: {}", v4.gamma_endpoint);
  607. }
  608. return true;
  609. }
  610. static bool decode_bmp_v5_dib(BMPLoadingContext& context, InputStreamer& streamer)
  611. {
  612. if (!decode_bmp_v4_dib(context, streamer))
  613. return false;
  614. auto& v5 = context.dib.v5;
  615. v5.intent = streamer.read_u32();
  616. v5.profile_data = streamer.read_u32();
  617. v5.profile_size = streamer.read_u32();
  618. if constexpr (BMP_DEBUG) {
  619. dbgln("BMP intent: {}", v5.intent);
  620. dbgln("BMP profile data: {}", v5.profile_data);
  621. dbgln("BMP profile size: {}", v5.profile_size);
  622. }
  623. return true;
  624. }
  625. static bool decode_bmp_dib(BMPLoadingContext& context)
  626. {
  627. if (context.state == BMPLoadingContext::State::Error)
  628. return false;
  629. if (context.state >= BMPLoadingContext::State::DIBDecoded)
  630. return true;
  631. if (context.state < BMPLoadingContext::State::HeaderDecoded && !decode_bmp_header(context))
  632. return false;
  633. if (context.file_size < bmp_header_size + 4)
  634. return false;
  635. InputStreamer streamer(context.file_bytes + bmp_header_size, 4);
  636. u32 dib_size = streamer.read_u32();
  637. if (context.file_size < bmp_header_size + dib_size)
  638. return false;
  639. if (context.data_offset < bmp_header_size + dib_size) {
  640. dbgln("Shenanigans! BMP pixel data and header usually don't overlap.");
  641. return false;
  642. }
  643. streamer = InputStreamer(context.file_bytes + bmp_header_size + 4, context.data_offset - bmp_header_size - 4);
  644. dbgln_if(BMP_DEBUG, "BMP dib size: {}", dib_size);
  645. bool error = false;
  646. if (dib_size == 12) {
  647. context.dib_type = DIBType::Core;
  648. if (!decode_bmp_core_dib(context, streamer))
  649. error = true;
  650. } else if (dib_size == 64) {
  651. context.dib_type = DIBType::OSV2;
  652. if (!decode_bmp_osv2_dib(context, streamer))
  653. error = true;
  654. } else if (dib_size == 16) {
  655. context.dib_type = DIBType::OSV2Short;
  656. if (!decode_bmp_osv2_dib(context, streamer, true))
  657. error = true;
  658. } else if (dib_size == 40) {
  659. context.dib_type = DIBType::Info;
  660. if (!decode_bmp_info_dib(context, streamer))
  661. error = true;
  662. } else if (dib_size == 52) {
  663. context.dib_type = DIBType::V2;
  664. if (!decode_bmp_v2_dib(context, streamer))
  665. error = true;
  666. } else if (dib_size == 56) {
  667. context.dib_type = DIBType::V3;
  668. if (!decode_bmp_v3_dib(context, streamer))
  669. error = true;
  670. } else if (dib_size == 108) {
  671. context.dib_type = DIBType::V4;
  672. if (!decode_bmp_v4_dib(context, streamer))
  673. error = true;
  674. } else if (dib_size == 124) {
  675. context.dib_type = DIBType::V5;
  676. if (!decode_bmp_v5_dib(context, streamer))
  677. error = true;
  678. } else {
  679. dbgln("Unsupported BMP DIB size: {}", dib_size);
  680. error = true;
  681. }
  682. switch (context.dib.info.compression) {
  683. case Compression::RGB:
  684. case Compression::RLE8:
  685. case Compression::RLE4:
  686. case Compression::BITFIELDS:
  687. case Compression::RLE24:
  688. case Compression::PNG:
  689. case Compression::ALPHABITFIELDS:
  690. case Compression::CMYK:
  691. case Compression::CMYKRLE8:
  692. case Compression::CMYKRLE4:
  693. break;
  694. default:
  695. error = true;
  696. }
  697. if (!error && !set_dib_bitmasks(context, streamer))
  698. error = true;
  699. if (error) {
  700. dbgln("BMP has an invalid DIB");
  701. context.state = BMPLoadingContext::State::Error;
  702. return false;
  703. }
  704. context.state = BMPLoadingContext::State::DIBDecoded;
  705. return true;
  706. }
  707. static bool decode_bmp_color_table(BMPLoadingContext& context)
  708. {
  709. if (context.state == BMPLoadingContext::State::Error)
  710. return false;
  711. if (context.state < BMPLoadingContext::State::DIBDecoded && !decode_bmp_dib(context))
  712. return false;
  713. if (context.state >= BMPLoadingContext::State::ColorTableDecoded)
  714. return true;
  715. if (context.dib.core.bpp > 8) {
  716. context.state = BMPLoadingContext::State::ColorTableDecoded;
  717. return true;
  718. }
  719. auto bytes_per_color = context.dib_type == DIBType::Core ? 3 : 4;
  720. u32 max_colors = 1 << context.dib.core.bpp;
  721. VERIFY(context.data_offset >= bmp_header_size + context.dib_size());
  722. auto size_of_color_table = context.data_offset - bmp_header_size - context.dib_size();
  723. if (context.dib_type <= DIBType::OSV2) {
  724. // Partial color tables are not supported, so the space of the color
  725. // table must be at least enough for the maximum amount of colors
  726. if (size_of_color_table < 3 * max_colors) {
  727. // This is against the spec, but most viewers process it anyways
  728. dbgln("BMP with CORE header does not have enough colors. Has: {}, expected: {}", size_of_color_table, (3 * max_colors));
  729. }
  730. }
  731. InputStreamer streamer(context.file_bytes + bmp_header_size + context.dib_size(), size_of_color_table);
  732. for (u32 i = 0; !streamer.at_end() && i < max_colors; ++i) {
  733. if (bytes_per_color == 4) {
  734. if (!streamer.has_u32())
  735. return false;
  736. context.color_table.append(streamer.read_u32());
  737. } else {
  738. if (!streamer.has_u24())
  739. return false;
  740. context.color_table.append(streamer.read_u24());
  741. }
  742. }
  743. context.state = BMPLoadingContext::State::ColorTableDecoded;
  744. return true;
  745. }
  746. struct RLEState {
  747. enum : u8 {
  748. PixelCount = 0,
  749. PixelValue,
  750. Meta, // Represents just consuming a null byte, which indicates something special
  751. };
  752. };
  753. static bool uncompress_bmp_rle_data(BMPLoadingContext& context, ByteBuffer& buffer)
  754. {
  755. // RLE-compressed images cannot be stored top-down
  756. if (context.dib.core.height < 0) {
  757. dbgln_if(BMP_DEBUG, "BMP is top-down and RLE compressed");
  758. context.state = BMPLoadingContext::State::Error;
  759. return false;
  760. }
  761. InputStreamer streamer(context.file_bytes + context.data_offset, context.file_size - context.data_offset);
  762. auto compression = context.dib.info.compression;
  763. u32 total_rows = static_cast<u32>(context.dib.core.height);
  764. u32 total_columns = round_up_to_power_of_two(static_cast<u32>(context.dib.core.width), 4);
  765. u32 column = 0;
  766. u32 row = 0;
  767. auto currently_consuming = RLEState::PixelCount;
  768. i16 pixel_count = 0;
  769. // ByteBuffer asserts that allocating the memory never fails.
  770. // FIXME: ByteBuffer should return either RefPtr<> or Optional<>.
  771. // Decoding the RLE data on-the-fly might actually be faster, and avoids this topic entirely.
  772. u32 buffer_size;
  773. if (compression == Compression::RLE24) {
  774. buffer_size = total_rows * round_up_to_power_of_two(total_columns, 4) * 4;
  775. } else {
  776. buffer_size = total_rows * round_up_to_power_of_two(total_columns, 4);
  777. }
  778. if (buffer_size > 300 * MiB) {
  779. dbgln("Suspiciously large amount of RLE data");
  780. return false;
  781. }
  782. auto buffer_result = ByteBuffer::create_zeroed(buffer_size);
  783. if (!buffer_result.has_value()) {
  784. dbgln("Not enough memory for buffer allocation");
  785. return false;
  786. }
  787. buffer = buffer_result.release_value();
  788. // Avoid as many if statements as possible by pulling out
  789. // compression-dependent actions into separate lambdas
  790. Function<u32()> get_buffer_index;
  791. Function<bool(u32, bool)> set_byte;
  792. Function<Optional<u32>()> read_byte;
  793. if (compression == Compression::RLE8) {
  794. get_buffer_index = [&]() -> u32 { return row * total_columns + column; };
  795. } else if (compression == Compression::RLE4) {
  796. get_buffer_index = [&]() -> u32 { return (row * total_columns + column) / 2; };
  797. } else {
  798. get_buffer_index = [&]() -> u32 { return (row * total_columns + column) * 3; };
  799. }
  800. if (compression == Compression::RLE8) {
  801. set_byte = [&](u32 color, bool) -> bool {
  802. if (column >= total_columns) {
  803. column = 0;
  804. row++;
  805. }
  806. auto index = get_buffer_index();
  807. if (index >= buffer.size()) {
  808. dbgln("BMP has badly-formatted RLE data");
  809. return false;
  810. }
  811. buffer[index] = color;
  812. column++;
  813. return true;
  814. };
  815. } else if (compression == Compression::RLE24) {
  816. set_byte = [&](u32 color, bool) -> bool {
  817. if (column >= total_columns) {
  818. column = 0;
  819. row++;
  820. }
  821. auto index = get_buffer_index();
  822. if (index + 3 >= buffer.size()) {
  823. dbgln("BMP has badly-formatted RLE data");
  824. return false;
  825. }
  826. ((u32&)buffer[index]) = color;
  827. column++;
  828. return true;
  829. };
  830. } else {
  831. set_byte = [&](u32 byte, bool rle4_set_second_nibble) -> bool {
  832. if (column >= total_columns) {
  833. column = 0;
  834. row++;
  835. }
  836. u32 index = get_buffer_index();
  837. if (index >= buffer.size() || (rle4_set_second_nibble && index + 1 >= buffer.size())) {
  838. dbgln("BMP has badly-formatted RLE data");
  839. return false;
  840. }
  841. if (column % 2) {
  842. buffer[index] |= byte >> 4;
  843. if (rle4_set_second_nibble) {
  844. buffer[index + 1] |= byte << 4;
  845. column++;
  846. }
  847. } else {
  848. if (rle4_set_second_nibble) {
  849. buffer[index] = byte;
  850. column++;
  851. } else {
  852. buffer[index] |= byte & 0xf0;
  853. }
  854. }
  855. column++;
  856. return true;
  857. };
  858. }
  859. if (compression == Compression::RLE24) {
  860. read_byte = [&]() -> Optional<u32> {
  861. if (!streamer.has_u24()) {
  862. dbgln("BMP has badly-formatted RLE data");
  863. return {};
  864. }
  865. return streamer.read_u24();
  866. };
  867. } else {
  868. read_byte = [&]() -> Optional<u32> {
  869. if (!streamer.has_u8()) {
  870. dbgln("BMP has badly-formatted RLE data");
  871. return {};
  872. }
  873. return streamer.read_u8();
  874. };
  875. }
  876. while (true) {
  877. u32 byte;
  878. switch (currently_consuming) {
  879. case RLEState::PixelCount:
  880. if (!streamer.has_u8())
  881. return false;
  882. byte = streamer.read_u8();
  883. if (!byte) {
  884. currently_consuming = RLEState::Meta;
  885. } else {
  886. pixel_count = byte;
  887. currently_consuming = RLEState::PixelValue;
  888. }
  889. break;
  890. case RLEState::PixelValue: {
  891. auto result = read_byte();
  892. if (!result.has_value())
  893. return false;
  894. byte = result.value();
  895. for (u16 i = 0; i < pixel_count; ++i) {
  896. if (compression != Compression::RLE4) {
  897. if (!set_byte(byte, true))
  898. return false;
  899. } else {
  900. if (!set_byte(byte, i != pixel_count - 1))
  901. return false;
  902. i++;
  903. }
  904. }
  905. currently_consuming = RLEState::PixelCount;
  906. break;
  907. }
  908. case RLEState::Meta:
  909. if (!streamer.has_u8())
  910. return false;
  911. byte = streamer.read_u8();
  912. if (!byte) {
  913. column = 0;
  914. row++;
  915. currently_consuming = RLEState::PixelCount;
  916. continue;
  917. }
  918. if (byte == 1)
  919. return true;
  920. if (byte == 2) {
  921. if (!streamer.has_u8())
  922. return false;
  923. u8 offset_x = streamer.read_u8();
  924. if (!streamer.has_u8())
  925. return false;
  926. u8 offset_y = streamer.read_u8();
  927. column += offset_x;
  928. if (column >= total_columns) {
  929. column -= total_columns;
  930. row++;
  931. }
  932. row += offset_y;
  933. currently_consuming = RLEState::PixelCount;
  934. continue;
  935. }
  936. // Consume literal bytes
  937. pixel_count = byte;
  938. i16 i = byte;
  939. while (i >= 1) {
  940. auto result = read_byte();
  941. if (!result.has_value())
  942. return false;
  943. byte = result.value();
  944. if (!set_byte(byte, i != 1))
  945. return false;
  946. i--;
  947. if (compression == Compression::RLE4)
  948. i--;
  949. }
  950. // Optionally consume a padding byte
  951. if (compression != Compression::RLE4) {
  952. if (pixel_count % 2) {
  953. if (!streamer.has_u8())
  954. return false;
  955. byte = streamer.read_u8();
  956. }
  957. } else {
  958. if (((pixel_count + 1) / 2) % 2) {
  959. if (!streamer.has_u8())
  960. return false;
  961. byte = streamer.read_u8();
  962. }
  963. }
  964. currently_consuming = RLEState::PixelCount;
  965. break;
  966. }
  967. }
  968. VERIFY_NOT_REACHED();
  969. }
  970. static bool decode_bmp_pixel_data(BMPLoadingContext& context)
  971. {
  972. if (context.state == BMPLoadingContext::State::Error)
  973. return false;
  974. if (context.state <= BMPLoadingContext::State::ColorTableDecoded && !decode_bmp_color_table(context))
  975. return false;
  976. const u16 bits_per_pixel = context.dib.core.bpp;
  977. BitmapFormat format = [&]() -> BitmapFormat {
  978. switch (bits_per_pixel) {
  979. case 1:
  980. return BitmapFormat::Indexed1;
  981. case 2:
  982. return BitmapFormat::Indexed2;
  983. case 4:
  984. return BitmapFormat::Indexed4;
  985. case 8:
  986. return BitmapFormat::Indexed8;
  987. case 16:
  988. if (context.dib.info.masks.size() == 4)
  989. return BitmapFormat::BGRA8888;
  990. return BitmapFormat::BGRx8888;
  991. case 24:
  992. return BitmapFormat::BGRx8888;
  993. case 32:
  994. return BitmapFormat::BGRA8888;
  995. default:
  996. return BitmapFormat::Invalid;
  997. }
  998. }();
  999. if (format == BitmapFormat::Invalid) {
  1000. dbgln("BMP has invalid bpp of {}", bits_per_pixel);
  1001. context.state = BMPLoadingContext::State::Error;
  1002. return false;
  1003. }
  1004. const u32 width = abs(context.dib.core.width);
  1005. const u32 height = abs(context.dib.core.height);
  1006. auto bitmap_or_error = Bitmap::try_create(format, { static_cast<int>(width), static_cast<int>(height) });
  1007. if (bitmap_or_error.is_error()) {
  1008. // FIXME: Propagate the *real* error.
  1009. return false;
  1010. }
  1011. context.bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  1012. ByteBuffer rle_buffer;
  1013. ReadonlyBytes bytes { context.file_bytes + context.data_offset, context.file_size - context.data_offset };
  1014. if (context.dib.info.compression == Compression::RLE4 || context.dib.info.compression == Compression::RLE8
  1015. || context.dib.info.compression == Compression::RLE24) {
  1016. if (!uncompress_bmp_rle_data(context, rle_buffer))
  1017. return false;
  1018. bytes = rle_buffer.bytes();
  1019. }
  1020. InputStreamer streamer(bytes.data(), bytes.size());
  1021. auto process_row = [&](u32 row) -> bool {
  1022. u32 space_remaining_before_consuming_row = streamer.remaining();
  1023. for (u32 column = 0; column < width;) {
  1024. switch (bits_per_pixel) {
  1025. case 1: {
  1026. if (!streamer.has_u8())
  1027. return false;
  1028. u8 byte = streamer.read_u8();
  1029. u8 mask = 8;
  1030. while (column < width && mask > 0) {
  1031. mask -= 1;
  1032. context.bitmap->scanline_u8(row)[column++] = (byte >> mask) & 0x1;
  1033. }
  1034. break;
  1035. }
  1036. case 2: {
  1037. if (!streamer.has_u8())
  1038. return false;
  1039. u8 byte = streamer.read_u8();
  1040. u8 mask = 8;
  1041. while (column < width && mask > 0) {
  1042. mask -= 2;
  1043. context.bitmap->scanline_u8(row)[column++] = (byte >> mask) & 0x3;
  1044. }
  1045. break;
  1046. }
  1047. case 4: {
  1048. if (!streamer.has_u8())
  1049. return false;
  1050. u8 byte = streamer.read_u8();
  1051. context.bitmap->scanline_u8(row)[column++] = (byte >> 4) & 0xf;
  1052. if (column < width)
  1053. context.bitmap->scanline_u8(row)[column++] = byte & 0xf;
  1054. break;
  1055. }
  1056. case 8:
  1057. if (!streamer.has_u8())
  1058. return false;
  1059. context.bitmap->scanline_u8(row)[column++] = streamer.read_u8();
  1060. break;
  1061. case 16: {
  1062. if (!streamer.has_u16())
  1063. return false;
  1064. context.bitmap->scanline(row)[column++] = int_to_scaled_rgb(context, streamer.read_u16());
  1065. break;
  1066. }
  1067. case 24: {
  1068. if (!streamer.has_u24())
  1069. return false;
  1070. context.bitmap->scanline(row)[column++] = streamer.read_u24();
  1071. break;
  1072. }
  1073. case 32:
  1074. if (!streamer.has_u32())
  1075. return false;
  1076. if (context.dib.info.masks.is_empty()) {
  1077. context.bitmap->scanline(row)[column++] = streamer.read_u32() | 0xff000000;
  1078. } else {
  1079. context.bitmap->scanline(row)[column++] = int_to_scaled_rgb(context, streamer.read_u32());
  1080. }
  1081. break;
  1082. }
  1083. }
  1084. auto consumed = space_remaining_before_consuming_row - streamer.remaining();
  1085. // Calculate padding
  1086. u8 bytes_to_drop = [consumed]() -> u8 {
  1087. switch (consumed % 4) {
  1088. case 0:
  1089. return 0;
  1090. case 1:
  1091. return 3;
  1092. case 2:
  1093. return 2;
  1094. case 3:
  1095. return 1;
  1096. }
  1097. VERIFY_NOT_REACHED();
  1098. }();
  1099. if (streamer.remaining() < bytes_to_drop)
  1100. return false;
  1101. streamer.drop_bytes(bytes_to_drop);
  1102. return true;
  1103. };
  1104. if (context.dib.core.height < 0) {
  1105. // BMP is stored top-down
  1106. for (u32 row = 0; row < height; ++row) {
  1107. if (!process_row(row))
  1108. return false;
  1109. }
  1110. } else {
  1111. for (i32 row = height - 1; row >= 0; --row) {
  1112. if (!process_row(row))
  1113. return false;
  1114. }
  1115. }
  1116. for (size_t i = 0; i < context.color_table.size(); ++i)
  1117. context.bitmap->set_palette_color(i, Color::from_rgb(context.color_table[i]));
  1118. context.state = BMPLoadingContext::State::PixelDataDecoded;
  1119. return true;
  1120. }
  1121. BMPImageDecoderPlugin::BMPImageDecoderPlugin(const u8* data, size_t data_size)
  1122. {
  1123. m_context = make<BMPLoadingContext>();
  1124. m_context->file_bytes = data;
  1125. m_context->file_size = data_size;
  1126. }
  1127. BMPImageDecoderPlugin::~BMPImageDecoderPlugin()
  1128. {
  1129. }
  1130. IntSize BMPImageDecoderPlugin::size()
  1131. {
  1132. if (m_context->state == BMPLoadingContext::State::Error)
  1133. return {};
  1134. if (m_context->state < BMPLoadingContext::State::DIBDecoded && !decode_bmp_dib(*m_context))
  1135. return {};
  1136. return { m_context->dib.core.width, abs(m_context->dib.core.height) };
  1137. }
  1138. RefPtr<Gfx::Bitmap> BMPImageDecoderPlugin::bitmap()
  1139. {
  1140. if (m_context->state == BMPLoadingContext::State::Error)
  1141. return nullptr;
  1142. if (m_context->state < BMPLoadingContext::State::PixelDataDecoded && !decode_bmp_pixel_data(*m_context))
  1143. return nullptr;
  1144. VERIFY(m_context->bitmap);
  1145. return m_context->bitmap;
  1146. }
  1147. void BMPImageDecoderPlugin::set_volatile()
  1148. {
  1149. if (m_context->bitmap)
  1150. m_context->bitmap->set_volatile();
  1151. }
  1152. bool BMPImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  1153. {
  1154. if (!m_context->bitmap)
  1155. return false;
  1156. return m_context->bitmap->set_nonvolatile(was_purged);
  1157. }
  1158. bool BMPImageDecoderPlugin::sniff()
  1159. {
  1160. return decode_bmp_header(*m_context);
  1161. }
  1162. bool BMPImageDecoderPlugin::is_animated()
  1163. {
  1164. return false;
  1165. }
  1166. size_t BMPImageDecoderPlugin::loop_count()
  1167. {
  1168. return 0;
  1169. }
  1170. size_t BMPImageDecoderPlugin::frame_count()
  1171. {
  1172. return 1;
  1173. }
  1174. ImageFrameDescriptor BMPImageDecoderPlugin::frame(size_t i)
  1175. {
  1176. if (i > 0)
  1177. return {};
  1178. return { bitmap(), 0 };
  1179. }
  1180. }