PNGLoader.cpp 31 KB

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