ImageCodecPluginSerenity.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 "ImageCodecPluginSerenity.h"
  8. #include <LibImageDecoderClient/Client.h>
  9. namespace WebContent {
  10. ImageCodecPluginSerenity::ImageCodecPluginSerenity() = default;
  11. ImageCodecPluginSerenity::~ImageCodecPluginSerenity() = default;
  12. NonnullRefPtr<Core::Promise<Web::Platform::DecodedImage>> ImageCodecPluginSerenity::decode_image(ReadonlyBytes bytes, Function<ErrorOr<void>(Web::Platform::DecodedImage&)> on_resolved, Function<void(Error&)> on_rejected)
  13. {
  14. if (!m_client) {
  15. m_client = ImageDecoderClient::Client::try_create().release_value_but_fixme_should_propagate_errors();
  16. m_client->on_death = [&] {
  17. m_client = nullptr;
  18. };
  19. }
  20. auto promise = Core::Promise<Web::Platform::DecodedImage>::construct();
  21. if (on_resolved)
  22. promise->on_resolution = move(on_resolved);
  23. if (on_rejected)
  24. promise->on_rejection = move(on_rejected);
  25. auto image_decoder_promise = m_client->decode_image(
  26. bytes,
  27. [promise](ImageDecoderClient::DecodedImage& result) -> ErrorOr<void> {
  28. // FIXME: Remove this codec plugin and just use the ImageDecoderClient directly to avoid these copies
  29. Web::Platform::DecodedImage decoded_image;
  30. decoded_image.is_animated = result.is_animated;
  31. decoded_image.loop_count = result.loop_count;
  32. for (auto const& frame : result.frames) {
  33. decoded_image.frames.empend(move(frame.bitmap), frame.duration);
  34. }
  35. promise->resolve(move(decoded_image));
  36. return {};
  37. },
  38. [promise](auto& error) {
  39. promise->reject(Error::copy(error));
  40. });
  41. return promise;
  42. }
  43. }