AnimatedBitmapDecodedImageData.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Bitmap.h>
  7. #include <LibJS/Heap/Heap.h>
  8. #include <LibJS/Runtime/Realm.h>
  9. #include <LibWeb/HTML/AnimatedBitmapDecodedImageData.h>
  10. namespace Web::HTML {
  11. JS_DEFINE_ALLOCATOR(AnimatedBitmapDecodedImageData);
  12. ErrorOr<JS::NonnullGCPtr<AnimatedBitmapDecodedImageData>> AnimatedBitmapDecodedImageData::create(JS::Realm& realm, Vector<Frame>&& frames, size_t loop_count, bool animated)
  13. {
  14. return realm.heap().allocate<AnimatedBitmapDecodedImageData>(realm, move(frames), loop_count, animated);
  15. }
  16. AnimatedBitmapDecodedImageData::AnimatedBitmapDecodedImageData(Vector<Frame>&& frames, size_t loop_count, bool animated)
  17. : m_frames(move(frames))
  18. , m_loop_count(loop_count)
  19. , m_animated(animated)
  20. {
  21. }
  22. AnimatedBitmapDecodedImageData::~AnimatedBitmapDecodedImageData() = default;
  23. RefPtr<Gfx::ImmutableBitmap> AnimatedBitmapDecodedImageData::bitmap(size_t frame_index, Gfx::IntSize) const
  24. {
  25. if (frame_index >= m_frames.size())
  26. return nullptr;
  27. return m_frames[frame_index].bitmap;
  28. }
  29. int AnimatedBitmapDecodedImageData::frame_duration(size_t frame_index) const
  30. {
  31. if (frame_index >= m_frames.size())
  32. return 0;
  33. return m_frames[frame_index].duration;
  34. }
  35. Optional<CSSPixels> AnimatedBitmapDecodedImageData::intrinsic_width() const
  36. {
  37. return m_frames.first().bitmap->width();
  38. }
  39. Optional<CSSPixels> AnimatedBitmapDecodedImageData::intrinsic_height() const
  40. {
  41. return m_frames.first().bitmap->height();
  42. }
  43. Optional<CSSPixelFraction> AnimatedBitmapDecodedImageData::intrinsic_aspect_ratio() const
  44. {
  45. return CSSPixels(m_frames.first().bitmap->width()) / CSSPixels(m_frames.first().bitmap->height());
  46. }
  47. }