PNGLoader.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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/Endian.h>
  27. #include <AK/LexicalPath.h>
  28. #include <AK/MappedFile.h>
  29. #include <LibCore/puff.h>
  30. #include <LibGfx/PNGLoader.h>
  31. #include <fcntl.h>
  32. #include <math.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <sys/mman.h>
  36. #include <sys/stat.h>
  37. #include <unistd.h>
  38. #ifdef __serenity__
  39. # include <serenity.h>
  40. #endif
  41. //#define PNG_DEBUG
  42. namespace Gfx {
  43. static const u8 png_header[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
  44. struct PNG_IHDR {
  45. NetworkOrdered<u32> width;
  46. NetworkOrdered<u32> height;
  47. u8 bit_depth { 0 };
  48. u8 color_type { 0 };
  49. u8 compression_method { 0 };
  50. u8 filter_method { 0 };
  51. u8 interlace_method { 0 };
  52. };
  53. static_assert(sizeof(PNG_IHDR) == 13);
  54. struct Scanline {
  55. u8 filter { 0 };
  56. ReadonlyBytes data {};
  57. };
  58. struct [[gnu::packed]] PaletteEntry {
  59. u8 r;
  60. u8 g;
  61. u8 b;
  62. //u8 a;
  63. };
  64. template<typename T>
  65. struct [[gnu::packed]] Tuple {
  66. T gray;
  67. T a;
  68. };
  69. template<typename T>
  70. struct [[gnu::packed]] Triplet {
  71. T r;
  72. T g;
  73. T b;
  74. };
  75. template<typename T>
  76. struct [[gnu::packed]] Quad {
  77. T r;
  78. T g;
  79. T b;
  80. T a;
  81. };
  82. enum PngInterlaceMethod {
  83. Null = 0,
  84. Adam7 = 1
  85. };
  86. struct PNGLoadingContext {
  87. enum State {
  88. NotDecoded = 0,
  89. Error,
  90. HeaderDecoded,
  91. SizeDecoded,
  92. ChunksDecoded,
  93. BitmapDecoded,
  94. };
  95. State state { State::NotDecoded };
  96. const u8* data { nullptr };
  97. size_t data_size { 0 };
  98. int width { -1 };
  99. int height { -1 };
  100. u8 bit_depth { 0 };
  101. u8 color_type { 0 };
  102. u8 compression_method { 0 };
  103. u8 filter_method { 0 };
  104. u8 interlace_method { 0 };
  105. u8 channels { 0 };
  106. bool has_seen_zlib_header { false };
  107. bool has_alpha() const { return color_type & 4 || palette_transparency_data.size() > 0; }
  108. Vector<Scanline> scanlines;
  109. RefPtr<Gfx::Bitmap> bitmap;
  110. u8* decompression_buffer { nullptr };
  111. size_t decompression_buffer_size { 0 };
  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. MappedFile mapped_file(path);
  174. if (!mapped_file.is_valid())
  175. return nullptr;
  176. auto bitmap = load_png_impl((const u8*)mapped_file.data(), mapped_file.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. ASSERT_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. ASSERT_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. ASSERT_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. ASSERT_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. ASSERT_NOT_REACHED();
  441. }
  442. break;
  443. default:
  444. ASSERT_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. #ifdef 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. #ifdef 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. context.state = PNGLoadingContext::State::Error;
  547. return false;
  548. }
  549. }
  550. context.state = PNGLoadingContext::State::ChunksDecoded;
  551. return true;
  552. }
  553. static bool decode_png_bitmap_simple(PNGLoadingContext& context)
  554. {
  555. Streamer streamer(context.decompression_buffer, context.decompression_buffer_size);
  556. for (int y = 0; y < context.height; ++y) {
  557. u8 filter;
  558. if (!streamer.read(filter)) {
  559. context.state = PNGLoadingContext::State::Error;
  560. return false;
  561. }
  562. if (filter > 4) {
  563. #ifdef PNG_DEBUG
  564. dbg() << "Invalid PNG filter: " << filter;
  565. #endif
  566. context.state = PNGLoadingContext::State::Error;
  567. return false;
  568. }
  569. context.scanlines.append({ filter });
  570. auto& scanline_buffer = context.scanlines.last().data;
  571. auto row_size = context.compute_row_size_for_width(context.width);
  572. if (row_size.has_overflow())
  573. return false;
  574. if (!streamer.wrap_bytes(scanline_buffer, row_size.value())) {
  575. context.state = PNGLoadingContext::State::Error;
  576. return false;
  577. }
  578. }
  579. context.bitmap = Bitmap::create_purgeable(context.has_alpha() ? BitmapFormat::RGBA32 : BitmapFormat::RGB32, { context.width, context.height });
  580. if (!context.bitmap) {
  581. context.state = PNGLoadingContext::State::Error;
  582. return false;
  583. }
  584. return unfilter(context);
  585. }
  586. static int adam7_height(PNGLoadingContext& context, int pass)
  587. {
  588. switch (pass) {
  589. case 1:
  590. return (context.height + 7) / 8;
  591. case 2:
  592. return (context.height + 7) / 8;
  593. case 3:
  594. return (context.height + 3) / 8;
  595. case 4:
  596. return (context.height + 3) / 4;
  597. case 5:
  598. return (context.height + 1) / 4;
  599. case 6:
  600. return (context.height + 1) / 2;
  601. case 7:
  602. return context.height / 2;
  603. default:
  604. ASSERT_NOT_REACHED();
  605. }
  606. }
  607. static int adam7_width(PNGLoadingContext& context, int pass)
  608. {
  609. switch (pass) {
  610. case 1:
  611. return (context.width + 7) / 8;
  612. case 2:
  613. return (context.width + 3) / 8;
  614. case 3:
  615. return (context.width + 3) / 4;
  616. case 4:
  617. return (context.width + 1) / 4;
  618. case 5:
  619. return (context.width + 1) / 2;
  620. case 6:
  621. return context.width / 2;
  622. case 7:
  623. return context.width;
  624. default:
  625. ASSERT_NOT_REACHED();
  626. }
  627. }
  628. // Index 0 unused (non-interlaced case)
  629. static int adam7_starty[8] = { 0, 0, 0, 4, 0, 2, 0, 1 };
  630. static int adam7_startx[8] = { 0, 0, 4, 0, 2, 0, 1, 0 };
  631. static int adam7_stepy[8] = { 1, 8, 8, 8, 4, 4, 2, 2 };
  632. static int adam7_stepx[8] = { 1, 8, 8, 4, 4, 2, 2, 1 };
  633. static bool decode_adam7_pass(PNGLoadingContext& context, Streamer& streamer, int pass)
  634. {
  635. PNGLoadingContext subimage_context;
  636. subimage_context.width = adam7_width(context, pass);
  637. subimage_context.height = adam7_height(context, pass);
  638. subimage_context.channels = context.channels;
  639. subimage_context.color_type = context.color_type;
  640. subimage_context.palette_data = context.palette_data;
  641. subimage_context.palette_transparency_data = context.palette_transparency_data;
  642. subimage_context.bit_depth = context.bit_depth;
  643. subimage_context.filter_method = context.filter_method;
  644. // For small images, some passes might be empty
  645. if (!subimage_context.width || !subimage_context.height)
  646. return true;
  647. subimage_context.scanlines.clear_with_capacity();
  648. for (int y = 0; y < subimage_context.height; ++y) {
  649. u8 filter;
  650. if (!streamer.read(filter)) {
  651. context.state = PNGLoadingContext::State::Error;
  652. return false;
  653. }
  654. if (filter > 4) {
  655. #ifdef PNG_DEBUG
  656. dbg() << "Invalid PNG filter: " << filter;
  657. #endif
  658. context.state = PNGLoadingContext::State::Error;
  659. return false;
  660. }
  661. subimage_context.scanlines.append({ filter });
  662. auto& scanline_buffer = subimage_context.scanlines.last().data;
  663. auto row_size = context.compute_row_size_for_width(subimage_context.width);
  664. if (row_size.has_overflow())
  665. return false;
  666. if (!streamer.wrap_bytes(scanline_buffer, row_size.value())) {
  667. context.state = PNGLoadingContext::State::Error;
  668. return false;
  669. }
  670. }
  671. subimage_context.bitmap = Bitmap::create(context.bitmap->format(), { subimage_context.width, subimage_context.height });
  672. if (!unfilter(subimage_context)) {
  673. subimage_context.bitmap = nullptr;
  674. return false;
  675. }
  676. // Copy the subimage data into the main image according to the pass pattern
  677. for (int y = 0, dy = adam7_starty[pass]; y < subimage_context.height && dy < context.height; ++y, dy += adam7_stepy[pass]) {
  678. for (int x = 0, dx = adam7_startx[pass]; x < subimage_context.width && dy < context.width; ++x, dx += adam7_stepx[pass]) {
  679. context.bitmap->set_pixel(dx, dy, subimage_context.bitmap->get_pixel(x, y));
  680. }
  681. }
  682. return true;
  683. }
  684. static bool decode_png_adam7(PNGLoadingContext& context)
  685. {
  686. Streamer streamer(context.decompression_buffer, context.decompression_buffer_size);
  687. context.bitmap = Bitmap::create_purgeable(context.has_alpha() ? BitmapFormat::RGBA32 : BitmapFormat::RGB32, { context.width, context.height });
  688. if (!context.bitmap)
  689. return false;
  690. for (int pass = 1; pass <= 7; ++pass) {
  691. if (!decode_adam7_pass(context, streamer, pass))
  692. return false;
  693. }
  694. return true;
  695. }
  696. static bool decode_png_bitmap(PNGLoadingContext& context)
  697. {
  698. if (context.state < PNGLoadingContext::State::ChunksDecoded) {
  699. if (!decode_png_chunks(context))
  700. return false;
  701. }
  702. if (context.state >= PNGLoadingContext::State::BitmapDecoded)
  703. return true;
  704. if (context.width == -1 || context.height == -1)
  705. return false; // Didn't see an IHDR chunk.
  706. if (context.color_type == 3 && context.palette_data.is_empty())
  707. return false; // Didn't see a PLTE chunk for a palettized image, or it was empty.
  708. unsigned long srclen = context.compressed_data.size() - 6;
  709. unsigned long destlen = 0;
  710. int ret = puff(nullptr, &destlen, context.compressed_data.data() + 2, &srclen);
  711. if (ret != 0) {
  712. context.state = PNGLoadingContext::State::Error;
  713. return false;
  714. }
  715. context.decompression_buffer_size = destlen;
  716. #ifdef __serenity__
  717. context.decompression_buffer = (u8*)mmap_with_name(nullptr, context.decompression_buffer_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "PNG decompression buffer");
  718. #else
  719. context.decompression_buffer = (u8*)mmap(nullptr, context.decompression_buffer_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  720. #endif
  721. ret = puff(context.decompression_buffer, &destlen, context.compressed_data.data() + 2, &srclen);
  722. if (ret != 0) {
  723. context.state = PNGLoadingContext::State::Error;
  724. return false;
  725. }
  726. context.compressed_data.clear();
  727. context.scanlines.ensure_capacity(context.height);
  728. switch (context.interlace_method) {
  729. case PngInterlaceMethod::Null:
  730. if (!decode_png_bitmap_simple(context))
  731. return false;
  732. break;
  733. case PngInterlaceMethod::Adam7:
  734. if (!decode_png_adam7(context))
  735. return false;
  736. break;
  737. default:
  738. ASSERT_NOT_REACHED();
  739. }
  740. munmap(context.decompression_buffer, context.decompression_buffer_size);
  741. context.decompression_buffer = nullptr;
  742. context.decompression_buffer_size = 0;
  743. context.state = PNGLoadingContext::State::BitmapDecoded;
  744. return true;
  745. }
  746. static RefPtr<Gfx::Bitmap> load_png_impl(const u8* data, size_t data_size)
  747. {
  748. PNGLoadingContext context;
  749. context.data = data;
  750. context.data_size = data_size;
  751. if (!decode_png_chunks(context))
  752. return nullptr;
  753. if (!decode_png_bitmap(context))
  754. return nullptr;
  755. return context.bitmap;
  756. }
  757. static bool is_valid_compression_method(u8 compression_method)
  758. {
  759. return compression_method == 0;
  760. }
  761. static bool is_valid_filter_method(u8 filter_method)
  762. {
  763. return filter_method <= 4;
  764. }
  765. static bool process_IHDR(ReadonlyBytes data, PNGLoadingContext& context)
  766. {
  767. if (data.size() < (int)sizeof(PNG_IHDR))
  768. return false;
  769. auto& ihdr = *(const PNG_IHDR*)data.data();
  770. if (ihdr.width > maximum_width_for_decoded_images || ihdr.height > maximum_height_for_decoded_images) {
  771. dbgln("This PNG is too large for comfort: {}x{}", (u32)ihdr.width, (u32)ihdr.height);
  772. return false;
  773. }
  774. if (!is_valid_compression_method(ihdr.compression_method)) {
  775. dbgln("PNG has invalid compression method {}", ihdr.compression_method);
  776. return false;
  777. }
  778. if (!is_valid_filter_method(ihdr.filter_method)) {
  779. dbgln("PNG has invalid filter method {}", ihdr.filter_method);
  780. return false;
  781. }
  782. context.width = ihdr.width;
  783. context.height = ihdr.height;
  784. context.bit_depth = ihdr.bit_depth;
  785. context.color_type = ihdr.color_type;
  786. context.compression_method = ihdr.compression_method;
  787. context.filter_method = ihdr.filter_method;
  788. context.interlace_method = ihdr.interlace_method;
  789. #ifdef PNG_DEBUG
  790. printf("PNG: %dx%d (%d bpp)\n", context.width, context.height, context.bit_depth);
  791. printf(" Color type: %d\n", context.color_type);
  792. printf("Compress Method: %d\n", context.compression_method);
  793. printf(" Filter Method: %d\n", context.filter_method);
  794. printf(" Interlace type: %d\n", context.interlace_method);
  795. #endif
  796. if (context.interlace_method != PngInterlaceMethod::Null && context.interlace_method != PngInterlaceMethod::Adam7) {
  797. #ifdef PNG_DEBUG
  798. dbgln("PNGLoader::process_IHDR: unknown interlace method: {}", context.interlace_method);
  799. #endif
  800. return false;
  801. }
  802. switch (context.color_type) {
  803. case 0: // Each pixel is a grayscale sample.
  804. if (context.bit_depth != 1 && context.bit_depth != 2 && context.bit_depth != 4 && context.bit_depth != 8 && context.bit_depth != 16)
  805. return false;
  806. context.channels = 1;
  807. break;
  808. case 4: // Each pixel is a grayscale sample, followed by an alpha sample.
  809. if (context.bit_depth != 8 && context.bit_depth != 16)
  810. return false;
  811. context.channels = 2;
  812. break;
  813. case 2: // Each pixel is an RGB sample
  814. if (context.bit_depth != 8 && context.bit_depth != 16)
  815. return false;
  816. context.channels = 3;
  817. break;
  818. case 3: // Each pixel is a palette index; a PLTE chunk must appear.
  819. if (context.bit_depth != 1 && context.bit_depth != 2 && context.bit_depth != 4 && context.bit_depth != 8)
  820. return false;
  821. context.channels = 1;
  822. break;
  823. case 6: // Each pixel is an RGB sample, followed by an alpha sample.
  824. if (context.bit_depth != 8 && context.bit_depth != 16)
  825. return false;
  826. context.channels = 4;
  827. break;
  828. default:
  829. return false;
  830. }
  831. return true;
  832. }
  833. static bool process_IDAT(ReadonlyBytes data, PNGLoadingContext& context)
  834. {
  835. context.compressed_data.append(data.data(), data.size());
  836. return true;
  837. }
  838. static bool process_PLTE(ReadonlyBytes data, PNGLoadingContext& context)
  839. {
  840. context.palette_data.append((const PaletteEntry*)data.data(), data.size() / 3);
  841. return true;
  842. }
  843. static bool process_tRNS(ReadonlyBytes data, PNGLoadingContext& context)
  844. {
  845. switch (context.color_type) {
  846. case 3:
  847. context.palette_transparency_data.append(data.data(), data.size());
  848. break;
  849. }
  850. return true;
  851. }
  852. static bool process_chunk(Streamer& streamer, PNGLoadingContext& context)
  853. {
  854. u32 chunk_size;
  855. if (!streamer.read(chunk_size)) {
  856. #ifdef PNG_DEBUG
  857. printf("Bail at chunk_size\n");
  858. #endif
  859. return false;
  860. }
  861. u8 chunk_type[5];
  862. chunk_type[4] = '\0';
  863. if (!streamer.read_bytes(chunk_type, 4)) {
  864. #ifdef PNG_DEBUG
  865. printf("Bail at chunk_type\n");
  866. #endif
  867. return false;
  868. }
  869. ReadonlyBytes chunk_data;
  870. if (!streamer.wrap_bytes(chunk_data, chunk_size)) {
  871. #ifdef PNG_DEBUG
  872. printf("Bail at chunk_data\n");
  873. #endif
  874. return false;
  875. }
  876. u32 chunk_crc;
  877. if (!streamer.read(chunk_crc)) {
  878. #ifdef PNG_DEBUG
  879. printf("Bail at chunk_crc\n");
  880. #endif
  881. return false;
  882. }
  883. #ifdef PNG_DEBUG
  884. printf("Chunk type: '%s', size: %u, crc: %x\n", chunk_type, chunk_size, chunk_crc);
  885. #endif
  886. if (!strcmp((const char*)chunk_type, "IHDR"))
  887. return process_IHDR(chunk_data, context);
  888. if (!strcmp((const char*)chunk_type, "IDAT"))
  889. return process_IDAT(chunk_data, context);
  890. if (!strcmp((const char*)chunk_type, "PLTE"))
  891. return process_PLTE(chunk_data, context);
  892. if (!strcmp((const char*)chunk_type, "tRNS"))
  893. return process_tRNS(chunk_data, context);
  894. return true;
  895. }
  896. PNGImageDecoderPlugin::PNGImageDecoderPlugin(const u8* data, size_t size)
  897. {
  898. m_context = make<PNGLoadingContext>();
  899. m_context->data = data;
  900. m_context->data_size = size;
  901. }
  902. PNGImageDecoderPlugin::~PNGImageDecoderPlugin()
  903. {
  904. }
  905. IntSize PNGImageDecoderPlugin::size()
  906. {
  907. if (m_context->state == PNGLoadingContext::State::Error)
  908. return {};
  909. if (m_context->state < PNGLoadingContext::State::SizeDecoded) {
  910. bool success = decode_png_size(*m_context);
  911. if (!success)
  912. return {};
  913. }
  914. return { m_context->width, m_context->height };
  915. }
  916. RefPtr<Gfx::Bitmap> PNGImageDecoderPlugin::bitmap()
  917. {
  918. if (m_context->state == PNGLoadingContext::State::Error)
  919. return nullptr;
  920. if (m_context->state < PNGLoadingContext::State::BitmapDecoded) {
  921. // NOTE: This forces the chunk decoding to happen.
  922. bool success = decode_png_bitmap(*m_context);
  923. if (!success)
  924. return nullptr;
  925. }
  926. ASSERT(m_context->bitmap);
  927. return m_context->bitmap;
  928. }
  929. void PNGImageDecoderPlugin::set_volatile()
  930. {
  931. if (m_context->bitmap)
  932. m_context->bitmap->set_volatile();
  933. }
  934. bool PNGImageDecoderPlugin::set_nonvolatile()
  935. {
  936. if (!m_context->bitmap)
  937. return false;
  938. return m_context->bitmap->set_nonvolatile();
  939. }
  940. bool PNGImageDecoderPlugin::sniff()
  941. {
  942. return decode_png_header(*m_context);
  943. }
  944. bool PNGImageDecoderPlugin::is_animated()
  945. {
  946. return false;
  947. }
  948. size_t PNGImageDecoderPlugin::loop_count()
  949. {
  950. return 0;
  951. }
  952. size_t PNGImageDecoderPlugin::frame_count()
  953. {
  954. return 1;
  955. }
  956. ImageFrameDescriptor PNGImageDecoderPlugin::frame(size_t i)
  957. {
  958. if (i > 0) {
  959. return { bitmap(), 0 };
  960. }
  961. return {};
  962. }
  963. }