ConnectionFromClient.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/ImageFormats/ImageDecoder.h>
  11. namespace ImageDecoder {
  12. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
  13. : IPC::ConnectionFromClient<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket), 1)
  14. {
  15. }
  16. void ConnectionFromClient::die()
  17. {
  18. Core::EventLoop::current().quit(0);
  19. }
  20. static void decode_image_to_bitmaps_and_durations_with_decoder(Gfx::ImageDecoder const& decoder, Vector<Gfx::ShareableBitmap>& bitmaps, Vector<u32>& durations)
  21. {
  22. for (size_t i = 0; i < decoder.frame_count(); ++i) {
  23. auto frame_or_error = decoder.frame(i);
  24. if (frame_or_error.is_error()) {
  25. bitmaps.append(Gfx::ShareableBitmap {});
  26. durations.append(0);
  27. } else {
  28. auto frame = frame_or_error.release_value();
  29. bitmaps.append(frame.image->to_shareable_bitmap());
  30. durations.append(frame.duration);
  31. }
  32. }
  33. }
  34. static void decode_image_to_details(Core::AnonymousBuffer const& encoded_buffer, Optional<DeprecatedString> const& known_mime_type, bool& is_animated, u32& loop_count, Vector<Gfx::ShareableBitmap>& bitmaps, Vector<u32>& durations)
  35. {
  36. VERIFY(bitmaps.size() == 0);
  37. VERIFY(durations.size() == 0);
  38. auto decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(ReadonlyBytes { encoded_buffer.data<u8>(), encoded_buffer.size() }, known_mime_type);
  39. if (!decoder) {
  40. dbgln_if(IMAGE_DECODER_DEBUG, "Could not find suitable image decoder plugin for data");
  41. return;
  42. }
  43. if (!decoder->frame_count()) {
  44. dbgln_if(IMAGE_DECODER_DEBUG, "Could not decode image from encoded data");
  45. return;
  46. }
  47. is_animated = decoder->is_animated();
  48. loop_count = decoder->loop_count();
  49. decode_image_to_bitmaps_and_durations_with_decoder(*decoder, bitmaps, durations);
  50. }
  51. Messages::ImageDecoderServer::DecodeImageResponse ConnectionFromClient::decode_image(Core::AnonymousBuffer const& encoded_buffer, Optional<DeprecatedString> const& mime_type)
  52. {
  53. if (!encoded_buffer.is_valid()) {
  54. dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid");
  55. return nullptr;
  56. }
  57. bool is_animated = false;
  58. u32 loop_count = 0;
  59. Vector<Gfx::ShareableBitmap> bitmaps;
  60. Vector<u32> durations;
  61. decode_image_to_details(encoded_buffer, mime_type, is_animated, loop_count, bitmaps, durations);
  62. return { is_animated, loop_count, bitmaps, durations };
  63. }
  64. }