TestImageDecoder.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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/JBIG2Loader.h>
  16. #include <LibGfx/ImageFormats/JPEGLoader.h>
  17. #include <LibGfx/ImageFormats/JPEGXLLoader.h>
  18. #include <LibGfx/ImageFormats/PAMLoader.h>
  19. #include <LibGfx/ImageFormats/PBMLoader.h>
  20. #include <LibGfx/ImageFormats/PGMLoader.h>
  21. #include <LibGfx/ImageFormats/PNGLoader.h>
  22. #include <LibGfx/ImageFormats/PPMLoader.h>
  23. #include <LibGfx/ImageFormats/TGALoader.h>
  24. #include <LibGfx/ImageFormats/TIFFLoader.h>
  25. #include <LibGfx/ImageFormats/TinyVGLoader.h>
  26. #include <LibGfx/ImageFormats/WebPLoader.h>
  27. #include <LibTest/TestCase.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #ifdef AK_OS_SERENITY
  31. # define TEST_INPUT(x) ("/usr/Tests/LibGfx/test-inputs/" x)
  32. #else
  33. # define TEST_INPUT(x) ("test-inputs/" x)
  34. #endif
  35. static ErrorOr<Gfx::ImageFrameDescriptor> expect_single_frame(Gfx::ImageDecoderPlugin& plugin_decoder)
  36. {
  37. EXPECT_EQ(plugin_decoder.frame_count(), 1u);
  38. EXPECT(!plugin_decoder.is_animated());
  39. EXPECT(!plugin_decoder.loop_count());
  40. auto frame = TRY(plugin_decoder.frame(0));
  41. EXPECT_EQ(frame.duration, 0);
  42. return frame;
  43. }
  44. static ErrorOr<Gfx::ImageFrameDescriptor> expect_single_frame_of_size(Gfx::ImageDecoderPlugin& plugin_decoder, Gfx::IntSize size)
  45. {
  46. EXPECT_EQ(plugin_decoder.size(), size);
  47. auto frame = TRY(expect_single_frame(plugin_decoder));
  48. EXPECT_EQ(frame.image->size(), size);
  49. return frame;
  50. }
  51. TEST_CASE(test_bmp)
  52. {
  53. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("bmp/rgba32-1.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_top_down)
  59. {
  60. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("bmp/top-down.bmp"sv)));
  61. EXPECT(Gfx::BMPImageDecoderPlugin::sniff(file->bytes()));
  62. auto plugin_decoder = TRY_OR_FAIL(Gfx::BMPImageDecoderPlugin::create(file->bytes()));
  63. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  64. }
  65. TEST_CASE(test_bmp_1bpp)
  66. {
  67. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("bmp/bitmap.bmp"sv)));
  68. EXPECT(Gfx::BMPImageDecoderPlugin::sniff(file->bytes()));
  69. auto plugin_decoder = TRY_OR_FAIL(Gfx::BMPImageDecoderPlugin::create(file->bytes()));
  70. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 399, 400 }));
  71. EXPECT_EQ(frame.image->begin()[0], 0xff'ff'ff'ff);
  72. }
  73. TEST_CASE(test_ico_malformed_frame)
  74. {
  75. Array test_inputs = {
  76. TEST_INPUT("ico/oss-fuzz-testcase-62541.ico"sv),
  77. TEST_INPUT("ico/oss-fuzz-testcase-63177.ico"sv),
  78. TEST_INPUT("ico/oss-fuzz-testcase-63357.ico"sv)
  79. };
  80. for (auto test_input : test_inputs) {
  81. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  82. auto plugin_decoder = TRY_OR_FAIL(Gfx::ICOImageDecoderPlugin::create(file->bytes()));
  83. auto frame_or_error = plugin_decoder->frame(0);
  84. EXPECT(frame_or_error.is_error());
  85. }
  86. }
  87. TEST_CASE(test_gif)
  88. {
  89. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("download-animation.gif"sv)));
  90. EXPECT(Gfx::GIFImageDecoderPlugin::sniff(file->bytes()));
  91. auto plugin_decoder = TRY_OR_FAIL(Gfx::GIFImageDecoderPlugin::create(file->bytes()));
  92. EXPECT(plugin_decoder->frame_count());
  93. EXPECT(plugin_decoder->is_animated());
  94. EXPECT(!plugin_decoder->loop_count());
  95. auto frame = TRY_OR_FAIL(plugin_decoder->frame(1));
  96. EXPECT(frame.duration == 400);
  97. }
  98. TEST_CASE(test_gif_without_global_color_table)
  99. {
  100. Array<u8, 35> gif_data {
  101. // Header (6 bytes): "GIF89a"
  102. 0x47,
  103. 0x49,
  104. 0x46,
  105. 0x38,
  106. 0x39,
  107. 0x61,
  108. // Logical Screen Descriptor (7 bytes)
  109. 0x01,
  110. 0x00, // Width (1)
  111. 0x01,
  112. 0x00, // Height (1)
  113. 0x00, // Packed fields (NOTE: the MSB here is the Global Color Table flag!)
  114. 0x00, // Background Color Index
  115. 0x00, // Pixel Aspect Ratio
  116. // Image Descriptor (10 bytes)
  117. 0x2C,
  118. 0x00,
  119. 0x00,
  120. 0x00,
  121. 0x00,
  122. 0x01,
  123. 0x00,
  124. 0x01,
  125. 0x00,
  126. 0x80,
  127. // Local Color Table (6 bytes: 2 colors, 3 bytes per color)
  128. 0x00,
  129. 0x00,
  130. 0x00, // Color 1: Black (RGB: 0, 0, 0)
  131. 0xff,
  132. 0x00,
  133. 0x00, // Color 2: Red (RGB: 255, 0, 0)
  134. // Image Data (8 bytes)
  135. 0x02, // LZW Minimum Code Size
  136. 0x02, // Data Sub-block size (2 bytes)
  137. 0x4C,
  138. 0x01, // Image Data
  139. 0x00, // Data Sub-block Terminator
  140. // Trailer (1 byte)
  141. 0x3B,
  142. };
  143. auto plugin_decoder = TRY_OR_FAIL(Gfx::GIFImageDecoderPlugin::create(gif_data));
  144. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  145. auto frame = TRY_OR_FAIL(plugin_decoder->frame(0));
  146. EXPECT(frame.image);
  147. EXPECT_EQ(frame.image->size(), Gfx::IntSize(1, 1));
  148. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::Red);
  149. }
  150. TEST_CASE(test_not_ico)
  151. {
  152. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("png/buggie.png"sv)));
  153. EXPECT(!Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
  154. EXPECT(Gfx::ICOImageDecoderPlugin::create(file->bytes()).is_error());
  155. }
  156. TEST_CASE(test_bmp_embedded_in_ico)
  157. {
  158. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("ico/serenity.ico"sv)));
  159. EXPECT(Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
  160. auto plugin_decoder = TRY_OR_FAIL(Gfx::ICOImageDecoderPlugin::create(file->bytes()));
  161. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 16, 16 }));
  162. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::Transparent);
  163. EXPECT_EQ(frame.image->get_pixel(7, 4), Gfx::Color(161, 0, 0));
  164. }
  165. TEST_CASE(test_malformed_maskless_ico)
  166. {
  167. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("ico/malformed_maskless.ico"sv)));
  168. EXPECT(Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
  169. auto plugin_decoder = TRY_OR_FAIL(Gfx::ICOImageDecoderPlugin::create(file->bytes()));
  170. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 16, 16 }));
  171. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::Transparent);
  172. EXPECT_EQ(frame.image->get_pixel(7, 4), Gfx::Color(161, 0, 0));
  173. }
  174. TEST_CASE(test_ilbm)
  175. {
  176. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("ilbm/gradient.iff"sv)));
  177. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  178. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  179. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 200 }));
  180. EXPECT_EQ(frame.image->get_pixel(8, 0), Gfx::Color(0xee, 0xbb, 0, 255));
  181. }
  182. TEST_CASE(test_ilbm_uncompressed)
  183. {
  184. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("ilbm/gradient-uncompressed.iff"sv)));
  185. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  186. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  187. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 200 }));
  188. EXPECT_EQ(frame.image->get_pixel(8, 0), Gfx::Color(0xee, 0xbb, 0, 255));
  189. }
  190. TEST_CASE(test_ilbm_ham6)
  191. {
  192. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("ilbm/ham6.iff"sv)));
  193. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  194. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  195. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 256, 256 }));
  196. EXPECT_EQ(frame.image->get_pixel(77, 107), Gfx::Color(0xf0, 0x40, 0x40, 0xff));
  197. }
  198. TEST_CASE(test_ilbm_dos)
  199. {
  200. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("ilbm/serenity.lbm"sv)));
  201. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  202. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  203. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 640, 480 }));
  204. EXPECT_EQ(frame.image->get_pixel(315, 134), Gfx::Color::NamedColor::Red);
  205. }
  206. TEST_CASE(test_24bit)
  207. {
  208. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("ilbm/serenity-24bit.iff"sv)));
  209. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  210. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  211. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 640, 640 }));
  212. EXPECT_EQ(frame.image->get_pixel(158, 270), Gfx::Color(0xee, 0x3d, 0x3c, 255));
  213. }
  214. TEST_CASE(test_brush_transparent_color)
  215. {
  216. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("ilbm/brush-transparent-color.iff"sv)));
  217. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  218. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  219. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 266, 309 }));
  220. EXPECT_EQ(frame.image->get_pixel(114, 103), Gfx::Color::NamedColor::Black);
  221. }
  222. TEST_CASE(test_small_24bit)
  223. {
  224. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("ilbm/small-24bit.iff"sv)));
  225. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  226. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  227. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 10, 10 }));
  228. EXPECT_EQ(frame.image->get_pixel(0, 4), Gfx::Color(1, 0, 1, 255));
  229. }
  230. TEST_CASE(test_stencil_mask)
  231. {
  232. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("ilbm/test-stencil.iff"sv)));
  233. EXPECT(Gfx::ILBMImageDecoderPlugin::sniff(file->bytes()));
  234. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  235. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 200 }));
  236. EXPECT_EQ(frame.image->get_pixel(0, 4), Gfx::Color(0, 0, 0, 255));
  237. }
  238. TEST_CASE(test_ilbm_malformed_header)
  239. {
  240. Array test_inputs = {
  241. TEST_INPUT("ilbm/truncated-bmhd-chunk.iff"sv)
  242. };
  243. for (auto test_input : test_inputs) {
  244. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  245. auto plugin_decoder_or_error = Gfx::ILBMImageDecoderPlugin::create(file->bytes());
  246. EXPECT(plugin_decoder_or_error.is_error());
  247. }
  248. }
  249. TEST_CASE(test_ilbm_malformed_frame)
  250. {
  251. Array test_inputs = {
  252. TEST_INPUT("ilbm/incorrect-cmap-size.iff"sv),
  253. TEST_INPUT("ilbm/incorrect-uncompressed-size.iff"sv),
  254. TEST_INPUT("ilbm/missing-body-chunk.iff"sv)
  255. };
  256. for (auto test_input : test_inputs) {
  257. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  258. auto plugin_decoder = TRY_OR_FAIL(Gfx::ILBMImageDecoderPlugin::create(file->bytes()));
  259. auto frame_or_error = plugin_decoder->frame(0);
  260. EXPECT(frame_or_error.is_error());
  261. }
  262. }
  263. TEST_CASE(test_jbig2_size)
  264. {
  265. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jbig2/bitmap.jbig2"sv)));
  266. EXPECT(Gfx::JBIG2ImageDecoderPlugin::sniff(file->bytes()));
  267. auto plugin_decoder = TRY_OR_FAIL(Gfx::JBIG2ImageDecoderPlugin::create(file->bytes()));
  268. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(399, 400));
  269. }
  270. TEST_CASE(test_jbig2_black_47x23)
  271. {
  272. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jbig2/black_47x23.jbig2"sv)));
  273. EXPECT(Gfx::JBIG2ImageDecoderPlugin::sniff(file->bytes()));
  274. auto plugin_decoder = TRY_OR_FAIL(Gfx::JBIG2ImageDecoderPlugin::create(file->bytes()));
  275. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 47, 23 }));
  276. for (auto pixel : *frame.image)
  277. EXPECT_EQ(pixel, Gfx::Color(Gfx::Color::Black).value());
  278. }
  279. TEST_CASE(test_jbig2_white_47x23)
  280. {
  281. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jbig2/white_47x23.jbig2"sv)));
  282. EXPECT(Gfx::JBIG2ImageDecoderPlugin::sniff(file->bytes()));
  283. auto plugin_decoder = TRY_OR_FAIL(Gfx::JBIG2ImageDecoderPlugin::create(file->bytes()));
  284. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 47, 23 }));
  285. for (auto pixel : *frame.image)
  286. EXPECT_EQ(pixel, Gfx::Color(Gfx::Color::White).value());
  287. }
  288. TEST_CASE(test_jbig2_decode)
  289. {
  290. auto bmp_file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("bmp/bitmap.bmp"sv)));
  291. auto bmp_plugin_decoder = TRY_OR_FAIL(Gfx::BMPImageDecoderPlugin::create(bmp_file->bytes()));
  292. auto bmp_frame = TRY_OR_FAIL(expect_single_frame_of_size(*bmp_plugin_decoder, { 399, 400 }));
  293. Array test_inputs = {
  294. TEST_INPUT("jbig2/bitmap.jbig2"sv),
  295. TEST_INPUT("jbig2/bitmap-tpgdon.jbig2"sv),
  296. TEST_INPUT("jbig2/bitmap-template1.jbig2"sv),
  297. TEST_INPUT("jbig2/bitmap-template1-tpgdon.jbig2"sv),
  298. TEST_INPUT("jbig2/bitmap-template2.jbig2"sv),
  299. TEST_INPUT("jbig2/bitmap-template2-tpgdon.jbig2"sv),
  300. TEST_INPUT("jbig2/bitmap-template3.jbig2"sv),
  301. TEST_INPUT("jbig2/bitmap-template3-tpgdon.jbig2"sv),
  302. TEST_INPUT("jbig2/bitmap-symbol.jbig2"sv),
  303. TEST_INPUT("jbig2/bitmap-symbol-textrefine.jbig2"sv),
  304. };
  305. for (auto test_input : test_inputs) {
  306. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  307. EXPECT(Gfx::JBIG2ImageDecoderPlugin::sniff(file->bytes()));
  308. auto plugin_decoder = TRY_OR_FAIL(Gfx::JBIG2ImageDecoderPlugin::create(file->bytes()));
  309. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 399, 400 }));
  310. for (int y = 0; y < frame.image->height(); ++y)
  311. for (int x = 0; x < frame.image->width(); ++x)
  312. EXPECT_EQ(frame.image->get_pixel(x, y), bmp_frame.image->get_pixel(x, y));
  313. }
  314. }
  315. TEST_CASE(test_jbig2_arithmetic_decoder)
  316. {
  317. // https://www.itu.int/rec/T-REC-T.88-201808-I
  318. // H.2 Test sequence for arithmetic coder
  319. // clang-format off
  320. constexpr auto input = to_array<u8>({
  321. 0x84, 0xC7, 0x3B, 0xFC, 0xE1, 0xA1, 0x43, 0x04,
  322. 0x02, 0x20, 0x00, 0x00, 0x41, 0x0D, 0xBB, 0x86,
  323. 0xF4, 0x31, 0x7F, 0xFF, 0x88, 0xFF, 0x37, 0x47,
  324. 0x1A, 0xDB, 0x6A, 0xDF, 0xFF, 0xAC
  325. });
  326. constexpr auto output = to_array<u8>({
  327. 0x00, 0x02, 0x00, 0x51, 0x00, 0x00, 0x00, 0xC0,
  328. 0x03, 0x52, 0x87, 0x2A, 0xAA, 0xAA, 0xAA, 0xAA,
  329. 0x82, 0xC0, 0x20, 0x00, 0xFC, 0xD7, 0x9E, 0xF6,
  330. 0xBF, 0x7F, 0xED, 0x90, 0x4F, 0x46, 0xA3, 0xBF
  331. });
  332. // clang-format on
  333. // "For this entire test, a single value of CX is used. I(CX) is initially 0 and MPS(CX) is initially 0."
  334. Gfx::JBIG2::ArithmeticDecoder::Context context { 0, 0 };
  335. auto decoder = MUST(Gfx::JBIG2::ArithmeticDecoder::initialize(input));
  336. for (auto expected : output) {
  337. u8 actual = 0;
  338. for (size_t i = 0; i < 8; ++i) {
  339. actual <<= 1;
  340. actual |= static_cast<u8>(decoder.get_next_bit(context));
  341. }
  342. EXPECT_EQ(actual, expected);
  343. }
  344. }
  345. TEST_CASE(test_jpeg_sof0_one_scan)
  346. {
  347. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/rgb24.jpg"sv)));
  348. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  349. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  350. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  351. }
  352. TEST_CASE(test_jpeg_sof0_several_scans)
  353. {
  354. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/several_scans.jpg"sv)));
  355. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  356. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  357. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  358. }
  359. TEST_CASE(test_odd_mcu_restart_interval)
  360. {
  361. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/odd-restart.jpg"sv)));
  362. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  363. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  364. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 102, 77 }));
  365. }
  366. TEST_CASE(test_jpeg_rgb_components)
  367. {
  368. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/rgb_components.jpg"sv)));
  369. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  370. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  371. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  372. }
  373. TEST_CASE(test_jpeg_ycck)
  374. {
  375. Array test_inputs = {
  376. TEST_INPUT("jpg/ycck-1111.jpg"sv),
  377. TEST_INPUT("jpg/ycck-2111.jpg"sv),
  378. TEST_INPUT("jpg/ycck-2112.jpg"sv),
  379. };
  380. for (auto test_input : test_inputs) {
  381. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  382. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  383. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  384. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  385. // Compare difference between pixels so we don't depend on exact CMYK->RGB conversion behavior.
  386. // These two pixels are currently off by one in R.
  387. // FIXME: For 2111, they're off by way more.
  388. EXPECT(frame.image->get_pixel(6, 319).distance_squared_to(frame.image->get_pixel(6, 320)) < 1.0f / 255.0f);
  389. }
  390. }
  391. TEST_CASE(test_jpeg_sof2_spectral_selection)
  392. {
  393. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/spectral_selection.jpg"sv)));
  394. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  395. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  396. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 592, 800 }));
  397. }
  398. TEST_CASE(test_jpeg_sof0_several_scans_odd_number_mcu)
  399. {
  400. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/several_scans_odd_number_mcu.jpg"sv)));
  401. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  402. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  403. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 600, 600 }));
  404. }
  405. TEST_CASE(test_jpeg_sof2_successive_aproximation)
  406. {
  407. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/successive_approximation.jpg"sv)));
  408. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  409. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  410. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 600, 800 }));
  411. }
  412. TEST_CASE(test_jpeg_sof1_12bits)
  413. {
  414. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/12-bit.jpg"sv)));
  415. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  416. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  417. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 240 }));
  418. }
  419. TEST_CASE(test_jpeg_sof2_12bits)
  420. {
  421. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/12-bit-progressive.jpg"sv)));
  422. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  423. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  424. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 240 }));
  425. }
  426. TEST_CASE(test_jpeg_empty_icc)
  427. {
  428. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/gradient_empty_icc.jpg"sv)));
  429. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  430. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  431. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 80, 80 }));
  432. }
  433. TEST_CASE(test_jpeg_grayscale_with_app14)
  434. {
  435. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/grayscale_app14.jpg"sv)));
  436. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  437. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  438. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 80, 80 }));
  439. }
  440. TEST_CASE(test_jpeg_grayscale_with_weird_mcu_and_reset_marker)
  441. {
  442. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/grayscale_mcu.jpg"sv)));
  443. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  444. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  445. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 320, 240 }));
  446. }
  447. TEST_CASE(test_jpeg_malformed_header)
  448. {
  449. Array test_inputs = {
  450. TEST_INPUT("jpg/oss-fuzz-testcase-59785.jpg"sv)
  451. };
  452. for (auto test_input : test_inputs) {
  453. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  454. auto plugin_decoder_or_error = Gfx::JPEGImageDecoderPlugin::create(file->bytes());
  455. EXPECT(plugin_decoder_or_error.is_error());
  456. }
  457. }
  458. TEST_CASE(test_jpeg_malformed_frame)
  459. {
  460. Array test_inputs = {
  461. TEST_INPUT("jpg/oss-fuzz-testcase-62584.jpg"sv),
  462. TEST_INPUT("jpg/oss-fuzz-testcase-63815.jpg"sv)
  463. };
  464. for (auto test_input : test_inputs) {
  465. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  466. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  467. auto frame_or_error = plugin_decoder->frame(0);
  468. EXPECT(frame_or_error.is_error());
  469. }
  470. }
  471. TEST_CASE(test_pam_rgb)
  472. {
  473. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("pnm/2x1.pam"sv)));
  474. EXPECT(Gfx::PAMImageDecoderPlugin::sniff(file->bytes()));
  475. auto plugin_decoder = TRY_OR_FAIL(Gfx::PAMImageDecoderPlugin::create(file->bytes()));
  476. auto frame = TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  477. EXPECT_EQ(frame.image->size(), Gfx::IntSize(2, 1));
  478. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color('0', 'z', '0'));
  479. EXPECT_EQ(frame.image->get_pixel(1, 0), Gfx::Color('0', '0', 'z'));
  480. }
  481. TEST_CASE(test_pam_cmyk)
  482. {
  483. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("pnm/2x1-cmyk.pam"sv)));
  484. EXPECT(Gfx::PAMImageDecoderPlugin::sniff(file->bytes()));
  485. auto plugin_decoder = TRY_OR_FAIL(Gfx::PAMImageDecoderPlugin::create(file->bytes()));
  486. EXPECT_EQ(plugin_decoder->natural_frame_format(), Gfx::NaturalFrameFormat::CMYK);
  487. auto cmyk_frame = TRY_OR_FAIL(plugin_decoder->cmyk_frame());
  488. EXPECT_EQ(cmyk_frame->size(), Gfx::IntSize(2, 1));
  489. EXPECT_EQ(cmyk_frame->begin()[0], (Gfx::CMYK { '0', 'z', '0', 'y' }));
  490. EXPECT_EQ(cmyk_frame->begin()[1], (Gfx::CMYK { '0', '0', 'z', 'y' }));
  491. auto frame = TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  492. EXPECT_EQ(frame.image->size(), Gfx::IntSize(2, 1));
  493. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color('l', 'E', 'l'));
  494. EXPECT_EQ(frame.image->get_pixel(1, 0), Gfx::Color('l', 'l', 'E'));
  495. }
  496. TEST_CASE(test_pbm)
  497. {
  498. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.pbm"sv)));
  499. EXPECT(Gfx::PBMImageDecoderPlugin::sniff(file->bytes()));
  500. auto plugin_decoder = TRY_OR_FAIL(Gfx::PBMImageDecoderPlugin::create(file->bytes()));
  501. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  502. }
  503. TEST_CASE(test_pgm)
  504. {
  505. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.pgm"sv)));
  506. EXPECT(Gfx::PGMImageDecoderPlugin::sniff(file->bytes()));
  507. auto plugin_decoder = TRY_OR_FAIL(Gfx::PGMImageDecoderPlugin::create(file->bytes()));
  508. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  509. }
  510. TEST_CASE(test_png)
  511. {
  512. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("png/buggie.png"sv)));
  513. EXPECT(Gfx::PNGImageDecoderPlugin::sniff(file->bytes()));
  514. auto plugin_decoder = TRY_OR_FAIL(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
  515. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  516. }
  517. TEST_CASE(test_png_malformed_frame)
  518. {
  519. Array test_inputs = {
  520. TEST_INPUT("png/oss-fuzz-testcase-62371.png"sv),
  521. TEST_INPUT("png/oss-fuzz-testcase-63052.png"sv)
  522. };
  523. for (auto test_input : test_inputs) {
  524. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  525. auto plugin_decoder = TRY_OR_FAIL(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
  526. auto frame_or_error = plugin_decoder->frame(0);
  527. EXPECT(frame_or_error.is_error());
  528. }
  529. }
  530. TEST_CASE(test_ppm)
  531. {
  532. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("pnm/buggie-raw.ppm"sv)));
  533. EXPECT(Gfx::PPMImageDecoderPlugin::sniff(file->bytes()));
  534. auto plugin_decoder = TRY_OR_FAIL(Gfx::PPMImageDecoderPlugin::create(file->bytes()));
  535. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  536. }
  537. TEST_CASE(test_targa_bottom_left)
  538. {
  539. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-uncompressed.tga"sv)));
  540. EXPECT(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()));
  541. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  542. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  543. }
  544. TEST_CASE(test_targa_top_left)
  545. {
  546. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-uncompressed.tga"sv)));
  547. EXPECT(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()));
  548. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  549. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  550. }
  551. TEST_CASE(test_targa_bottom_left_compressed)
  552. {
  553. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-compressed.tga"sv)));
  554. EXPECT(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()));
  555. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  556. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  557. }
  558. TEST_CASE(test_targa_top_left_compressed)
  559. {
  560. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-compressed.tga"sv)));
  561. EXPECT(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()));
  562. auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  563. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  564. }
  565. TEST_CASE(test_tiff_uncompressed)
  566. {
  567. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/uncompressed.tiff"sv)));
  568. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  569. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  570. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  571. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  572. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  573. }
  574. TEST_CASE(test_tiff_ccitt_rle)
  575. {
  576. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/ccitt_rle.tiff"sv)));
  577. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  578. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  579. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  580. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  581. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Black);
  582. }
  583. TEST_CASE(test_tiff_ccitt3)
  584. {
  585. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/ccitt3.tiff"sv)));
  586. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  587. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  588. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  589. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  590. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Black);
  591. }
  592. TEST_CASE(test_tiff_ccitt3_fill)
  593. {
  594. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/ccitt3_1d_fill.tiff"sv)));
  595. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  596. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  597. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 6, 4 }));
  598. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  599. EXPECT_EQ(frame.image->get_pixel(3, 0), Gfx::Color::NamedColor::Black);
  600. EXPECT_EQ(frame.image->get_pixel(2, 2), Gfx::Color::NamedColor::White);
  601. EXPECT_EQ(frame.image->get_pixel(5, 3), Gfx::Color::NamedColor::White);
  602. }
  603. TEST_CASE(test_tiff_ccitt3_2d)
  604. {
  605. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/ccitt3_2d.tiff"sv)));
  606. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  607. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  608. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  609. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  610. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Black);
  611. }
  612. TEST_CASE(test_tiff_ccitt3_2d_fill)
  613. {
  614. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/ccitt3_2d_fill.tiff"sv)));
  615. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  616. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  617. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  618. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  619. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Black);
  620. }
  621. TEST_CASE(test_tiff_ccitt4)
  622. {
  623. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/ccitt4.tiff"sv)));
  624. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  625. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  626. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  627. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  628. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Black);
  629. }
  630. TEST_CASE(test_tiff_lzw)
  631. {
  632. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/lzw.tiff"sv)));
  633. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  634. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  635. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  636. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  637. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  638. }
  639. TEST_CASE(test_tiff_deflate)
  640. {
  641. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/deflate.tiff"sv)));
  642. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  643. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  644. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  645. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  646. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  647. }
  648. TEST_CASE(test_tiff_krita)
  649. {
  650. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/krita.tif"sv)));
  651. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  652. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  653. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  654. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  655. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  656. }
  657. TEST_CASE(test_tiff_orientation)
  658. {
  659. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/orientation.tiff"sv)));
  660. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  661. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  662. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 300, 400 }));
  663. // Orientation is Rotate90Clockwise
  664. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  665. EXPECT_EQ(frame.image->get_pixel(300 - 75, 60), Gfx::Color::NamedColor::Red);
  666. }
  667. TEST_CASE(test_tiff_packed_bits)
  668. {
  669. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/packed_bits.tiff"sv)));
  670. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  671. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  672. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  673. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  674. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  675. }
  676. TEST_CASE(test_tiff_grayscale)
  677. {
  678. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/grayscale.tiff"sv)));
  679. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  680. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  681. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  682. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  683. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color(130, 130, 130));
  684. }
  685. TEST_CASE(test_tiff_grayscale_alpha)
  686. {
  687. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/grayscale_alpha.tiff"sv)));
  688. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  689. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  690. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  691. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 0);
  692. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color(130, 130, 130));
  693. }
  694. TEST_CASE(test_tiff_rgb_alpha)
  695. {
  696. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/rgb_alpha.tiff"sv)));
  697. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  698. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  699. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  700. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 0);
  701. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  702. }
  703. TEST_CASE(test_tiff_palette_alpha)
  704. {
  705. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/rgb_palette_alpha.tiff"sv)));
  706. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  707. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  708. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  709. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 0);
  710. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  711. }
  712. TEST_CASE(test_tiff_alpha_predictor)
  713. {
  714. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/alpha_predictor.tiff"sv)));
  715. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  716. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  717. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  718. EXPECT_EQ(frame.image->get_pixel(0, 0).alpha(), 255);
  719. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  720. }
  721. TEST_CASE(test_tiff_16_bits)
  722. {
  723. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/16_bits.tiff"sv)));
  724. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  725. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  726. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  727. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  728. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  729. }
  730. TEST_CASE(test_tiff_cmyk)
  731. {
  732. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/cmyk.tiff"sv)));
  733. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  734. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  735. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  736. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  737. // I stripped the ICC profile from the image, so we can't test for equality with Red here.
  738. EXPECT_NE(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::White);
  739. }
  740. TEST_CASE(test_tiff_tiled)
  741. {
  742. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/tiled.tiff"sv)));
  743. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  744. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  745. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 300 }));
  746. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
  747. EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
  748. }
  749. TEST_CASE(test_tiff_invalid_tag)
  750. {
  751. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/invalid_tag.tiff"sv)));
  752. EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
  753. auto plugin_decoder = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
  754. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 10, 10 }));
  755. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::Black);
  756. EXPECT_EQ(frame.image->get_pixel(0, 9), Gfx::Color::NamedColor::White);
  757. }
  758. TEST_CASE(test_webp_simple_lossy)
  759. {
  760. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8.webp"sv)));
  761. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  762. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  763. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 240, 240 }));
  764. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  765. // So pixels changing by 1 or so below is fine if you change code.
  766. EXPECT_EQ(frame.image->get_pixel(120, 232), Gfx::Color(0xf2, 0xef, 0xf0, 255));
  767. EXPECT_EQ(frame.image->get_pixel(198, 202), Gfx::Color(0x7b, 0xaa, 0xd5, 255));
  768. }
  769. TEST_CASE(test_webp_simple_lossless)
  770. {
  771. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8l.webp"sv)));
  772. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  773. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  774. // Ironically, simple-vp8l.webp is a much more complex file than extended-lossless.webp tested below.
  775. // extended-lossless.webp tests the decoding basics.
  776. // This here tests the predictor, color, and subtract green transforms,
  777. // as well as meta prefix images, one-element canonical code handling,
  778. // and handling of canonical codes with more than 288 elements.
  779. // This image uses all 13 predictor modes of the predictor transform.
  780. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 386, 395 }));
  781. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0));
  782. // This pixel tests all predictor modes except 5, 7, 8, 9, and 13.
  783. EXPECT_EQ(frame.image->get_pixel(289, 332), Gfx::Color(0xf2, 0xee, 0xd3, 255));
  784. }
  785. TEST_CASE(test_webp_simple_lossless_alpha_used_false)
  786. {
  787. // This file is identical to simple-vp8l.webp, but the `is_alpha_used` used bit is false.
  788. // The file still contains alpha data. This tests that the decoder replaces the stored alpha data with 0xff if `is_alpha_used` is false.
  789. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8l-alpha-used-false.webp"sv)));
  790. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  791. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  792. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 386, 395 }));
  793. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0xff));
  794. }
  795. TEST_CASE(test_webp_extended_lossy)
  796. {
  797. // This extended lossy image has an ALPH chunk for (losslessly compressed) alpha data.
  798. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/extended-lossy.webp"sv)));
  799. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  800. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  801. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 417, 223 }));
  802. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  803. // So pixels changing by 1 or so below is fine if you change code.
  804. EXPECT_EQ(frame.image->get_pixel(89, 72), Gfx::Color(255, 1, 0, 255));
  805. EXPECT_EQ(frame.image->get_pixel(174, 69), Gfx::Color(0, 255, 0, 255));
  806. EXPECT_EQ(frame.image->get_pixel(245, 84), Gfx::Color(0, 0, 255, 255));
  807. EXPECT_EQ(frame.image->get_pixel(352, 125), Gfx::Color(0, 0, 0, 128));
  808. EXPECT_EQ(frame.image->get_pixel(355, 106), Gfx::Color(0, 0, 0, 0));
  809. // Check same basic pixels as in test_webp_extended_lossless too.
  810. // (The top-left pixel in the lossy version is fully transparent white, compared to fully transparent black in the lossless version).
  811. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(255, 255, 255, 0));
  812. EXPECT_EQ(frame.image->get_pixel(43, 75), Gfx::Color(255, 0, 2, 255));
  813. EXPECT_EQ(frame.image->get_pixel(141, 75), Gfx::Color(0, 255, 3, 255));
  814. EXPECT_EQ(frame.image->get_pixel(235, 75), Gfx::Color(0, 0, 255, 255));
  815. EXPECT_EQ(frame.image->get_pixel(341, 75), Gfx::Color(0, 0, 0, 128));
  816. }
  817. TEST_CASE(test_webp_extended_lossy_alpha_horizontal_filter)
  818. {
  819. // Also lossy rgb + lossless alpha, but with a horizontal alpha filtering method.
  820. // The image should look like smolkling.webp, but with a horizontal alpha gradient.
  821. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/smolkling-horizontal-alpha.webp"sv)));
  822. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  823. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  824. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  825. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  826. // So pixels changing by 1 or so below is fine if you change code.
  827. // 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.
  828. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x8f, 0x51, 0x2f, 0x4b));
  829. }
  830. TEST_CASE(test_webp_extended_lossy_alpha_vertical_filter)
  831. {
  832. // Also lossy rgb + lossless alpha, but with a vertical alpha filtering method.
  833. // The image should look like smolkling.webp, but with a vertical alpha gradient, and with a fully transparent first column.
  834. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/smolkling-vertical-alpha.webp"sv)));
  835. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  836. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  837. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  838. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  839. // So pixels changing by 1 or so below is fine if you change code.
  840. // 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.
  841. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x94, 0x50, 0x32, 0x4c));
  842. }
  843. TEST_CASE(test_webp_extended_lossy_alpha_gradient_filter)
  844. {
  845. // Also lossy rgb + lossless alpha, but with a gradient alpha filtering method.
  846. // 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.
  847. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/smolkling-gradient-alpha.webp"sv)));
  848. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  849. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  850. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  851. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  852. // So pixels changing by 1 or so below is fine if you change code.
  853. // 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.
  854. // In particular, the center of the image should be fully opaque, not fully transparent.
  855. EXPECT_EQ(frame.image->get_pixel(131, 131), Gfx::Color(0x8c, 0x47, 0x2e, 255));
  856. }
  857. TEST_CASE(test_webp_extended_lossy_uncompressed_alpha)
  858. {
  859. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/extended-lossy-uncompressed-alpha.webp"sv)));
  860. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  861. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  862. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 417, 223 }));
  863. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  864. // So pixels changing by 1 or so below is fine if you change code.
  865. EXPECT_EQ(frame.image->get_pixel(89, 72), Gfx::Color(255, 0, 4, 255));
  866. EXPECT_EQ(frame.image->get_pixel(174, 69), Gfx::Color(4, 255, 0, 255));
  867. EXPECT_EQ(frame.image->get_pixel(245, 84), Gfx::Color(0, 0, 255, 255));
  868. EXPECT_EQ(frame.image->get_pixel(352, 125), Gfx::Color(0, 0, 0, 128));
  869. EXPECT_EQ(frame.image->get_pixel(355, 106), Gfx::Color(0, 0, 0, 0));
  870. }
  871. TEST_CASE(test_webp_extended_lossy_negative_quantization_offset)
  872. {
  873. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/smolkling.webp"sv)));
  874. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  875. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  876. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 264, 264 }));
  877. // While VP8 YUV contents are defined bit-exact, the YUV->RGB conversion isn't.
  878. // So pixels changing by 1 or so below is fine if you change code.
  879. EXPECT_EQ(frame.image->get_pixel(16, 16), Gfx::Color(0x3c, 0x24, 0x1a, 255));
  880. }
  881. TEST_CASE(test_webp_lossy_4)
  882. {
  883. // This is https://commons.wikimedia.org/wiki/File:Fr%C3%BChling_bl%C3%BChender_Kirschenbaum.jpg,
  884. // under the Creative Commons Attribution-Share Alike 3.0 Unported license. The image was re-encoded
  885. // as webp at https://developers.google.com/speed/webp/gallery1 and the webp version is from there.
  886. // No other changes have been made.
  887. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/4.webp"sv)));
  888. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  889. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  890. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1024, 772 }));
  891. // This image tests macroblocks that have `skip_coefficients` set to true, and it test a boolean entropy decoder edge case.
  892. EXPECT_EQ(frame.image->get_pixel(780, 570), Gfx::Color(0x72, 0xc8, 0xf6, 255));
  893. }
  894. TEST_CASE(test_webp_lossy_4_with_partitions)
  895. {
  896. // Same input file as in the previous test, but re-encoded to use 8 secondary partitions.
  897. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/4-with-8-partitions.webp"sv)));
  898. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  899. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  900. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1024, 772 }));
  901. EXPECT_EQ(frame.image->get_pixel(780, 570), Gfx::Color(0x73, 0xc9, 0xf9, 255));
  902. }
  903. TEST_CASE(test_webp_extended_lossless)
  904. {
  905. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/extended-lossless.webp"sv)));
  906. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  907. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  908. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 417, 223 }));
  909. // Check some basic pixels.
  910. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0));
  911. EXPECT_EQ(frame.image->get_pixel(43, 75), Gfx::Color(255, 0, 0, 255));
  912. EXPECT_EQ(frame.image->get_pixel(141, 75), Gfx::Color(0, 255, 0, 255));
  913. EXPECT_EQ(frame.image->get_pixel(235, 75), Gfx::Color(0, 0, 255, 255));
  914. EXPECT_EQ(frame.image->get_pixel(341, 75), Gfx::Color(0, 0, 0, 128));
  915. // Check pixels using the color cache.
  916. EXPECT_EQ(frame.image->get_pixel(94, 73), Gfx::Color(255, 0, 0, 255));
  917. EXPECT_EQ(frame.image->get_pixel(176, 115), Gfx::Color(0, 255, 0, 255));
  918. EXPECT_EQ(frame.image->get_pixel(290, 89), Gfx::Color(0, 0, 255, 255));
  919. EXPECT_EQ(frame.image->get_pixel(359, 73), Gfx::Color(0, 0, 0, 128));
  920. }
  921. TEST_CASE(test_webp_simple_lossless_color_index_transform)
  922. {
  923. // In addition to testing the index transform, this file also tests handling of explicity setting max_symbol.
  924. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/Qpalette.webp"sv)));
  925. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  926. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  927. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 256, 256 }));
  928. EXPECT_EQ(frame.image->get_pixel(100, 100), Gfx::Color(0x73, 0x37, 0x23, 0xff));
  929. }
  930. TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling)
  931. {
  932. struct TestCase {
  933. StringView file_name;
  934. Gfx::Color line_color;
  935. Gfx::Color background_color;
  936. };
  937. // The number after the dash is the number of colors in each file's color index bitmap.
  938. // catdog-alert-2 tests the 1-bit-per-pixel case,
  939. // catdog-alert-3 tests the 2-bit-per-pixel case,
  940. // catdog-alert-8 and catdog-alert-13 both test the 4-bits-per-pixel case.
  941. // catdog-alert-13-alpha-used-false is like catdog-alert-13, but with is_alpha_used set to false in the header
  942. // (which has the effect of ignoring the alpha information in the palette and instead always setting alpha to 0xff).
  943. TestCase test_cases[] = {
  944. { "webp/catdog-alert-2.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0xf3, 0xe6, 0xd8, 0xff) },
  945. { "webp/catdog-alert-3.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0, 0, 0, 0) },
  946. { "webp/catdog-alert-8.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  947. { "webp/catdog-alert-13.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  948. { "webp/catdog-alert-13-alpha-used-false.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 255) },
  949. };
  950. for (auto test_case : test_cases) {
  951. auto file = TRY_OR_FAIL(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), test_case.file_name))));
  952. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  953. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  954. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 32, 32 }));
  955. EXPECT_EQ(frame.image->get_pixel(4, 0), test_case.background_color);
  956. EXPECT_EQ(frame.image->get_pixel(5, 0), test_case.line_color);
  957. EXPECT_EQ(frame.image->get_pixel(9, 5), test_case.background_color);
  958. EXPECT_EQ(frame.image->get_pixel(10, 5), test_case.line_color);
  959. EXPECT_EQ(frame.image->get_pixel(11, 5), test_case.background_color);
  960. }
  961. }
  962. TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling_odd_width)
  963. {
  964. StringView file_names[] = {
  965. "webp/width11-height11-colors2.webp"sv,
  966. "webp/width11-height11-colors3.webp"sv,
  967. "webp/width11-height11-colors15.webp"sv,
  968. };
  969. for (auto file_name : file_names) {
  970. auto file = TRY_OR_FAIL(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), file_name))));
  971. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  972. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 11, 11 }));
  973. }
  974. }
  975. TEST_CASE(test_webp_extended_lossless_animated)
  976. {
  977. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/extended-lossless-animated.webp"sv)));
  978. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  979. auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  980. EXPECT_EQ(plugin_decoder->frame_count(), 8u);
  981. EXPECT(plugin_decoder->is_animated());
  982. EXPECT_EQ(plugin_decoder->loop_count(), 42u);
  983. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(990, 1050));
  984. for (size_t frame_index = 0; frame_index < plugin_decoder->frame_count(); ++frame_index) {
  985. auto frame = TRY_OR_FAIL(plugin_decoder->frame(frame_index));
  986. EXPECT_EQ(frame.image->size(), Gfx::IntSize(990, 1050));
  987. // This pixel happens to be the same color in all frames.
  988. EXPECT_EQ(frame.image->get_pixel(500, 700), Gfx::Color::Yellow);
  989. // This one isn't the same in all frames.
  990. EXPECT_EQ(frame.image->get_pixel(500, 0), (frame_index == 2 || frame_index == 6) ? Gfx::Color::Black : Gfx::Color(255, 255, 255, 0));
  991. }
  992. }
  993. TEST_CASE(test_tvg)
  994. {
  995. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tvg/yak.tvg"sv)));
  996. EXPECT(Gfx::TinyVGImageDecoderPlugin::sniff(file->bytes()));
  997. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  998. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1024, 1024 }));
  999. }
  1000. TEST_CASE(test_everything_tvg)
  1001. {
  1002. Array file_names {
  1003. TEST_INPUT("tvg/everything.tvg"sv),
  1004. TEST_INPUT("tvg/everything-32.tvg"sv)
  1005. };
  1006. for (auto file_name : file_names) {
  1007. auto file = TRY_OR_FAIL(Core::MappedFile::map(file_name));
  1008. EXPECT(Gfx::TinyVGImageDecoderPlugin::sniff(file->bytes()));
  1009. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  1010. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 400, 768 }));
  1011. }
  1012. }
  1013. TEST_CASE(test_tvg_malformed)
  1014. {
  1015. Array test_inputs = {
  1016. TEST_INPUT("tvg/bogus-color-table-size.tvg"sv)
  1017. };
  1018. for (auto test_input : test_inputs) {
  1019. auto file = TRY_OR_FAIL(Core::MappedFile::map(test_input));
  1020. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  1021. auto frame_or_error = plugin_decoder->frame(0);
  1022. EXPECT(frame_or_error.is_error());
  1023. }
  1024. }
  1025. TEST_CASE(test_tvg_rgb565)
  1026. {
  1027. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tvg/green-rgb565.tvg"sv)));
  1028. EXPECT(Gfx::TinyVGImageDecoderPlugin::sniff(file->bytes()));
  1029. auto plugin_decoder = TRY_OR_FAIL(Gfx::TinyVGImageDecoderPlugin::create(file->bytes()));
  1030. auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 100, 100 }));
  1031. // Should be a solid dark green:
  1032. EXPECT_EQ(frame.image->get_pixel(50, 50), Gfx::Color(0, 130, 0));
  1033. }
  1034. TEST_CASE(test_jxl_modular_simple_tree_upsample2_10bits)
  1035. {
  1036. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jxl/modular_simple_tree_upsample2_10bits_rct.jxl"sv)));
  1037. EXPECT(Gfx::JPEGXLImageDecoderPlugin::sniff(file->bytes()));
  1038. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGXLImageDecoderPlugin::create(file->bytes()));
  1039. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 128, 128 }));
  1040. auto frame = TRY_OR_FAIL(plugin_decoder->frame(0));
  1041. EXPECT_EQ(frame.image->get_pixel(42, 57), Gfx::Color::from_string("#4c0072"sv));
  1042. }
  1043. TEST_CASE(test_jxl_modular_property_8)
  1044. {
  1045. auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jxl/modular_property_8.jxl"sv)));
  1046. EXPECT(Gfx::JPEGXLImageDecoderPlugin::sniff(file->bytes()));
  1047. auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGXLImageDecoderPlugin::create(file->bytes()));
  1048. TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 32, 32 }));
  1049. auto frame = TRY_OR_FAIL(plugin_decoder->frame(0));
  1050. for (u8 i = 0; i < 32; ++i) {
  1051. for (u8 j = 0; j < 32; ++j) {
  1052. auto const color = frame.image->get_pixel(i, j);
  1053. if ((i + j) % 2 == 0)
  1054. EXPECT_EQ(color, Gfx::Color::Black);
  1055. else
  1056. EXPECT_EQ(color, Gfx::Color::Yellow);
  1057. }
  1058. }
  1059. }
  1060. TEST_CASE(test_dds)
  1061. {
  1062. Array file_names = {
  1063. TEST_INPUT("dds/catdog-alert-29x29.dds"sv),
  1064. TEST_INPUT("dds/catdog-alert-32x32.dds"sv)
  1065. };
  1066. for (auto file_name : file_names) {
  1067. auto file = TRY_OR_FAIL(Core::MappedFile::map(file_name));
  1068. EXPECT(Gfx::DDSImageDecoderPlugin::sniff(file->bytes()));
  1069. auto plugin_decoder = TRY_OR_FAIL(Gfx::DDSImageDecoderPlugin::create(file->bytes()));
  1070. TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
  1071. }
  1072. }