ImageDecoder.cpp 4.1 KB

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