2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2021-07-26 20:12:53 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2023-01-15 16:50:32 -03:00
|
|
|
#include <AK/LexicalPath.h>
|
2024-07-06 04:33:38 -03:00
|
|
|
#include <LibGfx/ImageFormats/AVIFLoader.h>
|
2023-03-21 15:58:06 -03:00
|
|
|
#include <LibGfx/ImageFormats/BMPLoader.h>
|
|
|
|
|
#include <LibGfx/ImageFormats/GIFLoader.h>
|
|
|
|
|
#include <LibGfx/ImageFormats/ICOLoader.h>
|
|
|
|
|
#include <LibGfx/ImageFormats/ImageDecoder.h>
|
|
|
|
|
#include <LibGfx/ImageFormats/JPEGLoader.h>
|
2023-07-04 01:13:34 -03:00
|
|
|
#include <LibGfx/ImageFormats/JPEGXLLoader.h>
|
2023-03-21 15:58:06 -03:00
|
|
|
#include <LibGfx/ImageFormats/PNGLoader.h>
|
2023-10-28 19:05:26 -03:00
|
|
|
#include <LibGfx/ImageFormats/TIFFLoader.h>
|
2023-07-02 18:24:01 -03:00
|
|
|
#include <LibGfx/ImageFormats/TinyVGLoader.h>
|
2023-03-21 15:58:06 -03:00
|
|
|
#include <LibGfx/ImageFormats/WebPLoader.h>
|
2019-10-19 14:56:49 -03:00
|
|
|
|
2020-02-06 07:56:38 -03:00
|
|
|
namespace Gfx {
|
|
|
|
|
|
2024-03-04 20:07:43 -03:00
|
|
|
static ErrorOr<OwnPtr<ImageDecoderPlugin>> probe_and_sniff_for_appropriate_plugin(ReadonlyBytes bytes)
|
2019-10-19 14:56:49 -03:00
|
|
|
{
|
2023-04-21 09:32:02 -03:00
|
|
|
struct ImagePluginInitializer {
|
|
|
|
|
bool (*sniff)(ReadonlyBytes) = nullptr;
|
|
|
|
|
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> (*create)(ReadonlyBytes) = nullptr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static constexpr ImagePluginInitializer s_initializers[] = {
|
|
|
|
|
{ BMPImageDecoderPlugin::sniff, BMPImageDecoderPlugin::create },
|
2023-10-28 20:52:36 -03:00
|
|
|
{ GIFImageDecoderPlugin::sniff, GIFImageDecoderPlugin::create },
|
2023-04-21 09:32:02 -03:00
|
|
|
{ ICOImageDecoderPlugin::sniff, ICOImageDecoderPlugin::create },
|
|
|
|
|
{ JPEGImageDecoderPlugin::sniff, JPEGImageDecoderPlugin::create },
|
2023-07-04 01:13:34 -03:00
|
|
|
{ JPEGXLImageDecoderPlugin::sniff, JPEGXLImageDecoderPlugin::create },
|
2023-10-28 20:52:36 -03:00
|
|
|
{ PNGImageDecoderPlugin::sniff, PNGImageDecoderPlugin::create },
|
2023-10-28 19:05:26 -03:00
|
|
|
{ TIFFImageDecoderPlugin::sniff, TIFFImageDecoderPlugin::create },
|
2023-07-02 18:24:01 -03:00
|
|
|
{ TinyVGImageDecoderPlugin::sniff, TinyVGImageDecoderPlugin::create },
|
2023-04-21 09:32:02 -03:00
|
|
|
{ WebPImageDecoderPlugin::sniff, WebPImageDecoderPlugin::create },
|
2024-07-06 04:33:38 -03:00
|
|
|
{ AVIFImageDecoderPlugin::sniff, AVIFImageDecoderPlugin::create }
|
2023-04-21 09:32:02 -03:00
|
|
|
};
|
|
|
|
|
|
2023-01-20 05:13:14 -03:00
|
|
|
for (auto& plugin : s_initializers) {
|
2023-02-26 15:02:50 -03:00
|
|
|
auto sniff_result = plugin.sniff(bytes);
|
2023-01-20 05:13:14 -03:00
|
|
|
if (!sniff_result)
|
|
|
|
|
continue;
|
2024-03-04 20:07:43 -03:00
|
|
|
return TRY(plugin.create(bytes));
|
2023-01-20 05:13:14 -03:00
|
|
|
}
|
2024-03-04 20:07:43 -03:00
|
|
|
return OwnPtr<ImageDecoderPlugin> {};
|
2023-01-15 16:50:32 -03:00
|
|
|
}
|
|
|
|
|
|
2024-06-17 14:45:06 -03:00
|
|
|
ErrorOr<RefPtr<ImageDecoder>> ImageDecoder::try_create_for_raw_bytes(ReadonlyBytes bytes, [[maybe_unused]] Optional<ByteString> mime_type)
|
2023-01-15 16:50:32 -03:00
|
|
|
{
|
2024-03-04 20:07:43 -03:00
|
|
|
if (auto plugin = TRY(probe_and_sniff_for_appropriate_plugin(bytes)); plugin)
|
2023-04-22 11:17:46 -03:00
|
|
|
return adopt_ref_if_nonnull(new (nothrow) ImageDecoder(plugin.release_nonnull()));
|
|
|
|
|
|
2024-03-04 20:07:43 -03:00
|
|
|
return RefPtr<ImageDecoder> {};
|
2021-07-26 20:12:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ImageDecoder::ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin> plugin)
|
|
|
|
|
: m_plugin(move(plugin))
|
|
|
|
|
{
|
2019-10-19 14:56:49 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-06 07:56:38 -03:00
|
|
|
}
|