/* * Copyright (c) 2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include namespace ImageDecoder { ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr socket) : IPC::ConnectionFromClient(*this, move(socket), 1) { } void ConnectionFromClient::die() { Core::EventLoop::current().quit(0); } Messages::ImageDecoderServer::DecodeImageResponse ConnectionFromClient::decode_image(Core::AnonymousBuffer const& encoded_buffer) { if (!encoded_buffer.is_valid()) { dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid"); return nullptr; } auto decoder = Gfx::ImageDecoder::try_create(ReadonlyBytes { encoded_buffer.data(), encoded_buffer.size() }); if (!decoder) { dbgln_if(IMAGE_DECODER_DEBUG, "Could not find suitable image decoder plugin for data"); return { false, 0, Vector {}, Vector {} }; } if (!decoder->frame_count()) { dbgln_if(IMAGE_DECODER_DEBUG, "Could not decode image from encoded data"); return { false, 0, Vector {}, Vector {} }; } Vector bitmaps; Vector durations; for (size_t i = 0; i < decoder->frame_count(); ++i) { auto frame_or_error = decoder->frame(i); if (frame_or_error.is_error()) { bitmaps.append(Gfx::ShareableBitmap {}); durations.append(0); } else { auto frame = frame_or_error.release_value(); bitmaps.append(frame.image->to_shareable_bitmap()); durations.append(frame.duration); } } return { decoder->is_animated(), static_cast(decoder->loop_count()), bitmaps, durations }; } }