TestImageDecoder.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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/ByteString.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 ErrorOr<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 = TRY(plugin_decoder.frame(0));
  39. EXPECT_EQ(frame.duration, 0);
  40. return frame;
  41. }
  42. static ErrorOr<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 = TRY(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 = TRY_OR_FAIL(Gfx::BMPImageDecoderPlugin::create(file->bytes()));
  54. TRY_OR_FAIL(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 = TRY_OR_FAIL(Gfx::BMPImageDecoderPlugin::create(file->bytes()));
  61. TRY_OR_FAIL(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 = TRY_OR_FAIL(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_gif_without_global_color_table)
  89. {
  90. Array<u8, 35> gif_data {
  91. // Header (6 bytes): "GIF89a"
  92. 0x47,
  93. 0x49,
  94. 0x46,
  95. 0x38,
  96. 0x39,
  97. 0x61,
  98. // Logical Screen Descriptor (7 bytes)
  99. 0x01,
  100. 0x00, // Width (1)
  101. 0x01,
  102. 0x00, // Height (1)
  103. 0x00, // Packed fields (NOTE: the MSB here is the Global Color Table flag!)
  104. 0x00, // Background Color Index
  105. 0x00, // Pixel Aspect Ratio
  106. // Image Descriptor (10 bytes)
  107. 0x2C,
  108. 0x00,
  109. 0x00,
  110. 0x00,
  111. 0x00,
  112. 0x01,
  113. 0x00,
  114. 0x01,
  115. 0x00,
  116. 0x80,
  117. // Local Color Table (6 bytes: 2 colors, 3 bytes per color)
  118. 0x00,
  119. 0x00,
  120. 0x00, // Color 1: Black (RGB: 0, 0, 0)
  121. 0xff,
  122. 0x00,
  123. 0x00, // Color 2: Red (RGB: 255, 0, 0)
  124. // Image Data (8 bytes)
  125. 0x02, // LZW Minimum Code Size
  126. 0x02, // Data Sub-block size (2 bytes)
  127. 0x4C,
  128. 0x01, // Image Data
  129. 0x00, // Data Sub-block Terminator
  130. // Trailer (1 byte)
  131. 0x3B,
  132. };
  133. auto plugin_decoder = TRY_OR_FAIL(Gfx::GIFImageDecoderPlugin::create(gif_data));
  134. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  135. auto frame = TRY_OR_FAIL(plugin_decoder->frame(0));
  136. EXPECT(frame.image);
  137. EXPECT_EQ(frame.image->size(), Gfx::IntSize(1, 1));
  138. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::Red);
  139. }
  140. TEST_CASE(test_not_ico)
  141. {
  142. auto file = MUST(Core::MappedFile::map(TEST_INPUT("png/buggie.png"sv)));
  143. EXPECT(!Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
  144. EXPECT(Gfx::ICOImageDecoderPlugin::create(file->bytes()).is_error());
  145. }
  146. TEST_CASE(test_bmp_embedded_in_ico)
  147. {
  148. auto file = MUST(Core::MappedFile::map(TEST_INPUT("serenity.ico"sv)));
  149. EXPECT(Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
  150. auto plugin_decoder = TRY_OR_FAIL(Gfx::ICOImageDecoderPlugin::create(file->bytes()));
  151. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  152. }
  153. TEST_CASE(test_ilbm)
  154. {
  155. auto file = MUST(Core::MappedFile::map(TEST_INPUT("ilbm/gradient.iff"sv)));
  156. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  157. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  158. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 200 }));
  159. EXPECT_EQ(frame.image->get_pixel(8, 0), Gfx::Color(0xee, 0xbb, 0, 255));
  160. }
  161. TEST_CASE(test_ilbm_uncompressed)
  162. {
  163. auto file = MUST(Core::MappedFile::map(TEST_INPUT("ilbm/gradient-uncompressed.iff"sv)));
  164. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  165. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  166. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 200 }));
  167. EXPECT_EQ(frame.image->get_pixel(8, 0), Gfx::Color(0xee, 0xbb, 0, 255));
  168. }
  169. TEST_CASE(test_ilbm_ham6)
  170. {
  171. auto file = MUST(Core::MappedFile::map(TEST_INPUT("ilbm/ham6.iff"sv)));
  172. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  173. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  174. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 256, 256 }));
  175. EXPECT_EQ(frame.image->get_pixel(77, 107), Gfx::Color(0xf0, 0x40, 0x40, 0xff));
  176. }
  177. TEST_CASE(test_ilbm_dos)
  178. {
  179. auto file = MUST(Core::MappedFile::map(TEST_INPUT("ilbm/serenity.lbm"sv)));
  180. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  181. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  182. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 640, 480 }));
  183. EXPECT_EQ(frame.image->get_pixel(315, 134), Gfx::Color::NamedColor::Red);
  184. }
  185. TEST_CASE(test_24bit)
  186. {
  187. auto file = MUST(Core::MappedFile::map(TEST_INPUT("ilbm/serenity-24bit.iff"sv)));
  188. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  189. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  190. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 640, 640 }));
  191. EXPECT_EQ(frame.image->get_pixel(158, 270), Gfx::Color(0xee, 0x3d, 0x3c, 255));
  192. }
  193. TEST_CASE(test_ilbm_malformed_header)
  194. {
  195. Array test_inputs = {
  196. TEST_INPUT("ilbm/truncated-bmhd-chunk.iff"sv)
  197. };
  198. for (auto test_input : test_inputs) {
  199. auto file = MUST(Core::MappedFile::map(test_input));
  200. auto plugin_decoder_or_error = Gfx::ILBMImageDecoderPlugin::create(file->bytes());
  201. EXPECT(plugin_decoder_or_error.is_error());
  202. }
  203. }
  204. TEST_CASE(test_ilbm_malformed_frame)
  205. {
  206. Array test_inputs = {
  207. TEST_INPUT("ilbm/incorrect-cmap-size.iff"sv),
  208. TEST_INPUT("ilbm/incorrect-uncompressed-size.iff"sv),
  209. TEST_INPUT("ilbm/missing-body-chunk.iff"sv)
  210. };
  211. for (auto test_input : test_inputs) {
  212. auto file = MUST(Core::MappedFile::map(test_input));
  213. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  214. auto frame_or_error = plugin_decoder->frame(0);
  215. EXPECT(frame_or_error.is_error());
  216. }
  217. }
  218. TEST_CASE(test_jpeg_sof0_one_scan)
  219. {
  220. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/rgb24.jpg"sv)));
  221. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  222. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  223. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  224. }
  225. TEST_CASE(test_jpeg_sof0_several_scans)
  226. {
  227. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/several_scans.jpg"sv)));
  228. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  229. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  230. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  231. }
  232. TEST_CASE(test_jpeg_rgb_components)
  233. {
  234. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/rgb_components.jpg"sv)));
  235. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  236. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  237. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  238. }
  239. TEST_CASE(test_jpeg_ycck)
  240. {
  241. Array test_inputs = {
  242. TEST_INPUT("jpg/ycck-1111.jpg"sv),
  243. // TEST_INPUT("jpg/ycck-2111.jpg"sv), // FIXME: Enable once this decodes correctly
  244. TEST_INPUT("jpg/ycck-2112.jpg"sv),
  245. };
  246. for (auto test_input : test_inputs) {
  247. auto file = MUST(Core::MappedFile::map(test_input));
  248. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  249. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  250. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  251. // Compare difference between pixels so we don't depend on exact CMYK->RGB conversion behavior.
  252. // These two pixels are currently off by one in R.
  253. // FIXME: For 2111, they're off by way more.
  254. EXPECT(frame.image->get_pixel(6, 319).distance_squared_to(frame.image->get_pixel(6, 320)) < 1.0f / 255.0f);
  255. }
  256. }
  257. TEST_CASE(test_jpeg_sof2_spectral_selection)
  258. {
  259. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/spectral_selection.jpg"sv)));
  260. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  261. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  262. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  263. }
  264. TEST_CASE(test_jpeg_sof0_several_scans_odd_number_mcu)
  265. {
  266. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/several_scans_odd_number_mcu.jpg"sv)));
  267. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  268. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  269. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 600, 600 }));
  270. }
  271. TEST_CASE(test_jpeg_sof2_successive_aproximation)
  272. {
  273. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/successive_approximation.jpg"sv)));
  274. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  275. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  276. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 600, 800 }));
  277. }
  278. TEST_CASE(test_jpeg_sof1_12bits)
  279. {
  280. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/12-bit.jpg"sv)));
  281. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  282. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  283. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 240 }));
  284. }
  285. TEST_CASE(test_jpeg_sof2_12bits)
  286. {
  287. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/12-bit-progressive.jpg"sv)));
  288. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  289. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  290. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 240 }));
  291. }
  292. TEST_CASE(test_jpeg_empty_icc)
  293. {
  294. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/gradient_empty_icc.jpg"sv)));
  295. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  296. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  297. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 80, 80 }));
  298. }
  299. TEST_CASE(test_jpeg_grayscale_with_app14)
  300. {
  301. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/grayscale_app14.jpg"sv)));
  302. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  303. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  304. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 80, 80 }));
  305. }
  306. TEST_CASE(test_jpeg_malformed_header)
  307. {
  308. Array test_inputs = {
  309. TEST_INPUT("jpg/oss-fuzz-testcase-59785.jpg"sv)
  310. };
  311. for (auto test_input : test_inputs) {
  312. auto file = MUST(Core::MappedFile::map(test_input));
  313. auto plugin_decoder_or_error = Gfx::JPEGImageDecoderPlugin::create(file->bytes());
  314. EXPECT(plugin_decoder_or_error.is_error());
  315. }
  316. }
  317. TEST_CASE(test_jpeg_malformed_frame)
  318. {
  319. Array test_inputs = {
  320. TEST_INPUT("jpg/oss-fuzz-testcase-62584.jpg"sv),
  321. TEST_INPUT("jpg/oss-fuzz-testcase-63815.jpg"sv)
  322. };
  323. for (auto test_input : test_inputs) {
  324. auto file = MUST(Core::MappedFile::map(test_input));
  325. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  326. auto frame_or_error = plugin_decoder->frame(0);
  327. EXPECT(frame_or_error.is_error());
  328. }
  329. }
  330. TEST_CASE(test_pbm)
  331. {
  332. auto file = MUST(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.pbm"sv)));
  333. EXPECT(Gfx::PBMImageDecoderPlugin::sniff(file->bytes()));
  334. auto plugin_decoder = TRY_OR_FAIL(Gfx::PBMImageDecoderPlugin::create(file->bytes()));
  335. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  336. }
  337. TEST_CASE(test_pgm)
  338. {
  339. auto file = MUST(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.pgm"sv)));
  340. EXPECT(Gfx::PGMImageDecoderPlugin::sniff(file->bytes()));
  341. auto plugin_decoder = TRY_OR_FAIL(Gfx::PGMImageDecoderPlugin::create(file->bytes()));
  342. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  343. }
  344. TEST_CASE(test_png)
  345. {
  346. auto file = MUST(Core::MappedFile::map(TEST_INPUT("png/buggie.png"sv)));
  347. EXPECT(Gfx::PNGImageDecoderPlugin::sniff(file->bytes()));
  348. auto plugin_decoder = TRY_OR_FAIL(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
  349. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  350. }
  351. TEST_CASE(test_png_malformed_frame)
  352. {
  353. Array test_inputs = {
  354. TEST_INPUT("png/oss-fuzz-testcase-62371.png"sv),
  355. TEST_INPUT("png/oss-fuzz-testcase-63052.png"sv)
  356. };
  357. for (auto test_input : test_inputs) {
  358. auto file = MUST(Core::MappedFile::map(test_input));
  359. auto plugin_decoder = TRY_OR_FAIL(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
  360. auto frame_or_error = plugin_decoder->frame(0);
  361. EXPECT(frame_or_error.is_error());
  362. }
  363. }
  364. TEST_CASE(test_ppm)
  365. {
  366. auto file = MUST(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.ppm"sv)));
  367. EXPECT(Gfx::PPMImageDecoderPlugin::sniff(file->bytes()));
  368. auto plugin_decoder = TRY_OR_FAIL(Gfx::PPMImageDecoderPlugin::create(file->bytes()));
  369. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  370. }
  371. TEST_CASE(test_targa_bottom_left)
  372. {
  373. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-uncompressed.tga"sv)));
  374. EXPECT(TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  375. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  376. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  377. }
  378. TEST_CASE(test_targa_top_left)
  379. {
  380. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-uncompressed.tga"sv)));
  381. EXPECT(TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  382. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  383. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  384. }
  385. TEST_CASE(test_targa_bottom_left_compressed)
  386. {
  387. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-compressed.tga"sv)));
  388. EXPECT(TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  389. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  390. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  391. }
  392. TEST_CASE(test_targa_top_left_compressed)
  393. {
  394. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-compressed.tga"sv)));
  395. EXPECT(TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  396. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  397. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  398. }
  399. TEST_CASE(test_tiff_uncompressed)
  400. {
  401. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/uncompressed.tiff"sv)));
  402. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  403. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  404. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  405. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  406. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  407. }
  408. TEST_CASE(test_tiff_ccitt3_1d)
  409. {
  410. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/ccitt3_1d.tiff"sv)));
  411. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  412. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  413. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  414. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  415. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Black);
  416. }
  417. TEST_CASE(test_tiff_lzw)
  418. {
  419. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/lzw.tiff"sv)));
  420. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  421. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  422. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  423. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  424. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  425. }
  426. TEST_CASE(test_tiff_defalte)
  427. {
  428. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/deflate.tiff"sv)));
  429. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  430. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  431. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  432. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  433. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  434. }
  435. TEST_CASE(test_tiff_packed_bits)
  436. {
  437. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/packed_bits.tiff"sv)));
  438. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  439. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  440. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  441. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  442. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  443. }
  444. TEST_CASE(test_tiff_grayscale)
  445. {
  446. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/grayscale.tiff"sv)));
  447. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  448. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  449. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  450. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  451. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color(130, 130, 130));
  452. }
  453. TEST_CASE(test_tiff_grayscale_alpha)
  454. {
  455. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/grayscale_alpha.tiff"sv)));
  456. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  457. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  458. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  459. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 0);
  460. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color(130, 130, 130));
  461. }
  462. TEST_CASE(test_tiff_rgb_alpha)
  463. {
  464. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/rgb_alpha.tiff"sv)));
  465. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  466. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  467. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  468. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 0);
  469. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  470. }
  471. TEST_CASE(test_tiff_palette_alpha)
  472. {
  473. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/rgb_palette_alpha.tiff"sv)));
  474. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  475. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  476. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  477. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 0);
  478. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  479. }
  480. TEST_CASE(test_tiff_16_bits)
  481. {
  482. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/16_bits.tiff"sv)));
  483. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  484. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  485. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  486. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  487. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  488. }
  489. TEST_CASE(test_tiff_invalid_tag)
  490. {
  491. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/invalid_tag.tiff"sv)));
  492. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  493. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  494. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 10, 10 }));
  495. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::Black);
  496. EXPECT_EQ(frame.image->get_pixel(0, 9), Gfx::Color::NamedColor::White);
  497. }
  498. TEST_CASE(test_webp_simple_lossy)
  499. {
  500. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8.webp"sv)));
  501. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  502. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  503. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 240, 240 }));
  504. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  505. // So pixels changing by 1 or so below is fine if you change code.
  506. EXPECT_EQ(frame.image->get_pixel(120, 232), Gfx::Color(0xf2, 0xef, 0xf0, 255));
  507. EXPECT_EQ(frame.image->get_pixel(198, 202), Gfx::Color(0x7b, 0xaa, 0xd5, 255));
  508. }
  509. TEST_CASE(test_webp_simple_lossless)
  510. {
  511. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8l.webp"sv)));
  512. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  513. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  514. // Ironically, simple-vp8l.webp is a much more complex file than extended-lossless.webp tested below.
  515. // extended-lossless.webp tests the decoding basics.
  516. // This here tests the predictor, color, and subtract green transforms,
  517. // as well as meta prefix images, one-element canonical code handling,
  518. // and handling of canonical codes with more than 288 elements.
  519. // This image uses all 13 predictor modes of the predictor transform.
  520. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 386, 395 }));
  521. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0));
  522. // This pixel tests all predictor modes except 5, 7, 8, 9, and 13.
  523. EXPECT_EQ(frame.image->get_pixel(289, 332), Gfx::Color(0xf2, 0xee, 0xd3, 255));
  524. }
  525. TEST_CASE(test_webp_simple_lossless_alpha_used_false)
  526. {
  527. // This file is identical to simple-vp8l.webp, but the `is_alpha_used` used bit is false.
  528. // The file still contains alpha data. This tests that the decoder replaces the stored alpha data with 0xff if `is_alpha_used` is false.
  529. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8l-alpha-used-false.webp"sv)));
  530. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  531. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  532. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 386, 395 }));
  533. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0xff));
  534. }
  535. TEST_CASE(test_webp_extended_lossy)
  536. {
  537. // This extended lossy image has an ALPH chunk for (losslessly compressed) alpha data.
  538. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/extended-lossy.webp"sv)));
  539. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  540. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  541. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 417, 223 }));
  542. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  543. // So pixels changing by 1 or so below is fine if you change code.
  544. EXPECT_EQ(frame.image->get_pixel(89, 72), Gfx::Color(255, 1, 0, 255));
  545. EXPECT_EQ(frame.image->get_pixel(174, 69), Gfx::Color(0, 255, 0, 255));
  546. EXPECT_EQ(frame.image->get_pixel(245, 84), Gfx::Color(0, 0, 255, 255));
  547. EXPECT_EQ(frame.image->get_pixel(352, 125), Gfx::Color(0, 0, 0, 128));
  548. EXPECT_EQ(frame.image->get_pixel(355, 106), Gfx::Color(0, 0, 0, 0));
  549. // Check same basic pixels as in test_webp_extended_lossless too.
  550. // (The top-left pixel in the lossy version is fully transparent white, compared to fully transparent black in the lossless version).
  551. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(255, 255, 255, 0));
  552. EXPECT_EQ(frame.image->get_pixel(43, 75), Gfx::Color(255, 0, 2, 255));
  553. EXPECT_EQ(frame.image->get_pixel(141, 75), Gfx::Color(0, 255, 3, 255));
  554. EXPECT_EQ(frame.image->get_pixel(235, 75), Gfx::Color(0, 0, 255, 255));
  555. EXPECT_EQ(frame.image->get_pixel(341, 75), Gfx::Color(0, 0, 0, 128));
  556. }
  557. TEST_CASE(test_webp_extended_lossy_alpha_horizontal_filter)
  558. {
  559. // Also lossy rgb + lossless alpha, but with a horizontal alpha filtering method.
  560. // The image should look like smolkling.webp, but with a horizontal alpha gradient.
  561. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/smolkling-horizontal-alpha.webp"sv)));
  562. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  563. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  564. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  565. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  566. // So pixels changing by 1 or so below is fine if you change code.
  567. // 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.
  568. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x8f, 0x51, 0x2f, 0x4b));
  569. }
  570. TEST_CASE(test_webp_extended_lossy_alpha_vertical_filter)
  571. {
  572. // Also lossy rgb + lossless alpha, but with a vertical alpha filtering method.
  573. // The image should look like smolkling.webp, but with a vertical alpha gradient, and with a fully transparent first column.
  574. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/smolkling-vertical-alpha.webp"sv)));
  575. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  576. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  577. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  578. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  579. // So pixels changing by 1 or so below is fine if you change code.
  580. // 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.
  581. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x94, 0x50, 0x32, 0x4c));
  582. }
  583. TEST_CASE(test_webp_extended_lossy_alpha_gradient_filter)
  584. {
  585. // Also lossy rgb + lossless alpha, but with a gradient alpha filtering method.
  586. // 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.
  587. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/smolkling-gradient-alpha.webp"sv)));
  588. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  589. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  590. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  591. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  592. // So pixels changing by 1 or so below is fine if you change code.
  593. // 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.
  594. // In particular, the center of the image should be fully opaque, not fully transparent.
  595. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x8c, 0x47, 0x2e, 255));
  596. }
  597. TEST_CASE(test_webp_extended_lossy_uncompressed_alpha)
  598. {
  599. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/extended-lossy-uncompressed-alpha.webp"sv)));
  600. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  601. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  602. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 417, 223 }));
  603. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  604. // So pixels changing by 1 or so below is fine if you change code.
  605. EXPECT_EQ(frame.image->get_pixel(89, 72), Gfx::Color(255, 0, 4, 255));
  606. EXPECT_EQ(frame.image->get_pixel(174, 69), Gfx::Color(4, 255, 0, 255));
  607. EXPECT_EQ(frame.image->get_pixel(245, 84), Gfx::Color(0, 0, 255, 255));
  608. EXPECT_EQ(frame.image->get_pixel(352, 125), Gfx::Color(0, 0, 0, 128));
  609. EXPECT_EQ(frame.image->get_pixel(355, 106), Gfx::Color(0, 0, 0, 0));
  610. }
  611. TEST_CASE(test_webp_extended_lossy_negative_quantization_offset)
  612. {
  613. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/smolkling.webp"sv)));
  614. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  615. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  616. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  617. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  618. // So pixels changing by 1 or so below is fine if you change code.
  619. EXPECT_EQ(frame.image->get_pixel(16, 16), Gfx::Color(0x3c, 0x24, 0x1a, 255));
  620. }
  621. TEST_CASE(test_webp_lossy_4)
  622. {
  623. // This is https://commons.wikimedia.org/wiki/File:Fr%C3%BChling_bl%C3%BChender_Kirschenbaum.jpg,
  624. // under the Creative Commons Attribution-Share Alike 3.0 Unported license. The image was re-encoded
  625. // as webp at https://developers.google.com/speed/webp/gallery1 and the webp version is from there.
  626. // No other changes have been made.
  627. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/4.webp"sv)));
  628. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  629. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  630. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1024, 772 }));
  631. // This image tests macroblocks that have `skip_coefficients` set to true, and it test a boolean entropy decoder edge case.
  632. EXPECT_EQ(frame.image->get_pixel(780, 570), Gfx::Color(0x72, 0xc8, 0xf6, 255));
  633. }
  634. TEST_CASE(test_webp_lossy_4_with_partitions)
  635. {
  636. // Same input file as in the previous test, but re-encoded to use 8 secondary partitions.
  637. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/4-with-8-partitions.webp"sv)));
  638. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  639. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  640. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1024, 772 }));
  641. EXPECT_EQ(frame.image->get_pixel(780, 570), Gfx::Color(0x73, 0xc9, 0xf9, 255));
  642. }
  643. TEST_CASE(test_webp_extended_lossless)
  644. {
  645. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/extended-lossless.webp"sv)));
  646. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  647. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  648. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 417, 223 }));
  649. // Check some basic pixels.
  650. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0));
  651. EXPECT_EQ(frame.image->get_pixel(43, 75), Gfx::Color(255, 0, 0, 255));
  652. EXPECT_EQ(frame.image->get_pixel(141, 75), Gfx::Color(0, 255, 0, 255));
  653. EXPECT_EQ(frame.image->get_pixel(235, 75), Gfx::Color(0, 0, 255, 255));
  654. EXPECT_EQ(frame.image->get_pixel(341, 75), Gfx::Color(0, 0, 0, 128));
  655. // Check pixels using the color cache.
  656. EXPECT_EQ(frame.image->get_pixel(94, 73), Gfx::Color(255, 0, 0, 255));
  657. EXPECT_EQ(frame.image->get_pixel(176, 115), Gfx::Color(0, 255, 0, 255));
  658. EXPECT_EQ(frame.image->get_pixel(290, 89), Gfx::Color(0, 0, 255, 255));
  659. EXPECT_EQ(frame.image->get_pixel(359, 73), Gfx::Color(0, 0, 0, 128));
  660. }
  661. TEST_CASE(test_webp_simple_lossless_color_index_transform)
  662. {
  663. // In addition to testing the index transform, this file also tests handling of explicity setting max_symbol.
  664. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/Qpalette.webp"sv)));
  665. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  666. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  667. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 256, 256 }));
  668. EXPECT_EQ(frame.image->get_pixel(100, 100), Gfx::Color(0x73, 0x37, 0x23, 0xff));
  669. }
  670. TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling)
  671. {
  672. struct TestCase {
  673. StringView file_name;
  674. Gfx::Color line_color;
  675. Gfx::Color background_color;
  676. };
  677. // The number after the dash is the number of colors in each file's color index bitmap.
  678. // catdog-alert-2 tests the 1-bit-per-pixel case,
  679. // catdog-alert-3 tests the 2-bit-per-pixel case,
  680. // catdog-alert-8 and catdog-alert-13 both test the 4-bits-per-pixel case.
  681. // catdog-alert-13-alpha-used-false is like catdog-alert-13, but with is_alpha_used set to false in the header
  682. // (which has the effect of ignoring the alpha information in the palette and instead always setting alpha to 0xff).
  683. TestCase test_cases[] = {
  684. { "webp/catdog-alert-2.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0xf3, 0xe6, 0xd8, 0xff) },
  685. { "webp/catdog-alert-3.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0, 0, 0, 0) },
  686. { "webp/catdog-alert-8.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  687. { "webp/catdog-alert-13.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  688. { "webp/catdog-alert-13-alpha-used-false.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 255) },
  689. };
  690. for (auto test_case : test_cases) {
  691. auto file = MUST(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), test_case.file_name))));
  692. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  693. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  694. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 32, 32 }));
  695. EXPECT_EQ(frame.image->get_pixel(4, 0), test_case.background_color);
  696. EXPECT_EQ(frame.image->get_pixel(5, 0), test_case.line_color);
  697. EXPECT_EQ(frame.image->get_pixel(9, 5), test_case.background_color);
  698. EXPECT_EQ(frame.image->get_pixel(10, 5), test_case.line_color);
  699. EXPECT_EQ(frame.image->get_pixel(11, 5), test_case.background_color);
  700. }
  701. }
  702. TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling_odd_width)
  703. {
  704. StringView file_names[] = {
  705. "webp/width11-height11-colors2.webp"sv,
  706. "webp/width11-height11-colors3.webp"sv,
  707. "webp/width11-height11-colors15.webp"sv,
  708. };
  709. for (auto file_name : file_names) {
  710. auto file = MUST(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), file_name))));
  711. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  712. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 11, 11 }));
  713. }
  714. }
  715. TEST_CASE(test_webp_extended_lossless_animated)
  716. {
  717. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/extended-lossless-animated.webp"sv)));
  718. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  719. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  720. EXPECT_EQ(plugin_decoder->frame_count(), 8u);
  721. EXPECT(plugin_decoder->is_animated());
  722. EXPECT_EQ(plugin_decoder->loop_count(), 42u);
  723. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(990, 1050));
  724. for (size_t frame_index = 0; frame_index < plugin_decoder->frame_count(); ++frame_index) {
  725. auto frame = MUST(plugin_decoder->frame(frame_index));
  726. EXPECT_EQ(frame.image->size(), Gfx::IntSize(990, 1050));
  727. // This pixel happens to be the same color in all frames.
  728. EXPECT_EQ(frame.image->get_pixel(500, 700), Gfx::Color::Yellow);
  729. // This one isn't the same in all frames.
  730. EXPECT_EQ(frame.image->get_pixel(500, 0), (frame_index == 2 || frame_index == 6) ? Gfx::Color::Black : Gfx::Color(255, 255, 255, 0));
  731. }
  732. }
  733. TEST_CASE(test_tvg)
  734. {
  735. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tvg/yak.tvg"sv)));
  736. EXPECT(Gfx::TinyVGImageDecoderPlugin::sniff(file->bytes()));
  737. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  738. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1024, 1024 }));
  739. }
  740. TEST_CASE(test_everything_tvg)
  741. {
  742. Array file_names {
  743. TEST_INPUT("tvg/everything.tvg"sv),
  744. TEST_INPUT("tvg/everything-32.tvg"sv)
  745. };
  746. for (auto file_name : file_names) {
  747. auto file = MUST(Core::MappedFile::map(file_name));
  748. EXPECT(Gfx::TinyVGImageDecoderPlugin::sniff(file->bytes()));
  749. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  750. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 768 }));
  751. }
  752. }
  753. TEST_CASE(test_tvg_malformed)
  754. {
  755. Array test_inputs = {
  756. TEST_INPUT("tvg/bogus-color-table-size.tvg"sv)
  757. };
  758. for (auto test_input : test_inputs) {
  759. auto file = MUST(Core::MappedFile::map(test_input));
  760. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  761. auto frame_or_error = plugin_decoder->frame(0);
  762. EXPECT(frame_or_error.is_error());
  763. }
  764. }
  765. TEST_CASE(test_jxl_modular_simple_tree_upsample2_10bits)
  766. {
  767. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jxl/modular_simple_tree_upsample2_10bits_rct.jxl"sv)));
  768. EXPECT(Gfx::JPEGXLImageDecoderPlugin::sniff(file->bytes()));
  769. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGXLImageDecoderPlugin::create(file->bytes()));
  770. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 128, 128 }));
  771. auto frame = MUST(plugin_decoder->frame(0));
  772. EXPECT_EQ(frame.image->get_pixel(42, 57), Gfx::Color::from_string("#4c0072"sv));
  773. }
  774. TEST_CASE(test_jxl_modular_property_8)
  775. {
  776. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jxl/modular_property_8.jxl"sv)));
  777. EXPECT(Gfx::JPEGXLImageDecoderPlugin::sniff(file->bytes()));
  778. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGXLImageDecoderPlugin::create(file->bytes()));
  779. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 32, 32 }));
  780. auto frame = MUST(plugin_decoder->frame(0));
  781. for (u8 i = 0; i < 32; ++i) {
  782. for (u8 j = 0; j < 32; ++j) {
  783. auto const color = frame.image->get_pixel(i, j);
  784. if ((i + j) % 2 == 0)
  785. EXPECT_EQ(color, Gfx::Color::Black);
  786. else
  787. EXPECT_EQ(color, Gfx::Color::Yellow);
  788. }
  789. }
  790. }
  791. TEST_CASE(test_dds)
  792. {
  793. Array file_names = {
  794. TEST_INPUT("dds/catdog-alert-29x29.dds"sv),
  795. TEST_INPUT("dds/catdog-alert-32x32.dds"sv)
  796. };
  797. for (auto file_name : file_names) {
  798. auto file = MUST(Core::MappedFile::map(file_name));
  799. EXPECT(Gfx::DDSImageDecoderPlugin::sniff(file->bytes()));
  800. auto plugin_decoder = TRY_OR_FAIL(Gfx::DDSImageDecoderPlugin::create(file->bytes()));
  801. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  802. }
  803. }