2020-06-22 19:35:22 +00:00
|
|
|
/*
|
2021-01-31 08:24:46 +00:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-06-22 19:35:22 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-22 19:35:22 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-16 22:58:57 +00:00
|
|
|
#include <LibCore/AnonymousBuffer.h>
|
2020-06-22 19:35:22 +00:00
|
|
|
#include <LibImageDecoderClient/Client.h>
|
|
|
|
|
|
|
|
namespace ImageDecoderClient {
|
|
|
|
|
2022-01-14 13:12:49 +00:00
|
|
|
Client::Client(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
|
2022-02-25 10:27:37 +00:00
|
|
|
: IPC::ConnectionToServer<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket))
|
2020-06-22 19:35:22 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-02-20 10:35:00 +00:00
|
|
|
void Client::die()
|
|
|
|
{
|
|
|
|
if (on_death)
|
|
|
|
on_death();
|
|
|
|
}
|
|
|
|
|
2021-11-11 00:06:34 +00:00
|
|
|
Optional<DecodedImage> Client::decode_image(ReadonlyBytes encoded_data)
|
2020-06-22 19:35:22 +00:00
|
|
|
{
|
|
|
|
if (encoded_data.is_empty())
|
2021-01-29 21:30:48 +00:00
|
|
|
return {};
|
2020-06-22 19:35:22 +00:00
|
|
|
|
2021-11-06 00:20:51 +00:00
|
|
|
auto encoded_buffer_or_error = Core::AnonymousBuffer::create_with_size(encoded_data.size());
|
|
|
|
if (encoded_buffer_or_error.is_error()) {
|
2021-01-16 22:58:57 +00:00
|
|
|
dbgln("Could not allocate encoded buffer");
|
2021-01-29 21:30:48 +00:00
|
|
|
return {};
|
2020-06-22 19:35:22 +00:00
|
|
|
}
|
2021-11-06 00:20:51 +00:00
|
|
|
auto encoded_buffer = encoded_buffer_or_error.release_value();
|
2020-06-22 19:35:22 +00:00
|
|
|
|
2021-01-16 22:58:57 +00:00
|
|
|
memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size());
|
2021-05-03 14:51:42 +00:00
|
|
|
auto response_or_error = try_decode_image(move(encoded_buffer));
|
2021-02-20 10:33:34 +00:00
|
|
|
|
2021-05-03 14:51:42 +00:00
|
|
|
if (response_or_error.is_error()) {
|
2021-02-20 10:33:34 +00:00
|
|
|
dbgln("ImageDecoder died heroically");
|
|
|
|
return {};
|
|
|
|
}
|
2021-01-29 21:30:48 +00:00
|
|
|
|
2021-05-03 14:51:42 +00:00
|
|
|
auto& response = response_or_error.value();
|
|
|
|
|
2021-05-14 17:40:49 +00:00
|
|
|
if (response.bitmaps().is_empty())
|
|
|
|
return {};
|
|
|
|
|
2021-01-29 21:30:48 +00:00
|
|
|
DecodedImage image;
|
2021-05-03 14:51:42 +00:00
|
|
|
image.is_animated = response.is_animated();
|
|
|
|
image.loop_count = response.loop_count();
|
|
|
|
image.frames.resize(response.bitmaps().size());
|
2021-01-29 21:30:48 +00:00
|
|
|
for (size_t i = 0; i < image.frames.size(); ++i) {
|
|
|
|
auto& frame = image.frames[i];
|
2021-05-03 14:51:42 +00:00
|
|
|
frame.bitmap = response.bitmaps()[i].bitmap();
|
|
|
|
frame.duration = response.durations()[i];
|
2021-01-29 21:30:48 +00:00
|
|
|
}
|
2021-03-17 15:30:02 +00:00
|
|
|
return image;
|
2020-06-22 19:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|