TestImageDecoder.cpp 31 KB

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