TestImageDecoder.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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/ILBMLoader.h>
  13. #include <LibGfx/ImageFormats/ImageDecoder.h>
  14. #include <LibGfx/ImageFormats/JPEGLoader.h>
  15. #include <LibGfx/ImageFormats/JPEGXLLoader.h>
  16. #include <LibGfx/ImageFormats/PBMLoader.h>
  17. #include <LibGfx/ImageFormats/PGMLoader.h>
  18. #include <LibGfx/ImageFormats/PNGLoader.h>
  19. #include <LibGfx/ImageFormats/PPMLoader.h>
  20. #include <LibGfx/ImageFormats/TGALoader.h>
  21. #include <LibGfx/ImageFormats/TinyVGLoader.h>
  22. #include <LibGfx/ImageFormats/WebPLoader.h>
  23. #include <LibTest/TestCase.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #ifdef AK_OS_SERENITY
  27. # define TEST_INPUT(x) ("/usr/Tests/LibGfx/test-inputs/" x)
  28. #else
  29. # define TEST_INPUT(x) ("test-inputs/" x)
  30. #endif
  31. static Gfx::ImageFrameDescriptor expect_single_frame(Gfx::ImageDecoderPlugin& plugin_decoder)
  32. {
  33. EXPECT_EQ(plugin_decoder.frame_count(), 1u);
  34. EXPECT(!plugin_decoder.is_animated());
  35. EXPECT(!plugin_decoder.loop_count());
  36. auto frame = MUST(plugin_decoder.frame(0));
  37. EXPECT_EQ(frame.duration, 0);
  38. return frame;
  39. }
  40. static Gfx::ImageFrameDescriptor expect_single_frame_of_size(Gfx::ImageDecoderPlugin& plugin_decoder, Gfx::IntSize size)
  41. {
  42. EXPECT_EQ(plugin_decoder.size(), size);
  43. auto frame = expect_single_frame(plugin_decoder);
  44. EXPECT_EQ(frame.image->size(), size);
  45. return frame;
  46. }
  47. TEST_CASE(test_bmp)
  48. {
  49. auto file = MUST(Core::MappedFile::map(TEST_INPUT("rgba32-1.bmp"sv)));
  50. EXPECT(Gfx::BMPImageDecoderPlugin::sniff(file->bytes()));
  51. auto plugin_decoder = MUST(Gfx::BMPImageDecoderPlugin::create(file->bytes()));
  52. expect_single_frame(*plugin_decoder);
  53. }
  54. TEST_CASE(test_gif)
  55. {
  56. auto file = MUST(Core::MappedFile::map(TEST_INPUT("download-animation.gif"sv)));
  57. EXPECT(Gfx::GIFImageDecoderPlugin::sniff(file->bytes()));
  58. auto plugin_decoder = MUST(Gfx::GIFImageDecoderPlugin::create(file->bytes()));
  59. EXPECT(plugin_decoder->frame_count());
  60. EXPECT(plugin_decoder->is_animated());
  61. EXPECT(!plugin_decoder->loop_count());
  62. auto frame = MUST(plugin_decoder->frame(1));
  63. EXPECT(frame.duration == 400);
  64. }
  65. TEST_CASE(test_not_ico)
  66. {
  67. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie.png"sv)));
  68. EXPECT(!Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
  69. EXPECT(Gfx::ICOImageDecoderPlugin::create(file->bytes()).is_error());
  70. }
  71. TEST_CASE(test_bmp_embedded_in_ico)
  72. {
  73. auto file = MUST(Core::MappedFile::map(TEST_INPUT("serenity.ico"sv)));
  74. EXPECT(Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
  75. auto plugin_decoder = MUST(Gfx::ICOImageDecoderPlugin::create(file->bytes()));
  76. expect_single_frame(*plugin_decoder);
  77. }
  78. TEST_CASE(test_ilbm)
  79. {
  80. auto file = MUST(Core::MappedFile::map(TEST_INPUT("ilbm/gradient.iff"sv)));
  81. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  82. auto plugin_decoder = MUST(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  83. expect_single_frame_of_size(*plugin_decoder, { 320, 200 });
  84. }
  85. TEST_CASE(test_jpeg_sof0_one_scan)
  86. {
  87. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/rgb24.jpg"sv)));
  88. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  89. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  90. expect_single_frame(*plugin_decoder);
  91. }
  92. TEST_CASE(test_jpeg_sof0_several_scans)
  93. {
  94. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/several_scans.jpg"sv)));
  95. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  96. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  97. expect_single_frame_of_size(*plugin_decoder, { 592, 800 });
  98. }
  99. TEST_CASE(test_jpeg_rgb_components)
  100. {
  101. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/rgb_components.jpg"sv)));
  102. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  103. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  104. expect_single_frame_of_size(*plugin_decoder, { 592, 800 });
  105. }
  106. TEST_CASE(test_jpeg_sof2_spectral_selection)
  107. {
  108. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/spectral_selection.jpg"sv)));
  109. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  110. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  111. expect_single_frame_of_size(*plugin_decoder, { 592, 800 });
  112. }
  113. TEST_CASE(test_jpeg_sof0_several_scans_odd_number_mcu)
  114. {
  115. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/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. expect_single_frame_of_size(*plugin_decoder, { 600, 600 });
  119. }
  120. TEST_CASE(test_jpeg_sof2_successive_aproximation)
  121. {
  122. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/successive_approximation.jpg"sv)));
  123. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  124. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  125. expect_single_frame_of_size(*plugin_decoder, { 600, 800 });
  126. }
  127. TEST_CASE(test_jpeg_sof1_12bits)
  128. {
  129. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/12-bit.jpg"sv)));
  130. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  131. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  132. expect_single_frame_of_size(*plugin_decoder, { 320, 240 });
  133. }
  134. TEST_CASE(test_jpeg_sof2_12bits)
  135. {
  136. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/12-bit-progressive.jpg"sv)));
  137. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  138. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  139. expect_single_frame_of_size(*plugin_decoder, { 320, 240 });
  140. }
  141. TEST_CASE(test_jpeg_empty_icc)
  142. {
  143. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/gradient_empty_icc.jpg"sv)));
  144. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  145. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  146. expect_single_frame_of_size(*plugin_decoder, { 80, 80 });
  147. }
  148. TEST_CASE(test_jpeg_grayscale_with_app14)
  149. {
  150. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/grayscale_app14.jpg"sv)));
  151. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  152. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  153. expect_single_frame_of_size(*plugin_decoder, { 80, 80 });
  154. }
  155. TEST_CASE(test_pbm)
  156. {
  157. auto file = MUST(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.pbm"sv)));
  158. EXPECT(Gfx::PBMImageDecoderPlugin::sniff(file->bytes()));
  159. auto plugin_decoder = MUST(Gfx::PBMImageDecoderPlugin::create(file->bytes()));
  160. expect_single_frame(*plugin_decoder);
  161. }
  162. TEST_CASE(test_pgm)
  163. {
  164. auto file = MUST(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.pgm"sv)));
  165. EXPECT(Gfx::PGMImageDecoderPlugin::sniff(file->bytes()));
  166. auto plugin_decoder = MUST(Gfx::PGMImageDecoderPlugin::create(file->bytes()));
  167. expect_single_frame(*plugin_decoder);
  168. }
  169. TEST_CASE(test_png)
  170. {
  171. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie.png"sv)));
  172. EXPECT(Gfx::PNGImageDecoderPlugin::sniff(file->bytes()));
  173. auto plugin_decoder = MUST(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
  174. expect_single_frame(*plugin_decoder);
  175. }
  176. TEST_CASE(test_ppm)
  177. {
  178. auto file = MUST(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.ppm"sv)));
  179. EXPECT(Gfx::PPMImageDecoderPlugin::sniff(file->bytes()));
  180. auto plugin_decoder = MUST(Gfx::PPMImageDecoderPlugin::create(file->bytes()));
  181. expect_single_frame(*plugin_decoder);
  182. }
  183. TEST_CASE(test_targa_bottom_left)
  184. {
  185. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-uncompressed.tga"sv)));
  186. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  187. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  188. expect_single_frame(*plugin_decoder);
  189. }
  190. TEST_CASE(test_targa_top_left)
  191. {
  192. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-uncompressed.tga"sv)));
  193. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  194. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  195. expect_single_frame(*plugin_decoder);
  196. }
  197. TEST_CASE(test_targa_bottom_left_compressed)
  198. {
  199. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-compressed.tga"sv)));
  200. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  201. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  202. expect_single_frame(*plugin_decoder);
  203. }
  204. TEST_CASE(test_targa_top_left_compressed)
  205. {
  206. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-compressed.tga"sv)));
  207. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  208. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  209. expect_single_frame(*plugin_decoder);
  210. }
  211. TEST_CASE(test_webp_simple_lossy)
  212. {
  213. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8.webp"sv)));
  214. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  215. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  216. auto frame = expect_single_frame_of_size(*plugin_decoder, { 240, 240 });
  217. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  218. // So pixels changing by 1 or so below is fine if you change code.
  219. EXPECT_EQ(frame.image->get_pixel(120, 232), Gfx::Color(0xf2, 0xef, 0xf0, 255));
  220. EXPECT_EQ(frame.image->get_pixel(198, 202), Gfx::Color(0x7b, 0xaa, 0xd5, 255));
  221. }
  222. TEST_CASE(test_webp_simple_lossless)
  223. {
  224. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8l.webp"sv)));
  225. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  226. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  227. // Ironically, simple-vp8l.webp is a much more complex file than extended-lossless.webp tested below.
  228. // extended-lossless.webp tests the decoding basics.
  229. // This here tests the predictor, color, and subtract green transforms,
  230. // as well as meta prefix images, one-element canonical code handling,
  231. // and handling of canonical codes with more than 288 elements.
  232. // This image uses all 13 predictor modes of the predictor transform.
  233. auto frame = expect_single_frame_of_size(*plugin_decoder, { 386, 395 });
  234. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0));
  235. // This pixel tests all predictor modes except 5, 7, 8, 9, and 13.
  236. EXPECT_EQ(frame.image->get_pixel(289, 332), Gfx::Color(0xf2, 0xee, 0xd3, 255));
  237. }
  238. TEST_CASE(test_webp_simple_lossless_alpha_used_false)
  239. {
  240. // This file is identical to simple-vp8l.webp, but the `is_alpha_used` used bit is false.
  241. // The file still contains alpha data. This tests that the decoder replaces the stored alpha data with 0xff if `is_alpha_used` is false.
  242. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8l-alpha-used-false.webp"sv)));
  243. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  244. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  245. auto frame = expect_single_frame_of_size(*plugin_decoder, { 386, 395 });
  246. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0xff));
  247. }
  248. TEST_CASE(test_webp_extended_lossy)
  249. {
  250. // This extended lossy image has an ALPH chunk for (losslessly compressed) alpha data.
  251. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/extended-lossy.webp"sv)));
  252. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  253. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  254. auto frame = expect_single_frame_of_size(*plugin_decoder, { 417, 223 });
  255. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  256. // So pixels changing by 1 or so below is fine if you change code.
  257. EXPECT_EQ(frame.image->get_pixel(89, 72), Gfx::Color(255, 1, 0, 255));
  258. EXPECT_EQ(frame.image->get_pixel(174, 69), Gfx::Color(0, 255, 0, 255));
  259. EXPECT_EQ(frame.image->get_pixel(245, 84), Gfx::Color(0, 0, 255, 255));
  260. EXPECT_EQ(frame.image->get_pixel(352, 125), Gfx::Color(0, 0, 0, 128));
  261. EXPECT_EQ(frame.image->get_pixel(355, 106), Gfx::Color(0, 0, 0, 0));
  262. // Check same basic pixels as in test_webp_extended_lossless too.
  263. // (The top-left pixel in the lossy version is fully transparent white, compared to fully transparent black in the lossless version).
  264. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(255, 255, 255, 0));
  265. EXPECT_EQ(frame.image->get_pixel(43, 75), Gfx::Color(255, 0, 2, 255));
  266. EXPECT_EQ(frame.image->get_pixel(141, 75), Gfx::Color(0, 255, 3, 255));
  267. EXPECT_EQ(frame.image->get_pixel(235, 75), Gfx::Color(0, 0, 255, 255));
  268. EXPECT_EQ(frame.image->get_pixel(341, 75), Gfx::Color(0, 0, 0, 128));
  269. }
  270. TEST_CASE(test_webp_extended_lossy_alpha_horizontal_filter)
  271. {
  272. // Also lossy rgb + lossless alpha, but with a horizontal alpha filtering method.
  273. // The image should look like smolkling.webp, but with a horizontal alpha gradient.
  274. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/smolkling-horizontal-alpha.webp"sv)));
  275. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  276. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  277. auto frame = expect_single_frame_of_size(*plugin_decoder, { 264, 264 });
  278. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  279. // So pixels changing by 1 or so below is fine if you change code.
  280. // The important component in this test is alpha, and that shouldn't change even by 1 as it's losslessly compressed and doesn't use YUV.
  281. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x8f, 0x51, 0x2f, 0x4b));
  282. }
  283. TEST_CASE(test_webp_extended_lossy_alpha_vertical_filter)
  284. {
  285. // Also lossy rgb + lossless alpha, but with a vertical alpha filtering method.
  286. // The image should look like smolkling.webp, but with a vertical alpha gradient, and with a fully transparent first column.
  287. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/smolkling-vertical-alpha.webp"sv)));
  288. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  289. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  290. auto frame = expect_single_frame_of_size(*plugin_decoder, { 264, 264 });
  291. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  292. // So pixels changing by 1 or so below is fine if you change code.
  293. // The important component in this test is alpha, and that shouldn't change even by 1 as it's losslessly compressed and doesn't use YUV.
  294. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x94, 0x50, 0x32, 0x4c));
  295. }
  296. TEST_CASE(test_webp_extended_lossy_alpha_gradient_filter)
  297. {
  298. // Also lossy rgb + lossless alpha, but with a gradient alpha filtering method.
  299. // The image should look like smolkling.webp, but with a few transparent pixels in the shape of a C on it. Most of the image should not be transparent.
  300. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/smolkling-gradient-alpha.webp"sv)));
  301. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  302. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  303. auto frame = expect_single_frame_of_size(*plugin_decoder, { 264, 264 });
  304. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  305. // So pixels changing by 1 or so below is fine if you change code.
  306. // The important component in this test is alpha, and that shouldn't change even by 1 as it's losslessly compressed and doesn't use YUV.
  307. // In particular, the center of the image should be fully opaque, not fully transparent.
  308. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x8c, 0x47, 0x2e, 255));
  309. }
  310. TEST_CASE(test_webp_extended_lossy_uncompressed_alpha)
  311. {
  312. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/extended-lossy-uncompressed-alpha.webp"sv)));
  313. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  314. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  315. auto frame = expect_single_frame_of_size(*plugin_decoder, { 417, 223 });
  316. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  317. // So pixels changing by 1 or so below is fine if you change code.
  318. EXPECT_EQ(frame.image->get_pixel(89, 72), Gfx::Color(255, 0, 4, 255));
  319. EXPECT_EQ(frame.image->get_pixel(174, 69), Gfx::Color(4, 255, 0, 255));
  320. EXPECT_EQ(frame.image->get_pixel(245, 84), Gfx::Color(0, 0, 255, 255));
  321. EXPECT_EQ(frame.image->get_pixel(352, 125), Gfx::Color(0, 0, 0, 128));
  322. EXPECT_EQ(frame.image->get_pixel(355, 106), Gfx::Color(0, 0, 0, 0));
  323. }
  324. TEST_CASE(test_webp_extended_lossy_negative_quantization_offset)
  325. {
  326. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/smolkling.webp"sv)));
  327. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  328. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  329. auto frame = expect_single_frame_of_size(*plugin_decoder, { 264, 264 });
  330. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  331. // So pixels changing by 1 or so below is fine if you change code.
  332. EXPECT_EQ(frame.image->get_pixel(16, 16), Gfx::Color(0x3c, 0x24, 0x1a, 255));
  333. }
  334. TEST_CASE(test_webp_lossy_4)
  335. {
  336. // This is https://commons.wikimedia.org/wiki/File:Fr%C3%BChling_bl%C3%BChender_Kirschenbaum.jpg,
  337. // under the Creative Commons Attribution-Share Alike 3.0 Unported license. The image was re-encoded
  338. // as webp at https://developers.google.com/speed/webp/gallery1 and the webp version is from there.
  339. // No other changes have been made.
  340. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/4.webp"sv)));
  341. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  342. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  343. auto frame = expect_single_frame_of_size(*plugin_decoder, { 1024, 772 });
  344. // This image tests macroblocks that have `skip_coefficients` set to true, and it test a boolean entropy decoder edge case.
  345. EXPECT_EQ(frame.image->get_pixel(780, 570), Gfx::Color(0x72, 0xc8, 0xf6, 255));
  346. }
  347. TEST_CASE(test_webp_lossy_4_with_partitions)
  348. {
  349. // Same input file as in the previous test, but re-encoded to use 8 secondary partitions.
  350. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/4-with-8-partitions.webp"sv)));
  351. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  352. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  353. auto frame = expect_single_frame_of_size(*plugin_decoder, { 1024, 772 });
  354. EXPECT_EQ(frame.image->get_pixel(780, 570), Gfx::Color(0x73, 0xc9, 0xf9, 255));
  355. }
  356. TEST_CASE(test_webp_extended_lossless)
  357. {
  358. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/extended-lossless.webp"sv)));
  359. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  360. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  361. auto frame = expect_single_frame_of_size(*plugin_decoder, { 417, 223 });
  362. // Check some basic pixels.
  363. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0));
  364. EXPECT_EQ(frame.image->get_pixel(43, 75), Gfx::Color(255, 0, 0, 255));
  365. EXPECT_EQ(frame.image->get_pixel(141, 75), Gfx::Color(0, 255, 0, 255));
  366. EXPECT_EQ(frame.image->get_pixel(235, 75), Gfx::Color(0, 0, 255, 255));
  367. EXPECT_EQ(frame.image->get_pixel(341, 75), Gfx::Color(0, 0, 0, 128));
  368. // Check pixels using the color cache.
  369. EXPECT_EQ(frame.image->get_pixel(94, 73), Gfx::Color(255, 0, 0, 255));
  370. EXPECT_EQ(frame.image->get_pixel(176, 115), Gfx::Color(0, 255, 0, 255));
  371. EXPECT_EQ(frame.image->get_pixel(290, 89), Gfx::Color(0, 0, 255, 255));
  372. EXPECT_EQ(frame.image->get_pixel(359, 73), Gfx::Color(0, 0, 0, 128));
  373. }
  374. TEST_CASE(test_webp_simple_lossless_color_index_transform)
  375. {
  376. // In addition to testing the index transform, this file also tests handling of explicity setting max_symbol.
  377. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/Qpalette.webp"sv)));
  378. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  379. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  380. auto frame = expect_single_frame_of_size(*plugin_decoder, { 256, 256 });
  381. EXPECT_EQ(frame.image->get_pixel(100, 100), Gfx::Color(0x73, 0x37, 0x23, 0xff));
  382. }
  383. TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling)
  384. {
  385. struct TestCase {
  386. StringView file_name;
  387. Gfx::Color line_color;
  388. Gfx::Color background_color;
  389. };
  390. // The number after the dash is the number of colors in each file's color index bitmap.
  391. // catdog-alert-2 tests the 1-bit-per-pixel case,
  392. // catdog-alert-3 tests the 2-bit-per-pixel case,
  393. // catdog-alert-8 and catdog-alert-13 both test the 4-bits-per-pixel case.
  394. // catdog-alert-13-alpha-used-false is like catdog-alert-13, but with is_alpha_used set to false in the header
  395. // (which has the effect of ignoring the alpha information in the palette and instead always setting alpha to 0xff).
  396. TestCase test_cases[] = {
  397. { "webp/catdog-alert-2.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0xf3, 0xe6, 0xd8, 0xff) },
  398. { "webp/catdog-alert-3.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0, 0, 0, 0) },
  399. { "webp/catdog-alert-8.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  400. { "webp/catdog-alert-13.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  401. { "webp/catdog-alert-13-alpha-used-false.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 255) },
  402. };
  403. for (auto test_case : test_cases) {
  404. auto file = MUST(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), test_case.file_name))));
  405. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  406. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  407. auto frame = expect_single_frame_of_size(*plugin_decoder, { 32, 32 });
  408. EXPECT_EQ(frame.image->get_pixel(4, 0), test_case.background_color);
  409. EXPECT_EQ(frame.image->get_pixel(5, 0), test_case.line_color);
  410. EXPECT_EQ(frame.image->get_pixel(9, 5), test_case.background_color);
  411. EXPECT_EQ(frame.image->get_pixel(10, 5), test_case.line_color);
  412. EXPECT_EQ(frame.image->get_pixel(11, 5), test_case.background_color);
  413. }
  414. }
  415. TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling_odd_width)
  416. {
  417. StringView file_names[] = {
  418. "webp/width11-height11-colors2.webp"sv,
  419. "webp/width11-height11-colors3.webp"sv,
  420. "webp/width11-height11-colors15.webp"sv,
  421. };
  422. for (auto file_name : file_names) {
  423. auto file = MUST(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), file_name))));
  424. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  425. expect_single_frame_of_size(*plugin_decoder, { 11, 11 });
  426. }
  427. }
  428. TEST_CASE(test_webp_extended_lossless_animated)
  429. {
  430. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/extended-lossless-animated.webp"sv)));
  431. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  432. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  433. EXPECT_EQ(plugin_decoder->frame_count(), 8u);
  434. EXPECT(plugin_decoder->is_animated());
  435. EXPECT_EQ(plugin_decoder->loop_count(), 42u);
  436. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(990, 1050));
  437. for (size_t frame_index = 0; frame_index < plugin_decoder->frame_count(); ++frame_index) {
  438. auto frame = MUST(plugin_decoder->frame(frame_index));
  439. EXPECT_EQ(frame.image->size(), Gfx::IntSize(990, 1050));
  440. // This pixel happens to be the same color in all frames.
  441. EXPECT_EQ(frame.image->get_pixel(500, 700), Gfx::Color::Yellow);
  442. // This one isn't the same in all frames.
  443. EXPECT_EQ(frame.image->get_pixel(500, 0), (frame_index == 2 || frame_index == 6) ? Gfx::Color::Black : Gfx::Color(255, 255, 255, 0));
  444. }
  445. }
  446. TEST_CASE(test_tvg)
  447. {
  448. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tvg/yak.tvg"sv)));
  449. EXPECT(Gfx::TinyVGImageDecoderPlugin::sniff(file->bytes()));
  450. auto plugin_decoder = MUST(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  451. expect_single_frame_of_size(*plugin_decoder, { 1024, 1024 });
  452. }
  453. TEST_CASE(test_everything_tvg)
  454. {
  455. Array file_names {
  456. TEST_INPUT("tvg/everything.tvg"sv),
  457. TEST_INPUT("tvg/everything-32.tvg"sv)
  458. };
  459. for (auto file_name : file_names) {
  460. auto file = MUST(Core::MappedFile::map(file_name));
  461. EXPECT(Gfx::TinyVGImageDecoderPlugin::sniff(file->bytes()));
  462. auto plugin_decoder = MUST(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  463. expect_single_frame_of_size(*plugin_decoder, { 400, 768 });
  464. }
  465. }
  466. TEST_CASE(test_jxl_modular_simple_tree_upsample2_10bits)
  467. {
  468. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jxl/modular_simple_tree_upsample2_10bits_rct.jxl"sv)));
  469. EXPECT(Gfx::JPEGXLImageDecoderPlugin::sniff(file->bytes()));
  470. auto plugin_decoder = MUST(Gfx::JPEGXLImageDecoderPlugin::create(file->bytes()));
  471. expect_single_frame_of_size(*plugin_decoder, { 128, 128 });
  472. auto frame = MUST(plugin_decoder->frame(0));
  473. EXPECT_EQ(frame.image->get_pixel(42, 57), Gfx::Color::from_string("#4c0072"sv));
  474. }
  475. TEST_CASE(test_jxl_modular_property_8)
  476. {
  477. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jxl/modular_property_8.jxl"sv)));
  478. EXPECT(Gfx::JPEGXLImageDecoderPlugin::sniff(file->bytes()));
  479. auto plugin_decoder = MUST(Gfx::JPEGXLImageDecoderPlugin::create(file->bytes()));
  480. expect_single_frame_of_size(*plugin_decoder, { 32, 32 });
  481. auto frame = MUST(plugin_decoder->frame(0));
  482. for (u8 i = 0; i < 32; ++i) {
  483. for (u8 j = 0; j < 32; ++j) {
  484. auto const color = frame.image->get_pixel(i, j);
  485. if ((i + j) % 2 == 0)
  486. EXPECT_EQ(color, Gfx::Color::Black);
  487. else
  488. EXPECT_EQ(color, Gfx::Color::Yellow);
  489. }
  490. }
  491. }