ImageCodecPlugin.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 "ImageCodecPlugin.h"
  8. #include "HelperProcess.h"
  9. #include "Utilities.h"
  10. #include <LibGfx/Bitmap.h>
  11. #include <LibGfx/ImageFormats/ImageDecoder.h>
  12. #include <LibImageDecoderClient/Client.h>
  13. namespace Ladybird {
  14. ImageCodecPlugin::~ImageCodecPlugin() = default;
  15. Optional<Web::Platform::DecodedImage> ImageCodecPlugin::decode_image(ReadonlyBytes bytes)
  16. {
  17. if (!m_client) {
  18. auto candidate_image_decoder_paths = get_paths_for_helper_process("ImageDecoder"sv).release_value_but_fixme_should_propagate_errors();
  19. m_client = launch_image_decoder_process(candidate_image_decoder_paths).release_value_but_fixme_should_propagate_errors();
  20. m_client->on_death = [&] {
  21. m_client = nullptr;
  22. };
  23. }
  24. auto result_or_empty = m_client->decode_image(bytes);
  25. if (!result_or_empty.has_value())
  26. return {};
  27. auto result = result_or_empty.release_value();
  28. Web::Platform::DecodedImage decoded_image;
  29. decoded_image.is_animated = result.is_animated;
  30. decoded_image.loop_count = result.loop_count;
  31. for (auto const& frame : result.frames) {
  32. decoded_image.frames.empend(move(frame.bitmap), frame.duration);
  33. }
  34. return decoded_image;
  35. }
  36. }