ClientConnection.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <ImageDecoder/ClientConnection.h>
  8. #include <ImageDecoder/ImageDecoderClientEndpoint.h>
  9. #include <LibGfx/Bitmap.h>
  10. #include <LibGfx/ImageDecoder.h>
  11. namespace ImageDecoder {
  12. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  13. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  14. : IPC::ClientConnection<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket), client_id)
  15. {
  16. s_connections.set(client_id, *this);
  17. }
  18. ClientConnection::~ClientConnection()
  19. {
  20. }
  21. void ClientConnection::die()
  22. {
  23. s_connections.remove(client_id());
  24. exit(0);
  25. }
  26. Messages::ImageDecoderServer::DecodeImageResponse ClientConnection::decode_image(Core::AnonymousBuffer const& encoded_buffer)
  27. {
  28. if (!encoded_buffer.is_valid()) {
  29. dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid");
  30. return nullptr;
  31. }
  32. auto decoder = Gfx::ImageDecoder::create(encoded_buffer.data<u8>(), encoded_buffer.size());
  33. if (!decoder->frame_count()) {
  34. dbgln_if(IMAGE_DECODER_DEBUG, "Could not decode image from encoded data");
  35. return { false, 0, Vector<Gfx::ShareableBitmap> {}, Vector<u32> {} };
  36. }
  37. Vector<Gfx::ShareableBitmap> bitmaps;
  38. Vector<u32> durations;
  39. for (size_t i = 0; i < decoder->frame_count(); ++i) {
  40. // FIXME: All image decoder plugins should be rewritten to return frame() instead of bitmap().
  41. // Non-animated images can simply return 1 frame.
  42. Gfx::ImageFrameDescriptor frame;
  43. if (decoder->is_animated()) {
  44. frame = decoder->frame(i);
  45. } else {
  46. frame.image = decoder->bitmap();
  47. }
  48. if (frame.image)
  49. bitmaps.append(frame.image->to_shareable_bitmap());
  50. else
  51. bitmaps.append(Gfx::ShareableBitmap {});
  52. durations.append(frame.duration);
  53. }
  54. return { decoder->is_animated(), static_cast<u32>(decoder->loop_count()), bitmaps, durations };
  55. }
  56. }