TestImageDecoder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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_pbm)
  114. {
  115. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.pbm"sv)));
  116. EXPECT(Gfx::PBMImageDecoderPlugin::sniff(file->bytes()));
  117. auto plugin_decoder = MUST(Gfx::PBMImageDecoderPlugin::create(file->bytes()));
  118. EXPECT(plugin_decoder->initialize());
  119. EXPECT(plugin_decoder->frame_count());
  120. EXPECT(!plugin_decoder->is_animated());
  121. EXPECT(!plugin_decoder->loop_count());
  122. auto frame = MUST(plugin_decoder->frame(0));
  123. EXPECT(frame.duration == 0);
  124. }
  125. TEST_CASE(test_pgm)
  126. {
  127. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.pgm"sv)));
  128. EXPECT(Gfx::PGMImageDecoderPlugin::sniff(file->bytes()));
  129. auto plugin_decoder = MUST(Gfx::PGMImageDecoderPlugin::create(file->bytes()));
  130. EXPECT(plugin_decoder->initialize());
  131. EXPECT(plugin_decoder->frame_count());
  132. EXPECT(!plugin_decoder->is_animated());
  133. EXPECT(!plugin_decoder->loop_count());
  134. auto frame = MUST(plugin_decoder->frame(0));
  135. EXPECT(frame.duration == 0);
  136. }
  137. TEST_CASE(test_png)
  138. {
  139. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie.png"sv)));
  140. EXPECT(Gfx::PNGImageDecoderPlugin::sniff(file->bytes()));
  141. auto plugin_decoder = MUST(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
  142. EXPECT(plugin_decoder->initialize());
  143. EXPECT(plugin_decoder->frame_count());
  144. EXPECT(!plugin_decoder->is_animated());
  145. EXPECT(!plugin_decoder->loop_count());
  146. auto frame = MUST(plugin_decoder->frame(0));
  147. EXPECT(frame.duration == 0);
  148. }
  149. TEST_CASE(test_ppm)
  150. {
  151. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.ppm"sv)));
  152. EXPECT(Gfx::PPMImageDecoderPlugin::sniff(file->bytes()));
  153. auto plugin_decoder = MUST(Gfx::PPMImageDecoderPlugin::create(file->bytes()));
  154. EXPECT(plugin_decoder->initialize());
  155. EXPECT(plugin_decoder->frame_count());
  156. EXPECT(!plugin_decoder->is_animated());
  157. EXPECT(!plugin_decoder->loop_count());
  158. auto frame = MUST(plugin_decoder->frame(0));
  159. EXPECT(frame.duration == 0);
  160. }
  161. TEST_CASE(test_targa_bottom_left)
  162. {
  163. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-bottom-left-uncompressed.tga"sv)));
  164. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  165. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  166. EXPECT(plugin_decoder->initialize());
  167. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  168. EXPECT(!plugin_decoder->is_animated());
  169. EXPECT(!plugin_decoder->loop_count());
  170. auto frame = MUST(plugin_decoder->frame(0));
  171. EXPECT(frame.duration == 0);
  172. }
  173. TEST_CASE(test_targa_top_left)
  174. {
  175. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-top-left-uncompressed.tga"sv)));
  176. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  177. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  178. EXPECT(plugin_decoder->initialize());
  179. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  180. EXPECT(!plugin_decoder->is_animated());
  181. EXPECT(!plugin_decoder->loop_count());
  182. auto frame = MUST(plugin_decoder->frame(0));
  183. EXPECT(frame.duration == 0);
  184. }
  185. TEST_CASE(test_targa_bottom_left_compressed)
  186. {
  187. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-bottom-left-compressed.tga"sv)));
  188. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  189. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  190. EXPECT(plugin_decoder->initialize());
  191. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  192. EXPECT(!plugin_decoder->is_animated());
  193. EXPECT(!plugin_decoder->loop_count());
  194. auto frame = MUST(plugin_decoder->frame(0));
  195. EXPECT(frame.duration == 0);
  196. }
  197. TEST_CASE(test_targa_top_left_compressed)
  198. {
  199. auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-top-left-compressed.tga"sv)));
  200. EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
  201. auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
  202. EXPECT(plugin_decoder->initialize());
  203. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  204. EXPECT(!plugin_decoder->is_animated());
  205. EXPECT(!plugin_decoder->loop_count());
  206. auto frame = MUST(plugin_decoder->frame(0));
  207. EXPECT(frame.duration == 0);
  208. }
  209. TEST_CASE(test_webp_simple_lossy)
  210. {
  211. auto file = MUST(Core::MappedFile::map(TEST_INPUT("simple-vp8.webp"sv)));
  212. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  213. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  214. EXPECT(plugin_decoder->initialize());
  215. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  216. EXPECT(!plugin_decoder->is_animated());
  217. EXPECT(!plugin_decoder->loop_count());
  218. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(240, 240));
  219. }
  220. TEST_CASE(test_webp_simple_lossless)
  221. {
  222. auto file = MUST(Core::MappedFile::map(TEST_INPUT("simple-vp8l.webp"sv)));
  223. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  224. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  225. EXPECT(plugin_decoder->initialize());
  226. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  227. EXPECT(!plugin_decoder->is_animated());
  228. EXPECT(!plugin_decoder->loop_count());
  229. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(386, 395));
  230. }
  231. TEST_CASE(test_webp_extended_lossy)
  232. {
  233. auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossy.webp"sv)));
  234. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  235. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  236. EXPECT(plugin_decoder->initialize());
  237. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  238. EXPECT(!plugin_decoder->is_animated());
  239. EXPECT(!plugin_decoder->loop_count());
  240. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(417, 223));
  241. }
  242. TEST_CASE(test_webp_extended_lossless)
  243. {
  244. auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossless.webp"sv)));
  245. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  246. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  247. EXPECT(plugin_decoder->initialize());
  248. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  249. EXPECT(!plugin_decoder->is_animated());
  250. EXPECT(!plugin_decoder->loop_count());
  251. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(417, 223));
  252. }
  253. TEST_CASE(test_webp_extended_lossless_animated)
  254. {
  255. auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossless-animated.webp"sv)));
  256. EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
  257. auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
  258. EXPECT(plugin_decoder->initialize());
  259. EXPECT_EQ(plugin_decoder->frame_count(), 8u);
  260. EXPECT(plugin_decoder->is_animated());
  261. EXPECT_EQ(plugin_decoder->loop_count(), 42u);
  262. EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(990, 1050));
  263. }