mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:37:45 +00:00
PixelPaint: Use ErrorOr<T> for Image writing/exporting functions
This commit is contained in:
parent
a7f1f1c34b
commit
77fd4625b5
2 changed files with 15 additions and 18 deletions
|
@ -150,19 +150,16 @@ void Image::serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void, String> Image::write_to_file(const String& file_path) const
|
ErrorOr<void> Image::write_to_file(const String& file_path) const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
JsonObjectSerializer json(builder);
|
JsonObjectSerializer json(builder);
|
||||||
serialize_as_json(json);
|
serialize_as_json(json);
|
||||||
json.finish();
|
json.finish();
|
||||||
|
|
||||||
auto file_or_error = Core::File::open(file_path, (Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::Truncate));
|
auto file = TRY(Core::File::open(file_path, (Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::Truncate)));
|
||||||
if (file_or_error.is_error())
|
if (!file->write(builder.string_view()))
|
||||||
return String { strerror(file_or_error.error().code()) };
|
return Error::from_errno(file->error());
|
||||||
|
|
||||||
if (!file_or_error.value()->write(builder.string_view()))
|
|
||||||
return String { file_or_error.value()->error_string() };
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,42 +191,42 @@ RefPtr<Gfx::Bitmap> Image::try_copy_bitmap(Selection const& selection) const
|
||||||
return cropped_bitmap_or_error.release_value_but_fixme_should_propagate_errors();
|
return cropped_bitmap_or_error.release_value_but_fixme_should_propagate_errors();
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void, String> Image::export_bmp_to_fd_and_close(int fd, bool preserve_alpha_channel)
|
ErrorOr<void> Image::export_bmp_to_fd_and_close(int fd, bool preserve_alpha_channel)
|
||||||
{
|
{
|
||||||
auto file = Core::File::construct();
|
auto file = Core::File::construct();
|
||||||
file->open(fd, Core::OpenMode::WriteOnly | Core::OpenMode::Truncate, Core::File::ShouldCloseFileDescriptor::Yes);
|
file->open(fd, Core::OpenMode::WriteOnly | Core::OpenMode::Truncate, Core::File::ShouldCloseFileDescriptor::Yes);
|
||||||
if (file->has_error())
|
if (file->has_error())
|
||||||
return String { file->error_string() };
|
return Error::from_errno(file->error());
|
||||||
|
|
||||||
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
|
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
|
||||||
auto bitmap = try_compose_bitmap(bitmap_format);
|
auto bitmap = try_compose_bitmap(bitmap_format);
|
||||||
if (!bitmap)
|
if (!bitmap)
|
||||||
return String { "Failed to allocate bitmap for encoding"sv };
|
return Error::from_string_literal("Failed to allocate bitmap for encoding"sv);
|
||||||
|
|
||||||
Gfx::BMPWriter dumper;
|
Gfx::BMPWriter dumper;
|
||||||
auto encoded_data = dumper.dump(bitmap);
|
auto encoded_data = dumper.dump(bitmap);
|
||||||
|
|
||||||
if (!file->write(encoded_data.data(), encoded_data.size()))
|
if (!file->write(encoded_data.data(), encoded_data.size()))
|
||||||
return String { "Failed to write encoded BMP data to file"sv };
|
return Error::from_errno(file->error());
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void, String> Image::export_png_to_fd_and_close(int fd, bool preserve_alpha_channel)
|
ErrorOr<void> Image::export_png_to_fd_and_close(int fd, bool preserve_alpha_channel)
|
||||||
{
|
{
|
||||||
auto file = Core::File::construct();
|
auto file = Core::File::construct();
|
||||||
file->open(fd, Core::OpenMode::WriteOnly | Core::OpenMode::Truncate, Core::File::ShouldCloseFileDescriptor::Yes);
|
file->open(fd, Core::OpenMode::WriteOnly | Core::OpenMode::Truncate, Core::File::ShouldCloseFileDescriptor::Yes);
|
||||||
if (file->has_error())
|
if (file->has_error())
|
||||||
return String { file->error_string() };
|
return Error::from_errno(file->error());
|
||||||
|
|
||||||
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
|
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
|
||||||
auto bitmap = try_compose_bitmap(bitmap_format);
|
auto bitmap = try_compose_bitmap(bitmap_format);
|
||||||
if (!bitmap)
|
if (!bitmap)
|
||||||
return String { "Failed to allocate bitmap for encoding"sv };
|
return Error::from_string_literal("Failed to allocate bitmap for encoding"sv);
|
||||||
|
|
||||||
auto encoded_data = Gfx::PNGWriter::encode(*bitmap);
|
auto encoded_data = Gfx::PNGWriter::encode(*bitmap);
|
||||||
if (!file->write(encoded_data.data(), encoded_data.size()))
|
if (!file->write(encoded_data.data(), encoded_data.size()))
|
||||||
return String { "Failed to write encoded PNG data to file"sv };
|
return Error::from_errno(file->error());
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,9 +70,9 @@ public:
|
||||||
void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const;
|
void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const;
|
||||||
|
|
||||||
void serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const;
|
void serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const;
|
||||||
Result<void, String> write_to_file(String const& file_path) const;
|
ErrorOr<void> write_to_file(String const& file_path) const;
|
||||||
Result<void, String> export_bmp_to_fd_and_close(int fd, bool preserve_alpha_channel);
|
ErrorOr<void> export_bmp_to_fd_and_close(int fd, bool preserve_alpha_channel);
|
||||||
Result<void, String> export_png_to_fd_and_close(int fd, bool preserve_alpha_channel);
|
ErrorOr<void> export_png_to_fd_and_close(int fd, bool preserve_alpha_channel);
|
||||||
|
|
||||||
void move_layer_to_front(Layer&);
|
void move_layer_to_front(Layer&);
|
||||||
void move_layer_to_back(Layer&);
|
void move_layer_to_back(Layer&);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue