mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:17:44 +00:00
LibGfx: Re-structure the whole initialization pattern for image decoders
When trying to figure out the correct implementation, we now have a very strong distinction on plugins that are well suited for sniffing, and plugins that need a MIME type to be chosen. Instead of having multiple calls to non-static virtual sniff methods for each Image decoding plugin, we have 2 static methods for each implementation: 1. The sniff method, which in contrast to the old method, gets a ReadonlyBytes parameter and ensures we can figure out the result with zero heap allocations for most implementations. 2. The create method, which just creates a new instance so we don't expose the constructor to everyone anymore. In addition to that, we have a new virtual method called initialize, which has a per-implementation initialization pattern to actually ensure each implementation can construct a decoder object, and then have a correct context being applied to it for the actual decoding.
This commit is contained in:
parent
6e6999ce57
commit
57e19a7e56
33 changed files with 493 additions and 206 deletions
|
@ -136,7 +136,7 @@ static bool load_ico_directory(ICOLoadingContext& context)
|
|||
return true;
|
||||
}
|
||||
|
||||
static bool load_ico_bitmap(ICOLoadingContext& context, Optional<size_t> index)
|
||||
bool ICOImageDecoderPlugin::load_ico_bitmap(ICOLoadingContext& context, Optional<size_t> index)
|
||||
{
|
||||
if (context.state < ICOLoadingContext::State::DirectoryDecoded) {
|
||||
if (!load_ico_directory(context)) {
|
||||
|
@ -153,20 +153,25 @@ static bool load_ico_bitmap(ICOLoadingContext& context, Optional<size_t> index)
|
|||
}
|
||||
|
||||
ICOImageDescriptor& desc = context.images[real_index];
|
||||
|
||||
PNGImageDecoderPlugin png_decoder(context.data + desc.offset, desc.size);
|
||||
if (png_decoder.sniff()) {
|
||||
auto decoded_png_frame = png_decoder.frame(0);
|
||||
if (decoded_png_frame.is_error() || !decoded_png_frame.value().image) {
|
||||
dbgln_if(ICO_DEBUG, "load_ico_bitmap: failed to load PNG encoded image index: {}", real_index);
|
||||
return false;
|
||||
if (PNGImageDecoderPlugin::sniff({ context.data + desc.offset, desc.size }).release_value_but_fixme_should_propagate_errors()) {
|
||||
auto png_decoder = PNGImageDecoderPlugin::create({ context.data + desc.offset, desc.size }).release_value_but_fixme_should_propagate_errors();
|
||||
if (png_decoder->initialize()) {
|
||||
auto decoded_png_frame = png_decoder->frame(0);
|
||||
if (decoded_png_frame.is_error() || !decoded_png_frame.value().image) {
|
||||
dbgln_if(ICO_DEBUG, "load_ico_bitmap: failed to load PNG encoded image index: {}", real_index);
|
||||
return false;
|
||||
}
|
||||
desc.bitmap = decoded_png_frame.value().image;
|
||||
return true;
|
||||
}
|
||||
desc.bitmap = decoded_png_frame.value().image;
|
||||
return true;
|
||||
return false;
|
||||
} else {
|
||||
BMPImageDecoderPlugin bmp_decoder(context.data + desc.offset, desc.size, true);
|
||||
if (bmp_decoder.sniff_dib()) {
|
||||
auto decoded_bmp_frame = bmp_decoder.frame(0);
|
||||
auto bmp_decoder = BMPImageDecoderPlugin::create_as_included_in_ico({}, { context.data + desc.offset, desc.size }).release_value_but_fixme_should_propagate_errors();
|
||||
// NOTE: We don't initialize a BMP decoder in the usual way, but rather
|
||||
// we just create an object and try to sniff for a frame when it's included
|
||||
// inside an ICO image.
|
||||
if (bmp_decoder->sniff_dib()) {
|
||||
auto decoded_bmp_frame = bmp_decoder->frame(0);
|
||||
if (decoded_bmp_frame.is_error() || !decoded_bmp_frame.value().image) {
|
||||
dbgln_if(ICO_DEBUG, "load_ico_bitmap: failed to load BMP encoded image index: {}", real_index);
|
||||
return false;
|
||||
|
@ -180,6 +185,17 @@ static bool load_ico_bitmap(ICOLoadingContext& context, Optional<size_t> index)
|
|||
}
|
||||
}
|
||||
|
||||
ErrorOr<bool> ICOImageDecoderPlugin::sniff(ReadonlyBytes data)
|
||||
{
|
||||
InputMemoryStream stream { { data.data(), data.size() } };
|
||||
return decode_ico_header(stream).has_value();
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> ICOImageDecoderPlugin::create(ReadonlyBytes data)
|
||||
{
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) ICOImageDecoderPlugin(data.data(), data.size()));
|
||||
}
|
||||
|
||||
ICOImageDecoderPlugin::ICOImageDecoderPlugin(u8 const* data, size_t size)
|
||||
{
|
||||
m_context = make<ICOLoadingContext>();
|
||||
|
@ -219,7 +235,7 @@ bool ICOImageDecoderPlugin::set_nonvolatile(bool& was_purged)
|
|||
return m_context->images[0].bitmap->set_nonvolatile(was_purged);
|
||||
}
|
||||
|
||||
bool ICOImageDecoderPlugin::sniff()
|
||||
bool ICOImageDecoderPlugin::initialize()
|
||||
{
|
||||
InputMemoryStream stream { { m_context->data, m_context->data_size } };
|
||||
return decode_ico_header(stream).has_value();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue