diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp index 03ab22bb52..88020e81c3 100644 --- a/Userland/Applications/PixelPaint/Image.cpp +++ b/Userland/Applications/PixelPaint/Image.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -22,7 +22,7 @@ namespace PixelPaint { -RefPtr Image::create_with_size(Gfx::IntSize const& size) +RefPtr Image::try_create_with_size(Gfx::IntSize const& size) { if (size.is_empty()) return nullptr; @@ -52,9 +52,9 @@ void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect) } } -RefPtr Image::create_from_bitmap(RefPtr bitmap) +RefPtr Image::try_create_from_bitmap(RefPtr bitmap) { - auto image = create_with_size({ bitmap->width(), bitmap->height() }); + auto image = try_create_with_size({ bitmap->width(), bitmap->height() }); if (image.is_null()) return nullptr; @@ -67,7 +67,7 @@ RefPtr Image::create_from_bitmap(RefPtr bitmap) return image; } -RefPtr Image::create_from_pixel_paint_file(String const& file_path) +RefPtr Image::try_create_from_pixel_paint_file(String const& file_path) { auto file = fopen(file_path.characters(), "r"); fseek(file, 0L, SEEK_END); @@ -83,7 +83,7 @@ RefPtr Image::create_from_pixel_paint_file(String const& file_path) return nullptr; auto json = json_or_error.value().as_object(); - auto image = create_with_size({ json.get("width").to_i32(), json.get("height").to_i32() }); + auto image = try_create_with_size({ json.get("width").to_i32(), json.get("height").to_i32() }); json.get("layers").as_array().for_each([&](JsonValue json_layer) { auto json_layer_object = json_layer.as_object(); auto width = json_layer_object.get("width").to_i32(); @@ -105,14 +105,11 @@ RefPtr Image::create_from_pixel_paint_file(String const& file_path) return image; } -RefPtr Image::create_from_file(String const& file_path) +RefPtr Image::try_create_from_file(String const& file_path) { - auto bitmap = Gfx::Bitmap::load_from_file(file_path); - if (bitmap) { - return create_from_bitmap(bitmap); - } - - return create_from_pixel_paint_file(file_path); + if (auto bitmap = Gfx::Bitmap::load_from_file(file_path)) + return try_create_from_bitmap(bitmap); + return try_create_from_pixel_paint_file(file_path); } void Image::save(String const& file_path) const @@ -188,7 +185,9 @@ void Image::add_layer(NonnullRefPtr layer) RefPtr Image::take_snapshot() const { - auto snapshot = create_with_size(m_size); + auto snapshot = try_create_with_size(m_size); + if (!snapshot) + return nullptr; for (const auto& layer : m_layers) snapshot->add_layer(*Layer::create_snapshot(*snapshot, layer)); return snapshot; diff --git a/Userland/Applications/PixelPaint/Image.h b/Userland/Applications/PixelPaint/Image.h index 5aaebb1c05..ee1590af79 100644 --- a/Userland/Applications/PixelPaint/Image.h +++ b/Userland/Applications/PixelPaint/Image.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -36,9 +36,9 @@ protected: class Image : public RefCounted { public: - static RefPtr create_with_size(Gfx::IntSize const&); - static RefPtr create_from_file(String const& file_path); - static RefPtr create_from_bitmap(RefPtr bitmap); + static RefPtr try_create_with_size(Gfx::IntSize const&); + static RefPtr try_create_from_file(String const& file_path); + static RefPtr try_create_from_bitmap(RefPtr bitmap); size_t layer_count() const { return m_layers.size(); } Layer const& layer(size_t index) const { return m_layers.at(index); } @@ -75,7 +75,7 @@ public: private: explicit Image(Gfx::IntSize const&); - static RefPtr create_from_pixel_paint_file(String const& file_path); + static RefPtr try_create_from_pixel_paint_file(String const& file_path); void did_change(); void did_modify_layer_stack(); diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp index 24eb295f72..c6958b713f 100644 --- a/Userland/Applications/PixelPaint/main.cpp +++ b/Userland/Applications/PixelPaint/main.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -87,7 +87,7 @@ int main(int argc, char** argv) "&New Image...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](auto&) { auto dialog = PixelPaint::CreateNewImageDialog::construct(window); if (dialog->exec() == GUI::Dialog::ExecOK) { - auto image = PixelPaint::Image::create_with_size(dialog->image_size()); + auto image = PixelPaint::Image::try_create_with_size(dialog->image_size()); auto bg_layer = PixelPaint::Layer::create_with_size(*image, image->size(), "Background"); image->add_layer(*bg_layer); bg_layer->bitmap().fill(Color::White); @@ -100,7 +100,7 @@ int main(int argc, char** argv) window); auto open_image_file = [&](auto& path) { - auto image = PixelPaint::Image::create_from_file(path); + auto image = PixelPaint::Image::try_create_from_file(path); if (!image) { GUI::MessageBox::show_error(window, String::formatted("Invalid image file: {}", path)); return; @@ -395,7 +395,7 @@ int main(int argc, char** argv) if (Core::File::exists(image_file_real_path)) { open_image_file(image_file_real_path); } else { - auto image = PixelPaint::Image::create_with_size({ 480, 360 }); + auto image = PixelPaint::Image::try_create_with_size({ 480, 360 }); auto bg_layer = PixelPaint::Layer::create_with_size(*image, image->size(), "Background"); image->add_layer(*bg_layer);