PNGLoader.cpp 33 KB

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