TestImageDecoder.cpp 54 KB

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