فهرست منبع

LibGfx: Use ErrorOr<T> for Bitmap::scaled()

Andreas Kling 3 سال پیش
والد
کامیت
5e41c70e83

+ 1 - 1
Userland/Applications/DisplaySettings/MonitorWidget.cpp

@@ -123,7 +123,7 @@ void MonitorWidget::redraw_desktop_if_needed()
     float sh = (float)m_desktop_bitmap->height() / (float)m_desktop_resolution.height();
 
     auto scaled_size = m_wallpaper_bitmap->size().to_type<float>().scaled_by(sw, sh).to_type<int>();
-    auto scaled_bitmap = m_wallpaper_bitmap->scaled(sw, sh);
+    auto scaled_bitmap = m_wallpaper_bitmap->scaled(sw, sh).release_value_but_fixme_should_propagate_errors();
 
     if (m_desktop_wallpaper_mode == "center") {
         auto centered_rect = Gfx::IntRect({}, scaled_size).centered_within(m_desktop_bitmap->rect());

+ 13 - 9
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

+ 2 - 2
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;

+ 5 - 2
Userland/Services/Taskbar/TaskbarWindow.cpp

@@ -310,8 +310,11 @@ void TaskbarWindow::wm_event(GUI::WMEvent& event)
                 if (icon->height() != taskbar_icon_size() || icon->width() != taskbar_icon_size()) {
                     auto sw = taskbar_icon_size() / (float)icon->width();
                     auto sh = taskbar_icon_size() / (float)icon->height();
-                    auto scaled_bitmap = icon->scaled(sw, sh);
-                    window->button()->set_icon(move(scaled_bitmap));
+                    auto scaled_bitmap_or_error = icon->scaled(sw, sh);
+                    if (scaled_bitmap_or_error.is_error())
+                        window->button()->set_icon(nullptr);
+                    else
+                        window->button()->set_icon(scaled_bitmap_or_error.release_value());
                 } else {
                     window->button()->set_icon(icon);
                 }