TestImageDecoder.cpp 26 KB

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