PNGLoader.cpp 38 KB

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