TestImageDecoder.cpp 44 KB

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