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/DDSLoader.h>
|
|
|
|
#include <LibGfx/ImageFormats/GIFLoader.h>
|
|
|
|
#include <LibGfx/ImageFormats/ICOLoader.h>
|
|
|
|
#include <LibGfx/ImageFormats/ImageDecoder.h>
|
|
|
|
#include <LibGfx/ImageFormats/JPEGLoader.h>
|
|
|
|
#include <LibGfx/ImageFormats/PBMLoader.h>
|
|
|
|
#include <LibGfx/ImageFormats/PGMLoader.h>
|
|
|
|
#include <LibGfx/ImageFormats/PNGLoader.h>
|
|
|
|
#include <LibGfx/ImageFormats/PPMLoader.h>
|
|
|
|
#include <LibGfx/ImageFormats/QOILoader.h>
|
|
|
|
#include <LibGfx/ImageFormats/TGALoader.h>
|
|
|
|
#include <LibGfx/ImageFormats/WebPLoader.h>
|
2019-10-19 19:56:49 +02:00
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
namespace Gfx {
|
|
|
|
|
2023-01-20 10:13:14 +02:00
|
|
|
struct ImagePluginInitializer {
|
2023-02-26 18:02:50 +00:00
|
|
|
bool (*sniff)(ReadonlyBytes) = nullptr;
|
2023-01-20 10:13:14 +02:00
|
|
|
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> (*create)(ReadonlyBytes) = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr ImagePluginInitializer s_initializers[] = {
|
|
|
|
{ PNGImageDecoderPlugin::sniff, PNGImageDecoderPlugin::create },
|
|
|
|
{ GIFImageDecoderPlugin::sniff, GIFImageDecoderPlugin::create },
|
|
|
|
{ BMPImageDecoderPlugin::sniff, BMPImageDecoderPlugin::create },
|
|
|
|
{ PBMImageDecoderPlugin::sniff, PBMImageDecoderPlugin::create },
|
|
|
|
{ PGMImageDecoderPlugin::sniff, PGMImageDecoderPlugin::create },
|
|
|
|
{ PPMImageDecoderPlugin::sniff, PPMImageDecoderPlugin::create },
|
|
|
|
{ ICOImageDecoderPlugin::sniff, ICOImageDecoderPlugin::create },
|
2023-02-18 16:09:16 -05:00
|
|
|
{ JPEGImageDecoderPlugin::sniff, JPEGImageDecoderPlugin::create },
|
2023-01-20 10:13:14 +02:00
|
|
|
{ DDSImageDecoderPlugin::sniff, DDSImageDecoderPlugin::create },
|
|
|
|
{ QOIImageDecoderPlugin::sniff, QOIImageDecoderPlugin::create },
|
2023-02-23 22:37:08 -05:00
|
|
|
{ WebPImageDecoderPlugin::sniff, WebPImageDecoderPlugin::create },
|
2023-01-20 10:13:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ImagePluginWithMIMETypeInitializer {
|
|
|
|
ErrorOr<bool> (*validate_before_create)(ReadonlyBytes) = nullptr;
|
|
|
|
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> (*create)(ReadonlyBytes) = nullptr;
|
|
|
|
StringView mime_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr ImagePluginWithMIMETypeInitializer s_initializers_with_mime_type[] = {
|
|
|
|
{ TGAImageDecoderPlugin::validate_before_create, TGAImageDecoderPlugin::create, "image/x-targa"sv },
|
|
|
|
};
|
|
|
|
|
2023-01-15 21:50:32 +02:00
|
|
|
static OwnPtr<ImageDecoderPlugin> probe_and_sniff_for_appropriate_plugin(ReadonlyBytes bytes)
|
2019-10-19 19:56:49 +02:00
|
|
|
{
|
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;
|
|
|
|
auto plugin_decoder = plugin.create(bytes).release_value_but_fixme_should_propagate_errors();
|
|
|
|
if (plugin_decoder->initialize())
|
|
|
|
return plugin_decoder;
|
|
|
|
}
|
2023-01-15 21:50:32 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-01-20 03:27:10 +02:00
|
|
|
static OwnPtr<ImageDecoderPlugin> probe_and_sniff_for_appropriate_plugin_with_known_mime_type(StringView mime_type, ReadonlyBytes bytes)
|
2023-01-15 21:50:32 +02:00
|
|
|
{
|
2023-01-20 10:13:14 +02:00
|
|
|
for (auto& plugin : s_initializers_with_mime_type) {
|
|
|
|
if (plugin.mime_type != mime_type)
|
|
|
|
continue;
|
|
|
|
auto validation_result = plugin.validate_before_create(bytes).release_value_but_fixme_should_propagate_errors();
|
|
|
|
if (!validation_result)
|
|
|
|
continue;
|
|
|
|
auto plugin_decoder = plugin.create(bytes).release_value_but_fixme_should_propagate_errors();
|
|
|
|
if (plugin_decoder->initialize())
|
|
|
|
return plugin_decoder;
|
2023-01-15 21:50:32 +02:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2022-08-17 20:02:44 +01:00
|
|
|
|
2023-01-20 03:27:10 +02:00
|
|
|
RefPtr<ImageDecoder> ImageDecoder::try_create_for_raw_bytes(ReadonlyBytes bytes, Optional<DeprecatedString> mime_type)
|
2023-01-15 21:50:32 +02:00
|
|
|
{
|
2023-01-20 03:27:10 +02:00
|
|
|
OwnPtr<ImageDecoderPlugin> plugin = probe_and_sniff_for_appropriate_plugin(bytes);
|
|
|
|
if (!plugin) {
|
|
|
|
if (mime_type.has_value()) {
|
|
|
|
plugin = probe_and_sniff_for_appropriate_plugin_with_known_mime_type(mime_type.value(), bytes);
|
|
|
|
if (!plugin)
|
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
2021-07-27 01:12:53 +02:00
|
|
|
return adopt_ref_if_nonnull(new (nothrow) ImageDecoder(plugin.release_nonnull()));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|