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