TestImageDecoder.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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 = MUST(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 = MUST(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 = MUST(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 = MUST(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 = MUST(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 = MUST(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 = MUST(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 = MUST(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 = MUST(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 = MUST(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 = MUST(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 = MUST(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_ilbm_malformed_header)
  205. {
  206. Array test_inputs = {
  207. TEST_INPUT("ilbm/truncated-bmhd-chunk.iff"sv)
  208. };
  209. for (auto test_input : test_inputs) {
  210. auto file = MUST(Core::MappedFile::map(test_input));
  211. auto plugin_decoder_or_error = Gfx::ILBMImageDecoderPlugin::create(file->bytes());
  212. EXPECT(plugin_decoder_or_error.is_error());
  213. }
  214. }
  215. TEST_CASE(test_ilbm_malformed_frame)
  216. {
  217. Array test_inputs = {
  218. TEST_INPUT("ilbm/incorrect-cmap-size.iff"sv),
  219. TEST_INPUT("ilbm/incorrect-uncompressed-size.iff"sv),
  220. TEST_INPUT("ilbm/missing-body-chunk.iff"sv)
  221. };
  222. for (auto test_input : test_inputs) {
  223. auto file = MUST(Core::MappedFile::map(test_input));
  224. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  225. auto frame_or_error = plugin_decoder->frame(0);
  226. EXPECT(frame_or_error.is_error());
  227. }
  228. }
  229. TEST_CASE(test_jpeg_sof0_one_scan)
  230. {
  231. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/rgb24.jpg"sv)));
  232. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  233. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  234. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  235. }
  236. TEST_CASE(test_jpeg_sof0_several_scans)
  237. {
  238. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/several_scans.jpg"sv)));
  239. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  240. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  241. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  242. }
  243. TEST_CASE(test_jpeg_rgb_components)
  244. {
  245. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/rgb_components.jpg"sv)));
  246. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  247. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  248. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  249. }
  250. TEST_CASE(test_jpeg_ycck)
  251. {
  252. Array test_inputs = {
  253. TEST_INPUT("jpg/ycck-1111.jpg"sv),
  254. // TEST_INPUT("jpg/ycck-2111.jpg"sv), // FIXME: Enable once this decodes correctly
  255. TEST_INPUT("jpg/ycck-2112.jpg"sv),
  256. };
  257. for (auto test_input : test_inputs) {
  258. auto file = MUST(Core::MappedFile::map(test_input));
  259. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  260. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  261. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  262. // Compare difference between pixels so we don't depend on exact CMYK->RGB conversion behavior.
  263. // These two pixels are currently off by one in R.
  264. // FIXME: For 2111, they're off by way more.
  265. EXPECT(frame.image->get_pixel(6, 319).distance_squared_to(frame.image->get_pixel(6, 320)) < 1.0f / 255.0f);
  266. }
  267. }
  268. TEST_CASE(test_jpeg_sof2_spectral_selection)
  269. {
  270. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/spectral_selection.jpg"sv)));
  271. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  272. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  273. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  274. }
  275. TEST_CASE(test_jpeg_sof0_several_scans_odd_number_mcu)
  276. {
  277. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/several_scans_odd_number_mcu.jpg"sv)));
  278. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  279. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  280. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 600, 600 }));
  281. }
  282. TEST_CASE(test_jpeg_sof2_successive_aproximation)
  283. {
  284. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/successive_approximation.jpg"sv)));
  285. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  286. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  287. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 600, 800 }));
  288. }
  289. TEST_CASE(test_jpeg_sof1_12bits)
  290. {
  291. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/12-bit.jpg"sv)));
  292. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  293. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  294. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 240 }));
  295. }
  296. TEST_CASE(test_jpeg_sof2_12bits)
  297. {
  298. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/12-bit-progressive.jpg"sv)));
  299. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  300. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  301. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 240 }));
  302. }
  303. TEST_CASE(test_jpeg_empty_icc)
  304. {
  305. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/gradient_empty_icc.jpg"sv)));
  306. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  307. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  308. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 80, 80 }));
  309. }
  310. TEST_CASE(test_jpeg_grayscale_with_app14)
  311. {
  312. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jpg/grayscale_app14.jpg"sv)));
  313. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  314. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  315. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 80, 80 }));
  316. }
  317. TEST_CASE(test_jpeg_malformed_header)
  318. {
  319. Array test_inputs = {
  320. TEST_INPUT("jpg/oss-fuzz-testcase-59785.jpg"sv)
  321. };
  322. for (auto test_input : test_inputs) {
  323. auto file = MUST(Core::MappedFile::map(test_input));
  324. auto plugin_decoder_or_error = Gfx::JPEGImageDecoderPlugin::create(file->bytes());
  325. EXPECT(plugin_decoder_or_error.is_error());
  326. }
  327. }
  328. TEST_CASE(test_jpeg_malformed_frame)
  329. {
  330. Array test_inputs = {
  331. TEST_INPUT("jpg/oss-fuzz-testcase-62584.jpg"sv),
  332. TEST_INPUT("jpg/oss-fuzz-testcase-63815.jpg"sv)
  333. };
  334. for (auto test_input : test_inputs) {
  335. auto file = MUST(Core::MappedFile::map(test_input));
  336. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  337. auto frame_or_error = plugin_decoder->frame(0);
  338. EXPECT(frame_or_error.is_error());
  339. }
  340. }
  341. TEST_CASE(test_pbm)
  342. {
  343. auto file = MUST(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.pbm"sv)));
  344. EXPECT(Gfx::PBMImageDecoderPlugin::sniff(file->bytes()));
  345. auto plugin_decoder = TRY_OR_FAIL(Gfx::PBMImageDecoderPlugin::create(file->bytes()));
  346. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  347. }
  348. TEST_CASE(test_pgm)
  349. {
  350. auto file = MUST(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.pgm"sv)));
  351. EXPECT(Gfx::PGMImageDecoderPlugin::sniff(file->bytes()));
  352. auto plugin_decoder = TRY_OR_FAIL(Gfx::PGMImageDecoderPlugin::create(file->bytes()));
  353. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  354. }
  355. TEST_CASE(test_png)
  356. {
  357. auto file = MUST(Core::MappedFile::map(TEST_INPUT("png/buggie.png"sv)));
  358. EXPECT(Gfx::PNGImageDecoderPlugin::sniff(file->bytes()));
  359. auto plugin_decoder = TRY_OR_FAIL(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
  360. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  361. }
  362. TEST_CASE(test_png_malformed_frame)
  363. {
  364. Array test_inputs = {
  365. TEST_INPUT("png/oss-fuzz-testcase-62371.png"sv),
  366. TEST_INPUT("png/oss-fuzz-testcase-63052.png"sv)
  367. };
  368. for (auto test_input : test_inputs) {
  369. auto file = MUST(Core::MappedFile::map(test_input));
  370. auto plugin_decoder = TRY_OR_FAIL(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
  371. auto frame_or_error = plugin_decoder->frame(0);
  372. EXPECT(frame_or_error.is_error());
  373. }
  374. }
  375. TEST_CASE(test_ppm)
  376. {
  377. auto file = MUST(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.ppm"sv)));
  378. EXPECT(Gfx::PPMImageDecoderPlugin::sniff(file->bytes()));
  379. auto plugin_decoder = TRY_OR_FAIL(Gfx::PPMImageDecoderPlugin::create(file->bytes()));
  380. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  381. }
  382. TEST_CASE(test_targa_bottom_left)
  383. {
  384. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-uncompressed.tga"sv)));
  385. EXPECT(TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  386. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  387. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  388. }
  389. TEST_CASE(test_targa_top_left)
  390. {
  391. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-uncompressed.tga"sv)));
  392. EXPECT(TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  393. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  394. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  395. }
  396. TEST_CASE(test_targa_bottom_left_compressed)
  397. {
  398. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-compressed.tga"sv)));
  399. EXPECT(TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  400. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  401. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  402. }
  403. TEST_CASE(test_targa_top_left_compressed)
  404. {
  405. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-compressed.tga"sv)));
  406. EXPECT(TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  407. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  408. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  409. }
  410. TEST_CASE(test_tiff_uncompressed)
  411. {
  412. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/uncompressed.tiff"sv)));
  413. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  414. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  415. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  416. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  417. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  418. }
  419. TEST_CASE(test_tiff_ccitt3_1d)
  420. {
  421. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/ccitt3_1d.tiff"sv)));
  422. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  423. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  424. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  425. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  426. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Black);
  427. }
  428. TEST_CASE(test_tiff_lzw)
  429. {
  430. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/lzw.tiff"sv)));
  431. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  432. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  433. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  434. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  435. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  436. }
  437. TEST_CASE(test_tiff_deflate)
  438. {
  439. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/deflate.tiff"sv)));
  440. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  441. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  442. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  443. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  444. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  445. }
  446. TEST_CASE(test_tiff_orientation)
  447. {
  448. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/orientation.tiff"sv)));
  449. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  450. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  451. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 300, 400 }));
  452. // Orientation is Rotate90Clockwise
  453. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  454. EXPECT_EQ(frame.image->get_pixel(300 - 75, 60), Gfx::Color::NamedColor::Red);
  455. }
  456. TEST_CASE(test_tiff_packed_bits)
  457. {
  458. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/packed_bits.tiff"sv)));
  459. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  460. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  461. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  462. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  463. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  464. }
  465. TEST_CASE(test_tiff_grayscale)
  466. {
  467. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/grayscale.tiff"sv)));
  468. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  469. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  470. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  471. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  472. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color(130, 130, 130));
  473. }
  474. TEST_CASE(test_tiff_grayscale_alpha)
  475. {
  476. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/grayscale_alpha.tiff"sv)));
  477. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  478. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  479. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  480. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 0);
  481. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color(130, 130, 130));
  482. }
  483. TEST_CASE(test_tiff_rgb_alpha)
  484. {
  485. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/rgb_alpha.tiff"sv)));
  486. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  487. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  488. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  489. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 0);
  490. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  491. }
  492. TEST_CASE(test_tiff_palette_alpha)
  493. {
  494. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/rgb_palette_alpha.tiff"sv)));
  495. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  496. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  497. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  498. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 0);
  499. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  500. }
  501. TEST_CASE(test_tiff_16_bits)
  502. {
  503. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/16_bits.tiff"sv)));
  504. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  505. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  506. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  507. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  508. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  509. }
  510. TEST_CASE(test_tiff_invalid_tag)
  511. {
  512. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/invalid_tag.tiff"sv)));
  513. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  514. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  515. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 10, 10 }));
  516. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::Black);
  517. EXPECT_EQ(frame.image->get_pixel(0, 9), Gfx::Color::NamedColor::White);
  518. }
  519. TEST_CASE(test_webp_simple_lossy)
  520. {
  521. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8.webp"sv)));
  522. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  523. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  524. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 240, 240 }));
  525. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  526. // So pixels changing by 1 or so below is fine if you change code.
  527. EXPECT_EQ(frame.image->get_pixel(120, 232), Gfx::Color(0xf2, 0xef, 0xf0, 255));
  528. EXPECT_EQ(frame.image->get_pixel(198, 202), Gfx::Color(0x7b, 0xaa, 0xd5, 255));
  529. }
  530. TEST_CASE(test_webp_simple_lossless)
  531. {
  532. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8l.webp"sv)));
  533. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  534. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  535. // Ironically, simple-vp8l.webp is a much more complex file than extended-lossless.webp tested below.
  536. // extended-lossless.webp tests the decoding basics.
  537. // This here tests the predictor, color, and subtract green transforms,
  538. // as well as meta prefix images, one-element canonical code handling,
  539. // and handling of canonical codes with more than 288 elements.
  540. // This image uses all 13 predictor modes of the predictor transform.
  541. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 386, 395 }));
  542. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0));
  543. // This pixel tests all predictor modes except 5, 7, 8, 9, and 13.
  544. EXPECT_EQ(frame.image->get_pixel(289, 332), Gfx::Color(0xf2, 0xee, 0xd3, 255));
  545. }
  546. TEST_CASE(test_webp_simple_lossless_alpha_used_false)
  547. {
  548. // This file is identical to simple-vp8l.webp, but the `is_alpha_used` used bit is false.
  549. // The file still contains alpha data. This tests that the decoder replaces the stored alpha data with 0xff if `is_alpha_used` is false.
  550. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8l-alpha-used-false.webp"sv)));
  551. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  552. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  553. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 386, 395 }));
  554. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0xff));
  555. }
  556. TEST_CASE(test_webp_extended_lossy)
  557. {
  558. // This extended lossy image has an ALPH chunk for (losslessly compressed) alpha data.
  559. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/extended-lossy.webp"sv)));
  560. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  561. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  562. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 417, 223 }));
  563. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  564. // So pixels changing by 1 or so below is fine if you change code.
  565. EXPECT_EQ(frame.image->get_pixel(89, 72), Gfx::Color(255, 1, 0, 255));
  566. EXPECT_EQ(frame.image->get_pixel(174, 69), Gfx::Color(0, 255, 0, 255));
  567. EXPECT_EQ(frame.image->get_pixel(245, 84), Gfx::Color(0, 0, 255, 255));
  568. EXPECT_EQ(frame.image->get_pixel(352, 125), Gfx::Color(0, 0, 0, 128));
  569. EXPECT_EQ(frame.image->get_pixel(355, 106), Gfx::Color(0, 0, 0, 0));
  570. // Check same basic pixels as in test_webp_extended_lossless too.
  571. // (The top-left pixel in the lossy version is fully transparent white, compared to fully transparent black in the lossless version).
  572. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(255, 255, 255, 0));
  573. EXPECT_EQ(frame.image->get_pixel(43, 75), Gfx::Color(255, 0, 2, 255));
  574. EXPECT_EQ(frame.image->get_pixel(141, 75), Gfx::Color(0, 255, 3, 255));
  575. EXPECT_EQ(frame.image->get_pixel(235, 75), Gfx::Color(0, 0, 255, 255));
  576. EXPECT_EQ(frame.image->get_pixel(341, 75), Gfx::Color(0, 0, 0, 128));
  577. }
  578. TEST_CASE(test_webp_extended_lossy_alpha_horizontal_filter)
  579. {
  580. // Also lossy rgb + lossless alpha, but with a horizontal alpha filtering method.
  581. // The image should look like smolkling.webp, but with a horizontal alpha gradient.
  582. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/smolkling-horizontal-alpha.webp"sv)));
  583. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  584. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  585. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  586. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  587. // So pixels changing by 1 or so below is fine if you change code.
  588. // 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.
  589. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x8f, 0x51, 0x2f, 0x4b));
  590. }
  591. TEST_CASE(test_webp_extended_lossy_alpha_vertical_filter)
  592. {
  593. // Also lossy rgb + lossless alpha, but with a vertical alpha filtering method.
  594. // The image should look like smolkling.webp, but with a vertical alpha gradient, and with a fully transparent first column.
  595. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/smolkling-vertical-alpha.webp"sv)));
  596. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  597. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  598. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  599. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  600. // So pixels changing by 1 or so below is fine if you change code.
  601. // 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.
  602. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x94, 0x50, 0x32, 0x4c));
  603. }
  604. TEST_CASE(test_webp_extended_lossy_alpha_gradient_filter)
  605. {
  606. // Also lossy rgb + lossless alpha, but with a gradient alpha filtering method.
  607. // 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.
  608. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/smolkling-gradient-alpha.webp"sv)));
  609. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  610. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  611. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  612. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  613. // So pixels changing by 1 or so below is fine if you change code.
  614. // 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.
  615. // In particular, the center of the image should be fully opaque, not fully transparent.
  616. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x8c, 0x47, 0x2e, 255));
  617. }
  618. TEST_CASE(test_webp_extended_lossy_uncompressed_alpha)
  619. {
  620. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/extended-lossy-uncompressed-alpha.webp"sv)));
  621. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  622. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  623. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 417, 223 }));
  624. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  625. // So pixels changing by 1 or so below is fine if you change code.
  626. EXPECT_EQ(frame.image->get_pixel(89, 72), Gfx::Color(255, 0, 4, 255));
  627. EXPECT_EQ(frame.image->get_pixel(174, 69), Gfx::Color(4, 255, 0, 255));
  628. EXPECT_EQ(frame.image->get_pixel(245, 84), Gfx::Color(0, 0, 255, 255));
  629. EXPECT_EQ(frame.image->get_pixel(352, 125), Gfx::Color(0, 0, 0, 128));
  630. EXPECT_EQ(frame.image->get_pixel(355, 106), Gfx::Color(0, 0, 0, 0));
  631. }
  632. TEST_CASE(test_webp_extended_lossy_negative_quantization_offset)
  633. {
  634. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/smolkling.webp"sv)));
  635. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  636. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  637. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  638. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  639. // So pixels changing by 1 or so below is fine if you change code.
  640. EXPECT_EQ(frame.image->get_pixel(16, 16), Gfx::Color(0x3c, 0x24, 0x1a, 255));
  641. }
  642. TEST_CASE(test_webp_lossy_4)
  643. {
  644. // This is https://commons.wikimedia.org/wiki/File:Fr%C3%BChling_bl%C3%BChender_Kirschenbaum.jpg,
  645. // under the Creative Commons Attribution-Share Alike 3.0 Unported license. The image was re-encoded
  646. // as webp at https://developers.google.com/speed/webp/gallery1 and the webp version is from there.
  647. // No other changes have been made.
  648. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/4.webp"sv)));
  649. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  650. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  651. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1024, 772 }));
  652. // This image tests macroblocks that have `skip_coefficients` set to true, and it test a boolean entropy decoder edge case.
  653. EXPECT_EQ(frame.image->get_pixel(780, 570), Gfx::Color(0x72, 0xc8, 0xf6, 255));
  654. }
  655. TEST_CASE(test_webp_lossy_4_with_partitions)
  656. {
  657. // Same input file as in the previous test, but re-encoded to use 8 secondary partitions.
  658. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/4-with-8-partitions.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, { 1024, 772 }));
  662. EXPECT_EQ(frame.image->get_pixel(780, 570), Gfx::Color(0x73, 0xc9, 0xf9, 255));
  663. }
  664. TEST_CASE(test_webp_extended_lossless)
  665. {
  666. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/extended-lossless.webp"sv)));
  667. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  668. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  669. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 417, 223 }));
  670. // Check some basic pixels.
  671. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0));
  672. EXPECT_EQ(frame.image->get_pixel(43, 75), Gfx::Color(255, 0, 0, 255));
  673. EXPECT_EQ(frame.image->get_pixel(141, 75), Gfx::Color(0, 255, 0, 255));
  674. EXPECT_EQ(frame.image->get_pixel(235, 75), Gfx::Color(0, 0, 255, 255));
  675. EXPECT_EQ(frame.image->get_pixel(341, 75), Gfx::Color(0, 0, 0, 128));
  676. // Check pixels using the color cache.
  677. EXPECT_EQ(frame.image->get_pixel(94, 73), Gfx::Color(255, 0, 0, 255));
  678. EXPECT_EQ(frame.image->get_pixel(176, 115), Gfx::Color(0, 255, 0, 255));
  679. EXPECT_EQ(frame.image->get_pixel(290, 89), Gfx::Color(0, 0, 255, 255));
  680. EXPECT_EQ(frame.image->get_pixel(359, 73), Gfx::Color(0, 0, 0, 128));
  681. }
  682. TEST_CASE(test_webp_simple_lossless_color_index_transform)
  683. {
  684. // In addition to testing the index transform, this file also tests handling of explicity setting max_symbol.
  685. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/Qpalette.webp"sv)));
  686. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  687. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  688. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 256, 256 }));
  689. EXPECT_EQ(frame.image->get_pixel(100, 100), Gfx::Color(0x73, 0x37, 0x23, 0xff));
  690. }
  691. TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling)
  692. {
  693. struct TestCase {
  694. StringView file_name;
  695. Gfx::Color line_color;
  696. Gfx::Color background_color;
  697. };
  698. // The number after the dash is the number of colors in each file's color index bitmap.
  699. // catdog-alert-2 tests the 1-bit-per-pixel case,
  700. // catdog-alert-3 tests the 2-bit-per-pixel case,
  701. // catdog-alert-8 and catdog-alert-13 both test the 4-bits-per-pixel case.
  702. // catdog-alert-13-alpha-used-false is like catdog-alert-13, but with is_alpha_used set to false in the header
  703. // (which has the effect of ignoring the alpha information in the palette and instead always setting alpha to 0xff).
  704. TestCase test_cases[] = {
  705. { "webp/catdog-alert-2.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0xf3, 0xe6, 0xd8, 0xff) },
  706. { "webp/catdog-alert-3.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0, 0, 0, 0) },
  707. { "webp/catdog-alert-8.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  708. { "webp/catdog-alert-13.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  709. { "webp/catdog-alert-13-alpha-used-false.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 255) },
  710. };
  711. for (auto test_case : test_cases) {
  712. auto file = MUST(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), test_case.file_name))));
  713. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  714. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  715. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 32, 32 }));
  716. EXPECT_EQ(frame.image->get_pixel(4, 0), test_case.background_color);
  717. EXPECT_EQ(frame.image->get_pixel(5, 0), test_case.line_color);
  718. EXPECT_EQ(frame.image->get_pixel(9, 5), test_case.background_color);
  719. EXPECT_EQ(frame.image->get_pixel(10, 5), test_case.line_color);
  720. EXPECT_EQ(frame.image->get_pixel(11, 5), test_case.background_color);
  721. }
  722. }
  723. TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling_odd_width)
  724. {
  725. StringView file_names[] = {
  726. "webp/width11-height11-colors2.webp"sv,
  727. "webp/width11-height11-colors3.webp"sv,
  728. "webp/width11-height11-colors15.webp"sv,
  729. };
  730. for (auto file_name : file_names) {
  731. auto file = MUST(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), file_name))));
  732. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  733. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 11, 11 }));
  734. }
  735. }
  736. TEST_CASE(test_webp_extended_lossless_animated)
  737. {
  738. auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/extended-lossless-animated.webp"sv)));
  739. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  740. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  741. EXPECT_EQ(plugin_decoder->frame_count(), 8u);
  742. EXPECT(plugin_decoder->is_animated());
  743. EXPECT_EQ(plugin_decoder->loop_count(), 42u);
  744. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(990, 1050));
  745. for (size_t frame_index = 0; frame_index < plugin_decoder->frame_count(); ++frame_index) {
  746. auto frame = MUST(plugin_decoder->frame(frame_index));
  747. EXPECT_EQ(frame.image->size(), Gfx::IntSize(990, 1050));
  748. // This pixel happens to be the same color in all frames.
  749. EXPECT_EQ(frame.image->get_pixel(500, 700), Gfx::Color::Yellow);
  750. // This one isn't the same in all frames.
  751. EXPECT_EQ(frame.image->get_pixel(500, 0), (frame_index == 2 || frame_index == 6) ? Gfx::Color::Black : Gfx::Color(255, 255, 255, 0));
  752. }
  753. }
  754. TEST_CASE(test_tvg)
  755. {
  756. auto file = MUST(Core::MappedFile::map(TEST_INPUT("tvg/yak.tvg"sv)));
  757. EXPECT(Gfx::TinyVGImageDecoderPlugin::sniff(file->bytes()));
  758. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  759. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1024, 1024 }));
  760. }
  761. TEST_CASE(test_everything_tvg)
  762. {
  763. Array file_names {
  764. TEST_INPUT("tvg/everything.tvg"sv),
  765. TEST_INPUT("tvg/everything-32.tvg"sv)
  766. };
  767. for (auto file_name : file_names) {
  768. auto file = MUST(Core::MappedFile::map(file_name));
  769. EXPECT(Gfx::TinyVGImageDecoderPlugin::sniff(file->bytes()));
  770. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  771. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 768 }));
  772. }
  773. }
  774. TEST_CASE(test_tvg_malformed)
  775. {
  776. Array test_inputs = {
  777. TEST_INPUT("tvg/bogus-color-table-size.tvg"sv)
  778. };
  779. for (auto test_input : test_inputs) {
  780. auto file = MUST(Core::MappedFile::map(test_input));
  781. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  782. auto frame_or_error = plugin_decoder->frame(0);
  783. EXPECT(frame_or_error.is_error());
  784. }
  785. }
  786. TEST_CASE(test_jxl_modular_simple_tree_upsample2_10bits)
  787. {
  788. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jxl/modular_simple_tree_upsample2_10bits_rct.jxl"sv)));
  789. EXPECT(Gfx::JPEGXLImageDecoderPlugin::sniff(file->bytes()));
  790. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGXLImageDecoderPlugin::create(file->bytes()));
  791. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 128, 128 }));
  792. auto frame = MUST(plugin_decoder->frame(0));
  793. EXPECT_EQ(frame.image->get_pixel(42, 57), Gfx::Color::from_string("#4c0072"sv));
  794. }
  795. TEST_CASE(test_jxl_modular_property_8)
  796. {
  797. auto file = MUST(Core::MappedFile::map(TEST_INPUT("jxl/modular_property_8.jxl"sv)));
  798. EXPECT(Gfx::JPEGXLImageDecoderPlugin::sniff(file->bytes()));
  799. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGXLImageDecoderPlugin::create(file->bytes()));
  800. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 32, 32 }));
  801. auto frame = MUST(plugin_decoder->frame(0));
  802. for (u8 i = 0; i < 32; ++i) {
  803. for (u8 j = 0; j < 32; ++j) {
  804. auto const color = frame.image->get_pixel(i, j);
  805. if ((i + j) % 2 == 0)
  806. EXPECT_EQ(color, Gfx::Color::Black);
  807. else
  808. EXPECT_EQ(color, Gfx::Color::Yellow);
  809. }
  810. }
  811. }
  812. TEST_CASE(test_dds)
  813. {
  814. Array file_names = {
  815. TEST_INPUT("dds/catdog-alert-29x29.dds"sv),
  816. TEST_INPUT("dds/catdog-alert-32x32.dds"sv)
  817. };
  818. for (auto file_name : file_names) {
  819. auto file = MUST(Core::MappedFile::map(file_name));
  820. EXPECT(Gfx::DDSImageDecoderPlugin::sniff(file->bytes()));
  821. auto plugin_decoder = TRY_OR_FAIL(Gfx::DDSImageDecoderPlugin::create(file->bytes()));
  822. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  823. }
  824. }