mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
PixelPaint: Port to Core::Stream
:^)
This commit is contained in:
parent
4f699d0f58
commit
301f9de915
Notes:
sideshowbarker
2024-07-17 18:08:55 +09:00
Author: https://github.com/krkk Commit: https://github.com/SerenityOS/serenity/commit/301f9de915 Pull-request: https://github.com/SerenityOS/serenity/pull/16548 Reviewed-by: https://github.com/AtkinsSJ ✅ Reviewed-by: https://github.com/LucasChollet
11 changed files with 59 additions and 74 deletions
|
@ -171,40 +171,33 @@ RefPtr<Gfx::Bitmap> Image::try_copy_bitmap(Selection const& selection) const
|
|||
return cropped_bitmap_or_error.release_value_but_fixme_should_propagate_errors();
|
||||
}
|
||||
|
||||
ErrorOr<void> Image::export_bmp_to_file(Core::File& file, bool preserve_alpha_channel) 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));
|
||||
|
||||
Gfx::BMPWriter dumper;
|
||||
auto encoded_data = dumper.dump(bitmap);
|
||||
|
||||
if (!file.write(encoded_data.data(), encoded_data.size()))
|
||||
return Error::from_errno(file.error());
|
||||
|
||||
TRY(stream->write_entire_buffer(encoded_data));
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<void> Image::export_png_to_file(Core::File& file, bool preserve_alpha_channel) const
|
||||
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 encoded_data = TRY(Gfx::PNGWriter::encode(*bitmap));
|
||||
if (!file.write(encoded_data.data(), encoded_data.size()))
|
||||
return Error::from_errno(file.error());
|
||||
|
||||
TRY(stream->write_entire_buffer(encoded_data));
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<void> Image::export_qoi_to_file(Core::File& file) const
|
||||
ErrorOr<void> Image::export_qoi_to_file(NonnullOwnPtr<Core::Stream::Stream> stream) const
|
||||
{
|
||||
auto bitmap = TRY(try_compose_bitmap(Gfx::BitmapFormat::BGRA8888));
|
||||
|
||||
auto encoded_data = Gfx::QOIWriter::encode(bitmap);
|
||||
if (!file.write(encoded_data.data(), encoded_data.size()))
|
||||
return Error::from_errno(file.error());
|
||||
|
||||
TRY(stream->write_entire_buffer(encoded_data));
|
||||
return {};
|
||||
}
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
#include <AK/RefCounted.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/Result.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibGUI/Command.h>
|
||||
#include <LibGUI/Forward.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
|
@ -73,9 +72,9 @@ public:
|
|||
void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const;
|
||||
|
||||
ErrorOr<void> serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const;
|
||||
ErrorOr<void> export_bmp_to_file(Core::File&, bool preserve_alpha_channel) const;
|
||||
ErrorOr<void> export_png_to_file(Core::File&, bool preserve_alpha_channel) const;
|
||||
ErrorOr<void> export_qoi_to_file(Core::File&) const;
|
||||
ErrorOr<void> export_bmp_to_file(NonnullOwnPtr<Core::Stream::Stream>, bool preserve_alpha_channel) const;
|
||||
ErrorOr<void> export_png_to_file(NonnullOwnPtr<Core::Stream::Stream>, bool preserve_alpha_channel) const;
|
||||
ErrorOr<void> export_qoi_to_file(NonnullOwnPtr<Core::Stream::Stream>) const;
|
||||
|
||||
void move_layer_to_front(Layer&);
|
||||
void move_layer_to_back(Layer&);
|
||||
|
|
|
@ -710,10 +710,10 @@ void ImageEditor::save_project()
|
|||
save_project_as();
|
||||
return;
|
||||
}
|
||||
auto response = FileSystemAccessClient::Client::the().try_request_file_deprecated(window(), path(), Core::OpenMode::Truncate | Core::OpenMode::WriteOnly);
|
||||
auto response = FileSystemAccessClient::Client::the().request_file(window(), path(), Core::Stream::OpenMode::Truncate | Core::Stream::OpenMode::Write);
|
||||
if (response.is_error())
|
||||
return;
|
||||
auto result = save_project_to_file(*response.value());
|
||||
auto result = save_project_to_file(response.value().release_stream());
|
||||
if (result.is_error()) {
|
||||
GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Could not save {}: {}", path(), result.error()));
|
||||
return;
|
||||
|
@ -723,21 +723,21 @@ void ImageEditor::save_project()
|
|||
|
||||
void ImageEditor::save_project_as()
|
||||
{
|
||||
auto response = FileSystemAccessClient::Client::the().try_save_file_deprecated(window(), m_title, "pp");
|
||||
auto response = FileSystemAccessClient::Client::the().save_file(window(), m_title, "pp");
|
||||
if (response.is_error())
|
||||
return;
|
||||
auto file = response.value();
|
||||
auto result = save_project_to_file(*file);
|
||||
auto file = response.release_value();
|
||||
auto result = save_project_to_file(file.release_stream());
|
||||
if (result.is_error()) {
|
||||
GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Could not save {}: {}", file->filename(), result.error()));
|
||||
GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Could not save {}: {}", file.filename(), result.error()));
|
||||
return;
|
||||
}
|
||||
set_path(file->filename());
|
||||
set_path(file.filename().to_deprecated_string());
|
||||
set_loaded_from_image(false);
|
||||
set_unmodified();
|
||||
}
|
||||
|
||||
ErrorOr<void> ImageEditor::save_project_to_file(Core::File& file) const
|
||||
ErrorOr<void> ImageEditor::save_project_to_file(NonnullOwnPtr<Core::Stream::File> file) const
|
||||
{
|
||||
StringBuilder builder;
|
||||
auto json = TRY(JsonObjectSerializer<>::try_create(builder));
|
||||
|
@ -755,8 +755,7 @@ ErrorOr<void> ImageEditor::save_project_to_file(Core::File& file) const
|
|||
TRY(json_guides.finish());
|
||||
TRY(json.finish());
|
||||
|
||||
if (!file.write(builder.string_view()))
|
||||
return Error::from_errno(file.error());
|
||||
TRY(file->write_entire_buffer(builder.string_view().bytes()));
|
||||
return {};
|
||||
}
|
||||
|
||||
|
|
|
@ -151,7 +151,7 @@ private:
|
|||
GUI::MouseEvent event_adjusted_for_layer(GUI::MouseEvent const&, Layer const&) const;
|
||||
GUI::MouseEvent event_with_pan_and_scale_applied(GUI::MouseEvent const&) const;
|
||||
|
||||
ErrorOr<void> save_project_to_file(Core::File&) const;
|
||||
ErrorOr<void> save_project_to_file(NonnullOwnPtr<Core::Stream::File>) const;
|
||||
|
||||
int calculate_ruler_step_size() const;
|
||||
Gfx::IntRect mouse_indicator_rect_x() const;
|
||||
|
|
|
@ -19,9 +19,7 @@
|
|||
#include <Applications/PixelPaint/PixelPaintWindowGML.h>
|
||||
#include <LibConfig/Client.h>
|
||||
#include <LibCore/Debounce.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/MimeData.h>
|
||||
#include <LibFileSystemAccessClient/Client.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/Clipboard.h>
|
||||
#include <LibGUI/Icon.h>
|
||||
|
@ -192,10 +190,10 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
|
|||
});
|
||||
|
||||
m_open_image_action = GUI::CommonActions::make_open_action([&](auto&) {
|
||||
auto response = FileSystemAccessClient::Client::the().try_open_file_deprecated(&window);
|
||||
auto response = FileSystemAccessClient::Client::the().open_file(&window);
|
||||
if (response.is_error())
|
||||
return;
|
||||
open_image(response.value());
|
||||
open_image(response.release_value());
|
||||
});
|
||||
|
||||
m_save_image_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
|
||||
|
@ -223,11 +221,11 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
|
|||
"As &BMP", [&](auto&) {
|
||||
auto* editor = current_image_editor();
|
||||
VERIFY(editor);
|
||||
auto response = FileSystemAccessClient::Client::the().try_save_file_deprecated(&window, editor->title(), "bmp");
|
||||
auto response = FileSystemAccessClient::Client::the().save_file(&window, editor->title(), "bmp");
|
||||
if (response.is_error())
|
||||
return;
|
||||
auto preserve_alpha_channel = GUI::MessageBox::show(&window, "Do you wish to preserve transparency?"sv, "Preserve transparency?"sv, GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
|
||||
auto result = editor->image().export_bmp_to_file(response.value(), preserve_alpha_channel == GUI::MessageBox::ExecResult::Yes);
|
||||
auto result = editor->image().export_bmp_to_file(response.value().release_stream(), preserve_alpha_channel == GUI::MessageBox::ExecResult::Yes);
|
||||
if (result.is_error())
|
||||
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Export to BMP failed: {}", result.error()));
|
||||
}));
|
||||
|
@ -238,11 +236,11 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
|
|||
auto* editor = current_image_editor();
|
||||
VERIFY(editor);
|
||||
// TODO: fix bmp on line below?
|
||||
auto response = FileSystemAccessClient::Client::the().try_save_file_deprecated(&window, editor->title(), "png");
|
||||
auto response = FileSystemAccessClient::Client::the().save_file(&window, editor->title(), "png");
|
||||
if (response.is_error())
|
||||
return;
|
||||
auto preserve_alpha_channel = GUI::MessageBox::show(&window, "Do you wish to preserve transparency?"sv, "Preserve transparency?"sv, GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
|
||||
auto result = editor->image().export_png_to_file(response.value(), preserve_alpha_channel == GUI::MessageBox::ExecResult::Yes);
|
||||
auto result = editor->image().export_png_to_file(response.value().release_stream(), preserve_alpha_channel == GUI::MessageBox::ExecResult::Yes);
|
||||
if (result.is_error())
|
||||
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Export to PNG failed: {}", result.error()));
|
||||
}));
|
||||
|
@ -252,10 +250,10 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
|
|||
"As &QOI", [&](auto&) {
|
||||
auto* editor = current_image_editor();
|
||||
VERIFY(editor);
|
||||
auto response = FileSystemAccessClient::Client::the().try_save_file_deprecated(&window, editor->title(), "qoi");
|
||||
auto response = FileSystemAccessClient::Client::the().save_file(&window, editor->title(), "qoi");
|
||||
if (response.is_error())
|
||||
return;
|
||||
auto result = editor->image().export_qoi_to_file(response.value());
|
||||
auto result = editor->image().export_qoi_to_file(response.value().release_stream());
|
||||
if (result.is_error())
|
||||
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Export to QOI failed: {}", result.error()));
|
||||
}));
|
||||
|
@ -415,11 +413,11 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
|
|||
}));
|
||||
m_edit_menu->add_action(GUI::Action::create(
|
||||
"&Load Color Palette", g_icon_bag.load_color_palette, [&](auto&) {
|
||||
auto response = FileSystemAccessClient::Client::the().try_open_file_deprecated(&window, "Load Color Palette");
|
||||
auto response = FileSystemAccessClient::Client::the().open_file(&window, "Load Color Palette");
|
||||
if (response.is_error())
|
||||
return;
|
||||
|
||||
auto result = PixelPaint::PaletteWidget::load_palette_file(*response.value());
|
||||
auto result = PixelPaint::PaletteWidget::load_palette_file(response.release_value().release_stream());
|
||||
if (result.is_error()) {
|
||||
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Loading color palette failed: {}", result.error()));
|
||||
return;
|
||||
|
@ -429,11 +427,11 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
|
|||
}));
|
||||
m_edit_menu->add_action(GUI::Action::create(
|
||||
"Sa&ve Color Palette", g_icon_bag.save_color_palette, [&](auto&) {
|
||||
auto response = FileSystemAccessClient::Client::the().try_save_file_deprecated(&window, "untitled", "palette");
|
||||
auto response = FileSystemAccessClient::Client::the().save_file(&window, "untitled", "palette");
|
||||
if (response.is_error())
|
||||
return;
|
||||
|
||||
auto result = PixelPaint::PaletteWidget::save_palette_file(m_palette_widget->colors(), *response.value());
|
||||
auto result = PixelPaint::PaletteWidget::save_palette_file(m_palette_widget->colors(), response.release_value().release_stream());
|
||||
if (result.is_error())
|
||||
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Writing color palette failed: {}", result.error()));
|
||||
}));
|
||||
|
@ -1070,10 +1068,9 @@ void MainWidget::set_actions_enabled(bool enabled)
|
|||
m_zoom_combobox->set_enabled(enabled);
|
||||
}
|
||||
|
||||
void MainWidget::open_image(Core::File& file)
|
||||
void MainWidget::open_image(FileSystemAccessClient::File file)
|
||||
{
|
||||
auto try_load = m_loader.try_load_from_file(file);
|
||||
|
||||
auto try_load = m_loader.try_load_from_file(file.release_stream());
|
||||
if (try_load.is_error()) {
|
||||
GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Unable to open file: {}, {}", file.filename(), try_load.error()));
|
||||
return;
|
||||
|
@ -1082,7 +1079,7 @@ void MainWidget::open_image(Core::File& file)
|
|||
auto& image = *m_loader.release_image();
|
||||
auto& editor = create_new_editor(image);
|
||||
editor.set_loaded_from_image(m_loader.is_raw_image());
|
||||
editor.set_path(file.filename());
|
||||
editor.set_path(file.filename().to_deprecated_string());
|
||||
editor.set_unmodified();
|
||||
m_layer_list_widget->set_image(&image);
|
||||
}
|
||||
|
@ -1264,10 +1261,10 @@ void MainWidget::drop_event(GUI::DropEvent& event)
|
|||
if (url.scheme() != "file")
|
||||
continue;
|
||||
|
||||
auto response = FileSystemAccessClient::Client::the().try_request_file_deprecated(window(), url.path(), Core::OpenMode::ReadOnly);
|
||||
auto response = FileSystemAccessClient::Client::the().request_file(window(), url.path(), Core::Stream::OpenMode::Read);
|
||||
if (response.is_error())
|
||||
return;
|
||||
open_image(response.value());
|
||||
open_image(response.release_value());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "ToolboxWidget.h"
|
||||
#include "Tools/Tool.h"
|
||||
#include "VectorscopeWidget.h"
|
||||
#include <LibFileSystemAccessClient/Client.h>
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/ComboBox.h>
|
||||
#include <LibGUI/Forward.h>
|
||||
|
@ -40,7 +41,7 @@ public:
|
|||
|
||||
ErrorOr<void> initialize_menubar(GUI::Window&);
|
||||
|
||||
void open_image(Core::File&);
|
||||
void open_image(FileSystemAccessClient::File);
|
||||
ErrorOr<void> create_default_image();
|
||||
|
||||
bool request_close();
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
#include "ImageEditor.h"
|
||||
#include <AK/Result.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
#include <LibGUI/ColorPicker.h>
|
||||
#include <LibGUI/MessageBox.h>
|
||||
|
@ -225,11 +224,14 @@ Vector<Color> PaletteWidget::colors()
|
|||
return colors;
|
||||
}
|
||||
|
||||
Result<Vector<Color>, DeprecatedString> PaletteWidget::load_palette_file(Core::File& file)
|
||||
ErrorOr<Vector<Color>> PaletteWidget::load_palette_file(NonnullOwnPtr<Core::Stream::File> file)
|
||||
{
|
||||
Vector<Color> palette;
|
||||
Array<u8, PAGE_SIZE> buffer;
|
||||
auto buffered_file = TRY(Core::Stream::BufferedFile::create(move(file)));
|
||||
|
||||
for (auto line : file.lines()) {
|
||||
while (TRY(buffered_file->can_read_line())) {
|
||||
auto line = TRY(buffered_file->read_line(buffer));
|
||||
if (line.is_whitespace())
|
||||
continue;
|
||||
|
||||
|
@ -242,29 +244,23 @@ Result<Vector<Color>, DeprecatedString> PaletteWidget::load_palette_file(Core::F
|
|||
palette.append(color.value());
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
if (palette.is_empty())
|
||||
return DeprecatedString { "The palette file did not contain any usable colors"sv };
|
||||
return Error::from_string_literal("The palette file did not contain any usable colors");
|
||||
|
||||
return palette;
|
||||
}
|
||||
|
||||
Result<Vector<Color>, DeprecatedString> PaletteWidget::load_palette_path(DeprecatedString const& file_path)
|
||||
ErrorOr<Vector<Color>> PaletteWidget::load_palette_path(DeprecatedString const& file_path)
|
||||
{
|
||||
auto file_or_error = Core::File::open(file_path, Core::OpenMode::ReadOnly);
|
||||
if (file_or_error.is_error())
|
||||
return DeprecatedString { strerror(file_or_error.error().code()) };
|
||||
|
||||
auto& file = *file_or_error.value();
|
||||
return load_palette_file(file);
|
||||
auto file = TRY(Core::Stream::File::open(file_path, Core::Stream::OpenMode::Read));
|
||||
return load_palette_file(move(file));
|
||||
}
|
||||
|
||||
Result<void, DeprecatedString> PaletteWidget::save_palette_file(Vector<Color> palette, Core::File& file)
|
||||
ErrorOr<void> PaletteWidget::save_palette_file(Vector<Color> palette, NonnullOwnPtr<Core::Stream::File> file)
|
||||
{
|
||||
for (auto& color : palette) {
|
||||
file.write(color.to_deprecated_string_without_alpha());
|
||||
file.write("\n"sv);
|
||||
TRY(file->write_entire_buffer(color.to_deprecated_string_without_alpha().bytes()));
|
||||
TRY(file->write_entire_buffer({ "\n", 1 }));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
|
|
@ -31,9 +31,10 @@ public:
|
|||
|
||||
Vector<Color> colors();
|
||||
|
||||
static Result<Vector<Color>, DeprecatedString> load_palette_file(Core::File&);
|
||||
static Result<Vector<Color>, DeprecatedString> load_palette_path(DeprecatedString const&);
|
||||
static Result<void, DeprecatedString> save_palette_file(Vector<Color>, Core::File&);
|
||||
static ErrorOr<Vector<Color>> load_palette_file(NonnullOwnPtr<Core::Stream::File>);
|
||||
static ErrorOr<Vector<Color>> load_palette_path(DeprecatedString const&);
|
||||
static ErrorOr<void> save_palette_file(Vector<Color>, NonnullOwnPtr<Core::Stream::File>);
|
||||
|
||||
static Vector<Color> fallback_colors();
|
||||
|
||||
void set_image_editor(ImageEditor*);
|
||||
|
|
|
@ -10,15 +10,14 @@
|
|||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/Result.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/MappedFile.h>
|
||||
#include <LibImageDecoderClient/Client.h>
|
||||
|
||||
namespace PixelPaint {
|
||||
|
||||
ErrorOr<void> ProjectLoader::try_load_from_file(Core::File& file)
|
||||
ErrorOr<void> ProjectLoader::try_load_from_file(NonnullOwnPtr<Core::Stream::File> file)
|
||||
{
|
||||
auto contents = file.read_all();
|
||||
auto contents = TRY(file->read_until_eof());
|
||||
|
||||
auto json_or_error = JsonValue::from_string(contents);
|
||||
if (json_or_error.is_error()) {
|
||||
|
|
|
@ -18,7 +18,7 @@ public:
|
|||
ProjectLoader() = default;
|
||||
~ProjectLoader() = default;
|
||||
|
||||
ErrorOr<void> try_load_from_file(Core::File&);
|
||||
ErrorOr<void> try_load_from_file(NonnullOwnPtr<Core::Stream::File>);
|
||||
|
||||
bool is_raw_image() const { return m_is_raw_image; }
|
||||
bool has_image() const { return !m_image.is_null(); }
|
||||
|
|
|
@ -74,10 +74,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
window->show();
|
||||
|
||||
if (image_file) {
|
||||
auto response = FileSystemAccessClient::Client::the().try_request_file_read_only_approved_deprecated(window, image_file);
|
||||
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, image_file);
|
||||
if (response.is_error())
|
||||
return 1;
|
||||
main_widget->open_image(response.value());
|
||||
main_widget->open_image(response.release_value());
|
||||
} else {
|
||||
TRY(main_widget->create_default_image());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue