ImageDecoder.cpp 4.2 KB

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