ImageDecoder.cpp 4.5 KB

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