/* * Copyright (c) 2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include namespace ImageDecoder { static HashMap> s_connections; ClientConnection::ClientConnection(NonnullRefPtr socket, int client_id) : IPC::ClientConnection(*this, move(socket), client_id) { s_connections.set(client_id, *this); } ClientConnection::~ClientConnection() { } void ClientConnection::die() { s_connections.remove(client_id()); exit(0); } void ClientConnection::handle(const Messages::ImageDecoderServer::Greet&) { } Messages::ImageDecoderServer::DecodeImageResponse ClientConnection::handle(const Messages::ImageDecoderServer::DecodeImage& message) { auto encoded_buffer = message.data(); if (!encoded_buffer.is_valid()) { dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid"); return nullptr; } auto decoder = Gfx::ImageDecoder::create(encoded_buffer.data(), encoded_buffer.size()); 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) { // FIXME: All image decoder plugins should be rewritten to return frame() instead of bitmap(). // Non-animated images can simply return 1 frame. Gfx::ImageFrameDescriptor frame; if (decoder->is_animated()) { frame = decoder->frame(i); } else { frame.image = decoder->bitmap(); } if (frame.image) bitmaps.append(frame.image->to_shareable_bitmap()); else bitmaps.append(Gfx::ShareableBitmap {}); durations.append(frame.duration); } return { decoder->is_animated(), decoder->loop_count(), bitmaps, durations }; } }