1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 14:14:58 +00:00
serenity/Userland/Services/ImageDecoder/ClientConnection.cpp
Andreas Kling d01b4327fa LibGfx: Improve ImageDecoder construction
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.
2021-07-27 01:17:05 +02:00

73 lines
2.3 KiB
C++

/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Debug.h>
#include <ImageDecoder/ClientConnection.h>
#include <ImageDecoder/ImageDecoderClientEndpoint.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>
namespace ImageDecoder {
static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
: IPC::ClientConnection<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket), client_id)
{
s_connections.set(client_id, *this);
}
ClientConnection::~ClientConnection()
{
}
void ClientConnection::die()
{
s_connections.remove(client_id());
exit(0);
}
Messages::ImageDecoderServer::DecodeImageResponse ClientConnection::decode_image(Core::AnonymousBuffer const& encoded_buffer)
{
if (!encoded_buffer.is_valid()) {
dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid");
return nullptr;
}
auto decoder = Gfx::ImageDecoder::try_create(ReadonlyBytes { encoded_buffer.data<u8>(), encoded_buffer.size() });
if (!decoder) {
dbgln_if(IMAGE_DECODER_DEBUG, "Could not find suitable image decoder plugin for data");
return { false, 0, Vector<Gfx::ShareableBitmap> {}, Vector<u32> {} };
}
if (!decoder->frame_count()) {
dbgln_if(IMAGE_DECODER_DEBUG, "Could not decode image from encoded data");
return { false, 0, Vector<Gfx::ShareableBitmap> {}, Vector<u32> {} };
}
Vector<Gfx::ShareableBitmap> bitmaps;
Vector<u32> durations;
for (size_t i = 0; i < decoder->frame_count(); ++i) {
// FIXME: All image decoder plugins should be rewritten to return frame() instead of bitmap().
// Non-animated images can simply return 1 frame.
Gfx::ImageFrameDescriptor frame;
if (decoder->is_animated()) {
frame = decoder->frame(i);
} else {
frame.image = decoder->bitmap();
}
if (frame.image)
bitmaps.append(frame.image->to_shareable_bitmap());
else
bitmaps.append(Gfx::ShareableBitmap {});
durations.append(frame.duration);
}
return { decoder->is_animated(), static_cast<u32>(decoder->loop_count()), bitmaps, durations };
}
}