PNGLoader.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. #include <AK/FileSystemPath.h>
  2. #include <AK/MappedFile.h>
  3. #include <AK/NetworkOrdered.h>
  4. #include <LibDraw/PNGLoader.h>
  5. #include <LibDraw/puff.c>
  6. #include <fcntl.h>
  7. #include <serenity.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <sys/mman.h>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13. //#define PNG_STOPWATCH_DEBUG
  14. struct PNG_IHDR {
  15. NetworkOrdered<u32> width;
  16. NetworkOrdered<u32> height;
  17. u8 bit_depth { 0 };
  18. u8 color_type { 0 };
  19. u8 compression_method { 0 };
  20. u8 filter_method { 0 };
  21. u8 interlace_method { 0 };
  22. };
  23. static_assert(sizeof(PNG_IHDR) == 13);
  24. struct Scanline {
  25. u8 filter { 0 };
  26. ByteBuffer data {};
  27. };
  28. struct [[gnu::packed]] PaletteEntry
  29. {
  30. u8 r;
  31. u8 g;
  32. u8 b;
  33. //u8 a;
  34. };
  35. struct [[gnu::packed]] Triplet
  36. {
  37. u8 r;
  38. u8 g;
  39. u8 b;
  40. };
  41. struct [[gnu::packed]] Triplet16
  42. {
  43. u16 r;
  44. u16 g;
  45. u16 b;
  46. };
  47. struct [[gnu::packed]] Quad16
  48. {
  49. u16 r;
  50. u16 g;
  51. u16 b;
  52. u16 a;
  53. };
  54. struct PNGLoadingContext {
  55. enum class State {
  56. NotDecoded,
  57. ChunksDecoded,
  58. BitmapDecoded,
  59. };
  60. State state { State::NotDecoded };
  61. const u8* data { nullptr };
  62. size_t data_size { 0 };
  63. int width { -1 };
  64. int height { -1 };
  65. u8 bit_depth { 0 };
  66. u8 color_type { 0 };
  67. u8 compression_method { 0 };
  68. u8 filter_method { 0 };
  69. u8 interlace_method { 0 };
  70. u8 bytes_per_pixel { 0 };
  71. bool has_seen_zlib_header { false };
  72. bool has_alpha() const { return color_type & 4 || palette_transparency_data.size() > 0; }
  73. Vector<Scanline> scanlines;
  74. RefPtr<GraphicsBitmap> bitmap;
  75. u8* decompression_buffer { nullptr };
  76. int decompression_buffer_size { 0 };
  77. Vector<u8> compressed_data;
  78. Vector<PaletteEntry> palette_data;
  79. Vector<u8> palette_transparency_data;
  80. };
  81. class Streamer {
  82. public:
  83. Streamer(const u8* data, int size)
  84. : m_original_data(data)
  85. , m_original_size(size)
  86. , m_data_ptr(data)
  87. , m_size_remaining(size)
  88. {
  89. }
  90. template<typename T>
  91. bool read(T& value)
  92. {
  93. if (m_size_remaining < (int)sizeof(T))
  94. return false;
  95. value = *((const NetworkOrdered<T>*)m_data_ptr);
  96. m_data_ptr += sizeof(T);
  97. m_size_remaining -= sizeof(T);
  98. return true;
  99. }
  100. bool read_bytes(u8* buffer, int count)
  101. {
  102. if (m_size_remaining < count)
  103. return false;
  104. memcpy(buffer, m_data_ptr, count);
  105. m_data_ptr += count;
  106. m_size_remaining -= count;
  107. return true;
  108. }
  109. bool wrap_bytes(ByteBuffer& buffer, int count)
  110. {
  111. if (m_size_remaining < count)
  112. return false;
  113. buffer = ByteBuffer::wrap(m_data_ptr, count);
  114. m_data_ptr += count;
  115. m_size_remaining -= count;
  116. return true;
  117. }
  118. bool at_end() const { return !m_size_remaining; }
  119. private:
  120. const u8* m_original_data;
  121. int m_original_size;
  122. const u8* m_data_ptr;
  123. int m_size_remaining;
  124. };
  125. static RefPtr<GraphicsBitmap> load_png_impl(const u8*, int);
  126. static bool process_chunk(Streamer&, PNGLoadingContext& context);
  127. RefPtr<GraphicsBitmap> load_png(const StringView& path)
  128. {
  129. MappedFile mapped_file(path);
  130. if (!mapped_file.is_valid())
  131. return nullptr;
  132. auto bitmap = load_png_impl((const u8*)mapped_file.data(), mapped_file.size());
  133. if (bitmap)
  134. bitmap->set_mmap_name(String::format("GraphicsBitmap [%dx%d] - Decoded PNG: %s", bitmap->width(), bitmap->height(), canonicalized_path(path).characters()));
  135. return bitmap;
  136. }
  137. RefPtr<GraphicsBitmap> load_png_from_memory(const u8* data, size_t length)
  138. {
  139. auto bitmap = load_png_impl(data, length);
  140. if (bitmap)
  141. bitmap->set_mmap_name(String::format("GraphicsBitmap [%dx%d] - Decoded PNG: <memory>", bitmap->width(), bitmap->height()));
  142. return bitmap;
  143. }
  144. [[gnu::always_inline]] static inline u8 paeth_predictor(int a, int b, int c)
  145. {
  146. int p = a + b - c;
  147. int pa = abs(p - a);
  148. int pb = abs(p - b);
  149. int pc = abs(p - c);
  150. if (pa <= pb && pa <= pc)
  151. return a;
  152. if (pb <= pc)
  153. return b;
  154. return c;
  155. }
  156. union [[gnu::packed]] Pixel
  157. {
  158. RGBA32 rgba { 0 };
  159. u8 v[4];
  160. struct {
  161. u8 r;
  162. u8 g;
  163. u8 b;
  164. u8 a;
  165. };
  166. };
  167. static_assert(sizeof(Pixel) == 4);
  168. template<bool has_alpha, u8 filter_type>
  169. [[gnu::always_inline]] static inline void unfilter_impl(GraphicsBitmap& bitmap, int y, const void* dummy_scanline_data)
  170. {
  171. auto* dummy_scanline = (const Pixel*)dummy_scanline_data;
  172. if constexpr (filter_type == 0) {
  173. auto* pixels = (Pixel*)bitmap.scanline(y);
  174. for (int i = 0; i < bitmap.width(); ++i) {
  175. auto& x = pixels[i];
  176. swap(x.r, x.b);
  177. }
  178. }
  179. if constexpr (filter_type == 1) {
  180. auto* pixels = (Pixel*)bitmap.scanline(y);
  181. swap(pixels[0].r, pixels[0].b);
  182. for (int i = 1; i < bitmap.width(); ++i) {
  183. auto& x = pixels[i];
  184. swap(x.r, x.b);
  185. auto& a = (const Pixel&)pixels[i - 1];
  186. x.v[0] += a.v[0];
  187. x.v[1] += a.v[1];
  188. x.v[2] += a.v[2];
  189. if constexpr (has_alpha)
  190. x.v[3] += a.v[3];
  191. }
  192. return;
  193. }
  194. if constexpr (filter_type == 2) {
  195. auto* pixels = (Pixel*)bitmap.scanline(y);
  196. auto* pixels_y_minus_1 = y == 0 ? dummy_scanline : (Pixel*)bitmap.scanline(y - 1);
  197. for (int i = 0; i < bitmap.width(); ++i) {
  198. auto& x = pixels[i];
  199. swap(x.r, x.b);
  200. const Pixel& b = pixels_y_minus_1[i];
  201. x.v[0] += b.v[0];
  202. x.v[1] += b.v[1];
  203. x.v[2] += b.v[2];
  204. if constexpr (has_alpha)
  205. x.v[3] += b.v[3];
  206. }
  207. return;
  208. }
  209. if constexpr (filter_type == 3) {
  210. auto* pixels = (Pixel*)bitmap.scanline(y);
  211. auto* pixels_y_minus_1 = y == 0 ? dummy_scanline : (Pixel*)bitmap.scanline(y - 1);
  212. for (int i = 0; i < bitmap.width(); ++i) {
  213. auto& x = pixels[i];
  214. swap(x.r, x.b);
  215. Pixel a;
  216. if (i != 0)
  217. a = pixels[i - 1];
  218. const Pixel& b = pixels_y_minus_1[i];
  219. x.v[0] = x.v[0] + ((a.v[0] + b.v[0]) / 2);
  220. x.v[1] = x.v[1] + ((a.v[1] + b.v[1]) / 2);
  221. x.v[2] = x.v[2] + ((a.v[2] + b.v[2]) / 2);
  222. if constexpr (has_alpha)
  223. x.v[3] = x.v[3] + ((a.v[3] + b.v[3]) / 2);
  224. }
  225. return;
  226. }
  227. if constexpr (filter_type == 4) {
  228. auto* pixels = (Pixel*)bitmap.scanline(y);
  229. auto* pixels_y_minus_1 = y == 0 ? dummy_scanline : (Pixel*)bitmap.scanline(y - 1);
  230. for (int i = 0; i < bitmap.width(); ++i) {
  231. auto& x = pixels[i];
  232. swap(x.r, x.b);
  233. Pixel a;
  234. const Pixel& b = pixels_y_minus_1[i];
  235. Pixel c;
  236. if (i != 0) {
  237. a = pixels[i - 1];
  238. c = pixels_y_minus_1[i - 1];
  239. }
  240. x.v[0] += paeth_predictor(a.v[0], b.v[0], c.v[0]);
  241. x.v[1] += paeth_predictor(a.v[1], b.v[1], c.v[1]);
  242. x.v[2] += paeth_predictor(a.v[2], b.v[2], c.v[2]);
  243. if constexpr (has_alpha)
  244. x.v[3] += paeth_predictor(a.v[3], b.v[3], c.v[3]);
  245. }
  246. }
  247. }
  248. [[gnu::noinline]] static void unfilter(PNGLoadingContext& context)
  249. {
  250. {
  251. #ifdef PNG_STOPWATCH_DEBUG
  252. Stopwatch sw("load_png_impl: unfilter: unpack");
  253. #endif
  254. // First unpack the scanlines to RGBA:
  255. switch (context.color_type) {
  256. case 2:
  257. if (context.bit_depth == 8) {
  258. for (int y = 0; y < context.height; ++y) {
  259. auto* triplets = (Triplet*)context.scanlines[y].data.data();
  260. for (int i = 0; i < context.width; ++i) {
  261. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  262. pixel.r = triplets[i].r;
  263. pixel.g = triplets[i].g;
  264. pixel.b = triplets[i].b;
  265. pixel.a = 0xff;
  266. }
  267. }
  268. } else if (context.bit_depth == 16) {
  269. for (int y = 0; y < context.height; ++y) {
  270. auto* triplets = (Triplet16*)context.scanlines[y].data.data();
  271. for (int i = 0; i < context.width; ++i) {
  272. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  273. pixel.r = triplets[i].r & 0xFF;
  274. pixel.g = triplets[i].g & 0xFF;
  275. pixel.b = triplets[i].b & 0xFF;
  276. pixel.a = 0xff;
  277. }
  278. }
  279. } else {
  280. ASSERT_NOT_REACHED();
  281. }
  282. break;
  283. case 6:
  284. if (context.bit_depth == 8) {
  285. for (int y = 0; y < context.height; ++y) {
  286. memcpy(context.bitmap->scanline(y), context.scanlines[y].data.data(), context.scanlines[y].data.size());
  287. }
  288. } else if (context.bit_depth == 16) {
  289. for (int y = 0; y < context.height; ++y) {
  290. auto* triplets = (Quad16*)context.scanlines[y].data.data();
  291. for (int i = 0; i < context.width; ++i) {
  292. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  293. pixel.r = triplets[i].r & 0xFF;
  294. pixel.g = triplets[i].g & 0xFF;
  295. pixel.b = triplets[i].b & 0xFF;
  296. pixel.a = triplets[i].a & 0xFF;
  297. }
  298. }
  299. } else {
  300. ASSERT_NOT_REACHED();
  301. }
  302. break;
  303. case 3:
  304. for (int y = 0; y < context.height; ++y) {
  305. auto* palette_index = (u8*)context.scanlines[y].data.data();
  306. for (int i = 0; i < context.width; ++i) {
  307. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  308. auto& color = context.palette_data.at((int)palette_index[i]);
  309. auto transparency = context.palette_transparency_data.size() >= palette_index[i] + 1
  310. ? (int)context.palette_transparency_data.data()[palette_index[i]]
  311. : 0xFF;
  312. pixel.r = color.r;
  313. pixel.g = color.g;
  314. pixel.b = color.b;
  315. pixel.a = transparency;
  316. }
  317. }
  318. break;
  319. default:
  320. ASSERT_NOT_REACHED();
  321. break;
  322. }
  323. }
  324. auto dummy_scanline = ByteBuffer::create_zeroed(context.width * sizeof(RGBA32));
  325. #ifdef PNG_STOPWATCH_DEBUG
  326. Stopwatch sw("load_png_impl: unfilter: process");
  327. #endif
  328. for (int y = 0; y < context.height; ++y) {
  329. auto filter = context.scanlines[y].filter;
  330. if (filter == 0) {
  331. if (context.has_alpha())
  332. unfilter_impl<true, 0>(*context.bitmap, y, dummy_scanline.data());
  333. else
  334. unfilter_impl<false, 0>(*context.bitmap, y, dummy_scanline.data());
  335. continue;
  336. }
  337. if (filter == 1) {
  338. if (context.has_alpha())
  339. unfilter_impl<true, 1>(*context.bitmap, y, dummy_scanline.data());
  340. else
  341. unfilter_impl<false, 1>(*context.bitmap, y, dummy_scanline.data());
  342. continue;
  343. }
  344. if (filter == 2) {
  345. if (context.has_alpha())
  346. unfilter_impl<true, 2>(*context.bitmap, y, dummy_scanline.data());
  347. else
  348. unfilter_impl<false, 2>(*context.bitmap, y, dummy_scanline.data());
  349. continue;
  350. }
  351. if (filter == 3) {
  352. if (context.has_alpha())
  353. unfilter_impl<true, 3>(*context.bitmap, y, dummy_scanline.data());
  354. else
  355. unfilter_impl<false, 3>(*context.bitmap, y, dummy_scanline.data());
  356. continue;
  357. }
  358. if (filter == 4) {
  359. if (context.has_alpha())
  360. unfilter_impl<true, 4>(*context.bitmap, y, dummy_scanline.data());
  361. else
  362. unfilter_impl<false, 4>(*context.bitmap, y, dummy_scanline.data());
  363. continue;
  364. }
  365. }
  366. }
  367. static bool decode_png_chunks(PNGLoadingContext& context)
  368. {
  369. ASSERT(context.state == PNGLoadingContext::State::NotDecoded);
  370. #ifdef PNG_STOPWATCH_DEBUG
  371. Stopwatch sw("load_png_impl: total");
  372. #endif
  373. const u8* data_ptr = context.data;
  374. int data_remaining = context.data_size;
  375. const u8 png_header[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
  376. if (memcmp(context.data, png_header, sizeof(png_header))) {
  377. dbgprintf("Invalid PNG header\n");
  378. return false;
  379. }
  380. context.compressed_data.ensure_capacity(context.data_size);
  381. data_ptr += sizeof(png_header);
  382. data_remaining -= sizeof(png_header);
  383. {
  384. #ifdef PNG_STOPWATCH_DEBUG
  385. Stopwatch sw("load_png_impl: read chunks");
  386. #endif
  387. Streamer streamer(data_ptr, data_remaining);
  388. while (!streamer.at_end()) {
  389. if (!process_chunk(streamer, context)) {
  390. return false;
  391. }
  392. }
  393. }
  394. context.state = PNGLoadingContext::State::ChunksDecoded;
  395. return true;
  396. }
  397. static bool decode_png_bitmap(PNGLoadingContext& context)
  398. {
  399. ASSERT(context.state == PNGLoadingContext::State::ChunksDecoded);
  400. {
  401. #ifdef PNG_STOPWATCH_DEBUG
  402. Stopwatch sw("load_png_impl: uncompress");
  403. #endif
  404. unsigned long srclen = context.compressed_data.size() - 6;
  405. unsigned long destlen = context.decompression_buffer_size;
  406. int ret = puff(context.decompression_buffer, &destlen, context.compressed_data.data() + 2, &srclen);
  407. if (ret < 0)
  408. return false;
  409. context.compressed_data.clear();
  410. }
  411. {
  412. #ifdef PNG_STOPWATCH_DEBUG
  413. Stopwatch sw("load_png_impl: extract scanlines");
  414. #endif
  415. context.scanlines.ensure_capacity(context.height);
  416. Streamer streamer(context.decompression_buffer, context.decompression_buffer_size);
  417. for (int y = 0; y < context.height; ++y) {
  418. u8 filter;
  419. if (!streamer.read(filter))
  420. return false;
  421. context.scanlines.append({ filter });
  422. auto& scanline_buffer = context.scanlines.last().data;
  423. if (!streamer.wrap_bytes(scanline_buffer, context.width * context.bytes_per_pixel))
  424. return false;
  425. }
  426. }
  427. {
  428. #ifdef PNG_STOPWATCH_DEBUG
  429. Stopwatch sw("load_png_impl: create bitmap");
  430. #endif
  431. context.bitmap = GraphicsBitmap::create(context.has_alpha() ? GraphicsBitmap::Format::RGBA32 : GraphicsBitmap::Format::RGB32, { context.width, context.height });
  432. }
  433. unfilter(context);
  434. munmap(context.decompression_buffer, context.decompression_buffer_size);
  435. context.decompression_buffer = nullptr;
  436. context.decompression_buffer_size = 0;
  437. context.state = PNGLoadingContext::State::BitmapDecoded;
  438. return true;
  439. }
  440. static RefPtr<GraphicsBitmap> load_png_impl(const u8* data, int data_size)
  441. {
  442. PNGLoadingContext context;
  443. context.data = data;
  444. context.data_size = data_size;
  445. if (!decode_png_chunks(context))
  446. return nullptr;
  447. if (!decode_png_bitmap(context))
  448. return nullptr;
  449. return context.bitmap;
  450. }
  451. static bool process_IHDR(const ByteBuffer& data, PNGLoadingContext& context)
  452. {
  453. if (data.size() < (int)sizeof(PNG_IHDR))
  454. return false;
  455. auto& ihdr = *(const PNG_IHDR*)data.data();
  456. context.width = ihdr.width;
  457. context.height = ihdr.height;
  458. context.bit_depth = ihdr.bit_depth;
  459. context.color_type = ihdr.color_type;
  460. context.compression_method = ihdr.compression_method;
  461. context.filter_method = ihdr.filter_method;
  462. context.interlace_method = ihdr.interlace_method;
  463. #ifdef PNG_DEBUG
  464. printf("PNG: %dx%d (%d bpp)\n", context.width, context.height, context.bit_depth);
  465. printf(" Color type: %d\n", context.color_type);
  466. printf("Compress Method: %d\n", context.compression_method);
  467. printf(" Filter Method: %d\n", context.filter_method);
  468. printf(" Interlace type: %d\n", context.interlace_method);
  469. #endif
  470. // FIXME: Implement Adam7 deinterlacing
  471. if (context.interlace_method != 0) {
  472. dbgprintf("PNGLoader::process_IHDR: Interlaced PNGs not currently supported.\n");
  473. return false;
  474. }
  475. switch (context.color_type) {
  476. case 0: // Each pixel is a grayscale sample.
  477. case 4: // Each pixel is a grayscale sample, followed by an alpha sample.
  478. // FIXME: Implement grayscale PNG support.
  479. dbgprintf("PNGLoader::process_IHDR: Unsupported grayscale format.\n");
  480. return false;
  481. case 2:
  482. context.bytes_per_pixel = 3 * (ihdr.bit_depth / 8);
  483. break;
  484. case 3: // Each pixel is a palette index; a PLTE chunk must appear.
  485. // FIXME: Implement support for 1/2/4 bit palette based images.
  486. if (ihdr.bit_depth != 8) {
  487. dbgprintf("PNGLoader::process_IHDR: Unsupported index-based format (%d bpp).\n", context.bit_depth);
  488. return false;
  489. }
  490. context.bytes_per_pixel = 1;
  491. break;
  492. case 6:
  493. context.bytes_per_pixel = 4 * (ihdr.bit_depth / 8);
  494. break;
  495. default:
  496. ASSERT_NOT_REACHED();
  497. }
  498. context.decompression_buffer_size = (context.width * context.height * context.bytes_per_pixel + context.height);
  499. 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");
  500. return true;
  501. }
  502. static bool process_IDAT(const ByteBuffer& data, PNGLoadingContext& context)
  503. {
  504. context.compressed_data.append(data.data(), data.size());
  505. return true;
  506. }
  507. static bool process_PLTE(const ByteBuffer& data, PNGLoadingContext& context)
  508. {
  509. context.palette_data.append((const PaletteEntry*)data.data(), data.size() / 3);
  510. return true;
  511. }
  512. static bool process_tRNS(const ByteBuffer& data, PNGLoadingContext& context)
  513. {
  514. switch (context.color_type) {
  515. case 3:
  516. context.palette_transparency_data.append(data.data(), data.size());
  517. break;
  518. }
  519. return true;
  520. }
  521. static bool process_chunk(Streamer& streamer, PNGLoadingContext& context)
  522. {
  523. u32 chunk_size;
  524. if (!streamer.read(chunk_size)) {
  525. printf("Bail at chunk_size\n");
  526. return false;
  527. }
  528. u8 chunk_type[5];
  529. chunk_type[4] = '\0';
  530. if (!streamer.read_bytes(chunk_type, 4)) {
  531. printf("Bail at chunk_type\n");
  532. return false;
  533. }
  534. ByteBuffer chunk_data;
  535. if (!streamer.wrap_bytes(chunk_data, chunk_size)) {
  536. printf("Bail at chunk_data\n");
  537. return false;
  538. }
  539. u32 chunk_crc;
  540. if (!streamer.read(chunk_crc)) {
  541. printf("Bail at chunk_crc\n");
  542. return false;
  543. }
  544. #ifdef PNG_DEBUG
  545. printf("Chunk type: '%s', size: %u, crc: %x\n", chunk_type, chunk_size, chunk_crc);
  546. #endif
  547. if (!strcmp((const char*)chunk_type, "IHDR"))
  548. return process_IHDR(chunk_data, context);
  549. if (!strcmp((const char*)chunk_type, "IDAT"))
  550. return process_IDAT(chunk_data, context);
  551. if (!strcmp((const char*)chunk_type, "PLTE"))
  552. return process_PLTE(chunk_data, context);
  553. if (!strcmp((const char*)chunk_type, "tRNS"))
  554. return process_tRNS(chunk_data, context);
  555. return true;
  556. }
  557. PNGImageLoaderPlugin::PNGImageLoaderPlugin(const u8* data, size_t size)
  558. {
  559. m_context = make<PNGLoadingContext>();
  560. m_context->data = data;
  561. m_context->data_size = size;
  562. }
  563. PNGImageLoaderPlugin::~PNGImageLoaderPlugin()
  564. {
  565. }
  566. Size PNGImageLoaderPlugin::size()
  567. {
  568. if (m_context->state == PNGLoadingContext::State::NotDecoded) {
  569. bool success = decode_png_chunks(*m_context);
  570. ASSERT(success);
  571. }
  572. return { m_context->width, m_context->height };
  573. }
  574. RefPtr<GraphicsBitmap> PNGImageLoaderPlugin::bitmap()
  575. {
  576. if (m_context->state != PNGLoadingContext::State::BitmapDecoded) {
  577. // NOTE: This forces the chunk decoding to happen.
  578. size();
  579. bool success = decode_png_bitmap(*m_context);
  580. ASSERT(success);
  581. }
  582. ASSERT(m_context->bitmap);
  583. return m_context->bitmap;
  584. }