ImageDecoder.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/LexicalPath.h>
  7. #include <LibGfx/BMPLoader.h>
  8. #include <LibGfx/DDSLoader.h>
  9. #include <LibGfx/GIFLoader.h>
  10. #include <LibGfx/ICOLoader.h>
  11. #include <LibGfx/ImageDecoder.h>
  12. #include <LibGfx/JPGLoader.h>
  13. #include <LibGfx/PBMLoader.h>
  14. #include <LibGfx/PGMLoader.h>
  15. #include <LibGfx/PNGLoader.h>
  16. #include <LibGfx/PPMLoader.h>
  17. #include <LibGfx/QOILoader.h>
  18. #include <LibGfx/TGALoader.h>
  19. namespace Gfx {
  20. struct ImagePluginInitializer {
  21. ErrorOr<bool> (*sniff)(ReadonlyBytes) = nullptr;
  22. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> (*create)(ReadonlyBytes) = nullptr;
  23. };
  24. static constexpr ImagePluginInitializer s_initializers[] = {
  25. { PNGImageDecoderPlugin::sniff, PNGImageDecoderPlugin::create },
  26. { GIFImageDecoderPlugin::sniff, GIFImageDecoderPlugin::create },
  27. { BMPImageDecoderPlugin::sniff, BMPImageDecoderPlugin::create },
  28. { PBMImageDecoderPlugin::sniff, PBMImageDecoderPlugin::create },
  29. { PGMImageDecoderPlugin::sniff, PGMImageDecoderPlugin::create },
  30. { PPMImageDecoderPlugin::sniff, PPMImageDecoderPlugin::create },
  31. { ICOImageDecoderPlugin::sniff, ICOImageDecoderPlugin::create },
  32. { JPGImageDecoderPlugin::sniff, JPGImageDecoderPlugin::create },
  33. { DDSImageDecoderPlugin::sniff, DDSImageDecoderPlugin::create },
  34. { QOIImageDecoderPlugin::sniff, QOIImageDecoderPlugin::create },
  35. };
  36. struct ImagePluginWithMIMETypeInitializer {
  37. ErrorOr<bool> (*validate_before_create)(ReadonlyBytes) = nullptr;
  38. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> (*create)(ReadonlyBytes) = nullptr;
  39. StringView mime_type;
  40. };
  41. static constexpr ImagePluginWithMIMETypeInitializer s_initializers_with_mime_type[] = {
  42. { TGAImageDecoderPlugin::validate_before_create, TGAImageDecoderPlugin::create, "image/x-targa"sv },
  43. };
  44. static OwnPtr<ImageDecoderPlugin> probe_and_sniff_for_appropriate_plugin(ReadonlyBytes bytes)
  45. {
  46. for (auto& plugin : s_initializers) {
  47. auto sniff_result = plugin.sniff(bytes).release_value_but_fixme_should_propagate_errors();
  48. if (!sniff_result)
  49. continue;
  50. auto plugin_decoder = plugin.create(bytes).release_value_but_fixme_should_propagate_errors();
  51. if (plugin_decoder->initialize())
  52. return plugin_decoder;
  53. }
  54. return {};
  55. }
  56. static OwnPtr<ImageDecoderPlugin> probe_and_sniff_for_appropriate_plugin_with_known_mime_type(StringView mime_type, ReadonlyBytes bytes)
  57. {
  58. for (auto& plugin : s_initializers_with_mime_type) {
  59. if (plugin.mime_type != mime_type)
  60. continue;
  61. auto validation_result = plugin.validate_before_create(bytes).release_value_but_fixme_should_propagate_errors();
  62. if (!validation_result)
  63. continue;
  64. auto plugin_decoder = plugin.create(bytes).release_value_but_fixme_should_propagate_errors();
  65. if (plugin_decoder->initialize())
  66. return plugin_decoder;
  67. }
  68. return {};
  69. }
  70. RefPtr<ImageDecoder> ImageDecoder::try_create_for_raw_bytes(ReadonlyBytes bytes, Optional<DeprecatedString> mime_type)
  71. {
  72. OwnPtr<ImageDecoderPlugin> plugin = probe_and_sniff_for_appropriate_plugin(bytes);
  73. if (!plugin) {
  74. if (mime_type.has_value()) {
  75. plugin = probe_and_sniff_for_appropriate_plugin_with_known_mime_type(mime_type.value(), bytes);
  76. if (!plugin)
  77. return {};
  78. } else {
  79. return {};
  80. }
  81. }
  82. return adopt_ref_if_nonnull(new (nothrow) ImageDecoder(plugin.release_nonnull()));
  83. }
  84. ImageDecoder::ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin> plugin)
  85. : m_plugin(move(plugin))
  86. {
  87. }
  88. }