ImageCodecPluginSerenity.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.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. Optional<Web::Platform::DecodedImage> ImageCodecPluginSerenity::decode_image(ReadonlyBytes bytes)
  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 result_or_empty = m_client->decode_image(bytes);
  21. if (!result_or_empty.has_value())
  22. return {};
  23. auto result = result_or_empty.release_value();
  24. Web::Platform::DecodedImage decoded_image;
  25. decoded_image.is_animated = result.is_animated;
  26. decoded_image.loop_count = result.loop_count;
  27. for (auto const& frame : result.frames) {
  28. decoded_image.frames.empend(frame.bitmap, frame.duration);
  29. }
  30. return decoded_image;
  31. }
  32. }