PNGLoader.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/Endian.h>
  8. #include <LibCompress/Zlib.h>
  9. #include <LibGfx/PNGLoader.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <sys/mman.h>
  14. #include <sys/stat.h>
  15. #include <unistd.h>
  16. #ifdef __serenity__
  17. # include <LibCompress/Deflate.h>
  18. # include <serenity.h>
  19. #endif
  20. namespace Gfx {
  21. static const u8 png_header[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
  22. struct PNG_IHDR {
  23. NetworkOrdered<u32> width;
  24. NetworkOrdered<u32> height;
  25. u8 bit_depth { 0 };
  26. u8 color_type { 0 };
  27. u8 compression_method { 0 };
  28. u8 filter_method { 0 };
  29. u8 interlace_method { 0 };
  30. };
  31. static_assert(AssertSize<PNG_IHDR, 13>());
  32. struct Scanline {
  33. u8 filter { 0 };
  34. ReadonlyBytes data {};
  35. };
  36. struct [[gnu::packed]] PaletteEntry {
  37. u8 r;
  38. u8 g;
  39. u8 b;
  40. // u8 a;
  41. };
  42. template<typename T>
  43. struct [[gnu::packed]] Tuple {
  44. T gray;
  45. T a;
  46. };
  47. template<typename T>
  48. struct [[gnu::packed]] Triplet {
  49. T r;
  50. T g;
  51. T b;
  52. };
  53. template<typename T>
  54. struct [[gnu::packed]] Quad {
  55. T r;
  56. T g;
  57. T b;
  58. T a;
  59. };
  60. enum PngInterlaceMethod {
  61. Null = 0,
  62. Adam7 = 1
  63. };
  64. struct PNGLoadingContext {
  65. enum State {
  66. NotDecoded = 0,
  67. Error,
  68. HeaderDecoded,
  69. SizeDecoded,
  70. ChunksDecoded,
  71. BitmapDecoded,
  72. };
  73. State state { State::NotDecoded };
  74. const u8* data { nullptr };
  75. size_t data_size { 0 };
  76. int width { -1 };
  77. int height { -1 };
  78. u8 bit_depth { 0 };
  79. u8 color_type { 0 };
  80. u8 compression_method { 0 };
  81. u8 filter_method { 0 };
  82. u8 interlace_method { 0 };
  83. u8 channels { 0 };
  84. bool has_seen_zlib_header { false };
  85. bool has_alpha() const { return color_type & 4 || palette_transparency_data.size() > 0; }
  86. Vector<Scanline> scanlines;
  87. RefPtr<Gfx::Bitmap> bitmap;
  88. ByteBuffer* decompression_buffer { nullptr };
  89. Vector<u8> compressed_data;
  90. Vector<PaletteEntry> palette_data;
  91. Vector<u8> palette_transparency_data;
  92. Checked<int> compute_row_size_for_width(int width)
  93. {
  94. Checked<int> row_size = width;
  95. row_size *= channels;
  96. row_size *= bit_depth;
  97. row_size += 7;
  98. row_size /= 8;
  99. if (row_size.has_overflow()) {
  100. dbgln("PNG too large, integer overflow while computing row size");
  101. state = State::Error;
  102. }
  103. return row_size;
  104. }
  105. };
  106. class Streamer {
  107. public:
  108. Streamer(const u8* data, size_t size)
  109. : m_data_ptr(data)
  110. , m_size_remaining(size)
  111. {
  112. }
  113. template<typename T>
  114. bool read(T& value)
  115. {
  116. if (m_size_remaining < sizeof(T))
  117. return false;
  118. value = *((const NetworkOrdered<T>*)m_data_ptr);
  119. m_data_ptr += sizeof(T);
  120. m_size_remaining -= sizeof(T);
  121. return true;
  122. }
  123. bool read_bytes(u8* buffer, size_t count)
  124. {
  125. if (m_size_remaining < count)
  126. return false;
  127. memcpy(buffer, m_data_ptr, count);
  128. m_data_ptr += count;
  129. m_size_remaining -= count;
  130. return true;
  131. }
  132. bool wrap_bytes(ReadonlyBytes& buffer, size_t count)
  133. {
  134. if (m_size_remaining < count)
  135. return false;
  136. buffer = ReadonlyBytes { m_data_ptr, count };
  137. m_data_ptr += count;
  138. m_size_remaining -= count;
  139. return true;
  140. }
  141. bool at_end() const { return !m_size_remaining; }
  142. private:
  143. const u8* m_data_ptr { nullptr };
  144. size_t m_size_remaining { 0 };
  145. };
  146. static RefPtr<Gfx::Bitmap> load_png_impl(const u8*, size_t);
  147. static bool process_chunk(Streamer&, PNGLoadingContext& context);
  148. RefPtr<Gfx::Bitmap> load_png_from_memory(u8 const* data, size_t length, String const& mmap_name)
  149. {
  150. auto bitmap = load_png_impl(data, length);
  151. if (bitmap)
  152. bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PNG: {}", bitmap->size(), mmap_name));
  153. return bitmap;
  154. }
  155. ALWAYS_INLINE static u8 paeth_predictor(int a, int b, int c)
  156. {
  157. int p = a + b - c;
  158. int pa = abs(p - a);
  159. int pb = abs(p - b);
  160. int pc = abs(p - c);
  161. if (pa <= pb && pa <= pc)
  162. return a;
  163. if (pb <= pc)
  164. return b;
  165. return c;
  166. }
  167. union [[gnu::packed]] Pixel {
  168. RGBA32 rgba { 0 };
  169. u8 v[4];
  170. struct {
  171. u8 r;
  172. u8 g;
  173. u8 b;
  174. u8 a;
  175. };
  176. };
  177. static_assert(AssertSize<Pixel, 4>());
  178. template<bool has_alpha, u8 filter_type>
  179. ALWAYS_INLINE static void unfilter_impl(Gfx::Bitmap& bitmap, int y, const void* dummy_scanline_data)
  180. {
  181. auto* dummy_scanline = (const Pixel*)dummy_scanline_data;
  182. if constexpr (filter_type == 0) {
  183. auto* pixels = (Pixel*)bitmap.scanline(y);
  184. for (int i = 0; i < bitmap.width(); ++i) {
  185. auto& x = pixels[i];
  186. swap(x.r, x.b);
  187. }
  188. }
  189. if constexpr (filter_type == 1) {
  190. auto* pixels = (Pixel*)bitmap.scanline(y);
  191. swap(pixels[0].r, pixels[0].b);
  192. for (int i = 1; i < bitmap.width(); ++i) {
  193. auto& x = pixels[i];
  194. swap(x.r, x.b);
  195. auto& a = (const Pixel&)pixels[i - 1];
  196. x.v[0] += a.v[0];
  197. x.v[1] += a.v[1];
  198. x.v[2] += a.v[2];
  199. if constexpr (has_alpha)
  200. x.v[3] += a.v[3];
  201. }
  202. return;
  203. }
  204. if constexpr (filter_type == 2) {
  205. auto* pixels = (Pixel*)bitmap.scanline(y);
  206. auto* pixels_y_minus_1 = y == 0 ? dummy_scanline : (const Pixel*)bitmap.scanline(y - 1);
  207. for (int i = 0; i < bitmap.width(); ++i) {
  208. auto& x = pixels[i];
  209. swap(x.r, x.b);
  210. const Pixel& b = pixels_y_minus_1[i];
  211. x.v[0] += b.v[0];
  212. x.v[1] += b.v[1];
  213. x.v[2] += b.v[2];
  214. if constexpr (has_alpha)
  215. x.v[3] += b.v[3];
  216. }
  217. return;
  218. }
  219. if constexpr (filter_type == 3) {
  220. auto* pixels = (Pixel*)bitmap.scanline(y);
  221. auto* pixels_y_minus_1 = y == 0 ? dummy_scanline : (const Pixel*)bitmap.scanline(y - 1);
  222. for (int i = 0; i < bitmap.width(); ++i) {
  223. auto& x = pixels[i];
  224. swap(x.r, x.b);
  225. Pixel a;
  226. if (i != 0)
  227. a = pixels[i - 1];
  228. const Pixel& b = pixels_y_minus_1[i];
  229. x.v[0] = x.v[0] + ((a.v[0] + b.v[0]) / 2);
  230. x.v[1] = x.v[1] + ((a.v[1] + b.v[1]) / 2);
  231. x.v[2] = x.v[2] + ((a.v[2] + b.v[2]) / 2);
  232. if constexpr (has_alpha)
  233. x.v[3] = x.v[3] + ((a.v[3] + b.v[3]) / 2);
  234. }
  235. return;
  236. }
  237. if constexpr (filter_type == 4) {
  238. auto* pixels = (Pixel*)bitmap.scanline(y);
  239. auto* pixels_y_minus_1 = y == 0 ? dummy_scanline : (Pixel*)bitmap.scanline(y - 1);
  240. for (int i = 0; i < bitmap.width(); ++i) {
  241. auto& x = pixels[i];
  242. swap(x.r, x.b);
  243. Pixel a;
  244. const Pixel& b = pixels_y_minus_1[i];
  245. Pixel c;
  246. if (i != 0) {
  247. a = pixels[i - 1];
  248. c = pixels_y_minus_1[i - 1];
  249. }
  250. x.v[0] += paeth_predictor(a.v[0], b.v[0], c.v[0]);
  251. x.v[1] += paeth_predictor(a.v[1], b.v[1], c.v[1]);
  252. x.v[2] += paeth_predictor(a.v[2], b.v[2], c.v[2]);
  253. if constexpr (has_alpha)
  254. x.v[3] += paeth_predictor(a.v[3], b.v[3], c.v[3]);
  255. }
  256. }
  257. }
  258. template<typename T>
  259. ALWAYS_INLINE static void unpack_grayscale_without_alpha(PNGLoadingContext& context)
  260. {
  261. for (int y = 0; y < context.height; ++y) {
  262. auto* gray_values = reinterpret_cast<const T*>(context.scanlines[y].data.data());
  263. for (int i = 0; i < context.width; ++i) {
  264. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  265. pixel.r = gray_values[i];
  266. pixel.g = gray_values[i];
  267. pixel.b = gray_values[i];
  268. pixel.a = 0xff;
  269. }
  270. }
  271. }
  272. template<typename T>
  273. ALWAYS_INLINE static void unpack_grayscale_with_alpha(PNGLoadingContext& context)
  274. {
  275. for (int y = 0; y < context.height; ++y) {
  276. auto* tuples = reinterpret_cast<const Tuple<T>*>(context.scanlines[y].data.data());
  277. for (int i = 0; i < context.width; ++i) {
  278. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  279. pixel.r = tuples[i].gray;
  280. pixel.g = tuples[i].gray;
  281. pixel.b = tuples[i].gray;
  282. pixel.a = tuples[i].a;
  283. }
  284. }
  285. }
  286. template<typename T>
  287. ALWAYS_INLINE static void unpack_triplets_without_alpha(PNGLoadingContext& context)
  288. {
  289. for (int y = 0; y < context.height; ++y) {
  290. auto* triplets = reinterpret_cast<const Triplet<T>*>(context.scanlines[y].data.data());
  291. for (int i = 0; i < context.width; ++i) {
  292. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  293. pixel.r = triplets[i].r;
  294. pixel.g = triplets[i].g;
  295. pixel.b = triplets[i].b;
  296. pixel.a = 0xff;
  297. }
  298. }
  299. }
  300. NEVER_INLINE FLATTEN static bool unfilter(PNGLoadingContext& context)
  301. {
  302. // First unpack the scanlines to RGBA:
  303. switch (context.color_type) {
  304. case 0:
  305. if (context.bit_depth == 8) {
  306. unpack_grayscale_without_alpha<u8>(context);
  307. } else if (context.bit_depth == 16) {
  308. unpack_grayscale_without_alpha<u16>(context);
  309. } else if (context.bit_depth == 1 || context.bit_depth == 2 || context.bit_depth == 4) {
  310. auto bit_depth_squared = context.bit_depth * context.bit_depth;
  311. auto pixels_per_byte = 8 / context.bit_depth;
  312. auto mask = (1 << context.bit_depth) - 1;
  313. for (int y = 0; y < context.height; ++y) {
  314. auto* gray_values = context.scanlines[y].data.data();
  315. for (int x = 0; x < context.width; ++x) {
  316. auto bit_offset = (8 - context.bit_depth) - (context.bit_depth * (x % pixels_per_byte));
  317. auto value = (gray_values[x / pixels_per_byte] >> bit_offset) & mask;
  318. auto& pixel = (Pixel&)context.bitmap->scanline(y)[x];
  319. pixel.r = value * (0xff / bit_depth_squared);
  320. pixel.g = value * (0xff / bit_depth_squared);
  321. pixel.b = value * (0xff / bit_depth_squared);
  322. pixel.a = 0xff;
  323. }
  324. }
  325. } else {
  326. VERIFY_NOT_REACHED();
  327. }
  328. break;
  329. case 4:
  330. if (context.bit_depth == 8) {
  331. unpack_grayscale_with_alpha<u8>(context);
  332. } else if (context.bit_depth == 16) {
  333. unpack_grayscale_with_alpha<u16>(context);
  334. } else {
  335. VERIFY_NOT_REACHED();
  336. }
  337. break;
  338. case 2:
  339. if (context.bit_depth == 8) {
  340. unpack_triplets_without_alpha<u8>(context);
  341. } else if (context.bit_depth == 16) {
  342. unpack_triplets_without_alpha<u16>(context);
  343. } else {
  344. VERIFY_NOT_REACHED();
  345. }
  346. break;
  347. case 6:
  348. if (context.bit_depth == 8) {
  349. for (int y = 0; y < context.height; ++y) {
  350. memcpy(context.bitmap->scanline(y), context.scanlines[y].data.data(), context.scanlines[y].data.size());
  351. }
  352. } else if (context.bit_depth == 16) {
  353. for (int y = 0; y < context.height; ++y) {
  354. auto* triplets = reinterpret_cast<const Quad<u16>*>(context.scanlines[y].data.data());
  355. for (int i = 0; i < context.width; ++i) {
  356. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  357. pixel.r = triplets[i].r & 0xFF;
  358. pixel.g = triplets[i].g & 0xFF;
  359. pixel.b = triplets[i].b & 0xFF;
  360. pixel.a = triplets[i].a & 0xFF;
  361. }
  362. }
  363. } else {
  364. VERIFY_NOT_REACHED();
  365. }
  366. break;
  367. case 3:
  368. if (context.bit_depth == 8) {
  369. for (int y = 0; y < context.height; ++y) {
  370. auto* palette_index = context.scanlines[y].data.data();
  371. for (int i = 0; i < context.width; ++i) {
  372. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  373. if (palette_index[i] >= context.palette_data.size())
  374. return false;
  375. auto& color = context.palette_data.at((int)palette_index[i]);
  376. auto transparency = context.palette_transparency_data.size() >= palette_index[i] + 1u
  377. ? context.palette_transparency_data.data()[palette_index[i]]
  378. : 0xff;
  379. pixel.r = color.r;
  380. pixel.g = color.g;
  381. pixel.b = color.b;
  382. pixel.a = transparency;
  383. }
  384. }
  385. } else if (context.bit_depth == 1 || context.bit_depth == 2 || context.bit_depth == 4) {
  386. auto pixels_per_byte = 8 / context.bit_depth;
  387. auto mask = (1 << context.bit_depth) - 1;
  388. for (int y = 0; y < context.height; ++y) {
  389. auto* palette_indices = context.scanlines[y].data.data();
  390. for (int i = 0; i < context.width; ++i) {
  391. auto bit_offset = (8 - context.bit_depth) - (context.bit_depth * (i % pixels_per_byte));
  392. auto palette_index = (palette_indices[i / pixels_per_byte] >> bit_offset) & mask;
  393. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  394. if ((size_t)palette_index >= context.palette_data.size())
  395. return false;
  396. auto& color = context.palette_data.at(palette_index);
  397. auto transparency = context.palette_transparency_data.size() >= palette_index + 1u
  398. ? context.palette_transparency_data.data()[palette_index]
  399. : 0xff;
  400. pixel.r = color.r;
  401. pixel.g = color.g;
  402. pixel.b = color.b;
  403. pixel.a = transparency;
  404. }
  405. }
  406. } else {
  407. VERIFY_NOT_REACHED();
  408. }
  409. break;
  410. default:
  411. VERIFY_NOT_REACHED();
  412. break;
  413. }
  414. u8 dummy_scanline[context.width * sizeof(RGBA32)];
  415. memset(dummy_scanline, 0, sizeof(dummy_scanline));
  416. for (int y = 0; y < context.height; ++y) {
  417. auto filter = context.scanlines[y].filter;
  418. if (filter == 0) {
  419. if (context.has_alpha())
  420. unfilter_impl<true, 0>(*context.bitmap, y, dummy_scanline);
  421. else
  422. unfilter_impl<false, 0>(*context.bitmap, y, dummy_scanline);
  423. continue;
  424. }
  425. if (filter == 1) {
  426. if (context.has_alpha())
  427. unfilter_impl<true, 1>(*context.bitmap, y, dummy_scanline);
  428. else
  429. unfilter_impl<false, 1>(*context.bitmap, y, dummy_scanline);
  430. continue;
  431. }
  432. if (filter == 2) {
  433. if (context.has_alpha())
  434. unfilter_impl<true, 2>(*context.bitmap, y, dummy_scanline);
  435. else
  436. unfilter_impl<false, 2>(*context.bitmap, y, dummy_scanline);
  437. continue;
  438. }
  439. if (filter == 3) {
  440. if (context.has_alpha())
  441. unfilter_impl<true, 3>(*context.bitmap, y, dummy_scanline);
  442. else
  443. unfilter_impl<false, 3>(*context.bitmap, y, dummy_scanline);
  444. continue;
  445. }
  446. if (filter == 4) {
  447. if (context.has_alpha())
  448. unfilter_impl<true, 4>(*context.bitmap, y, dummy_scanline);
  449. else
  450. unfilter_impl<false, 4>(*context.bitmap, y, dummy_scanline);
  451. continue;
  452. }
  453. }
  454. return true;
  455. }
  456. static bool decode_png_header(PNGLoadingContext& context)
  457. {
  458. if (context.state >= PNGLoadingContext::HeaderDecoded)
  459. return true;
  460. if (!context.data || context.data_size < sizeof(png_header)) {
  461. dbgln_if(PNG_DEBUG, "Missing PNG header");
  462. context.state = PNGLoadingContext::State::Error;
  463. return false;
  464. }
  465. if (memcmp(context.data, png_header, sizeof(png_header)) != 0) {
  466. dbgln_if(PNG_DEBUG, "Invalid PNG header");
  467. context.state = PNGLoadingContext::State::Error;
  468. return false;
  469. }
  470. context.state = PNGLoadingContext::HeaderDecoded;
  471. return true;
  472. }
  473. static bool decode_png_size(PNGLoadingContext& context)
  474. {
  475. if (context.state >= PNGLoadingContext::SizeDecoded)
  476. return true;
  477. if (context.state < PNGLoadingContext::HeaderDecoded) {
  478. if (!decode_png_header(context))
  479. return false;
  480. }
  481. const u8* data_ptr = context.data + sizeof(png_header);
  482. size_t data_remaining = context.data_size - sizeof(png_header);
  483. Streamer streamer(data_ptr, data_remaining);
  484. while (!streamer.at_end()) {
  485. if (!process_chunk(streamer, context)) {
  486. context.state = PNGLoadingContext::State::Error;
  487. return false;
  488. }
  489. if (context.width && context.height) {
  490. context.state = PNGLoadingContext::State::SizeDecoded;
  491. return true;
  492. }
  493. }
  494. return false;
  495. }
  496. static bool decode_png_chunks(PNGLoadingContext& context)
  497. {
  498. if (context.state >= PNGLoadingContext::State::ChunksDecoded)
  499. return true;
  500. if (context.state < PNGLoadingContext::HeaderDecoded) {
  501. if (!decode_png_header(context))
  502. return false;
  503. }
  504. const u8* data_ptr = context.data + sizeof(png_header);
  505. int data_remaining = context.data_size - sizeof(png_header);
  506. context.compressed_data.ensure_capacity(context.data_size);
  507. Streamer streamer(data_ptr, data_remaining);
  508. while (!streamer.at_end()) {
  509. if (!process_chunk(streamer, context)) {
  510. // Ignore failed chunk and just consider chunk decoding being done.
  511. // decode_png_bitmap() will check whether we got all required ones anyway.
  512. break;
  513. }
  514. }
  515. context.state = PNGLoadingContext::State::ChunksDecoded;
  516. return true;
  517. }
  518. static bool decode_png_bitmap_simple(PNGLoadingContext& context)
  519. {
  520. Streamer streamer(context.decompression_buffer->data(), context.decompression_buffer->size());
  521. for (int y = 0; y < context.height; ++y) {
  522. u8 filter;
  523. if (!streamer.read(filter)) {
  524. context.state = PNGLoadingContext::State::Error;
  525. return false;
  526. }
  527. if (filter > 4) {
  528. dbgln_if(PNG_DEBUG, "Invalid PNG filter: {}", filter);
  529. context.state = PNGLoadingContext::State::Error;
  530. return false;
  531. }
  532. context.scanlines.append({ filter });
  533. auto& scanline_buffer = context.scanlines.last().data;
  534. auto row_size = context.compute_row_size_for_width(context.width);
  535. if (row_size.has_overflow())
  536. return false;
  537. if (!streamer.wrap_bytes(scanline_buffer, row_size.value())) {
  538. context.state = PNGLoadingContext::State::Error;
  539. return false;
  540. }
  541. }
  542. auto bitmap_or_error = Bitmap::try_create(context.has_alpha() ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888, { context.width, context.height });
  543. if (bitmap_or_error.is_error()) {
  544. context.state = PNGLoadingContext::State::Error;
  545. return false;
  546. }
  547. context.bitmap = bitmap_or_error.release_value();
  548. return unfilter(context);
  549. }
  550. static int adam7_height(PNGLoadingContext& context, int pass)
  551. {
  552. switch (pass) {
  553. case 1:
  554. return (context.height + 7) / 8;
  555. case 2:
  556. return (context.height + 7) / 8;
  557. case 3:
  558. return (context.height + 3) / 8;
  559. case 4:
  560. return (context.height + 3) / 4;
  561. case 5:
  562. return (context.height + 1) / 4;
  563. case 6:
  564. return (context.height + 1) / 2;
  565. case 7:
  566. return context.height / 2;
  567. default:
  568. VERIFY_NOT_REACHED();
  569. }
  570. }
  571. static int adam7_width(PNGLoadingContext& context, int pass)
  572. {
  573. switch (pass) {
  574. case 1:
  575. return (context.width + 7) / 8;
  576. case 2:
  577. return (context.width + 3) / 8;
  578. case 3:
  579. return (context.width + 3) / 4;
  580. case 4:
  581. return (context.width + 1) / 4;
  582. case 5:
  583. return (context.width + 1) / 2;
  584. case 6:
  585. return context.width / 2;
  586. case 7:
  587. return context.width;
  588. default:
  589. VERIFY_NOT_REACHED();
  590. }
  591. }
  592. // Index 0 unused (non-interlaced case)
  593. static int adam7_starty[8] = { 0, 0, 0, 4, 0, 2, 0, 1 };
  594. static int adam7_startx[8] = { 0, 0, 4, 0, 2, 0, 1, 0 };
  595. static int adam7_stepy[8] = { 1, 8, 8, 8, 4, 4, 2, 2 };
  596. static int adam7_stepx[8] = { 1, 8, 8, 4, 4, 2, 2, 1 };
  597. static bool decode_adam7_pass(PNGLoadingContext& context, Streamer& streamer, int pass)
  598. {
  599. PNGLoadingContext subimage_context;
  600. subimage_context.width = adam7_width(context, pass);
  601. subimage_context.height = adam7_height(context, pass);
  602. subimage_context.channels = context.channels;
  603. subimage_context.color_type = context.color_type;
  604. subimage_context.palette_data = context.palette_data;
  605. subimage_context.palette_transparency_data = context.palette_transparency_data;
  606. subimage_context.bit_depth = context.bit_depth;
  607. subimage_context.filter_method = context.filter_method;
  608. // For small images, some passes might be empty
  609. if (!subimage_context.width || !subimage_context.height)
  610. return true;
  611. subimage_context.scanlines.clear_with_capacity();
  612. for (int y = 0; y < subimage_context.height; ++y) {
  613. u8 filter;
  614. if (!streamer.read(filter)) {
  615. context.state = PNGLoadingContext::State::Error;
  616. return false;
  617. }
  618. if (filter > 4) {
  619. dbgln_if(PNG_DEBUG, "Invalid PNG filter: {}", filter);
  620. context.state = PNGLoadingContext::State::Error;
  621. return false;
  622. }
  623. subimage_context.scanlines.append({ filter });
  624. auto& scanline_buffer = subimage_context.scanlines.last().data;
  625. auto row_size = context.compute_row_size_for_width(subimage_context.width);
  626. if (row_size.has_overflow())
  627. return false;
  628. if (!streamer.wrap_bytes(scanline_buffer, row_size.value())) {
  629. context.state = PNGLoadingContext::State::Error;
  630. return false;
  631. }
  632. }
  633. auto bitmap_or_error = Bitmap::try_create(context.bitmap->format(), { subimage_context.width, subimage_context.height });
  634. if (bitmap_or_error.is_error())
  635. return false;
  636. subimage_context.bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  637. if (!unfilter(subimage_context)) {
  638. subimage_context.bitmap = nullptr;
  639. return false;
  640. }
  641. // Copy the subimage data into the main image according to the pass pattern
  642. for (int y = 0, dy = adam7_starty[pass]; y < subimage_context.height && dy < context.height; ++y, dy += adam7_stepy[pass]) {
  643. for (int x = 0, dx = adam7_startx[pass]; x < subimage_context.width && dy < context.width; ++x, dx += adam7_stepx[pass]) {
  644. context.bitmap->set_pixel(dx, dy, subimage_context.bitmap->get_pixel(x, y));
  645. }
  646. }
  647. return true;
  648. }
  649. static bool decode_png_adam7(PNGLoadingContext& context)
  650. {
  651. Streamer streamer(context.decompression_buffer->data(), context.decompression_buffer->size());
  652. auto bitmap_or_error = Bitmap::try_create(context.has_alpha() ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888, { context.width, context.height });
  653. if (bitmap_or_error.is_error())
  654. return false;
  655. context.bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  656. for (int pass = 1; pass <= 7; ++pass) {
  657. if (!decode_adam7_pass(context, streamer, pass))
  658. return false;
  659. }
  660. return true;
  661. }
  662. static bool decode_png_bitmap(PNGLoadingContext& context)
  663. {
  664. if (context.state < PNGLoadingContext::State::ChunksDecoded) {
  665. if (!decode_png_chunks(context))
  666. return false;
  667. }
  668. if (context.state >= PNGLoadingContext::State::BitmapDecoded)
  669. return true;
  670. if (context.width == -1 || context.height == -1)
  671. return false; // Didn't see an IHDR chunk.
  672. if (context.color_type == 3 && context.palette_data.is_empty())
  673. return false; // Didn't see a PLTE chunk for a palettized image, or it was empty.
  674. auto result = Compress::Zlib::decompress_all(context.compressed_data.span());
  675. if (!result.has_value()) {
  676. context.state = PNGLoadingContext::State::Error;
  677. return false;
  678. }
  679. context.decompression_buffer = &result.value();
  680. context.compressed_data.clear();
  681. context.scanlines.ensure_capacity(context.height);
  682. switch (context.interlace_method) {
  683. case PngInterlaceMethod::Null:
  684. if (!decode_png_bitmap_simple(context))
  685. return false;
  686. break;
  687. case PngInterlaceMethod::Adam7:
  688. if (!decode_png_adam7(context))
  689. return false;
  690. break;
  691. default:
  692. context.state = PNGLoadingContext::State::Error;
  693. return false;
  694. }
  695. context.decompression_buffer = nullptr;
  696. context.state = PNGLoadingContext::State::BitmapDecoded;
  697. return true;
  698. }
  699. static RefPtr<Gfx::Bitmap> load_png_impl(const u8* data, size_t data_size)
  700. {
  701. PNGLoadingContext context;
  702. context.data = data;
  703. context.data_size = data_size;
  704. if (!decode_png_chunks(context))
  705. return nullptr;
  706. if (!decode_png_bitmap(context))
  707. return nullptr;
  708. return context.bitmap;
  709. }
  710. static bool is_valid_compression_method(u8 compression_method)
  711. {
  712. return compression_method == 0;
  713. }
  714. static bool is_valid_filter_method(u8 filter_method)
  715. {
  716. return filter_method == 0;
  717. }
  718. static bool process_IHDR(ReadonlyBytes data, PNGLoadingContext& context)
  719. {
  720. if (data.size() < (int)sizeof(PNG_IHDR))
  721. return false;
  722. auto& ihdr = *(const PNG_IHDR*)data.data();
  723. if (ihdr.width > maximum_width_for_decoded_images || ihdr.height > maximum_height_for_decoded_images) {
  724. dbgln("This PNG is too large for comfort: {}x{}", (u32)ihdr.width, (u32)ihdr.height);
  725. return false;
  726. }
  727. if (!is_valid_compression_method(ihdr.compression_method)) {
  728. dbgln("PNG has invalid compression method {}", ihdr.compression_method);
  729. return false;
  730. }
  731. if (!is_valid_filter_method(ihdr.filter_method)) {
  732. dbgln("PNG has invalid filter method {}", ihdr.filter_method);
  733. return false;
  734. }
  735. context.width = ihdr.width;
  736. context.height = ihdr.height;
  737. context.bit_depth = ihdr.bit_depth;
  738. context.color_type = ihdr.color_type;
  739. context.compression_method = ihdr.compression_method;
  740. context.filter_method = ihdr.filter_method;
  741. context.interlace_method = ihdr.interlace_method;
  742. dbgln_if(PNG_DEBUG, "PNG: {}x{} ({} bpp)", context.width, context.height, context.bit_depth);
  743. dbgln_if(PNG_DEBUG, " Color type: {}", context.color_type);
  744. dbgln_if(PNG_DEBUG, "Compress Method: {}", context.compression_method);
  745. dbgln_if(PNG_DEBUG, " Filter Method: {}", context.filter_method);
  746. dbgln_if(PNG_DEBUG, " Interlace type: {}", context.interlace_method);
  747. if (context.interlace_method != PngInterlaceMethod::Null && context.interlace_method != PngInterlaceMethod::Adam7) {
  748. dbgln_if(PNG_DEBUG, "PNGLoader::process_IHDR: unknown interlace method: {}", context.interlace_method);
  749. return false;
  750. }
  751. switch (context.color_type) {
  752. case 0: // Each pixel is a grayscale sample.
  753. if (context.bit_depth != 1 && context.bit_depth != 2 && context.bit_depth != 4 && context.bit_depth != 8 && context.bit_depth != 16)
  754. return false;
  755. context.channels = 1;
  756. break;
  757. case 4: // Each pixel is a grayscale sample, followed by an alpha sample.
  758. if (context.bit_depth != 8 && context.bit_depth != 16)
  759. return false;
  760. context.channels = 2;
  761. break;
  762. case 2: // Each pixel is an RGB sample
  763. if (context.bit_depth != 8 && context.bit_depth != 16)
  764. return false;
  765. context.channels = 3;
  766. break;
  767. case 3: // Each pixel is a palette index; a PLTE chunk must appear.
  768. if (context.bit_depth != 1 && context.bit_depth != 2 && context.bit_depth != 4 && context.bit_depth != 8)
  769. return false;
  770. context.channels = 1;
  771. break;
  772. case 6: // Each pixel is an RGB sample, followed by an alpha sample.
  773. if (context.bit_depth != 8 && context.bit_depth != 16)
  774. return false;
  775. context.channels = 4;
  776. break;
  777. default:
  778. return false;
  779. }
  780. return true;
  781. }
  782. static bool process_IDAT(ReadonlyBytes data, PNGLoadingContext& context)
  783. {
  784. context.compressed_data.append(data.data(), data.size());
  785. return true;
  786. }
  787. static bool process_PLTE(ReadonlyBytes data, PNGLoadingContext& context)
  788. {
  789. context.palette_data.append((const PaletteEntry*)data.data(), data.size() / 3);
  790. return true;
  791. }
  792. static bool process_tRNS(ReadonlyBytes data, PNGLoadingContext& context)
  793. {
  794. switch (context.color_type) {
  795. case 3:
  796. context.palette_transparency_data.append(data.data(), data.size());
  797. break;
  798. }
  799. return true;
  800. }
  801. static bool process_chunk(Streamer& streamer, PNGLoadingContext& context)
  802. {
  803. u32 chunk_size;
  804. if (!streamer.read(chunk_size)) {
  805. dbgln_if(PNG_DEBUG, "Bail at chunk_size");
  806. return false;
  807. }
  808. u8 chunk_type[5];
  809. chunk_type[4] = '\0';
  810. if (!streamer.read_bytes(chunk_type, 4)) {
  811. dbgln_if(PNG_DEBUG, "Bail at chunk_type");
  812. return false;
  813. }
  814. ReadonlyBytes chunk_data;
  815. if (!streamer.wrap_bytes(chunk_data, chunk_size)) {
  816. dbgln_if(PNG_DEBUG, "Bail at chunk_data");
  817. return false;
  818. }
  819. u32 chunk_crc;
  820. if (!streamer.read(chunk_crc)) {
  821. dbgln_if(PNG_DEBUG, "Bail at chunk_crc");
  822. return false;
  823. }
  824. dbgln_if(PNG_DEBUG, "Chunk type: '{}', size: {}, crc: {:x}", chunk_type, chunk_size, chunk_crc);
  825. if (!strcmp((const char*)chunk_type, "IHDR"))
  826. return process_IHDR(chunk_data, context);
  827. if (!strcmp((const char*)chunk_type, "IDAT"))
  828. return process_IDAT(chunk_data, context);
  829. if (!strcmp((const char*)chunk_type, "PLTE"))
  830. return process_PLTE(chunk_data, context);
  831. if (!strcmp((const char*)chunk_type, "tRNS"))
  832. return process_tRNS(chunk_data, context);
  833. return true;
  834. }
  835. PNGImageDecoderPlugin::PNGImageDecoderPlugin(const u8* data, size_t size)
  836. {
  837. m_context = make<PNGLoadingContext>();
  838. m_context->data = data;
  839. m_context->data_size = size;
  840. }
  841. PNGImageDecoderPlugin::~PNGImageDecoderPlugin()
  842. {
  843. }
  844. IntSize PNGImageDecoderPlugin::size()
  845. {
  846. if (m_context->state == PNGLoadingContext::State::Error)
  847. return {};
  848. if (m_context->state < PNGLoadingContext::State::SizeDecoded) {
  849. bool success = decode_png_size(*m_context);
  850. if (!success)
  851. return {};
  852. }
  853. return { m_context->width, m_context->height };
  854. }
  855. RefPtr<Gfx::Bitmap> PNGImageDecoderPlugin::bitmap()
  856. {
  857. if (m_context->state == PNGLoadingContext::State::Error)
  858. return nullptr;
  859. if (m_context->state < PNGLoadingContext::State::BitmapDecoded) {
  860. // NOTE: This forces the chunk decoding to happen.
  861. bool success = decode_png_bitmap(*m_context);
  862. if (!success)
  863. return nullptr;
  864. }
  865. VERIFY(m_context->bitmap);
  866. return m_context->bitmap;
  867. }
  868. void PNGImageDecoderPlugin::set_volatile()
  869. {
  870. if (m_context->bitmap)
  871. m_context->bitmap->set_volatile();
  872. }
  873. bool PNGImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  874. {
  875. if (!m_context->bitmap)
  876. return false;
  877. return m_context->bitmap->set_nonvolatile(was_purged);
  878. }
  879. bool PNGImageDecoderPlugin::sniff()
  880. {
  881. return decode_png_header(*m_context);
  882. }
  883. bool PNGImageDecoderPlugin::is_animated()
  884. {
  885. return false;
  886. }
  887. size_t PNGImageDecoderPlugin::loop_count()
  888. {
  889. return 0;
  890. }
  891. size_t PNGImageDecoderPlugin::frame_count()
  892. {
  893. return 1;
  894. }
  895. ImageFrameDescriptor PNGImageDecoderPlugin::frame(size_t i)
  896. {
  897. if (i > 0)
  898. return {};
  899. return { bitmap(), 0 };
  900. }
  901. }