TestImageDecoder.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/DeprecatedString.h>
  8. #include <LibCore/MappedFile.h>
  9. #include <LibGfx/ImageFormats/BMPLoader.h>
  10. #include <LibGfx/ImageFormats/GIFLoader.h>
  11. #include <LibGfx/ImageFormats/ICOLoader.h>
  12. #include <LibGfx/ImageFormats/ImageDecoder.h>
  13. #include <LibGfx/ImageFormats/JPEGLoader.h>
  14. #include <LibGfx/ImageFormats/PBMLoader.h>
  15. #include <LibGfx/ImageFormats/PGMLoader.h>
  16. #include <LibGfx/ImageFormats/PNGLoader.h>
  17. #include <LibGfx/ImageFormats/PPMLoader.h>
  18. #include <LibGfx/ImageFormats/TGALoader.h>
  19. #include <LibGfx/ImageFormats/WebPLoader.h>
  20. #include <LibTest/TestCase.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #ifdef AK_OS_SERENITY
  24. # define TEST_INPUT(x) ("/usr/Tests/LibGfx/test-inputs/" x)
  25. #else
  26. # define TEST_INPUT(x) ("test-inputs/" x)
  27. #endif
  28. TEST_CASE(test_bmp)
  29. {
  30. auto file = MUST(Core::MappedFile::map(TEST_INPUT("rgba32-1.bmp"sv)));
  31. EXPECT(Gfx::BMPImageDecoderPlugin::sniff(file->bytes()));
  32. auto plugin_decoder = MUST(Gfx::BMPImageDecoderPlugin::create(file->bytes()));
  33. MUST(plugin_decoder->initialize());
  34. EXPECT(plugin_decoder->frame_count());
  35. EXPECT(!plugin_decoder->is_animated());
  36. EXPECT(!plugin_decoder->loop_count());
  37. auto frame = MUST(plugin_decoder->frame(0));
  38. EXPECT(frame.duration == 0);
  39. }
  40. TEST_CASE(test_gif)
  41. {
  42. auto file = MUST(Core::MappedFile::map(TEST_INPUT("download-animation.gif"sv)));
  43. EXPECT(Gfx::GIFImageDecoderPlugin::sniff(file->bytes()));
  44. auto plugin_decoder = MUST(Gfx::GIFImageDecoderPlugin::create(file->bytes()));
  45. MUST(plugin_decoder->initialize());
  46. EXPECT(plugin_decoder->frame_count());
  47. EXPECT(plugin_decoder->is_animated());
  48. EXPECT(!plugin_decoder->loop_count());
  49. auto frame = MUST(plugin_decoder->frame(1));
  50. EXPECT(frame.duration == 400);
  51. }
  52. TEST_CASE(test_not_ico)
  53. {
  54. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie.png"sv)));
  55. EXPECT(!Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
  56. auto plugin_decoder = MUST(Gfx::ICOImageDecoderPlugin::create(file->bytes()));
  57. EXPECT(plugin_decoder->initialize().is_error());
  58. EXPECT(plugin_decoder->frame_count());
  59. EXPECT(!plugin_decoder->is_animated());
  60. EXPECT(!plugin_decoder->loop_count());
  61. EXPECT(plugin_decoder->frame(0).is_error());
  62. }
  63. TEST_CASE(test_bmp_embedded_in_ico)
  64. {
  65. auto file = MUST(Core::MappedFile::map(TEST_INPUT("serenity.ico"sv)));
  66. EXPECT(Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
  67. auto plugin_decoder = MUST(Gfx::ICOImageDecoderPlugin::create(file->bytes()));
  68. MUST(plugin_decoder->initialize());
  69. EXPECT(plugin_decoder->frame_count());
  70. EXPECT(!plugin_decoder->is_animated());
  71. EXPECT(!plugin_decoder->loop_count());
  72. MUST(plugin_decoder->frame(0));
  73. }
  74. TEST_CASE(test_jpeg_sof0_one_scan)
  75. {
  76. auto file = MUST(Core::MappedFile::map(TEST_INPUT("rgb24.jpg"sv)));
  77. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  78. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  79. MUST(plugin_decoder->initialize());
  80. EXPECT(plugin_decoder->frame_count());
  81. EXPECT(!plugin_decoder->is_animated());
  82. EXPECT(!plugin_decoder->loop_count());
  83. auto frame = MUST(plugin_decoder->frame(0));
  84. EXPECT(frame.duration == 0);
  85. }
  86. TEST_CASE(test_jpeg_sof0_several_scans)
  87. {
  88. auto file = MUST(Core::MappedFile::map(TEST_INPUT("several_scans.jpg"sv)));
  89. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  90. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  91. MUST(plugin_decoder->initialize());
  92. auto frame = MUST(plugin_decoder->frame(0));
  93. EXPECT_EQ(frame.image->size(), Gfx::IntSize(592, 800));
  94. }
  95. TEST_CASE(test_jpeg_rgb_components)
  96. {
  97. auto file = MUST(Core::MappedFile::map(TEST_INPUT("rgb_components.jpg"sv)));
  98. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  99. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  100. MUST(plugin_decoder->initialize());
  101. auto frame = MUST(plugin_decoder->frame(0));
  102. EXPECT_EQ(frame.image->size(), Gfx::IntSize(592, 800));
  103. }
  104. TEST_CASE(test_jpeg_sof2_spectral_selection)
  105. {
  106. auto file = MUST(Core::MappedFile::map(TEST_INPUT("spectral_selection.jpg"sv)));
  107. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  108. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  109. MUST(plugin_decoder->initialize());
  110. auto frame = MUST(plugin_decoder->frame(0));
  111. EXPECT_EQ(frame.image->size(), Gfx::IntSize(592, 800));
  112. }
  113. TEST_CASE(test_jpeg_sof0_several_scans_odd_number_mcu)
  114. {
  115. auto file = MUST(Core::MappedFile::map(TEST_INPUT("several_scans_odd_number_mcu.jpg"sv)));
  116. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  117. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  118. MUST(plugin_decoder->initialize());
  119. auto frame = MUST(plugin_decoder->frame(0));
  120. EXPECT_EQ(frame.image->size(), Gfx::IntSize(600, 600));
  121. }
  122. TEST_CASE(test_jpeg_sof2_successive_aproximation)
  123. {
  124. auto file = MUST(Core::MappedFile::map(TEST_INPUT("successive_approximation.jpg"sv)));
  125. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  126. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  127. MUST(plugin_decoder->initialize());
  128. auto frame = MUST(plugin_decoder->frame(0));
  129. EXPECT_EQ(frame.image->size(), Gfx::IntSize(600, 800));
  130. }
  131. TEST_CASE(test_jpeg_sof1_12bits)
  132. {
  133. auto file = MUST(Core::MappedFile::map(TEST_INPUT("12-bit.jpg"sv)));
  134. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  135. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  136. MUST(plugin_decoder->initialize());
  137. auto frame = MUST(plugin_decoder->frame(0));
  138. EXPECT_EQ(frame.image->size(), Gfx::IntSize(320, 240));
  139. }
  140. TEST_CASE(test_jpeg_sof2_12bits)
  141. {
  142. auto file = MUST(Core::MappedFile::map(TEST_INPUT("12-bit-progressive.jpg"sv)));
  143. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  144. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  145. MUST(plugin_decoder->initialize());
  146. auto frame = MUST(plugin_decoder->frame(0));
  147. EXPECT_EQ(frame.image->size(), Gfx::IntSize(320, 240));
  148. }
  149. TEST_CASE(test_pbm)
  150. {
  151. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.pbm"sv)));
  152. EXPECT(Gfx::PBMImageDecoderPlugin::sniff(file->bytes()));
  153. auto plugin_decoder = MUST(Gfx::PBMImageDecoderPlugin::create(file->bytes()));
  154. MUST(plugin_decoder->initialize());
  155. EXPECT(plugin_decoder->frame_count());
  156. EXPECT(!plugin_decoder->is_animated());
  157. EXPECT(!plugin_decoder->loop_count());
  158. auto frame = MUST(plugin_decoder->frame(0));
  159. EXPECT(frame.duration == 0);
  160. }
  161. TEST_CASE(test_pgm)
  162. {
  163. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.pgm"sv)));
  164. EXPECT(Gfx::PGMImageDecoderPlugin::sniff(file->bytes()));
  165. auto plugin_decoder = MUST(Gfx::PGMImageDecoderPlugin::create(file->bytes()));
  166. MUST(plugin_decoder->initialize());
  167. EXPECT(plugin_decoder->frame_count());
  168. EXPECT(!plugin_decoder->is_animated());
  169. EXPECT(!plugin_decoder->loop_count());
  170. auto frame = MUST(plugin_decoder->frame(0));
  171. EXPECT(frame.duration == 0);
  172. }
  173. TEST_CASE(test_png)
  174. {
  175. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie.png"sv)));
  176. EXPECT(Gfx::PNGImageDecoderPlugin::sniff(file->bytes()));
  177. auto plugin_decoder = MUST(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
  178. MUST(plugin_decoder->initialize());
  179. EXPECT(plugin_decoder->frame_count());
  180. EXPECT(!plugin_decoder->is_animated());
  181. EXPECT(!plugin_decoder->loop_count());
  182. auto frame = MUST(plugin_decoder->frame(0));
  183. EXPECT(frame.duration == 0);
  184. }
  185. TEST_CASE(test_ppm)
  186. {
  187. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.ppm"sv)));
  188. EXPECT(Gfx::PPMImageDecoderPlugin::sniff(file->bytes()));
  189. auto plugin_decoder = MUST(Gfx::PPMImageDecoderPlugin::create(file->bytes()));
  190. MUST(plugin_decoder->initialize());
  191. EXPECT(plugin_decoder->frame_count());
  192. EXPECT(!plugin_decoder->is_animated());
  193. EXPECT(!plugin_decoder->loop_count());
  194. auto frame = MUST(plugin_decoder->frame(0));
  195. EXPECT(frame.duration == 0);
  196. }
  197. TEST_CASE(test_targa_bottom_left)
  198. {
  199. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-bottom-left-uncompressed.tga"sv)));
  200. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  201. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  202. MUST(plugin_decoder->initialize());
  203. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  204. EXPECT(!plugin_decoder->is_animated());
  205. EXPECT(!plugin_decoder->loop_count());
  206. auto frame = MUST(plugin_decoder->frame(0));
  207. EXPECT(frame.duration == 0);
  208. }
  209. TEST_CASE(test_targa_top_left)
  210. {
  211. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-top-left-uncompressed.tga"sv)));
  212. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  213. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  214. MUST(plugin_decoder->initialize());
  215. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  216. EXPECT(!plugin_decoder->is_animated());
  217. EXPECT(!plugin_decoder->loop_count());
  218. auto frame = MUST(plugin_decoder->frame(0));
  219. EXPECT(frame.duration == 0);
  220. }
  221. TEST_CASE(test_targa_bottom_left_compressed)
  222. {
  223. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-bottom-left-compressed.tga"sv)));
  224. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  225. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  226. MUST(plugin_decoder->initialize());
  227. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  228. EXPECT(!plugin_decoder->is_animated());
  229. EXPECT(!plugin_decoder->loop_count());
  230. auto frame = MUST(plugin_decoder->frame(0));
  231. EXPECT(frame.duration == 0);
  232. }
  233. TEST_CASE(test_targa_top_left_compressed)
  234. {
  235. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-top-left-compressed.tga"sv)));
  236. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  237. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  238. MUST(plugin_decoder->initialize());
  239. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  240. EXPECT(!plugin_decoder->is_animated());
  241. EXPECT(!plugin_decoder->loop_count());
  242. auto frame = MUST(plugin_decoder->frame(0));
  243. EXPECT(frame.duration == 0);
  244. }
  245. TEST_CASE(test_webp_simple_lossy)
  246. {
  247. auto file = MUST(Core::MappedFile::map(TEST_INPUT("simple-vp8.webp"sv)));
  248. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  249. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  250. MUST(plugin_decoder->initialize());
  251. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  252. EXPECT(!plugin_decoder->is_animated());
  253. EXPECT(!plugin_decoder->loop_count());
  254. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(240, 240));
  255. auto frame = MUST(plugin_decoder->frame(0));
  256. EXPECT_EQ(frame.image->size(), Gfx::IntSize(240, 240));
  257. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  258. // So pixels changing by 1 or so below is fine if you change code.
  259. EXPECT_EQ(frame.image->get_pixel(120, 232), Gfx::Color(0xf2, 0xef, 0xf0, 255));
  260. EXPECT_EQ(frame.image->get_pixel(198, 202), Gfx::Color(0x7b, 0xaa, 0xd5, 255));
  261. }
  262. TEST_CASE(test_webp_simple_lossless)
  263. {
  264. auto file = MUST(Core::MappedFile::map(TEST_INPUT("simple-vp8l.webp"sv)));
  265. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  266. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  267. MUST(plugin_decoder->initialize());
  268. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  269. EXPECT(!plugin_decoder->is_animated());
  270. EXPECT(!plugin_decoder->loop_count());
  271. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(386, 395));
  272. // Ironically, simple-vp8l.webp is a much more complex file than extended-lossless.webp tested below.
  273. // extended-lossless.webp tests the decoding basics.
  274. // This here tests the predictor, color, and subtract green transforms,
  275. // as well as meta prefix images, one-element canonical code handling,
  276. // and handling of canonical codes with more than 288 elements.
  277. // This image uses all 13 predictor modes of the predictor transform.
  278. auto frame = MUST(plugin_decoder->frame(0));
  279. EXPECT_EQ(frame.image->size(), Gfx::IntSize(386, 395));
  280. // This pixel tests all predictor modes except 5, 7, 8, 9, and 13.
  281. EXPECT_EQ(frame.image->get_pixel(289, 332), Gfx::Color(0xf2, 0xee, 0xd3, 255));
  282. }
  283. TEST_CASE(test_webp_extended_lossy)
  284. {
  285. auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossy.webp"sv)));
  286. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  287. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  288. MUST(plugin_decoder->initialize());
  289. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  290. EXPECT(!plugin_decoder->is_animated());
  291. EXPECT(!plugin_decoder->loop_count());
  292. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(417, 223));
  293. auto frame = MUST(plugin_decoder->frame(0));
  294. EXPECT_EQ(frame.image->size(), Gfx::IntSize(417, 223));
  295. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  296. // So pixels changing by 1 or so below is fine if you change code.
  297. EXPECT_EQ(frame.image->get_pixel(89, 72), Gfx::Color(255, 1, 0, 255));
  298. EXPECT_EQ(frame.image->get_pixel(174, 69), Gfx::Color(0, 255, 0, 255));
  299. EXPECT_EQ(frame.image->get_pixel(245, 84), Gfx::Color(0, 0, 255, 255));
  300. EXPECT_EQ(frame.image->get_pixel(352, 125), Gfx::Color(0, 0, 0, 128));
  301. EXPECT_EQ(frame.image->get_pixel(355, 106), Gfx::Color(0, 0, 0, 0));
  302. // Check same basic pixels as in test_webp_extended_lossless too.
  303. // (The top-left pixel in the lossy version is fully transparent white, compared to fully transparent black in the lossless version).
  304. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(255, 255, 255, 0));
  305. EXPECT_EQ(frame.image->get_pixel(43, 75), Gfx::Color(255, 0, 2, 255));
  306. EXPECT_EQ(frame.image->get_pixel(141, 75), Gfx::Color(0, 255, 3, 255));
  307. EXPECT_EQ(frame.image->get_pixel(235, 75), Gfx::Color(0, 0, 255, 255));
  308. EXPECT_EQ(frame.image->get_pixel(341, 75), Gfx::Color(0, 0, 0, 128));
  309. }
  310. TEST_CASE(test_webp_extended_lossless)
  311. {
  312. auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossless.webp"sv)));
  313. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  314. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  315. MUST(plugin_decoder->initialize());
  316. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  317. EXPECT(!plugin_decoder->is_animated());
  318. EXPECT(!plugin_decoder->loop_count());
  319. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(417, 223));
  320. auto frame = MUST(plugin_decoder->frame(0));
  321. EXPECT_EQ(frame.image->size(), Gfx::IntSize(417, 223));
  322. // Check some basic pixels.
  323. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0));
  324. EXPECT_EQ(frame.image->get_pixel(43, 75), Gfx::Color(255, 0, 0, 255));
  325. EXPECT_EQ(frame.image->get_pixel(141, 75), Gfx::Color(0, 255, 0, 255));
  326. EXPECT_EQ(frame.image->get_pixel(235, 75), Gfx::Color(0, 0, 255, 255));
  327. EXPECT_EQ(frame.image->get_pixel(341, 75), Gfx::Color(0, 0, 0, 128));
  328. // Check pixels using the color cache.
  329. EXPECT_EQ(frame.image->get_pixel(94, 73), Gfx::Color(255, 0, 0, 255));
  330. EXPECT_EQ(frame.image->get_pixel(176, 115), Gfx::Color(0, 255, 0, 255));
  331. EXPECT_EQ(frame.image->get_pixel(290, 89), Gfx::Color(0, 0, 255, 255));
  332. EXPECT_EQ(frame.image->get_pixel(359, 73), Gfx::Color(0, 0, 0, 128));
  333. }
  334. TEST_CASE(test_webp_simple_lossless_color_index_transform)
  335. {
  336. // In addition to testing the index transform, this file also tests handling of explicity setting max_symbol.
  337. auto file = MUST(Core::MappedFile::map(TEST_INPUT("Qpalette.webp"sv)));
  338. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  339. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  340. MUST(plugin_decoder->initialize());
  341. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  342. EXPECT(!plugin_decoder->is_animated());
  343. EXPECT(!plugin_decoder->loop_count());
  344. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(256, 256));
  345. auto frame = MUST(plugin_decoder->frame(0));
  346. EXPECT_EQ(frame.image->size(), Gfx::IntSize(256, 256));
  347. EXPECT_EQ(frame.image->get_pixel(100, 100), Gfx::Color(0x73, 0x37, 0x23, 0xff));
  348. }
  349. TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling)
  350. {
  351. struct TestCase {
  352. StringView file_name;
  353. Gfx::Color line_color;
  354. Gfx::Color background_color;
  355. };
  356. // The number after the dash is the number of colors in each file's color index bitmap.
  357. // catdog-alert-2 tests the 1-bit-per-pixel case,
  358. // catdog-alert-3 tests the 2-bit-per-pixel case,
  359. // catdog-alert-8 and catdog-alert-13 both test the 4-bits-per-pixel case.
  360. TestCase test_cases[] = {
  361. { "catdog-alert-2.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0xf3, 0xe6, 0xd8, 0xff) },
  362. { "catdog-alert-3.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0, 0, 0, 0) },
  363. { "catdog-alert-8.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  364. { "catdog-alert-13.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  365. };
  366. for (auto test_case : test_cases) {
  367. auto file = MUST(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), test_case.file_name))));
  368. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  369. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  370. MUST(plugin_decoder->initialize());
  371. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  372. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(32, 32));
  373. auto frame = MUST(plugin_decoder->frame(0));
  374. EXPECT_EQ(frame.image->size(), Gfx::IntSize(32, 32));
  375. EXPECT_EQ(frame.image->get_pixel(4, 0), test_case.background_color);
  376. EXPECT_EQ(frame.image->get_pixel(5, 0), test_case.line_color);
  377. EXPECT_EQ(frame.image->get_pixel(9, 5), test_case.background_color);
  378. EXPECT_EQ(frame.image->get_pixel(10, 5), test_case.line_color);
  379. EXPECT_EQ(frame.image->get_pixel(11, 5), test_case.background_color);
  380. }
  381. }
  382. TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling_odd_width)
  383. {
  384. StringView file_names[] = {
  385. "width11-height11-colors2.webp"sv,
  386. "width11-height11-colors3.webp"sv,
  387. "width11-height11-colors15.webp"sv,
  388. };
  389. for (auto file_name : file_names) {
  390. auto file = MUST(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), file_name))));
  391. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  392. MUST(plugin_decoder->initialize());
  393. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  394. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(11, 11));
  395. auto frame = MUST(plugin_decoder->frame(0));
  396. EXPECT_EQ(frame.image->size(), Gfx::IntSize(11, 11));
  397. }
  398. }
  399. TEST_CASE(test_webp_extended_lossless_animated)
  400. {
  401. auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossless-animated.webp"sv)));
  402. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  403. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  404. MUST(plugin_decoder->initialize());
  405. EXPECT_EQ(plugin_decoder->frame_count(), 8u);
  406. EXPECT(plugin_decoder->is_animated());
  407. EXPECT_EQ(plugin_decoder->loop_count(), 42u);
  408. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(990, 1050));
  409. for (size_t frame_index = 0; frame_index < plugin_decoder->frame_count(); ++frame_index) {
  410. auto frame = MUST(plugin_decoder->frame(frame_index));
  411. EXPECT_EQ(frame.image->size(), Gfx::IntSize(990, 1050));
  412. // This pixel happens to be the same color in all frames.
  413. EXPECT_EQ(frame.image->get_pixel(500, 700), Gfx::Color::Yellow);
  414. // This one isn't the same in all frames.
  415. EXPECT_EQ(frame.image->get_pixel(500, 0), (frame_index == 2 || frame_index == 6) ? Gfx::Color::Black : Gfx::Color(255, 255, 255, 0));
  416. }
  417. }