BMPLoader.cpp 41 KB

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