ImageCodecPlugin.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com>
  3. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "ImageCodecPlugin.h"
  8. #include "Utilities.h"
  9. #include <LibGfx/Bitmap.h>
  10. #include <LibGfx/ImageFormats/ImageDecoder.h>
  11. #include <LibImageDecoderClient/Client.h>
  12. namespace Ladybird {
  13. ImageCodecPlugin::ImageCodecPlugin(NonnullRefPtr<ImageDecoderClient::Client> client)
  14. : m_client(move(client))
  15. {
  16. m_client->on_death = [this] {
  17. m_client = nullptr;
  18. };
  19. }
  20. void ImageCodecPlugin::set_client(NonnullRefPtr<ImageDecoderClient::Client> client)
  21. {
  22. m_client = move(client);
  23. m_client->on_death = [this] {
  24. m_client = nullptr;
  25. };
  26. }
  27. ImageCodecPlugin::~ImageCodecPlugin() = default;
  28. NonnullRefPtr<Core::Promise<Web::Platform::DecodedImage>> ImageCodecPlugin::decode_image(ReadonlyBytes bytes, Function<ErrorOr<void>(Web::Platform::DecodedImage&)> on_resolved, Function<void(Error&)> on_rejected)
  29. {
  30. auto promise = Core::Promise<Web::Platform::DecodedImage>::construct();
  31. if (on_resolved)
  32. promise->on_resolution = move(on_resolved);
  33. if (on_rejected)
  34. promise->on_rejection = move(on_rejected);
  35. if (!m_client) {
  36. promise->reject(Error::from_string_literal("ImageDecoderClient is disconnected"));
  37. return promise;
  38. }
  39. auto image_decoder_promise = m_client->decode_image(
  40. bytes,
  41. [promise](ImageDecoderClient::DecodedImage& result) -> ErrorOr<void> {
  42. // FIXME: Remove this codec plugin and just use the ImageDecoderClient directly to avoid these copies
  43. Web::Platform::DecodedImage decoded_image;
  44. decoded_image.is_animated = result.is_animated;
  45. decoded_image.loop_count = result.loop_count;
  46. for (auto& frame : result.frames) {
  47. decoded_image.frames.empend(move(frame.bitmap), frame.duration);
  48. }
  49. promise->resolve(move(decoded_image));
  50. return {};
  51. },
  52. [promise](auto& error) {
  53. promise->reject(Error::copy(error));
  54. });
  55. return promise;
  56. }
  57. }