ClientConnection.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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::try_create(ReadonlyBytes { encoded_buffer.data<u8>(), encoded_buffer.size() });
  33. if (!decoder) {
  34. dbgln_if(IMAGE_DECODER_DEBUG, "Could not find suitable image decoder plugin for data");
  35. return { false, 0, Vector<Gfx::ShareableBitmap> {}, Vector<u32> {} };
  36. }
  37. if (!decoder->frame_count()) {
  38. dbgln_if(IMAGE_DECODER_DEBUG, "Could not decode image from encoded data");
  39. return { false, 0, Vector<Gfx::ShareableBitmap> {}, Vector<u32> {} };
  40. }
  41. Vector<Gfx::ShareableBitmap> bitmaps;
  42. Vector<u32> durations;
  43. for (size_t i = 0; i < decoder->frame_count(); ++i) {
  44. // FIXME: All image decoder plugins should be rewritten to return frame() instead of bitmap().
  45. // Non-animated images can simply return 1 frame.
  46. Gfx::ImageFrameDescriptor frame;
  47. if (decoder->is_animated()) {
  48. frame = decoder->frame(i);
  49. } else {
  50. frame.image = decoder->bitmap();
  51. }
  52. if (frame.image)
  53. bitmaps.append(frame.image->to_shareable_bitmap());
  54. else
  55. bitmaps.append(Gfx::ShareableBitmap {});
  56. durations.append(frame.duration);
  57. }
  58. return { decoder->is_animated(), static_cast<u32>(decoder->loop_count()), bitmaps, durations };
  59. }
  60. }