TestImageDecoder.cpp 46 KB

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