TestImageDecoder.cpp 57 KB

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