TestImageDecoder.cpp 24 KB

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