TestImageDecoder.cpp 42 KB

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