/* * Copyright (c) 2023-2024, Aliaksandr Kalenik * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include namespace Gfx { struct ImmutableBitmapImpl { sk_sp sk_image; SkBitmap sk_bitmap; Variant, NonnullRefPtr, Empty> source; }; int ImmutableBitmap::width() const { return m_impl->sk_image->width(); } int ImmutableBitmap::height() const { return m_impl->sk_image->height(); } IntRect ImmutableBitmap::rect() const { return { {}, size() }; } IntSize ImmutableBitmap::size() const { return { width(), height() }; } Gfx::AlphaType ImmutableBitmap::alpha_type() const { return m_impl->sk_image->alphaType() == kPremul_SkAlphaType ? Gfx::AlphaType::Premultiplied : Gfx::AlphaType::Unpremultiplied; } SkImage const* ImmutableBitmap::sk_image() const { return m_impl->sk_image.get(); } RefPtr ImmutableBitmap::bitmap() const { // FIXME: Implement for PaintingSurface return m_impl->source.get>(); } Color ImmutableBitmap::get_pixel(int x, int y) const { // FIXME: Implement for PaintingSurface return m_impl->source.get>()->get_pixel(x, y); } static SkAlphaType to_skia_alpha_type(Gfx::AlphaType alpha_type) { switch (alpha_type) { case AlphaType::Premultiplied: return kPremul_SkAlphaType; case AlphaType::Unpremultiplied: return kUnpremul_SkAlphaType; default: VERIFY_NOT_REACHED(); } } NonnullRefPtr ImmutableBitmap::create(NonnullRefPtr bitmap) { ImmutableBitmapImpl impl; auto info = SkImageInfo::Make(bitmap->width(), bitmap->height(), to_skia_color_type(bitmap->format()), to_skia_alpha_type(bitmap->alpha_type())); impl.sk_bitmap.installPixels(info, const_cast(static_cast(bitmap->scanline(0))), bitmap->pitch()); impl.sk_bitmap.setImmutable(); impl.sk_image = impl.sk_bitmap.asImage(); impl.source = bitmap; return adopt_ref(*new ImmutableBitmap(make(impl))); } NonnullRefPtr ImmutableBitmap::create_snapshot_from_painting_surface(NonnullRefPtr painting_surface) { ImmutableBitmapImpl impl; impl.sk_image = painting_surface->sk_image_snapshot>(); impl.source = painting_surface; return adopt_ref(*new ImmutableBitmap(make(impl))); } ImmutableBitmap::ImmutableBitmap(NonnullOwnPtr impl) : m_impl(move(impl)) { } ImmutableBitmap::~ImmutableBitmap() = default; }