ladybird/Userland/Libraries/LibImageDecoderClient/Client.cpp
Brian Gianforcaro 1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00

64 lines
1.6 KiB
C++

/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/AnonymousBuffer.h>
#include <LibImageDecoderClient/Client.h>
namespace ImageDecoderClient {
Client::Client()
: IPC::ServerConnection<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, "/tmp/portal/image")
{
handshake();
}
void Client::die()
{
if (on_death)
on_death();
}
void Client::handshake()
{
send_sync<Messages::ImageDecoderServer::Greet>();
}
void Client::handle(const Messages::ImageDecoderClient::Dummy&)
{
}
Optional<DecodedImage> Client::decode_image(const ByteBuffer& encoded_data)
{
if (encoded_data.is_empty())
return {};
auto encoded_buffer = Core::AnonymousBuffer::create_with_size(encoded_data.size());
if (!encoded_buffer.is_valid()) {
dbgln("Could not allocate encoded buffer");
return {};
}
memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size());
auto response = send_sync_but_allow_failure<Messages::ImageDecoderServer::DecodeImage>(move(encoded_buffer));
if (!response) {
dbgln("ImageDecoder died heroically");
return {};
}
DecodedImage image;
image.is_animated = response->is_animated();
image.loop_count = response->loop_count();
image.frames.resize(response->bitmaps().size());
for (size_t i = 0; i < image.frames.size(); ++i) {
auto& frame = image.frames[i];
frame.bitmap = response->bitmaps()[i].bitmap();
frame.duration = response->durations()[i];
}
return image;
}
}