ImageDecoder.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. struct ImagePluginInitializer {
  22. bool (*sniff)(ReadonlyBytes) = nullptr;
  23. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> (*create)(ReadonlyBytes) = nullptr;
  24. };
  25. static constexpr ImagePluginInitializer s_initializers[] = {
  26. { PNGImageDecoderPlugin::sniff, PNGImageDecoderPlugin::create },
  27. { GIFImageDecoderPlugin::sniff, GIFImageDecoderPlugin::create },
  28. { BMPImageDecoderPlugin::sniff, BMPImageDecoderPlugin::create },
  29. { PBMImageDecoderPlugin::sniff, PBMImageDecoderPlugin::create },
  30. { PGMImageDecoderPlugin::sniff, PGMImageDecoderPlugin::create },
  31. { PPMImageDecoderPlugin::sniff, PPMImageDecoderPlugin::create },
  32. { ICOImageDecoderPlugin::sniff, ICOImageDecoderPlugin::create },
  33. { JPEGImageDecoderPlugin::sniff, JPEGImageDecoderPlugin::create },
  34. { DDSImageDecoderPlugin::sniff, DDSImageDecoderPlugin::create },
  35. { QOIImageDecoderPlugin::sniff, QOIImageDecoderPlugin::create },
  36. { WebPImageDecoderPlugin::sniff, WebPImageDecoderPlugin::create },
  37. };
  38. struct ImagePluginWithMIMETypeInitializer {
  39. ErrorOr<bool> (*validate_before_create)(ReadonlyBytes) = nullptr;
  40. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> (*create)(ReadonlyBytes) = nullptr;
  41. StringView mime_type;
  42. };
  43. static constexpr ImagePluginWithMIMETypeInitializer s_initializers_with_mime_type[] = {
  44. { TGAImageDecoderPlugin::validate_before_create, TGAImageDecoderPlugin::create, "image/x-targa"sv },
  45. };
  46. static OwnPtr<ImageDecoderPlugin> probe_and_sniff_for_appropriate_plugin(ReadonlyBytes bytes)
  47. {
  48. for (auto& plugin : s_initializers) {
  49. auto sniff_result = plugin.sniff(bytes);
  50. if (!sniff_result)
  51. continue;
  52. auto plugin_decoder = plugin.create(bytes).release_value_but_fixme_should_propagate_errors();
  53. if (plugin_decoder->initialize())
  54. return plugin_decoder;
  55. }
  56. return {};
  57. }
  58. static OwnPtr<ImageDecoderPlugin> probe_and_sniff_for_appropriate_plugin_with_known_mime_type(StringView mime_type, ReadonlyBytes bytes)
  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. OwnPtr<ImageDecoderPlugin> plugin = probe_and_sniff_for_appropriate_plugin(bytes);
  75. if (!plugin) {
  76. if (mime_type.has_value()) {
  77. plugin = probe_and_sniff_for_appropriate_plugin_with_known_mime_type(mime_type.value(), bytes);
  78. if (!plugin)
  79. return {};
  80. } else {
  81. return {};
  82. }
  83. }
  84. return adopt_ref_if_nonnull(new (nothrow) ImageDecoder(plugin.release_nonnull()));
  85. }
  86. ImageDecoder::ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin> plugin)
  87. : m_plugin(move(plugin))
  88. {
  89. }
  90. }