TestImageDecoder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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/BMPLoader.h>
  10. #include <LibGfx/GIFLoader.h>
  11. #include <LibGfx/ICOLoader.h>
  12. #include <LibGfx/ImageDecoder.h>
  13. #include <LibGfx/JPGLoader.h>
  14. #include <LibGfx/PBMLoader.h>
  15. #include <LibGfx/PGMLoader.h>
  16. #include <LibGfx/PNGLoader.h>
  17. #include <LibGfx/PPMLoader.h>
  18. #include <LibGfx/TGALoader.h>
  19. #include <LibTest/TestCase.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. TEST_CASE(test_bmp)
  23. {
  24. auto file = Core::MappedFile::map("/res/html/misc/bmpsuite_files/rgba32-1.bmp"sv).release_value();
  25. EXPECT_EQ(MUST(Gfx::BMPImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
  26. auto plugin_decoder_or_error = Gfx::BMPImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
  27. EXPECT(!plugin_decoder_or_error.is_error());
  28. auto plugin_decoder = plugin_decoder_or_error.release_value();
  29. EXPECT_EQ(plugin_decoder->initialize(), true);
  30. EXPECT(plugin_decoder->frame_count());
  31. EXPECT(!plugin_decoder->is_animated());
  32. EXPECT(!plugin_decoder->loop_count());
  33. auto frame = plugin_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
  34. EXPECT(frame.duration == 0);
  35. }
  36. TEST_CASE(test_gif)
  37. {
  38. auto file = Core::MappedFile::map("/res/graphics/download-animation.gif"sv).release_value();
  39. EXPECT_EQ(MUST(Gfx::GIFImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
  40. auto plugin_decoder_or_error = Gfx::GIFImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
  41. EXPECT(!plugin_decoder_or_error.is_error());
  42. auto plugin_decoder = plugin_decoder_or_error.release_value();
  43. EXPECT_EQ(plugin_decoder->initialize(), true);
  44. EXPECT(plugin_decoder->frame_count());
  45. EXPECT(!plugin_decoder->is_animated());
  46. EXPECT(!plugin_decoder->loop_count());
  47. auto frame = plugin_decoder->frame(1).release_value_but_fixme_should_propagate_errors();
  48. EXPECT(frame.duration == 400);
  49. }
  50. TEST_CASE(test_not_ico)
  51. {
  52. auto file = Core::MappedFile::map("/res/graphics/buggie.png"sv).release_value();
  53. EXPECT_EQ(MUST(Gfx::ICOImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
  54. auto plugin_decoder_or_error = Gfx::ICOImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
  55. EXPECT(!plugin_decoder_or_error.is_error());
  56. auto plugin_decoder = plugin_decoder_or_error.release_value();
  57. EXPECT_EQ(plugin_decoder->initialize(), true);
  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 = Core::MappedFile::map("/res/icons/16x16/serenity.ico"sv).release_value();
  66. EXPECT_EQ(MUST(Gfx::ICOImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
  67. auto plugin_decoder_or_error = Gfx::ICOImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
  68. EXPECT(!plugin_decoder_or_error.is_error());
  69. auto plugin_decoder = plugin_decoder_or_error.release_value();
  70. EXPECT_EQ(plugin_decoder->initialize(), true);
  71. EXPECT(plugin_decoder->frame_count());
  72. EXPECT(!plugin_decoder->is_animated());
  73. EXPECT(!plugin_decoder->loop_count());
  74. EXPECT(!plugin_decoder->frame(0).is_error());
  75. }
  76. TEST_CASE(test_jpg)
  77. {
  78. auto file = Core::MappedFile::map("/res/html/misc/bmpsuite_files/rgb24.jpg"sv).release_value();
  79. EXPECT_EQ(MUST(Gfx::JPGImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
  80. auto plugin_decoder_or_error = Gfx::JPGImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
  81. EXPECT(!plugin_decoder_or_error.is_error());
  82. auto plugin_decoder = plugin_decoder_or_error.release_value();
  83. EXPECT_EQ(plugin_decoder->initialize(), true);
  84. EXPECT(plugin_decoder->frame_count());
  85. EXPECT(!plugin_decoder->is_animated());
  86. EXPECT(!plugin_decoder->loop_count());
  87. EXPECT(!plugin_decoder->frame(0).is_error());
  88. auto frame = plugin_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
  89. EXPECT(frame.duration == 0);
  90. }
  91. TEST_CASE(test_pbm)
  92. {
  93. auto file = Core::MappedFile::map("/res/html/misc/pbmsuite_files/buggie-raw.pbm"sv).release_value();
  94. EXPECT_EQ(MUST(Gfx::PBMImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
  95. auto plugin_decoder_or_error = Gfx::PBMImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
  96. EXPECT(!plugin_decoder_or_error.is_error());
  97. auto plugin_decoder = plugin_decoder_or_error.release_value();
  98. EXPECT_EQ(plugin_decoder->initialize(), true);
  99. EXPECT(plugin_decoder->frame_count());
  100. EXPECT(!plugin_decoder->is_animated());
  101. EXPECT(!plugin_decoder->loop_count());
  102. EXPECT(!plugin_decoder->frame(0).is_error());
  103. auto frame = plugin_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
  104. EXPECT(frame.duration == 0);
  105. }
  106. TEST_CASE(test_pgm)
  107. {
  108. auto file = Core::MappedFile::map("/res/html/misc/pgmsuite_files/buggie-raw.pgm"sv).release_value();
  109. EXPECT_EQ(MUST(Gfx::PGMImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
  110. auto plugin_decoder_or_error = Gfx::PGMImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
  111. EXPECT(!plugin_decoder_or_error.is_error());
  112. auto plugin_decoder = plugin_decoder_or_error.release_value();
  113. EXPECT_EQ(plugin_decoder->initialize(), true);
  114. EXPECT(plugin_decoder->frame_count());
  115. EXPECT(!plugin_decoder->is_animated());
  116. EXPECT(!plugin_decoder->loop_count());
  117. EXPECT(!plugin_decoder->frame(0).is_error());
  118. auto frame = plugin_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
  119. EXPECT(frame.duration == 0);
  120. }
  121. TEST_CASE(test_png)
  122. {
  123. auto file = Core::MappedFile::map("/res/graphics/buggie.png"sv).release_value();
  124. EXPECT_EQ(MUST(Gfx::PNGImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
  125. auto plugin_decoder_or_error = Gfx::PNGImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
  126. EXPECT(!plugin_decoder_or_error.is_error());
  127. auto plugin_decoder = plugin_decoder_or_error.release_value();
  128. EXPECT_EQ(plugin_decoder->initialize(), true);
  129. EXPECT(plugin_decoder->frame_count());
  130. EXPECT(!plugin_decoder->is_animated());
  131. EXPECT(!plugin_decoder->loop_count());
  132. EXPECT(!plugin_decoder->frame(0).is_error());
  133. auto frame = plugin_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
  134. EXPECT(frame.duration == 0);
  135. }
  136. TEST_CASE(test_ppm)
  137. {
  138. auto file = Core::MappedFile::map("/res/html/misc/ppmsuite_files/buggie-raw.ppm"sv).release_value();
  139. EXPECT_EQ(MUST(Gfx::PPMImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
  140. auto plugin_decoder_or_error = Gfx::PPMImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
  141. EXPECT(!plugin_decoder_or_error.is_error());
  142. auto plugin_decoder = plugin_decoder_or_error.release_value();
  143. EXPECT_EQ(plugin_decoder->initialize(), true);
  144. EXPECT(plugin_decoder->frame_count());
  145. EXPECT(!plugin_decoder->is_animated());
  146. EXPECT(!plugin_decoder->loop_count());
  147. EXPECT(!plugin_decoder->frame(0).is_error());
  148. auto frame = plugin_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
  149. EXPECT(frame.duration == 0);
  150. }
  151. TEST_CASE(test_targa_bottom_left)
  152. {
  153. auto file = Core::MappedFile::map("/res/html/misc/targasuite_files/buggie-bottom-left-uncompressed.tga"sv).release_value();
  154. EXPECT_EQ(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create({ (u8 const*)file->data(), file->size() })), true);
  155. auto plugin_decoder_or_error = Gfx::TGAImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
  156. EXPECT(!plugin_decoder_or_error.is_error());
  157. auto plugin_decoder = plugin_decoder_or_error.release_value();
  158. EXPECT_EQ(plugin_decoder->initialize(), true);
  159. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  160. EXPECT(!plugin_decoder->is_animated());
  161. EXPECT(!plugin_decoder->loop_count());
  162. EXPECT(!plugin_decoder->frame(0).is_error());
  163. auto frame_or_error = plugin_decoder->frame(0);
  164. EXPECT(!frame_or_error.is_error());
  165. auto frame = frame_or_error.release_value();
  166. EXPECT(frame.duration == 0);
  167. }
  168. TEST_CASE(test_targa_top_left)
  169. {
  170. auto file = Core::MappedFile::map("/res/html/misc/targasuite_files/buggie-top-left-uncompressed.tga"sv).release_value();
  171. EXPECT_EQ(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create({ (u8 const*)file->data(), file->size() })), true);
  172. auto plugin_decoder_or_error = Gfx::TGAImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
  173. EXPECT(!plugin_decoder_or_error.is_error());
  174. auto plugin_decoder = plugin_decoder_or_error.release_value();
  175. EXPECT_EQ(plugin_decoder->initialize(), true);
  176. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  177. EXPECT(!plugin_decoder->is_animated());
  178. EXPECT(!plugin_decoder->loop_count());
  179. EXPECT(!plugin_decoder->frame(0).is_error());
  180. auto frame_or_error = plugin_decoder->frame(0);
  181. EXPECT(!frame_or_error.is_error());
  182. auto frame = frame_or_error.release_value();
  183. EXPECT(frame.duration == 0);
  184. }
  185. TEST_CASE(test_targa_bottom_left_compressed)
  186. {
  187. auto file = Core::MappedFile::map("/res/html/misc/targasuite_files/buggie-bottom-left-compressed.tga"sv).release_value();
  188. EXPECT_EQ(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create({ (u8 const*)file->data(), file->size() })), true);
  189. auto plugin_decoder_or_error = Gfx::TGAImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
  190. EXPECT(!plugin_decoder_or_error.is_error());
  191. auto plugin_decoder = plugin_decoder_or_error.release_value();
  192. EXPECT_EQ(plugin_decoder->initialize(), true);
  193. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  194. EXPECT(!plugin_decoder->is_animated());
  195. EXPECT(!plugin_decoder->loop_count());
  196. EXPECT(!plugin_decoder->frame(0).is_error());
  197. auto frame_or_error = plugin_decoder->frame(0);
  198. EXPECT(!frame_or_error.is_error());
  199. auto frame = frame_or_error.release_value();
  200. EXPECT(frame.duration == 0);
  201. }
  202. TEST_CASE(test_targa_top_left_compressed)
  203. {
  204. auto file = Core::MappedFile::map("/res/html/misc/targasuite_files/buggie-top-left-compressed.tga"sv).release_value();
  205. EXPECT_EQ(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create({ (u8 const*)file->data(), file->size() })), true);
  206. auto plugin_decoder_or_error = Gfx::TGAImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
  207. EXPECT(!plugin_decoder_or_error.is_error());
  208. auto plugin_decoder = plugin_decoder_or_error.release_value();
  209. EXPECT_EQ(plugin_decoder->initialize(), true);
  210. EXPECT_EQ(plugin_decoder->frame_count(), 1u);
  211. EXPECT(!plugin_decoder->is_animated());
  212. EXPECT(!plugin_decoder->loop_count());
  213. EXPECT(!plugin_decoder->frame(0).is_error());
  214. auto frame_or_error = plugin_decoder->frame(0);
  215. EXPECT(!frame_or_error.is_error());
  216. auto frame = frame_or_error.release_value();
  217. EXPECT(frame.duration == 0);
  218. }