ImageDecoderClientAdapter.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibImageDecoderClient/Client.h>
  7. #include <LibWebView/ImageDecoderClientAdapter.h>
  8. namespace WebView {
  9. NonnullRefPtr<ImageDecoderClientAdapter> ImageDecoderClientAdapter::create()
  10. {
  11. return adopt_ref(*new ImageDecoderClientAdapter());
  12. }
  13. Optional<Web::ImageDecoding::DecodedImage> ImageDecoderClientAdapter::decode_image(ReadonlyBytes bytes)
  14. {
  15. if (!m_client) {
  16. m_client = ImageDecoderClient::Client::try_create().release_value_but_fixme_should_propagate_errors();
  17. m_client->on_death = [&] {
  18. m_client = nullptr;
  19. };
  20. }
  21. auto result_or_empty = m_client->decode_image(bytes);
  22. if (!result_or_empty.has_value())
  23. return {};
  24. auto result = result_or_empty.release_value();
  25. Web::ImageDecoding::DecodedImage decoded_image;
  26. decoded_image.is_animated = result.is_animated;
  27. decoded_image.loop_count = result.loop_count;
  28. for (auto const& frame : result.frames) {
  29. decoded_image.frames.empend(move(frame.bitmap), frame.duration);
  30. }
  31. return decoded_image;
  32. }
  33. }