ConnectionFromClient.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/ConnectionFromClient.h>
  8. #include <ImageDecoder/ImageDecoderClientEndpoint.h>
  9. #include <LibGfx/Bitmap.h>
  10. #include <LibGfx/ImageDecoder.h>
  11. namespace ImageDecoder {
  12. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
  13. : IPC::ConnectionFromClient<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket), 1)
  14. {
  15. }
  16. ConnectionFromClient::~ConnectionFromClient()
  17. {
  18. }
  19. void ConnectionFromClient::die()
  20. {
  21. Core::EventLoop::current().quit(0);
  22. }
  23. Messages::ImageDecoderServer::DecodeImageResponse ConnectionFromClient::decode_image(Core::AnonymousBuffer const& encoded_buffer)
  24. {
  25. if (!encoded_buffer.is_valid()) {
  26. dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid");
  27. return nullptr;
  28. }
  29. auto decoder = Gfx::ImageDecoder::try_create(ReadonlyBytes { encoded_buffer.data<u8>(), encoded_buffer.size() });
  30. if (!decoder) {
  31. dbgln_if(IMAGE_DECODER_DEBUG, "Could not find suitable image decoder plugin for data");
  32. return { false, 0, Vector<Gfx::ShareableBitmap> {}, Vector<u32> {} };
  33. }
  34. if (!decoder->frame_count()) {
  35. dbgln_if(IMAGE_DECODER_DEBUG, "Could not decode image from encoded data");
  36. return { false, 0, Vector<Gfx::ShareableBitmap> {}, Vector<u32> {} };
  37. }
  38. Vector<Gfx::ShareableBitmap> bitmaps;
  39. Vector<u32> durations;
  40. for (size_t i = 0; i < decoder->frame_count(); ++i) {
  41. auto frame_or_error = decoder->frame(i);
  42. if (frame_or_error.is_error()) {
  43. bitmaps.append(Gfx::ShareableBitmap {});
  44. durations.append(0);
  45. } else {
  46. auto frame = frame_or_error.release_value();
  47. bitmaps.append(frame.image->to_shareable_bitmap());
  48. durations.append(frame.duration);
  49. }
  50. }
  51. return { decoder->is_animated(), static_cast<u32>(decoder->loop_count()), bitmaps, durations };
  52. }
  53. }