TestImageDecoder.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <LibTest/TestCase.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. TEST_CASE(test_bmp)
  22. {
  23. auto file = Core::MappedFile::map("/res/html/misc/bmpsuite_files/rgba32-1.bmp"sv).release_value();
  24. auto bmp = Gfx::BMPImageDecoderPlugin((u8 const*)file->data(), file->size());
  25. EXPECT(bmp.frame_count());
  26. EXPECT(bmp.sniff());
  27. EXPECT(!bmp.is_animated());
  28. EXPECT(!bmp.loop_count());
  29. auto frame = bmp.frame(0).release_value_but_fixme_should_propagate_errors();
  30. EXPECT(frame.duration == 0);
  31. }
  32. TEST_CASE(test_gif)
  33. {
  34. auto file = Core::MappedFile::map("/res/graphics/download-animation.gif"sv).release_value();
  35. auto gif = Gfx::GIFImageDecoderPlugin((u8 const*)file->data(), file->size());
  36. EXPECT(gif.frame_count());
  37. EXPECT(gif.sniff());
  38. EXPECT(gif.is_animated());
  39. EXPECT(!gif.loop_count());
  40. auto frame = gif.frame(1).release_value_but_fixme_should_propagate_errors();
  41. EXPECT(frame.duration == 400);
  42. }
  43. TEST_CASE(test_not_ico)
  44. {
  45. auto file = Core::MappedFile::map("/res/graphics/buggie.png"sv).release_value();
  46. auto ico = Gfx::ICOImageDecoderPlugin((u8 const*)file->data(), file->size());
  47. EXPECT(ico.frame_count());
  48. EXPECT(!ico.sniff());
  49. EXPECT(!ico.is_animated());
  50. EXPECT(!ico.loop_count());
  51. EXPECT(ico.frame(0).is_error());
  52. }
  53. TEST_CASE(test_bmp_embedded_in_ico)
  54. {
  55. auto file = Core::MappedFile::map("/res/icons/16x16/serenity.ico"sv).release_value();
  56. auto ico = Gfx::ICOImageDecoderPlugin((u8 const*)file->data(), file->size());
  57. EXPECT(ico.frame_count());
  58. EXPECT(ico.sniff());
  59. EXPECT(!ico.is_animated());
  60. EXPECT(!ico.loop_count());
  61. EXPECT(!ico.frame(0).is_error());
  62. }
  63. TEST_CASE(test_jpg)
  64. {
  65. auto file = Core::MappedFile::map("/res/html/misc/bmpsuite_files/rgb24.jpg"sv).release_value();
  66. auto jpg = Gfx::JPGImageDecoderPlugin((u8 const*)file->data(), file->size());
  67. EXPECT(jpg.frame_count());
  68. EXPECT(jpg.sniff());
  69. EXPECT(!jpg.is_animated());
  70. EXPECT(!jpg.loop_count());
  71. auto frame = jpg.frame(0).release_value_but_fixme_should_propagate_errors();
  72. EXPECT(frame.duration == 0);
  73. }
  74. TEST_CASE(test_pbm)
  75. {
  76. auto file = Core::MappedFile::map("/res/html/misc/pbmsuite_files/buggie-raw.pbm"sv).release_value();
  77. auto pbm = Gfx::PBMImageDecoderPlugin((u8 const*)file->data(), file->size());
  78. EXPECT(pbm.frame_count());
  79. EXPECT(pbm.sniff());
  80. EXPECT(!pbm.is_animated());
  81. EXPECT(!pbm.loop_count());
  82. auto frame = pbm.frame(0).release_value_but_fixme_should_propagate_errors();
  83. EXPECT(frame.duration == 0);
  84. }
  85. TEST_CASE(test_pgm)
  86. {
  87. auto file = Core::MappedFile::map("/res/html/misc/pgmsuite_files/buggie-raw.pgm"sv).release_value();
  88. auto pgm = Gfx::PGMImageDecoderPlugin((u8 const*)file->data(), file->size());
  89. EXPECT(pgm.frame_count());
  90. EXPECT(pgm.sniff());
  91. EXPECT(!pgm.is_animated());
  92. EXPECT(!pgm.loop_count());
  93. auto frame = pgm.frame(0).release_value_but_fixme_should_propagate_errors();
  94. EXPECT(frame.duration == 0);
  95. }
  96. TEST_CASE(test_png)
  97. {
  98. auto file = Core::MappedFile::map("/res/graphics/buggie.png"sv).release_value();
  99. auto png = Gfx::PNGImageDecoderPlugin((u8 const*)file->data(), file->size());
  100. EXPECT(png.frame_count());
  101. EXPECT(png.sniff());
  102. EXPECT(!png.is_animated());
  103. EXPECT(!png.loop_count());
  104. auto frame = png.frame(0).release_value_but_fixme_should_propagate_errors();
  105. EXPECT(frame.duration == 0);
  106. }
  107. TEST_CASE(test_ppm)
  108. {
  109. auto file = Core::MappedFile::map("/res/html/misc/ppmsuite_files/buggie-raw.ppm"sv).release_value();
  110. auto ppm = Gfx::PPMImageDecoderPlugin((u8 const*)file->data(), file->size());
  111. EXPECT(ppm.frame_count());
  112. EXPECT(ppm.sniff());
  113. EXPECT(!ppm.is_animated());
  114. EXPECT(!ppm.loop_count());
  115. auto frame = ppm.frame(0).release_value_but_fixme_should_propagate_errors();
  116. EXPECT(frame.duration == 0);
  117. }