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