PNGLoader.cpp 32 KB

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