summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-11-06 12:26:04 +0100
committerAndreas Kling <kling@serenityos.org>2021-11-08 00:35:27 +0100
commit5e41c70e835ce32ab353147df1d2aee3846a1eae (patch)
treef15ac73e132256cf9f9efb1ab814ae2ec2daf14d /Userland/Libraries
parentdb90b4554ec18e60c1435cc81d379a6afbd2a1fe (diff)
downloadserenity-5e41c70e835ce32ab353147df1d2aee3846a1eae.zip
LibGfx: Use ErrorOr<T> for Bitmap::scaled()
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibGfx/Bitmap.cpp22
-rw-r--r--Userland/Libraries/LibGfx/Bitmap.h4
2 files changed, 15 insertions, 11 deletions
diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp
index ac198aed3f..3d534285c7 100644
--- a/Userland/Libraries/LibGfx/Bitmap.cpp
+++ b/Userland/Libraries/LibGfx/Bitmap.cpp
@@ -395,15 +395,17 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::flipped(Gfx::Orientation orientation
return new_bitmap.release_nonnull();
}
-RefPtr<Gfx::Bitmap> Bitmap::scaled(int sx, int sy) const
+ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::scaled(int sx, int sy) const
{
VERIFY(sx >= 0 && sy >= 0);
if (sx == 1 && sy == 1)
- return this;
+ return NonnullRefPtr { *this };
auto new_bitmap = Gfx::Bitmap::try_create(format(), { width() * sx, height() * sy }, scale());
- if (!new_bitmap)
- return nullptr;
+ if (!new_bitmap) {
+ // FIXME: Propagate the *real* error, once we have it.
+ return Error::from_errno(ENOMEM);
+ }
auto old_width = physical_width();
auto old_height = physical_height();
@@ -422,11 +424,11 @@ RefPtr<Gfx::Bitmap> Bitmap::scaled(int sx, int sy) const
}
}
- return new_bitmap;
+ return new_bitmap.release_nonnull();
}
// http://fourier.eng.hmc.edu/e161/lectures/resize/node3.html
-RefPtr<Gfx::Bitmap> Bitmap::scaled(float sx, float sy) const
+ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::scaled(float sx, float sy) const
{
VERIFY(sx >= 0.0f && sy >= 0.0f);
if (floorf(sx) == sx && floorf(sy) == sy)
@@ -436,8 +438,10 @@ RefPtr<Gfx::Bitmap> Bitmap::scaled(float sx, float sy) const
int scaled_height = (int)ceilf(sy * (float)height());
auto new_bitmap = Gfx::Bitmap::try_create(format(), { scaled_width, scaled_height }, scale());
- if (!new_bitmap)
- return nullptr;
+ if (!new_bitmap) {
+ // FIXME: Propagate the *real* error, once we have it.
+ return Error::from_errno(ENOMEM);
+ }
auto old_width = physical_width();
auto old_height = physical_height();
@@ -504,7 +508,7 @@ RefPtr<Gfx::Bitmap> Bitmap::scaled(float sx, float sy) const
// Bottom-right pixel
new_bitmap->set_pixel(new_width - 1, new_height - 1, get_pixel(physical_width() - 1, physical_height() - 1));
- return new_bitmap;
+ return new_bitmap.release_nonnull();
}
RefPtr<Gfx::Bitmap> Bitmap::cropped(Gfx::IntRect crop) const
diff --git a/Userland/Libraries/LibGfx/Bitmap.h b/Userland/Libraries/LibGfx/Bitmap.h
index ce3726b9fe..abf7eadbfc 100644
--- a/Userland/Libraries/LibGfx/Bitmap.h
+++ b/Userland/Libraries/LibGfx/Bitmap.h
@@ -113,8 +113,8 @@ public:
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> rotated(Gfx::RotationDirection) const;
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> flipped(Gfx::Orientation) const;
- [[nodiscard]] RefPtr<Gfx::Bitmap> scaled(int sx, int sy) const;
- [[nodiscard]] RefPtr<Gfx::Bitmap> scaled(float sx, float sy) const;
+ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> scaled(int sx, int sy) const;
+ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> scaled(float sx, float sy) const;
[[nodiscard]] RefPtr<Gfx::Bitmap> cropped(Gfx::IntRect) const;
[[nodiscard]] RefPtr<Bitmap> to_bitmap_backed_by_anonymous_buffer() const;
[[nodiscard]] ByteBuffer serialize_to_byte_buffer() const;