TestImageDecoder.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/DeprecatedString.h>
  8. #include <LibCore/MappedFile.h>
  9. #include <LibGfx/ImageFormats/BMPLoader.h>
  10. #include <LibGfx/ImageFormats/GIFLoader.h>
  11. #include <LibGfx/ImageFormats/ICOLoader.h>
  12. #include <LibGfx/ImageFormats/ImageDecoder.h>
  13. #include <LibGfx/ImageFormats/JPEGLoader.h>
  14. #include <LibGfx/ImageFormats/PBMLoader.h>
  15. #include <LibGfx/ImageFormats/PGMLoader.h>
  16. #include <LibGfx/ImageFormats/PNGLoader.h>
  17. #include <LibGfx/ImageFormats/PPMLoader.h>
  18. #include <LibGfx/ImageFormats/TGALoader.h>
  19. #include <LibGfx/ImageFormats/WebPLoader.h>
  20. #include <LibTest/TestCase.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #ifdef AK_OS_SERENITY
  24. # define TEST_INPUT(x) ("/usr/Tests/LibGfx/test-inputs/" x)
  25. #else
  26. # define TEST_INPUT(x) ("test-inputs/" x)
  27. #endif
  28. TEST_CASE(test_bmp)
  29. {
  30. auto file = MUST(Core::MappedFile::map(TEST_INPUT("rgba32-1.bmp"sv)));
  31. EXPECT(Gfx::BMPImageDecoderPlugin::sniff(file->bytes()));
  32. auto plugin_decoder = MUST(Gfx::BMPImageDecoderPlugin::create(file->bytes()));
  33. EXPECT(plugin_decoder->initialize());
  34. EXPECT(plugin_decoder->frame_count());
  35. EXPECT(!plugin_decoder->is_animated());
  36. EXPECT(!plugin_decoder->loop_count());
  37. auto frame = MUST(plugin_decoder->frame(0));
  38. EXPECT(frame.duration == 0);
  39. }
  40. TEST_CASE(test_gif)
  41. {
  42. auto file = MUST(Core::MappedFile::map(TEST_INPUT("download-animation.gif"sv)));
  43. EXPECT(Gfx::GIFImageDecoderPlugin::sniff(file->bytes()));
  44. auto plugin_decoder = MUST(Gfx::GIFImageDecoderPlugin::create(file->bytes()));
  45. EXPECT(plugin_decoder->initialize());
  46. EXPECT(plugin_decoder->frame_count());
  47. EXPECT(plugin_decoder->is_animated());
  48. EXPECT(!plugin_decoder->loop_count());
  49. auto frame = MUST(plugin_decoder->frame(1));
  50. EXPECT(frame.duration == 400);
  51. }
  52. TEST_CASE(test_not_ico)
  53. {
  54. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie.png"sv)));
  55. EXPECT(!Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
  56. auto plugin_decoder = MUST(Gfx::ICOImageDecoderPlugin::create(file->bytes()));
  57. EXPECT(!plugin_decoder->initialize());
  58. EXPECT(plugin_decoder->frame_count());
  59. EXPECT(!plugin_decoder->is_animated());
  60. EXPECT(!plugin_decoder->loop_count());
  61. EXPECT(plugin_decoder->frame(0).is_error());
  62. }
  63. TEST_CASE(test_bmp_embedded_in_ico)
  64. {
  65. auto file = MUST(Core::MappedFile::map(TEST_INPUT("serenity.ico"sv)));
  66. EXPECT(Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
  67. auto plugin_decoder = MUST(Gfx::ICOImageDecoderPlugin::create(file->bytes()));
  68. EXPECT(plugin_decoder->initialize());
  69. EXPECT(plugin_decoder->frame_count());
  70. EXPECT(!plugin_decoder->is_animated());
  71. EXPECT(!plugin_decoder->loop_count());
  72. EXPECT(!plugin_decoder->frame(0).is_error());
  73. }
  74. TEST_CASE(test_jpeg_sof0_one_scan)
  75. {
  76. auto file = MUST(Core::MappedFile::map(TEST_INPUT("rgb24.jpg"sv)));
  77. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  78. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  79. EXPECT(plugin_decoder->initialize());
  80. EXPECT(plugin_decoder->frame_count());
  81. EXPECT(!plugin_decoder->is_animated());
  82. EXPECT(!plugin_decoder->loop_count());
  83. auto frame = MUST(plugin_decoder->frame(0));
  84. EXPECT(frame.duration == 0);
  85. }
  86. TEST_CASE(test_jpeg_sof0_several_scans)
  87. {
  88. auto file = MUST(Core::MappedFile::map(TEST_INPUT("several_scans.jpg"sv)));
  89. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  90. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  91. EXPECT(plugin_decoder->initialize());
  92. auto frame = MUST(plugin_decoder->frame(0));
  93. EXPECT_EQ(frame.image->size(), Gfx::IntSize(592, 800));
  94. }
  95. TEST_CASE(test_jpeg_rgb_components)
  96. {
  97. auto file = MUST(Core::MappedFile::map(TEST_INPUT("rgb_components.jpg"sv)));
  98. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  99. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  100. EXPECT(plugin_decoder->initialize());
  101. auto frame = MUST(plugin_decoder->frame(0));
  102. EXPECT_EQ(frame.image->size(), Gfx::IntSize(592, 800));
  103. }
  104. TEST_CASE(test_jpeg_sof2_spectral_selection)
  105. {
  106. auto file = MUST(Core::MappedFile::map(TEST_INPUT("spectral_selection.jpg"sv)));
  107. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  108. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  109. EXPECT(plugin_decoder->initialize());
  110. auto frame = MUST(plugin_decoder->frame(0));
  111. EXPECT_EQ(frame.image->size(), Gfx::IntSize(592, 800));
  112. }
  113. TEST_CASE(test_jpeg_sof0_several_scans_odd_number_mcu)
  114. {
  115. auto file = MUST(Core::MappedFile::map(TEST_INPUT("several_scans_odd_number_mcu.jpg"sv)));
  116. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  117. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  118. EXPECT(plugin_decoder->initialize());
  119. auto frame = MUST(plugin_decoder->frame(0));
  120. EXPECT_EQ(frame.image->size(), Gfx::IntSize(600, 600));
  121. }
  122. TEST_CASE(test_jpeg_sof2_successive_aproximation)
  123. {
  124. auto file = MUST(Core::MappedFile::map(TEST_INPUT("successive_approximation.jpg"sv)));
  125. EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
  126. auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
  127. EXPECT(plugin_decoder->initialize());
  128. auto frame = MUST(plugin_decoder->frame(0));
  129. EXPECT_EQ(frame.image->size(), Gfx::IntSize(600, 800));
  130. }
  131. TEST_CASE(test_pbm)
  132. {
  133. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.pbm"sv)));
  134. EXPECT(Gfx::PBMImageDecoderPlugin::sniff(file->bytes()));
  135. auto plugin_decoder = MUST(Gfx::PBMImageDecoderPlugin::create(file->bytes()));
  136. EXPECT(plugin_decoder->initialize());
  137. EXPECT(plugin_decoder->frame_count());
  138. EXPECT(!plugin_decoder->is_animated());
  139. EXPECT(!plugin_decoder->loop_count());
  140. auto frame = MUST(plugin_decoder->frame(0));
  141. EXPECT(frame.duration == 0);
  142. }
  143. TEST_CASE(test_pgm)
  144. {
  145. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.pgm"sv)));
  146. EXPECT(Gfx::PGMImageDecoderPlugin::sniff(file->bytes()));
  147. auto plugin_decoder = MUST(Gfx::PGMImageDecoderPlugin::create(file->bytes()));
  148. EXPECT(plugin_decoder->initialize());
  149. EXPECT(plugin_decoder->frame_count());
  150. EXPECT(!plugin_decoder->is_animated());
  151. EXPECT(!plugin_decoder->loop_count());
  152. auto frame = MUST(plugin_decoder->frame(0));
  153. EXPECT(frame.duration == 0);
  154. }
  155. TEST_CASE(test_png)
  156. {
  157. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie.png"sv)));
  158. EXPECT(Gfx::PNGImageDecoderPlugin::sniff(file->bytes()));
  159. auto plugin_decoder = MUST(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
  160. EXPECT(plugin_decoder->initialize());
  161. EXPECT(plugin_decoder->frame_count());
  162. EXPECT(!plugin_decoder->is_animated());
  163. EXPECT(!plugin_decoder->loop_count());
  164. auto frame = MUST(plugin_decoder->frame(0));
  165. EXPECT(frame.duration == 0);
  166. }
  167. TEST_CASE(test_ppm)
  168. {
  169. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.ppm"sv)));
  170. EXPECT(Gfx::PPMImageDecoderPlugin::sniff(file->bytes()));
  171. auto plugin_decoder = MUST(Gfx::PPMImageDecoderPlugin::create(file->bytes()));
  172. EXPECT(plugin_decoder->initialize());
  173. EXPECT(plugin_decoder->frame_count());
  174. EXPECT(!plugin_decoder->is_animated());
  175. EXPECT(!plugin_decoder->loop_count());
  176. auto frame = MUST(plugin_decoder->frame(0));
  177. EXPECT(frame.duration == 0);
  178. }
  179. TEST_CASE(test_targa_bottom_left)
  180. {
  181. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-bottom-left-uncompressed.tga"sv)));
  182. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  183. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  184. EXPECT(plugin_decoder->initialize());
  185. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  186. EXPECT(!plugin_decoder->is_animated());
  187. EXPECT(!plugin_decoder->loop_count());
  188. auto frame = MUST(plugin_decoder->frame(0));
  189. EXPECT(frame.duration == 0);
  190. }
  191. TEST_CASE(test_targa_top_left)
  192. {
  193. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-top-left-uncompressed.tga"sv)));
  194. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  195. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  196. EXPECT(plugin_decoder->initialize());
  197. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  198. EXPECT(!plugin_decoder->is_animated());
  199. EXPECT(!plugin_decoder->loop_count());
  200. auto frame = MUST(plugin_decoder->frame(0));
  201. EXPECT(frame.duration == 0);
  202. }
  203. TEST_CASE(test_targa_bottom_left_compressed)
  204. {
  205. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-bottom-left-compressed.tga"sv)));
  206. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  207. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  208. EXPECT(plugin_decoder->initialize());
  209. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  210. EXPECT(!plugin_decoder->is_animated());
  211. EXPECT(!plugin_decoder->loop_count());
  212. auto frame = MUST(plugin_decoder->frame(0));
  213. EXPECT(frame.duration == 0);
  214. }
  215. TEST_CASE(test_targa_top_left_compressed)
  216. {
  217. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-top-left-compressed.tga"sv)));
  218. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  219. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  220. EXPECT(plugin_decoder->initialize());
  221. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  222. EXPECT(!plugin_decoder->is_animated());
  223. EXPECT(!plugin_decoder->loop_count());
  224. auto frame = MUST(plugin_decoder->frame(0));
  225. EXPECT(frame.duration == 0);
  226. }
  227. TEST_CASE(test_webp_simple_lossy)
  228. {
  229. auto file = MUST(Core::MappedFile::map(TEST_INPUT("simple-vp8.webp"sv)));
  230. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  231. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  232. EXPECT(plugin_decoder->initialize());
  233. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  234. EXPECT(!plugin_decoder->is_animated());
  235. EXPECT(!plugin_decoder->loop_count());
  236. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(240, 240));
  237. }
  238. TEST_CASE(test_webp_simple_lossless)
  239. {
  240. auto file = MUST(Core::MappedFile::map(TEST_INPUT("simple-vp8l.webp"sv)));
  241. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  242. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  243. EXPECT(plugin_decoder->initialize());
  244. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  245. EXPECT(!plugin_decoder->is_animated());
  246. EXPECT(!plugin_decoder->loop_count());
  247. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(386, 395));
  248. // Ironically, simple-vp8l.webp is a much more complex file than extended-lossless.webp tested below.
  249. // extended-lossless.webp tests the decoding basics.
  250. // This here tests the predictor, color, and subtract green transforms,
  251. // as well as meta prefix images, one-element canonical code handling,
  252. // and handling of canonical codes with more than 288 elements.
  253. // This image uses all 13 predictor modes of the predictor transform.
  254. auto frame = MUST(plugin_decoder->frame(0));
  255. EXPECT_EQ(frame.image->size(), Gfx::IntSize(386, 395));
  256. // This pixel tests all predictor modes except 5, 7, 8, 9, and 13.
  257. EXPECT_EQ(frame.image->get_pixel(289, 332), Gfx::Color(0xf2, 0xee, 0xd3, 255));
  258. }
  259. TEST_CASE(test_webp_extended_lossy)
  260. {
  261. auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossy.webp"sv)));
  262. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  263. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  264. EXPECT(plugin_decoder->initialize());
  265. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  266. EXPECT(!plugin_decoder->is_animated());
  267. EXPECT(!plugin_decoder->loop_count());
  268. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(417, 223));
  269. }
  270. TEST_CASE(test_webp_extended_lossless)
  271. {
  272. auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossless.webp"sv)));
  273. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  274. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  275. EXPECT(plugin_decoder->initialize());
  276. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  277. EXPECT(!plugin_decoder->is_animated());
  278. EXPECT(!plugin_decoder->loop_count());
  279. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(417, 223));
  280. auto frame = MUST(plugin_decoder->frame(0));
  281. EXPECT_EQ(frame.image->size(), Gfx::IntSize(417, 223));
  282. // Check some basic pixels.
  283. EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(0, 0, 0, 0));
  284. EXPECT_EQ(frame.image->get_pixel(43, 75), Gfx::Color(255, 0, 0, 255));
  285. EXPECT_EQ(frame.image->get_pixel(141, 75), Gfx::Color(0, 255, 0, 255));
  286. EXPECT_EQ(frame.image->get_pixel(235, 75), Gfx::Color(0, 0, 255, 255));
  287. EXPECT_EQ(frame.image->get_pixel(341, 75), Gfx::Color(0, 0, 0, 128));
  288. // Check pixels using the color cache.
  289. EXPECT_EQ(frame.image->get_pixel(94, 73), Gfx::Color(255, 0, 0, 255));
  290. EXPECT_EQ(frame.image->get_pixel(176, 115), Gfx::Color(0, 255, 0, 255));
  291. EXPECT_EQ(frame.image->get_pixel(290, 89), Gfx::Color(0, 0, 255, 255));
  292. EXPECT_EQ(frame.image->get_pixel(359, 73), Gfx::Color(0, 0, 0, 128));
  293. }
  294. TEST_CASE(test_webp_simple_lossless_color_index_transform)
  295. {
  296. // In addition to testing the index transform, this file also tests handling of explicity setting max_symbol.
  297. auto file = MUST(Core::MappedFile::map(TEST_INPUT("Qpalette.webp"sv)));
  298. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  299. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  300. EXPECT(plugin_decoder->initialize());
  301. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  302. EXPECT(!plugin_decoder->is_animated());
  303. EXPECT(!plugin_decoder->loop_count());
  304. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(256, 256));
  305. auto frame = MUST(plugin_decoder->frame(0));
  306. EXPECT_EQ(frame.image->size(), Gfx::IntSize(256, 256));
  307. EXPECT_EQ(frame.image->get_pixel(100, 100), Gfx::Color(0x73, 0x37, 0x23, 0xff));
  308. }
  309. TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling)
  310. {
  311. struct TestCase {
  312. StringView file_name;
  313. Gfx::Color line_color;
  314. Gfx::Color background_color;
  315. };
  316. // The number after the dash is the number of colors in each file's color index bitmap.
  317. // catdog-alert-2 tests the 1-bit-per-pixel case,
  318. // catdog-alert-3 tests the 2-bit-per-pixel case,
  319. // catdog-alert-8 and catdog-alert-13 both test the 4-bits-per-pixel case.
  320. TestCase test_cases[] = {
  321. { "catdog-alert-2.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0xf3, 0xe6, 0xd8, 0xff) },
  322. { "catdog-alert-3.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0, 0, 0, 0) },
  323. { "catdog-alert-8.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  324. { "catdog-alert-13.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) },
  325. };
  326. for (auto test_case : test_cases) {
  327. auto file = MUST(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), test_case.file_name))));
  328. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  329. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  330. EXPECT(plugin_decoder->initialize());
  331. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  332. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(32, 32));
  333. auto frame = MUST(plugin_decoder->frame(0));
  334. EXPECT_EQ(frame.image->size(), Gfx::IntSize(32, 32));
  335. EXPECT_EQ(frame.image->get_pixel(4, 0), test_case.background_color);
  336. EXPECT_EQ(frame.image->get_pixel(5, 0), test_case.line_color);
  337. EXPECT_EQ(frame.image->get_pixel(9, 5), test_case.background_color);
  338. EXPECT_EQ(frame.image->get_pixel(10, 5), test_case.line_color);
  339. EXPECT_EQ(frame.image->get_pixel(11, 5), test_case.background_color);
  340. }
  341. }
  342. TEST_CASE(test_webp_extended_lossless_animated)
  343. {
  344. auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossless-animated.webp"sv)));
  345. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  346. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  347. EXPECT(plugin_decoder->initialize());
  348. EXPECT_EQ(plugin_decoder->frame_count(), 8u);
  349. EXPECT(plugin_decoder->is_animated());
  350. EXPECT_EQ(plugin_decoder->loop_count(), 42u);
  351. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(990, 1050));
  352. }