2020-06-22 21:35:22 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-22 21:35:22 +02:00
|
|
|
*/
|
|
|
|
|
2021-04-21 20:08:03 +02:00
|
|
|
#include <AK/Debug.h>
|
2020-06-22 21:35:22 +02:00
|
|
|
#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;
|
|
|
|
|
2020-07-06 13:23:39 +02:00
|
|
|
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
|
2020-09-12 11:44:00 +02:00
|
|
|
: IPC::ClientConnection<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket), client_id)
|
2020-06-22 21:35:22 +02:00
|
|
|
{
|
|
|
|
s_connections.set(client_id, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientConnection::~ClientConnection()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientConnection::die()
|
|
|
|
{
|
|
|
|
s_connections.remove(client_id());
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2021-05-02 19:54:34 +02:00
|
|
|
Messages::ImageDecoderServer::DecodeImageResponse ClientConnection::decode_image(Core::AnonymousBuffer const& encoded_buffer)
|
2020-06-22 21:35:22 +02:00
|
|
|
{
|
2021-01-16 23:58:57 +01:00
|
|
|
if (!encoded_buffer.is_valid()) {
|
2021-04-21 20:08:03 +02:00
|
|
|
dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid");
|
2021-05-02 04:39:36 +02:00
|
|
|
return nullptr;
|
2020-06-22 21:35:22 +02:00
|
|
|
}
|
|
|
|
|
2021-07-27 01:12:53 +02:00
|
|
|
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> {} };
|
|
|
|
}
|
2020-06-22 21:35:22 +02:00
|
|
|
|
2021-01-29 22:30:48 +01:00
|
|
|
if (!decoder->frame_count()) {
|
2021-04-21 20:08:03 +02:00
|
|
|
dbgln_if(IMAGE_DECODER_DEBUG, "Could not decode image from encoded data");
|
2021-05-02 04:39:36 +02:00
|
|
|
return { false, 0, Vector<Gfx::ShareableBitmap> {}, Vector<u32> {} };
|
2020-06-22 21:35:22 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 22:30:48 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-05-23 16:32:19 +02:00
|
|
|
return { decoder->is_animated(), static_cast<u32>(decoder->loop_count()), bitmaps, durations };
|
2020-06-22 21:35:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|