TestImageDecoder.cpp 43 KB

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