ImageDecoder.cpp 4.4 KB

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