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