mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-08 04:02:52 +00:00
Previously, ImageDecoder::create() would return a NonnullRefPtr and could not "fail", although the returned decoder may be "invalid" which you then had to check anyway. The new interface looks like this: static RefPtr<Gfx::ImageDecoder> try_create(ReadonlyBytes); This simplifies ImageDecoder since it no longer has to worry about its validity. Client code gets slightly clearer as well.
86 lines
2.1 KiB
C++
86 lines
2.1 KiB
C++
/*
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibGfx/BMPLoader.h>
|
|
#include <LibGfx/DDSLoader.h>
|
|
#include <LibGfx/GIFLoader.h>
|
|
#include <LibGfx/ICOLoader.h>
|
|
#include <LibGfx/ImageDecoder.h>
|
|
#include <LibGfx/JPGLoader.h>
|
|
#include <LibGfx/PBMLoader.h>
|
|
#include <LibGfx/PGMLoader.h>
|
|
#include <LibGfx/PNGLoader.h>
|
|
#include <LibGfx/PPMLoader.h>
|
|
|
|
namespace Gfx {
|
|
|
|
RefPtr<ImageDecoder> ImageDecoder::try_create(ReadonlyBytes bytes)
|
|
{
|
|
auto* data = bytes.data();
|
|
auto size = bytes.size();
|
|
|
|
auto plugin = [](auto* data, auto size) -> OwnPtr<ImageDecoderPlugin> {
|
|
OwnPtr<ImageDecoderPlugin> plugin;
|
|
|
|
plugin = make<PNGImageDecoderPlugin>(data, size);
|
|
if (plugin->sniff())
|
|
return plugin;
|
|
|
|
plugin = make<GIFImageDecoderPlugin>(data, size);
|
|
if (plugin->sniff())
|
|
return plugin;
|
|
|
|
plugin = make<BMPImageDecoderPlugin>(data, size);
|
|
if (plugin->sniff())
|
|
return plugin;
|
|
|
|
plugin = make<PBMImageDecoderPlugin>(data, size);
|
|
if (plugin->sniff())
|
|
return plugin;
|
|
|
|
plugin = make<PGMImageDecoderPlugin>(data, size);
|
|
if (plugin->sniff())
|
|
return plugin;
|
|
|
|
plugin = make<PPMImageDecoderPlugin>(data, size);
|
|
if (plugin->sniff())
|
|
return plugin;
|
|
|
|
plugin = make<ICOImageDecoderPlugin>(data, size);
|
|
if (plugin->sniff())
|
|
return plugin;
|
|
|
|
plugin = make<JPGImageDecoderPlugin>(data, size);
|
|
if (plugin->sniff())
|
|
return plugin;
|
|
|
|
plugin = make<DDSImageDecoderPlugin>(data, size);
|
|
if (plugin->sniff())
|
|
return plugin;
|
|
|
|
return {};
|
|
}(data, size);
|
|
|
|
if (!plugin)
|
|
return {};
|
|
return adopt_ref_if_nonnull(new (nothrow) ImageDecoder(plugin.release_nonnull()));
|
|
}
|
|
|
|
ImageDecoder::ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin> plugin)
|
|
: m_plugin(move(plugin))
|
|
{
|
|
}
|
|
|
|
ImageDecoder::~ImageDecoder()
|
|
{
|
|
}
|
|
|
|
RefPtr<Gfx::Bitmap> ImageDecoder::bitmap() const
|
|
{
|
|
return m_plugin->bitmap();
|
|
}
|
|
|
|
}
|