TestImageDecoder.cpp 43 KB

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