2020-06-22 22:35:22 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-22 22:35:22 +03:00
|
|
|
*/
|
|
|
|
|
2021-04-21 21:08:03 +03:00
|
|
|
#include <AK/Debug.h>
|
2020-06-22 22:35:22 +03:00
|
|
|
#include <ImageDecoder/ClientConnection.h>
|
|
|
|
#include <ImageDecoder/ImageDecoderClientEndpoint.h>
|
|
|
|
#include <LibGfx/Bitmap.h>
|
|
|
|
#include <LibGfx/ImageDecoder.h>
|
|
|
|
|
|
|
|
namespace ImageDecoder {
|
|
|
|
|
2021-11-29 19:12:55 +03:00
|
|
|
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket)
|
|
|
|
: IPC::ClientConnection<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket), 1)
|
2020-06-22 22:35:22 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientConnection::~ClientConnection()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientConnection::die()
|
|
|
|
{
|
2021-11-29 19:12:55 +03:00
|
|
|
Core::EventLoop::current().quit(0);
|
2020-06-22 22:35:22 +03:00
|
|
|
}
|
|
|
|
|
2021-05-02 20:54:34 +03:00
|
|
|
Messages::ImageDecoderServer::DecodeImageResponse ClientConnection::decode_image(Core::AnonymousBuffer const& encoded_buffer)
|
2020-06-22 22:35:22 +03:00
|
|
|
{
|
2021-01-17 01:58:57 +03:00
|
|
|
if (!encoded_buffer.is_valid()) {
|
2021-04-21 21:08:03 +03:00
|
|
|
dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid");
|
2021-05-02 05:39:36 +03:00
|
|
|
return nullptr;
|
2020-06-22 22:35:22 +03:00
|
|
|
}
|
|
|
|
|
2021-07-27 02:12:53 +03: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 22:35:22 +03:00
|
|
|
|
2021-01-30 00:30:48 +03:00
|
|
|
if (!decoder->frame_count()) {
|
2021-04-21 21:08:03 +03:00
|
|
|
dbgln_if(IMAGE_DECODER_DEBUG, "Could not decode image from encoded data");
|
2021-05-02 05:39:36 +03:00
|
|
|
return { false, 0, Vector<Gfx::ShareableBitmap> {}, Vector<u32> {} };
|
2020-06-22 22:35:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-30 00:30:48 +03:00
|
|
|
Vector<Gfx::ShareableBitmap> bitmaps;
|
|
|
|
Vector<u32> durations;
|
|
|
|
for (size_t i = 0; i < decoder->frame_count(); ++i) {
|
2021-11-20 16:29:33 +03:00
|
|
|
auto frame_or_error = decoder->frame(i);
|
2021-11-29 15:10:07 +03:00
|
|
|
if (frame_or_error.is_error()) {
|
2021-01-30 00:30:48 +03:00
|
|
|
bitmaps.append(Gfx::ShareableBitmap {});
|
2021-11-29 15:10:07 +03:00
|
|
|
durations.append(0);
|
|
|
|
} else {
|
|
|
|
auto frame = frame_or_error.release_value();
|
|
|
|
bitmaps.append(frame.image->to_shareable_bitmap());
|
|
|
|
durations.append(frame.duration);
|
|
|
|
}
|
2021-01-30 00:30:48 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 17:32:19 +03:00
|
|
|
return { decoder->is_animated(), static_cast<u32>(decoder->loop_count()), bitmaps, durations };
|
2020-06-22 22:35:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|