PNGLoader.cpp 33 KB

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