TestImageDecoder.cpp 27 KB

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