PixelPaint: Remove try_ prefix from fallible Image methods

This commit is contained in:
Linus Groh 2023-01-28 20:12:17 +00:00 committed by Jelle Raaijmakers
parent 9c08bb9555
commit 39f1a6eb6f
Notes: sideshowbarker 2024-07-17 08:59:18 +09:00
6 changed files with 31 additions and 31 deletions

View File

@ -21,7 +21,7 @@ ErrorOr<void> HistogramWidget::rebuild_histogram_data()
if (!m_image)
return {};
auto full_bitmap = TRY(m_image->try_compose_bitmap(Gfx::BitmapFormat::BGRA8888));
auto full_bitmap = TRY(m_image->compose_bitmap(Gfx::BitmapFormat::BGRA8888));
m_data.red.clear_with_capacity();
m_data.green.clear_with_capacity();

View File

@ -21,7 +21,7 @@
namespace PixelPaint {
ErrorOr<NonnullRefPtr<Image>> Image::try_create_with_size(Gfx::IntSize size)
ErrorOr<NonnullRefPtr<Image>> Image::create_with_size(Gfx::IntSize size)
{
VERIFY(!size.is_empty());
@ -51,7 +51,7 @@ void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect) con
}
}
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::try_decode_bitmap(ReadonlyBytes bitmap_data)
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::decode_bitmap(ReadonlyBytes bitmap_data)
{
// Spawn a new ImageDecoder service process and connect to it.
auto client = TRY(ImageDecoderClient::Client::try_create());
@ -72,18 +72,18 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::try_decode_bitmap(ReadonlyBytes bitma
return decoded_bitmap.release_nonnull();
}
ErrorOr<NonnullRefPtr<Image>> Image::try_create_from_bitmap(NonnullRefPtr<Gfx::Bitmap> const& bitmap)
ErrorOr<NonnullRefPtr<Image>> Image::create_from_bitmap(NonnullRefPtr<Gfx::Bitmap> const& bitmap)
{
auto image = TRY(try_create_with_size({ bitmap->width(), bitmap->height() }));
auto image = TRY(create_with_size({ bitmap->width(), bitmap->height() }));
auto layer = TRY(Layer::try_create_with_bitmap(*image, *bitmap, "Background"));
image->add_layer(move(layer));
return image;
}
ErrorOr<NonnullRefPtr<Image>> Image::try_create_from_pixel_paint_json(JsonObject const& json)
ErrorOr<NonnullRefPtr<Image>> Image::create_from_pixel_paint_json(JsonObject const& json)
{
// FIXME: Handle invalid JSON data
auto image = TRY(try_create_with_size({ json.get_i32("width"sv).value_or(0), json.get_i32("height"sv).value_or(0) }));
auto image = TRY(create_with_size({ json.get_i32("width"sv).value_or(0), json.get_i32("height"sv).value_or(0) }));
auto layers_value = json.get_array("layers"sv).value();
for (auto& layer_value : layers_value.values()) {
@ -92,13 +92,13 @@ ErrorOr<NonnullRefPtr<Image>> Image::try_create_from_pixel_paint_json(JsonObject
auto bitmap_base64_encoded = layer_object.get_deprecated_string("bitmap"sv).value();
auto bitmap_data = TRY(decode_base64(bitmap_base64_encoded));
auto bitmap = TRY(try_decode_bitmap(bitmap_data));
auto bitmap = TRY(decode_bitmap(bitmap_data));
auto layer = TRY(Layer::try_create_with_bitmap(*image, move(bitmap), name));
if (auto const& mask_object = layer_object.get_deprecated_string("mask"sv); mask_object.has_value()) {
auto mask_base64_encoded = mask_object.value();
auto mask_data = TRY(decode_base64(mask_base64_encoded));
auto mask = TRY(try_decode_bitmap(mask_data));
auto mask = TRY(decode_bitmap(mask_data));
TRY(layer->try_set_bitmaps(layer->content_bitmap(), mask));
}
@ -146,7 +146,7 @@ ErrorOr<void> Image::serialize_as_json(JsonObjectSerializer<StringBuilder>& json
return {};
}
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::try_compose_bitmap(Gfx::BitmapFormat format) const
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::compose_bitmap(Gfx::BitmapFormat format) const
{
auto bitmap = TRY(Gfx::Bitmap::create(format, m_size));
GUI::Painter painter(bitmap);
@ -154,14 +154,14 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::try_compose_bitmap(Gfx::BitmapFormat
return bitmap;
}
RefPtr<Gfx::Bitmap> Image::try_copy_bitmap(Selection const& selection) const
RefPtr<Gfx::Bitmap> Image::copy_bitmap(Selection const& selection) const
{
if (selection.is_empty())
return {};
auto selection_rect = selection.bounding_rect();
// FIXME: Add a way to only compose a certain part of the image
auto bitmap_or_error = try_compose_bitmap(Gfx::BitmapFormat::BGRA8888);
auto bitmap_or_error = compose_bitmap(Gfx::BitmapFormat::BGRA8888);
if (bitmap_or_error.is_error())
return {};
auto full_bitmap = bitmap_or_error.release_value();
@ -175,7 +175,7 @@ RefPtr<Gfx::Bitmap> Image::try_copy_bitmap(Selection const& selection) const
ErrorOr<void> Image::export_bmp_to_file(NonnullOwnPtr<Core::Stream::Stream> stream, bool preserve_alpha_channel) const
{
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
auto bitmap = TRY(try_compose_bitmap(bitmap_format));
auto bitmap = TRY(compose_bitmap(bitmap_format));
Gfx::BMPWriter dumper;
auto encoded_data = dumper.dump(bitmap);
@ -186,7 +186,7 @@ ErrorOr<void> Image::export_bmp_to_file(NonnullOwnPtr<Core::Stream::Stream> stre
ErrorOr<void> Image::export_png_to_file(NonnullOwnPtr<Core::Stream::Stream> stream, bool preserve_alpha_channel) const
{
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
auto bitmap = TRY(try_compose_bitmap(bitmap_format));
auto bitmap = TRY(compose_bitmap(bitmap_format));
auto encoded_data = TRY(Gfx::PNGWriter::encode(*bitmap));
TRY(stream->write_entire_buffer(encoded_data));
@ -195,7 +195,7 @@ ErrorOr<void> Image::export_png_to_file(NonnullOwnPtr<Core::Stream::Stream> stre
ErrorOr<void> Image::export_qoi_to_file(NonnullOwnPtr<Core::Stream::Stream> stream) const
{
auto bitmap = TRY(try_compose_bitmap(Gfx::BitmapFormat::BGRA8888));
auto bitmap = TRY(compose_bitmap(Gfx::BitmapFormat::BGRA8888));
auto encoded_data = Gfx::QOIWriter::encode(bitmap);
TRY(stream->write_entire_buffer(encoded_data));
@ -217,7 +217,7 @@ void Image::add_layer(NonnullRefPtr<Layer> layer)
ErrorOr<NonnullRefPtr<Image>> Image::take_snapshot() const
{
auto snapshot = TRY(try_create_with_size(m_size));
auto snapshot = TRY(create_with_size(m_size));
for (auto const& layer : m_layers) {
auto layer_snapshot = TRY(Layer::try_create_snapshot(*snapshot, layer));
snapshot->add_layer(move(layer_snapshot));

View File

@ -45,15 +45,15 @@ protected:
class Image : public RefCounted<Image> {
public:
static ErrorOr<NonnullRefPtr<Image>> try_create_with_size(Gfx::IntSize);
static ErrorOr<NonnullRefPtr<Image>> try_create_from_pixel_paint_json(JsonObject const&);
static ErrorOr<NonnullRefPtr<Image>> try_create_from_bitmap(NonnullRefPtr<Gfx::Bitmap> const&);
static ErrorOr<NonnullRefPtr<Image>> create_with_size(Gfx::IntSize);
static ErrorOr<NonnullRefPtr<Image>> create_from_pixel_paint_json(JsonObject const&);
static ErrorOr<NonnullRefPtr<Image>> create_from_bitmap(NonnullRefPtr<Gfx::Bitmap> const&);
static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> try_decode_bitmap(ReadonlyBytes);
static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> decode_bitmap(ReadonlyBytes);
// This generates a new Bitmap with the final image (all layers composed according to their attributes.)
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> try_compose_bitmap(Gfx::BitmapFormat format) const;
RefPtr<Gfx::Bitmap> try_copy_bitmap(Selection const&) const;
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> compose_bitmap(Gfx::BitmapFormat format) const;
RefPtr<Gfx::Bitmap> copy_bitmap(Selection const&) const;
Selection& selection() { return m_selection; }
Selection const& selection() const { return m_selection; }

View File

@ -152,7 +152,7 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
"&New Image...", { Mod_Ctrl, Key_N }, g_icon_bag.filetype_pixelpaint, [&](auto&) {
auto dialog = PixelPaint::CreateNewImageDialog::construct(&window);
if (dialog->exec() == GUI::Dialog::ExecResult::OK) {
auto image_result = PixelPaint::Image::try_create_with_size(dialog->image_size());
auto image_result = PixelPaint::Image::create_with_size(dialog->image_size());
if (image_result.is_error()) {
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Failed to create image with size {}, error: {}", dialog->image_size(), image_result.error()));
return;
@ -320,9 +320,9 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
auto* editor = current_image_editor();
VERIFY(editor);
auto bitmap = editor->image().try_copy_bitmap(editor->image().selection());
auto bitmap = editor->image().copy_bitmap(editor->image().selection());
if (!bitmap) {
dbgln("try_copy_bitmap() from Image failed");
dbgln("copy_bitmap() from Image failed");
return;
}
GUI::Clipboard::the().set_bitmap(*bitmap);
@ -1113,7 +1113,7 @@ void MainWidget::open_image(FileSystemAccessClient::File file)
ErrorOr<void> MainWidget::create_default_image()
{
auto image = TRY(Image::try_create_with_size({ 510, 356 }));
auto image = TRY(Image::create_with_size({ 510, 356 }));
auto bg_layer = TRY(Layer::try_create_with_size(*image, image->size(), "Background"));
image->add_layer(*bg_layer);
@ -1136,7 +1136,7 @@ ErrorOr<void> MainWidget::create_image_from_clipboard()
return Error::from_string_view("There is no image in a clipboard to paste."sv);
}
auto image = TRY(PixelPaint::Image::try_create_with_size(bitmap->size()));
auto image = TRY(PixelPaint::Image::create_with_size(bitmap->size()));
auto layer = TRY(PixelPaint::Layer::try_create_with_bitmap(image, *bitmap, "Pasted layer"));
image->add_layer(*layer);

View File

@ -24,15 +24,15 @@ ErrorOr<void> ProjectLoader::try_load_from_file(NonnullOwnPtr<Core::Stream::File
m_is_raw_image = true;
// FIXME: Find a way to avoid the memory copy here.
auto bitmap = TRY(Image::try_decode_bitmap(contents));
auto image = TRY(Image::try_create_from_bitmap(move(bitmap)));
auto bitmap = TRY(Image::decode_bitmap(contents));
auto image = TRY(Image::create_from_bitmap(move(bitmap)));
m_image = image;
return {};
}
auto& json = json_or_error.value().as_object();
auto image = TRY(Image::try_create_from_pixel_paint_json(json));
auto image = TRY(Image::create_from_pixel_paint_json(json));
if (json.has_array("guides"sv))
m_json_metadata = json.get_array("guides"sv).value();

View File

@ -36,7 +36,7 @@ ErrorOr<void> VectorscopeWidget::rebuild_vectorscope_data()
m_vectorscope_data.fill({});
VERIFY(AK::abs(m_vectorscope_data[0][0]) < 0.01f);
auto full_bitmap = TRY(m_image->try_compose_bitmap(Gfx::BitmapFormat::BGRA8888));
auto full_bitmap = TRY(m_image->compose_bitmap(Gfx::BitmapFormat::BGRA8888));
for (size_t x = 0; x < static_cast<size_t>(full_bitmap->width()); ++x) {
for (size_t y = 0; y < static_cast<size_t>(full_bitmap->height()); ++y) {