TestImageDecoder.cpp 26 KB

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