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