TestImageDecoder.cpp 43 KB

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