mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
image: Only create output file for known extensions
Previously `image -o foo.asdf foo.png` would create a 0-byte foo.asdf before complaining that it doesn't know how to write .asdf images. (Lifetimes of temporaries are extended to the end of the full expresion, so this is fine.)
This commit is contained in:
parent
1dfd68c798
commit
2298aac1d1
Notes:
sideshowbarker
2024-07-17 08:55:54 +09:00
Author: https://github.com/nico Commit: https://github.com/SerenityOS/serenity/commit/2298aac1d1 Pull-request: https://github.com/SerenityOS/serenity/pull/23034 Reviewed-by: https://github.com/LucasChollet ✅
1 changed files with 7 additions and 5 deletions
|
@ -129,16 +129,18 @@ static ErrorOr<void> save_image(LoadedImage& image, StringView out_path, bool pp
|
|||
return Error::from_string_view("Can't save CMYK bitmaps yet, convert to RGB first with --convert-to-color-profile"sv);
|
||||
auto& frame = image.bitmap.get<RefPtr<Gfx::Bitmap>>();
|
||||
|
||||
auto output_stream = TRY(Core::File::open(out_path, Core::File::OpenMode::Write));
|
||||
auto buffered_stream = TRY(Core::OutputBufferedFile::create(move(output_stream)));
|
||||
auto stream = [out_path]() -> ErrorOr<NonnullOwnPtr<Core::OutputBufferedFile>> {
|
||||
auto output_stream = TRY(Core::File::open(out_path, Core::File::OpenMode::Write));
|
||||
return Core::OutputBufferedFile::create(move(output_stream));
|
||||
};
|
||||
|
||||
if (out_path.ends_with(".jpg"sv, CaseSensitivity::CaseInsensitive) || out_path.ends_with(".jpeg"sv, CaseSensitivity::CaseInsensitive)) {
|
||||
TRY(Gfx::JPEGWriter::encode(*buffered_stream, *frame, { .icc_data = image.icc_data, .quality = jpeg_quality }));
|
||||
TRY(Gfx::JPEGWriter::encode(*TRY(stream()), *frame, { .icc_data = image.icc_data, .quality = jpeg_quality }));
|
||||
return {};
|
||||
}
|
||||
if (out_path.ends_with(".ppm"sv, CaseSensitivity::CaseInsensitive)) {
|
||||
auto const format = ppm_ascii ? Gfx::PortableFormatWriter::Options::Format::ASCII : Gfx::PortableFormatWriter::Options::Format::Raw;
|
||||
TRY(Gfx::PortableFormatWriter::encode(*buffered_stream, *frame, { .format = format }));
|
||||
TRY(Gfx::PortableFormatWriter::encode(*TRY(stream()), *frame, { .format = format }));
|
||||
return {};
|
||||
}
|
||||
|
||||
|
@ -152,7 +154,7 @@ static ErrorOr<void> save_image(LoadedImage& image, StringView out_path, bool pp
|
|||
} else {
|
||||
return Error::from_string_view("can only write .bmp, .jpg, .png, .ppm, and .qoi"sv);
|
||||
}
|
||||
TRY(buffered_stream->write_until_depleted(bytes));
|
||||
TRY(TRY(stream())->write_until_depleted(bytes));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue