TestImageDecoder.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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_decode)
  289. {
  290. auto bmp_file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("bmp/bitmap.bmp"sv)));
  291. auto bmp_plugin_decoder = TRY_OR_FAIL(Gfx::BMPImageDecoderPlugin::create(bmp_file->bytes()));
  292. auto bmp_frame = TRY_OR_FAIL(expect_single_frame_of_size(*bmp_plugin_decoder, { 399, 400 }));
  293. Array test_inputs = {
  294. TEST_INPUT("jbig2/bitmap.jbig2"sv),
  295. TEST_INPUT("jbig2/bitmap-tpgdon.jbig2"sv),
  296. TEST_INPUT("jbig2/bitmap-template1.jbig2"sv),
  297. TEST_INPUT("jbig2/bitmap-template1-tpgdon.jbig2"sv),
  298. TEST_INPUT("jbig2/bitmap-template2.jbig2"sv),
  299. TEST_INPUT("jbig2/bitmap-template2-tpgdon.jbig2"sv),
  300. TEST_INPUT("jbig2/bitmap-template3.jbig2"sv),
  301. TEST_INPUT("jbig2/bitmap-template3-tpgdon.jbig2"sv),
  302. TEST_INPUT("jbig2/bitmap-symbol.jbig2"sv),
  303. };
  304. for (auto test_input : test_inputs) {
  305. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  306. EXPECT(Gfx::JBIG2ImageDecoderPlugin::sniff(file->bytes()));
  307. auto plugin_decoder = TRY_OR_FAIL(Gfx::JBIG2ImageDecoderPlugin::create(file->bytes()));
  308. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 399, 400 }));
  309. for (int y = 0; y < frame.image->height(); ++y)
  310. for (int x = 0; x < frame.image->width(); ++x)
  311. EXPECT_EQ(frame.image->get_pixel(x, y), bmp_frame.image->get_pixel(x, y));
  312. }
  313. }
  314. TEST_CASE(test_jbig2_arithmetic_decoder)
  315. {
  316. // https://www.itu.int/rec/T-REC-T.88-201808-I
  317. // H.2 Test sequence for arithmetic coder
  318. // clang-format off
  319. constexpr auto input = to_array<u8>({
  320. 0x84, 0xC7, 0x3B, 0xFC, 0xE1, 0xA1, 0x43, 0x04,
  321. 0x02, 0x20, 0x00, 0x00, 0x41, 0x0D, 0xBB, 0x86,
  322. 0xF4, 0x31, 0x7F, 0xFF, 0x88, 0xFF, 0x37, 0x47,
  323. 0x1A, 0xDB, 0x6A, 0xDF, 0xFF, 0xAC
  324. });
  325. constexpr auto output = to_array<u8>({
  326. 0x00, 0x02, 0x00, 0x51, 0x00, 0x00, 0x00, 0xC0,
  327. 0x03, 0x52, 0x87, 0x2A, 0xAA, 0xAA, 0xAA, 0xAA,
  328. 0x82, 0xC0, 0x20, 0x00, 0xFC, 0xD7, 0x9E, 0xF6,
  329. 0xBF, 0x7F, 0xED, 0x90, 0x4F, 0x46, 0xA3, 0xBF
  330. });
  331. // clang-format on
  332. // "For this entire test, a single value of CX is used. I(CX) is initially 0 and MPS(CX) is initially 0."
  333. Gfx::JBIG2::ArithmeticDecoder::Context context { 0, 0 };
  334. auto decoder = MUST(Gfx::JBIG2::ArithmeticDecoder::initialize(input));
  335. for (auto expected : output) {
  336. u8 actual = 0;
  337. for (size_t i = 0; i < 8; ++i) {
  338. actual <<= 1;
  339. actual |= static_cast<u8>(decoder.get_next_bit(context));
  340. }
  341. EXPECT_EQ(actual, expected);
  342. }
  343. }
  344. TEST_CASE(test_jpeg_sof0_one_scan)
  345. {
  346. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/rgb24.jpg"sv)));
  347. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  348. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  349. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  350. }
  351. TEST_CASE(test_jpeg_sof0_several_scans)
  352. {
  353. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/several_scans.jpg"sv)));
  354. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  355. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  356. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  357. }
  358. TEST_CASE(test_odd_mcu_restart_interval)
  359. {
  360. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/odd-restart.jpg"sv)));
  361. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  362. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  363. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 102, 77 }));
  364. }
  365. TEST_CASE(test_jpeg_rgb_components)
  366. {
  367. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/rgb_components.jpg"sv)));
  368. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  369. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  370. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  371. }
  372. TEST_CASE(test_jpeg_ycck)
  373. {
  374. Array test_inputs = {
  375. TEST_INPUT("jpg/ycck-1111.jpg"sv),
  376. TEST_INPUT("jpg/ycck-2111.jpg"sv),
  377. TEST_INPUT("jpg/ycck-2112.jpg"sv),
  378. };
  379. for (auto test_input : test_inputs) {
  380. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  381. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  382. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  383. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  384. // Compare difference between pixels so we don't depend on exact CMYK->RGB conversion behavior.
  385. // These two pixels are currently off by one in R.
  386. // FIXME: For 2111, they're off by way more.
  387. EXPECT(frame.image->get_pixel(6, 319).distance_squared_to(frame.image->get_pixel(6, 320)) < 1.0f / 255.0f);
  388. }
  389. }
  390. TEST_CASE(test_jpeg_sof2_spectral_selection)
  391. {
  392. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/spectral_selection.jpg"sv)));
  393. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  394. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  395. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  396. }
  397. TEST_CASE(test_jpeg_sof0_several_scans_odd_number_mcu)
  398. {
  399. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/several_scans_odd_number_mcu.jpg"sv)));
  400. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  401. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  402. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 600, 600 }));
  403. }
  404. TEST_CASE(test_jpeg_sof2_successive_aproximation)
  405. {
  406. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/successive_approximation.jpg"sv)));
  407. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  408. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  409. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 600, 800 }));
  410. }
  411. TEST_CASE(test_jpeg_sof1_12bits)
  412. {
  413. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/12-bit.jpg"sv)));
  414. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  415. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  416. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 240 }));
  417. }
  418. TEST_CASE(test_jpeg_sof2_12bits)
  419. {
  420. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/12-bit-progressive.jpg"sv)));
  421. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  422. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  423. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 240 }));
  424. }
  425. TEST_CASE(test_jpeg_empty_icc)
  426. {
  427. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/gradient_empty_icc.jpg"sv)));
  428. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  429. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  430. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 80, 80 }));
  431. }
  432. TEST_CASE(test_jpeg_grayscale_with_app14)
  433. {
  434. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/grayscale_app14.jpg"sv)));
  435. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  436. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  437. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 80, 80 }));
  438. }
  439. TEST_CASE(test_jpeg_grayscale_with_weird_mcu_and_reset_marker)
  440. {
  441. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/grayscale_mcu.jpg"sv)));
  442. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  443. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  444. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 240 }));
  445. }
  446. TEST_CASE(test_jpeg_malformed_header)
  447. {
  448. Array test_inputs = {
  449. TEST_INPUT("jpg/oss-fuzz-testcase-59785.jpg"sv)
  450. };
  451. for (auto test_input : test_inputs) {
  452. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  453. auto plugin_decoder_or_error = Gfx::JPEGImageDecoderPlugin::create(file->bytes());
  454. EXPECT(plugin_decoder_or_error.is_error());
  455. }
  456. }
  457. TEST_CASE(test_jpeg_malformed_frame)
  458. {
  459. Array test_inputs = {
  460. TEST_INPUT("jpg/oss-fuzz-testcase-62584.jpg"sv),
  461. TEST_INPUT("jpg/oss-fuzz-testcase-63815.jpg"sv)
  462. };
  463. for (auto test_input : test_inputs) {
  464. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  465. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  466. auto frame_or_error = plugin_decoder->frame(0);
  467. EXPECT(frame_or_error.is_error());
  468. }
  469. }
  470. TEST_CASE(test_pam_rgb)
  471. {
  472. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("pnm/2x1.pam"sv)));
  473. EXPECT(Gfx::PAMImageDecoderPlugin::sniff(file->bytes()));
  474. auto plugin_decoder = TRY_OR_FAIL(Gfx::PAMImageDecoderPlugin::create(file->bytes()));
  475. auto frame = TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  476. EXPECT_EQ(frame.image->size(), Gfx::IntSize(2, 1));
  477. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color('0', 'z', '0'));
  478. EXPECT_EQ(frame.image->get_pixel(1, 0), Gfx::Color('0', '0', 'z'));
  479. }
  480. TEST_CASE(test_pam_cmyk)
  481. {
  482. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("pnm/2x1-cmyk.pam"sv)));
  483. EXPECT(Gfx::PAMImageDecoderPlugin::sniff(file->bytes()));
  484. auto plugin_decoder = TRY_OR_FAIL(Gfx::PAMImageDecoderPlugin::create(file->bytes()));
  485. EXPECT_EQ(plugin_decoder->natural_frame_format(), Gfx::NaturalFrameFormat::CMYK);
  486. auto cmyk_frame = TRY_OR_FAIL(plugin_decoder->cmyk_frame());
  487. EXPECT_EQ(cmyk_frame->size(), Gfx::IntSize(2, 1));
  488. EXPECT_EQ(cmyk_frame->begin()[0], (Gfx::CMYK { '0', 'z', '0', 'y' }));
  489. EXPECT_EQ(cmyk_frame->begin()[1], (Gfx::CMYK { '0', '0', 'z', 'y' }));
  490. auto frame = TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  491. EXPECT_EQ(frame.image->size(), Gfx::IntSize(2, 1));
  492. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color('l', 'E', 'l'));
  493. EXPECT_EQ(frame.image->get_pixel(1, 0), Gfx::Color('l', 'l', 'E'));
  494. }
  495. TEST_CASE(test_pbm)
  496. {
  497. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.pbm"sv)));
  498. EXPECT(Gfx::PBMImageDecoderPlugin::sniff(file->bytes()));
  499. auto plugin_decoder = TRY_OR_FAIL(Gfx::PBMImageDecoderPlugin::create(file->bytes()));
  500. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  501. }
  502. TEST_CASE(test_pgm)
  503. {
  504. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.pgm"sv)));
  505. EXPECT(Gfx::PGMImageDecoderPlugin::sniff(file->bytes()));
  506. auto plugin_decoder = TRY_OR_FAIL(Gfx::PGMImageDecoderPlugin::create(file->bytes()));
  507. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  508. }
  509. TEST_CASE(test_png)
  510. {
  511. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("png/buggie.png"sv)));
  512. EXPECT(Gfx::PNGImageDecoderPlugin::sniff(file->bytes()));
  513. auto plugin_decoder = TRY_OR_FAIL(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
  514. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  515. }
  516. TEST_CASE(test_png_malformed_frame)
  517. {
  518. Array test_inputs = {
  519. TEST_INPUT("png/oss-fuzz-testcase-62371.png"sv),
  520. TEST_INPUT("png/oss-fuzz-testcase-63052.png"sv)
  521. };
  522. for (auto test_input : test_inputs) {
  523. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  524. auto plugin_decoder = TRY_OR_FAIL(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
  525. auto frame_or_error = plugin_decoder->frame(0);
  526. EXPECT(frame_or_error.is_error());
  527. }
  528. }
  529. TEST_CASE(test_ppm)
  530. {
  531. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.ppm"sv)));
  532. EXPECT(Gfx::PPMImageDecoderPlugin::sniff(file->bytes()));
  533. auto plugin_decoder = TRY_OR_FAIL(Gfx::PPMImageDecoderPlugin::create(file->bytes()));
  534. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  535. }
  536. TEST_CASE(test_targa_bottom_left)
  537. {
  538. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-uncompressed.tga"sv)));
  539. EXPECT(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()));
  540. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  541. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  542. }
  543. TEST_CASE(test_targa_top_left)
  544. {
  545. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-uncompressed.tga"sv)));
  546. EXPECT(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()));
  547. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  548. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  549. }
  550. TEST_CASE(test_targa_bottom_left_compressed)
  551. {
  552. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-compressed.tga"sv)));
  553. EXPECT(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()));
  554. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  555. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  556. }
  557. TEST_CASE(test_targa_top_left_compressed)
  558. {
  559. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-compressed.tga"sv)));
  560. EXPECT(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()));
  561. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  562. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  563. }
  564. TEST_CASE(test_tiff_uncompressed)
  565. {
  566. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/uncompressed.tiff"sv)));
  567. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  568. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  569. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  570. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  571. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  572. }
  573. TEST_CASE(test_tiff_ccitt_rle)
  574. {
  575. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/ccitt_rle.tiff"sv)));
  576. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  577. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  578. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  579. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  580. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Black);
  581. }
  582. TEST_CASE(test_tiff_ccitt3)
  583. {
  584. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/ccitt3.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, { 400, 300 }));
  588. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  589. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Black);
  590. }
  591. TEST_CASE(test_tiff_ccitt3_fill)
  592. {
  593. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/ccitt3_1d_fill.tiff"sv)));
  594. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  595. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  596. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 6, 4 }));
  597. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  598. EXPECT_EQ(frame.image->get_pixel(3, 0), Gfx::Color::NamedColor::Black);
  599. EXPECT_EQ(frame.image->get_pixel(2, 2), Gfx::Color::NamedColor::White);
  600. EXPECT_EQ(frame.image->get_pixel(5, 3), Gfx::Color::NamedColor::White);
  601. }
  602. TEST_CASE(test_tiff_ccitt3_2d)
  603. {
  604. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/ccitt3_2d.tiff"sv)));
  605. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  606. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  607. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  608. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  609. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Black);
  610. }
  611. TEST_CASE(test_tiff_ccitt3_2d_fill)
  612. {
  613. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/ccitt3_2d_fill.tiff"sv)));
  614. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  615. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  616. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  617. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  618. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Black);
  619. }
  620. TEST_CASE(test_tiff_ccitt4)
  621. {
  622. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/ccitt4.tiff"sv)));
  623. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  624. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  625. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  626. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  627. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Black);
  628. }
  629. TEST_CASE(test_tiff_lzw)
  630. {
  631. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/lzw.tiff"sv)));
  632. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  633. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  634. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  635. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  636. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  637. }
  638. TEST_CASE(test_tiff_deflate)
  639. {
  640. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/deflate.tiff"sv)));
  641. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  642. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  643. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  644. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  645. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  646. }
  647. TEST_CASE(test_tiff_krita)
  648. {
  649. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/krita.tif"sv)));
  650. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  651. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  652. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  653. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  654. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  655. }
  656. TEST_CASE(test_tiff_orientation)
  657. {
  658. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/orientation.tiff"sv)));
  659. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  660. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  661. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 300, 400 }));
  662. // Orientation is Rotate90Clockwise
  663. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  664. EXPECT_EQ(frame.image->get_pixel(300 - 75, 60), Gfx::Color::NamedColor::Red);
  665. }
  666. TEST_CASE(test_tiff_packed_bits)
  667. {
  668. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/packed_bits.tiff"sv)));
  669. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  670. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  671. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  672. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  673. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  674. }
  675. TEST_CASE(test_tiff_grayscale)
  676. {
  677. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/grayscale.tiff"sv)));
  678. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  679. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  680. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  681. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  682. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color(130, 130, 130));
  683. }
  684. TEST_CASE(test_tiff_grayscale_alpha)
  685. {
  686. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/grayscale_alpha.tiff"sv)));
  687. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  688. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  689. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  690. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 0);
  691. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color(130, 130, 130));
  692. }
  693. TEST_CASE(test_tiff_rgb_alpha)
  694. {
  695. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/rgb_alpha.tiff"sv)));
  696. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  697. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  698. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  699. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 0);
  700. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  701. }
  702. TEST_CASE(test_tiff_palette_alpha)
  703. {
  704. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/rgb_palette_alpha.tiff"sv)));
  705. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  706. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  707. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  708. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 0);
  709. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  710. }
  711. TEST_CASE(test_tiff_alpha_predictor)
  712. {
  713. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/alpha_predictor.tiff"sv)));
  714. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  715. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  716. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  717. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 255);
  718. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  719. }
  720. TEST_CASE(test_tiff_16_bits)
  721. {
  722. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/16_bits.tiff"sv)));
  723. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  724. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  725. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  726. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  727. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  728. }
  729. TEST_CASE(test_tiff_cmyk)
  730. {
  731. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/cmyk.tiff"sv)));
  732. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  733. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  734. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  735. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  736. // I stripped the ICC profile from the image, so we can't test for equality with Red here.
  737. EXPECT_NE(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::White);
  738. }
  739. TEST_CASE(test_tiff_tiled)
  740. {
  741. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/tiled.tiff"sv)));
  742. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  743. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  744. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  745. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  746. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  747. }
  748. TEST_CASE(test_tiff_invalid_tag)
  749. {
  750. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/invalid_tag.tiff"sv)));
  751. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  752. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  753. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 10, 10 }));
  754. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::Black);
  755. EXPECT_EQ(frame.image->get_pixel(0, 9), Gfx::Color::NamedColor::White);
  756. }
  757. TEST_CASE(test_webp_simple_lossy)
  758. {
  759. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8.webp"sv)));
  760. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  761. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  762. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 240, 240 }));
  763. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  764. // So pixels changing by 1 or so below is fine if you change code.
  765. EXPECT_EQ(frame.image->get_pixel(120, 232), Gfx::Color(0xf2, 0xef, 0xf0, 255));
  766. EXPECT_EQ(frame.image->get_pixel(198, 202), Gfx::Color(0x7b, 0xaa, 0xd5, 255));
  767. }
  768. TEST_CASE(test_webp_simple_lossless)
  769. {
  770. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8l.webp"sv)));
  771. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  772. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  773. // Ironically, simple-vp8l.webp is a much more complex file than extended-lossless.webp tested below.
  774. // extended-lossless.webp tests the decoding basics.
  775. // This here tests the predictor, color, and subtract green transforms,
  776. // as well as meta prefix images, one-element canonical code handling,
  777. // and handling of canonical codes with more than 288 elements.
  778. // This image uses all 13 predictor modes of the predictor transform.
  779. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 386, 395 }));
  780. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0));
  781. // This pixel tests all predictor modes except 5, 7, 8, 9, and 13.
  782. EXPECT_EQ(frame.image->get_pixel(289, 332), Gfx::Color(0xf2, 0xee, 0xd3, 255));
  783. }
  784. TEST_CASE(test_webp_simple_lossless_alpha_used_false)
  785. {
  786. // This file is identical to simple-vp8l.webp, but the `is_alpha_used` used bit is false.
  787. // The file still contains alpha data. This tests that the decoder replaces the stored alpha data with 0xff if `is_alpha_used` is false.
  788. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8l-alpha-used-false.webp"sv)));
  789. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  790. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  791. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 386, 395 }));
  792. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0xff));
  793. }
  794. TEST_CASE(test_webp_extended_lossy)
  795. {
  796. // This extended lossy image has an ALPH chunk for (losslessly compressed) alpha data.
  797. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/extended-lossy.webp"sv)));
  798. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  799. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  800. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 417, 223 }));
  801. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  802. // So pixels changing by 1 or so below is fine if you change code.
  803. EXPECT_EQ(frame.image->get_pixel(89, 72), Gfx::Color(255, 1, 0, 255));
  804. EXPECT_EQ(frame.image->get_pixel(174, 69), Gfx::Color(0, 255, 0, 255));
  805. EXPECT_EQ(frame.image->get_pixel(245, 84), Gfx::Color(0, 0, 255, 255));
  806. EXPECT_EQ(frame.image->get_pixel(352, 125), Gfx::Color(0, 0, 0, 128));
  807. EXPECT_EQ(frame.image->get_pixel(355, 106), Gfx::Color(0, 0, 0, 0));
  808. // Check same basic pixels as in test_webp_extended_lossless too.
  809. // (The top-left pixel in the lossy version is fully transparent white, compared to fully transparent black in the lossless version).
  810. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(255, 255, 255, 0));
  811. EXPECT_EQ(frame.image->get_pixel(43, 75), Gfx::Color(255, 0, 2, 255));
  812. EXPECT_EQ(frame.image->get_pixel(141, 75), Gfx::Color(0, 255, 3, 255));
  813. EXPECT_EQ(frame.image->get_pixel(235, 75), Gfx::Color(0, 0, 255, 255));
  814. EXPECT_EQ(frame.image->get_pixel(341, 75), Gfx::Color(0, 0, 0, 128));
  815. }
  816. TEST_CASE(test_webp_extended_lossy_alpha_horizontal_filter)
  817. {
  818. // Also lossy rgb + lossless alpha, but with a horizontal alpha filtering method.
  819. // The image should look like smolkling.webp, but with a horizontal alpha gradient.
  820. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/smolkling-horizontal-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. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x8f, 0x51, 0x2f, 0x4b));
  828. }
  829. TEST_CASE(test_webp_extended_lossy_alpha_vertical_filter)
  830. {
  831. // Also lossy rgb + lossless alpha, but with a vertical alpha filtering method.
  832. // The image should look like smolkling.webp, but with a vertical alpha gradient, and with a fully transparent first column.
  833. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/smolkling-vertical-alpha.webp"sv)));
  834. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  835. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  836. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  837. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  838. // So pixels changing by 1 or so below is fine if you change code.
  839. // 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.
  840. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x94, 0x50, 0x32, 0x4c));
  841. }
  842. TEST_CASE(test_webp_extended_lossy_alpha_gradient_filter)
  843. {
  844. // Also lossy rgb + lossless alpha, but with a gradient alpha filtering method.
  845. // 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.
  846. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/smolkling-gradient-alpha.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. // 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.
  853. // In particular, the center of the image should be fully opaque, not fully transparent.
  854. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x8c, 0x47, 0x2e, 255));
  855. }
  856. TEST_CASE(test_webp_extended_lossy_uncompressed_alpha)
  857. {
  858. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/extended-lossy-uncompressed-alpha.webp"sv)));
  859. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  860. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  861. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 417, 223 }));
  862. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  863. // So pixels changing by 1 or so below is fine if you change code.
  864. EXPECT_EQ(frame.image->get_pixel(89, 72), Gfx::Color(255, 0, 4, 255));
  865. EXPECT_EQ(frame.image->get_pixel(174, 69), Gfx::Color(4, 255, 0, 255));
  866. EXPECT_EQ(frame.image->get_pixel(245, 84), Gfx::Color(0, 0, 255, 255));
  867. EXPECT_EQ(frame.image->get_pixel(352, 125), Gfx::Color(0, 0, 0, 128));
  868. EXPECT_EQ(frame.image->get_pixel(355, 106), Gfx::Color(0, 0, 0, 0));
  869. }
  870. TEST_CASE(test_webp_extended_lossy_negative_quantization_offset)
  871. {
  872. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/smolkling.webp"sv)));
  873. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  874. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  875. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  876. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  877. // So pixels changing by 1 or so below is fine if you change code.
  878. EXPECT_EQ(frame.image->get_pixel(16, 16), Gfx::Color(0x3c, 0x24, 0x1a, 255));
  879. }
  880. TEST_CASE(test_webp_lossy_4)
  881. {
  882. // This is https://commons.wikimedia.org/wiki/File:Fr%C3%BChling_bl%C3%BChender_Kirschenbaum.jpg,
  883. // under the Creative Commons Attribution-Share Alike 3.0 Unported license. The image was re-encoded
  884. // as webp at https://developers.google.com/speed/webp/gallery1 and the webp version is from there.
  885. // No other changes have been made.
  886. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/4.webp"sv)));
  887. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  888. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  889. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1024, 772 }));
  890. // This image tests macroblocks that have `skip_coefficients` set to true, and it test a boolean entropy decoder edge case.
  891. EXPECT_EQ(frame.image->get_pixel(780, 570), Gfx::Color(0x72, 0xc8, 0xf6, 255));
  892. }
  893. TEST_CASE(test_webp_lossy_4_with_partitions)
  894. {
  895. // Same input file as in the previous test, but re-encoded to use 8 secondary partitions.
  896. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/4-with-8-partitions.webp"sv)));
  897. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  898. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  899. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1024, 772 }));
  900. EXPECT_EQ(frame.image->get_pixel(780, 570), Gfx::Color(0x73, 0xc9, 0xf9, 255));
  901. }
  902. TEST_CASE(test_webp_extended_lossless)
  903. {
  904. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/extended-lossless.webp"sv)));
  905. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  906. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  907. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 417, 223 }));
  908. // Check some basic pixels.
  909. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0));
  910. EXPECT_EQ(frame.image->get_pixel(43, 75), Gfx::Color(255, 0, 0, 255));
  911. EXPECT_EQ(frame.image->get_pixel(141, 75), Gfx::Color(0, 255, 0, 255));
  912. EXPECT_EQ(frame.image->get_pixel(235, 75), Gfx::Color(0, 0, 255, 255));
  913. EXPECT_EQ(frame.image->get_pixel(341, 75), Gfx::Color(0, 0, 0, 128));
  914. // Check pixels using the color cache.
  915. EXPECT_EQ(frame.image->get_pixel(94, 73), Gfx::Color(255, 0, 0, 255));
  916. EXPECT_EQ(frame.image->get_pixel(176, 115), Gfx::Color(0, 255, 0, 255));
  917. EXPECT_EQ(frame.image->get_pixel(290, 89), Gfx::Color(0, 0, 255, 255));
  918. EXPECT_EQ(frame.image->get_pixel(359, 73), Gfx::Color(0, 0, 0, 128));
  919. }
  920. TEST_CASE(test_webp_simple_lossless_color_index_transform)
  921. {
  922. // In addition to testing the index transform, this file also tests handling of explicity setting max_symbol.
  923. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/Qpalette.webp"sv)));
  924. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  925. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  926. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 256, 256 }));
  927. EXPECT_EQ(frame.image->get_pixel(100, 100), Gfx::Color(0x73, 0x37, 0x23, 0xff));
  928. }
  929. TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling)
  930. {
  931. struct TestCase {
  932. StringView file_name;
  933. Gfx::Color line_color;
  934. Gfx::Color background_color;
  935. };
  936. // The number after the dash is the number of colors in each file's color index bitmap.
  937. // catdog-alert-2 tests the 1-bit-per-pixel case,
  938. // catdog-alert-3 tests the 2-bit-per-pixel case,
  939. // catdog-alert-8 and catdog-alert-13 both test the 4-bits-per-pixel case.
  940. // catdog-alert-13-alpha-used-false is like catdog-alert-13, but with is_alpha_used set to false in the header
  941. // (which has the effect of ignoring the alpha information in the palette and instead always setting alpha to 0xff).
  942. TestCase test_cases[] = {
  943. { "webp/catdog-alert-2.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0xf3, 0xe6, 0xd8, 0xff) },
  944. { "webp/catdog-alert-3.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0, 0, 0, 0) },
  945. { "webp/catdog-alert-8.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  946. { "webp/catdog-alert-13.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  947. { "webp/catdog-alert-13-alpha-used-false.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 255) },
  948. };
  949. for (auto test_case : test_cases) {
  950. auto file = TRY_OR_FAIL(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), test_case.file_name))));
  951. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  952. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  953. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 32, 32 }));
  954. EXPECT_EQ(frame.image->get_pixel(4, 0), test_case.background_color);
  955. EXPECT_EQ(frame.image->get_pixel(5, 0), test_case.line_color);
  956. EXPECT_EQ(frame.image->get_pixel(9, 5), test_case.background_color);
  957. EXPECT_EQ(frame.image->get_pixel(10, 5), test_case.line_color);
  958. EXPECT_EQ(frame.image->get_pixel(11, 5), test_case.background_color);
  959. }
  960. }
  961. TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling_odd_width)
  962. {
  963. StringView file_names[] = {
  964. "webp/width11-height11-colors2.webp"sv,
  965. "webp/width11-height11-colors3.webp"sv,
  966. "webp/width11-height11-colors15.webp"sv,
  967. };
  968. for (auto file_name : file_names) {
  969. auto file = TRY_OR_FAIL(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), file_name))));
  970. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  971. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 11, 11 }));
  972. }
  973. }
  974. TEST_CASE(test_webp_extended_lossless_animated)
  975. {
  976. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/extended-lossless-animated.webp"sv)));
  977. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  978. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  979. EXPECT_EQ(plugin_decoder->frame_count(), 8u);
  980. EXPECT(plugin_decoder->is_animated());
  981. EXPECT_EQ(plugin_decoder->loop_count(), 42u);
  982. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(990, 1050));
  983. for (size_t frame_index = 0; frame_index < plugin_decoder->frame_count(); ++frame_index) {
  984. auto frame = TRY_OR_FAIL(plugin_decoder->frame(frame_index));
  985. EXPECT_EQ(frame.image->size(), Gfx::IntSize(990, 1050));
  986. // This pixel happens to be the same color in all frames.
  987. EXPECT_EQ(frame.image->get_pixel(500, 700), Gfx::Color::Yellow);
  988. // This one isn't the same in all frames.
  989. EXPECT_EQ(frame.image->get_pixel(500, 0), (frame_index == 2 || frame_index == 6) ? Gfx::Color::Black : Gfx::Color(255, 255, 255, 0));
  990. }
  991. }
  992. TEST_CASE(test_tvg)
  993. {
  994. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tvg/yak.tvg"sv)));
  995. EXPECT(Gfx::TinyVGImageDecoderPlugin::sniff(file->bytes()));
  996. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  997. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1024, 1024 }));
  998. }
  999. TEST_CASE(test_everything_tvg)
  1000. {
  1001. Array file_names {
  1002. TEST_INPUT("tvg/everything.tvg"sv),
  1003. TEST_INPUT("tvg/everything-32.tvg"sv)
  1004. };
  1005. for (auto file_name : file_names) {
  1006. auto file = TRY_OR_FAIL(Core::MappedFile::map(file_name));
  1007. EXPECT(Gfx::TinyVGImageDecoderPlugin::sniff(file->bytes()));
  1008. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  1009. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 768 }));
  1010. }
  1011. }
  1012. TEST_CASE(test_tvg_malformed)
  1013. {
  1014. Array test_inputs = {
  1015. TEST_INPUT("tvg/bogus-color-table-size.tvg"sv)
  1016. };
  1017. for (auto test_input : test_inputs) {
  1018. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  1019. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  1020. auto frame_or_error = plugin_decoder->frame(0);
  1021. EXPECT(frame_or_error.is_error());
  1022. }
  1023. }
  1024. TEST_CASE(test_tvg_rgb565)
  1025. {
  1026. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tvg/green-rgb565.tvg"sv)));
  1027. EXPECT(Gfx::TinyVGImageDecoderPlugin::sniff(file->bytes()));
  1028. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  1029. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 100, 100 }));
  1030. // Should be a solid dark green:
  1031. EXPECT_EQ(frame.image->get_pixel(50, 50), Gfx::Color(0, 130, 0));
  1032. }
  1033. TEST_CASE(test_jxl_modular_simple_tree_upsample2_10bits)
  1034. {
  1035. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jxl/modular_simple_tree_upsample2_10bits_rct.jxl"sv)));
  1036. EXPECT(Gfx::JPEGXLImageDecoderPlugin::sniff(file->bytes()));
  1037. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGXLImageDecoderPlugin::create(file->bytes()));
  1038. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 128, 128 }));
  1039. auto frame = TRY_OR_FAIL(plugin_decoder->frame(0));
  1040. EXPECT_EQ(frame.image->get_pixel(42, 57), Gfx::Color::from_string("#4c0072"sv));
  1041. }
  1042. TEST_CASE(test_jxl_modular_property_8)
  1043. {
  1044. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jxl/modular_property_8.jxl"sv)));
  1045. EXPECT(Gfx::JPEGXLImageDecoderPlugin::sniff(file->bytes()));
  1046. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGXLImageDecoderPlugin::create(file->bytes()));
  1047. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 32, 32 }));
  1048. auto frame = TRY_OR_FAIL(plugin_decoder->frame(0));
  1049. for (u8 i = 0; i < 32; ++i) {
  1050. for (u8 j = 0; j < 32; ++j) {
  1051. auto const color = frame.image->get_pixel(i, j);
  1052. if ((i + j) % 2 == 0)
  1053. EXPECT_EQ(color, Gfx::Color::Black);
  1054. else
  1055. EXPECT_EQ(color, Gfx::Color::Yellow);
  1056. }
  1057. }
  1058. }
  1059. TEST_CASE(test_dds)
  1060. {
  1061. Array file_names = {
  1062. TEST_INPUT("dds/catdog-alert-29x29.dds"sv),
  1063. TEST_INPUT("dds/catdog-alert-32x32.dds"sv)
  1064. };
  1065. for (auto file_name : file_names) {
  1066. auto file = TRY_OR_FAIL(Core::MappedFile::map(file_name));
  1067. EXPECT(Gfx::DDSImageDecoderPlugin::sniff(file->bytes()));
  1068. auto plugin_decoder = TRY_OR_FAIL(Gfx::DDSImageDecoderPlugin::create(file->bytes()));
  1069. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  1070. }
  1071. }