1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:17:45 +00:00

ImageViewer: Support displaying vector graphics (at arbitrary scales)

With this, you can scale, flip, and rotate vector graphics in the image
viewer like any other image, but with no pixelation :^)

With this change, vector graphics are decoded in-process (since there's
no standard way to encode them over IPC, a new encoding would be needed
for each format, which would be pretty much just be recreating that
format).

Raster images are still decoded out of process, so the surface area for
attack is still kept to a minimum.
This commit is contained in:
MacDue 2023-07-08 01:34:25 +01:00 committed by Andreas Kling
parent 57c81c1719
commit 4aa0ef9f98
3 changed files with 197 additions and 47 deletions

View file

@ -5,6 +5,7 @@
* Copyright (c) 2022, Mustafa Quraish <mustafa@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2023, Caoimhe Byrne <caoimhebyrne06@gmail.com>
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -14,10 +15,80 @@
#include <LibCore/Timer.h>
#include <LibGUI/AbstractZoomPanWidget.h>
#include <LibGUI/Painter.h>
#include <LibImageDecoderClient/Client.h>
#include <LibGfx/VectorGraphic.h>
namespace ImageViewer {
class Image : public RefCounted<Image> {
public:
virtual Gfx::IntSize size() const = 0;
virtual Gfx::IntRect rect() const { return { {}, size() }; }
virtual void flip(Gfx::Orientation) = 0;
virtual void rotate(Gfx::RotationDirection) = 0;
virtual void draw_into(Gfx::Painter&, Gfx::IntRect const& dest, Gfx::Painter::ScalingMode) const = 0;
virtual ErrorOr<NonnullRefPtr<Gfx::Bitmap>> bitmap(Optional<Gfx::IntSize> ideal_size) const = 0;
virtual ~Image() = default;
};
class VectorImage final : public Image {
public:
static NonnullRefPtr<VectorImage> create(Gfx::VectorGraphic& vector) { return adopt_ref(*new VectorImage(vector)); }
virtual Gfx::IntSize size() const override { return m_size; }
virtual void flip(Gfx::Orientation) override;
virtual void rotate(Gfx::RotationDirection) override;
virtual void draw_into(Gfx::Painter&, Gfx::IntRect const& dest, Gfx::Painter::ScalingMode) const override;
virtual ErrorOr<NonnullRefPtr<Gfx::Bitmap>> bitmap(Optional<Gfx::IntSize> ideal_size) const override;
private:
VectorImage(Gfx::VectorGraphic& vector)
: m_vector(vector)
, m_size(vector.size())
{
}
void apply_transform(Gfx::AffineTransform transform)
{
m_transform = transform.multiply(m_transform);
}
NonnullRefPtr<Gfx::VectorGraphic> m_vector;
Gfx::IntSize m_size;
Gfx::AffineTransform m_transform;
};
class BitmapImage final : public Image {
public:
static NonnullRefPtr<BitmapImage> create(Gfx::Bitmap& bitmap) { return adopt_ref(*new BitmapImage(bitmap)); }
virtual Gfx::IntSize size() const override { return m_bitmap->size(); }
virtual void flip(Gfx::Orientation) override;
virtual void rotate(Gfx::RotationDirection) override;
virtual void draw_into(Gfx::Painter&, Gfx::IntRect const& dest, Gfx::Painter::ScalingMode) const override;
virtual ErrorOr<NonnullRefPtr<Gfx::Bitmap>> bitmap(Optional<Gfx::IntSize>) const override
{
return m_bitmap;
}
private:
BitmapImage(Gfx::Bitmap& bitmap)
: m_bitmap(bitmap)
{
}
NonnullRefPtr<Gfx::Bitmap> m_bitmap;
};
class ViewWidget final : public GUI::AbstractZoomPanWidget {
C_OBJECT(ViewWidget)
public:
@ -30,7 +101,7 @@ public:
virtual ~ViewWidget() override = default;
Gfx::Bitmap const* bitmap() const { return m_bitmap.ptr(); }
Image const* image() const { return m_image.ptr(); }
String const& path() const { return m_path; }
void set_toolbar_height(int height) { m_toolbar_height = height; }
int toolbar_height() { return m_toolbar_height; }
@ -52,7 +123,8 @@ public:
Function<void()> on_doubleclick;
Function<void(const GUI::DropEvent&)> on_drop;
Function<void(Gfx::Bitmap const*)> on_image_change;
Function<void(Image*)> on_image_change;
private:
ViewWidget();
@ -64,14 +136,25 @@ private:
virtual void drop_event(GUI::DropEvent&) override;
virtual void resize_event(GUI::ResizeEvent&) override;
void set_bitmap(Gfx::Bitmap const* bitmap);
void set_image(Image const* image);
void animate();
Vector<DeprecatedString> load_files_from_directory(DeprecatedString const& path) const;
ErrorOr<void> try_open_file(String const&, Core::File&);
String m_path;
RefPtr<Gfx::Bitmap const> m_bitmap;
Optional<ImageDecoderClient::DecodedImage> m_decoded_image;
RefPtr<Image> m_image;
struct Animation {
struct Frame {
RefPtr<Image> image;
int duration { 0 };
};
size_t loop_count { 0 };
Vector<Frame> frames;
};
Optional<Animation> m_animation;
size_t m_current_frame_index { 0 };
size_t m_loops_completed { 0 };