PNGLoader.cpp 33 KB

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