PNGLoader.cpp 33 KB

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