mirror of
https://github.com/SerenityOS/serenity.git
synced 2026-11-07 04:29:30 -10:00
animation: Allow multiple input files
This commit is contained in:
1 parent
27de99c59c
commit
c509332126
1 file changed
+39
-17
@@ -14,7 +14,7 @@
|
||||
#include <LibGfx/ImageFormats/WebPWriter.h>
|
||||
|
||||
struct Options {
|
||||
StringView in_path;
|
||||
Vector<StringView> in_paths;
|
||||
StringView out_path;
|
||||
bool write_full_frames { false };
|
||||
Gfx::AnimationWriter::AllowInterFrameCompression allow_inter_frame_compression { Gfx::AnimationWriter::AllowInterFrameCompression::Yes };
|
||||
@@ -24,7 +24,7 @@ static ErrorOr<Options> parse_options(Main::Arguments arguments)
|
||||
{
|
||||
Options options;
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(options.in_path, "Path to input image file", "FILE");
|
||||
args_parser.add_positional_argument(options.in_paths, "Paths to input image file", "FILE");
|
||||
args_parser.add_option(options.out_path, "Path to output image file", "output", 'o', "FILE");
|
||||
|
||||
bool inter_frame_compression_full = false;
|
||||
@@ -55,34 +55,56 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
Options options = TRY(parse_options(arguments));
|
||||
|
||||
// FIXME: Allow multiple single frames as input too, and allow manually setting their duration.
|
||||
if (options.in_paths.is_empty())
|
||||
return Error::from_string_literal("Need at least one input file");
|
||||
|
||||
auto file = TRY(Core::MappedFile::map(options.in_path));
|
||||
auto decoder = TRY(Gfx::ImageDecoder::try_create_for_raw_bytes(file->bytes()));
|
||||
if (!decoder)
|
||||
return Error::from_string_literal("Could not find decoder for input file");
|
||||
Vector<NonnullOwnPtr<Core::MappedFile>> files;
|
||||
Vector<NonnullRefPtr<Gfx::ImageDecoder>> decoders;
|
||||
for (auto in_path : options.in_paths) {
|
||||
files.append(TRY(Core::MappedFile::map(in_path)));
|
||||
auto decoder = TRY(Gfx::ImageDecoder::try_create_for_raw_bytes(files.last()->bytes()));
|
||||
if (!decoder)
|
||||
return Error::from_string_literal("Could not find decoder for input file");
|
||||
decoders.append(decoder.release_nonnull());
|
||||
}
|
||||
|
||||
VERIFY(!decoders.is_empty());
|
||||
auto output_size = decoders[0]->size();
|
||||
for (auto const& decoder : decoders) {
|
||||
if (decoder->size() != output_size)
|
||||
return Error::from_string_literal("All input images must have the same dimensions");
|
||||
}
|
||||
|
||||
// FIXME: Make overridable?
|
||||
auto output_loop_count = decoders[0]->loop_count();
|
||||
|
||||
auto output_file = TRY(Core::File::open(options.out_path, Core::File::OpenMode::Write));
|
||||
auto output_stream = TRY(Core::OutputBufferedFile::create(move(output_file)));
|
||||
|
||||
auto animation_writer = TRY([&]() -> ErrorOr<NonnullOwnPtr<Gfx::AnimationWriter>> {
|
||||
if (options.out_path.ends_with(".apng"sv))
|
||||
return Gfx::PNGWriter::start_encoding_animation(*output_stream, decoder->size(), decoder->loop_count());
|
||||
return Gfx::PNGWriter::start_encoding_animation(*output_stream, output_size, output_loop_count);
|
||||
if (options.out_path.ends_with(".webp"sv))
|
||||
return Gfx::WebPWriter::start_encoding_animation(*output_stream, decoder->size(), decoder->loop_count());
|
||||
return Gfx::WebPWriter::start_encoding_animation(*output_stream, output_size, output_loop_count);
|
||||
if (options.out_path.ends_with(".gif"sv))
|
||||
return Gfx::GIFWriter::start_encoding_animation(*output_stream, decoder->size(), decoder->loop_count());
|
||||
return Gfx::GIFWriter::start_encoding_animation(*output_stream, output_size, output_loop_count);
|
||||
return Error::from_string_literal("Unable to find a encoder for the requested extension.");
|
||||
}());
|
||||
|
||||
RefPtr<Gfx::Bitmap> last_frame;
|
||||
for (size_t i = 0; i < decoder->frame_count(); ++i) {
|
||||
auto frame = TRY(decoder->frame(i));
|
||||
if (options.write_full_frames) {
|
||||
TRY(animation_writer->add_frame(*frame.image, frame.duration));
|
||||
} else {
|
||||
TRY(animation_writer->add_frame_relative_to_last_frame(*frame.image, frame.duration, last_frame, options.allow_inter_frame_compression));
|
||||
last_frame = frame.image;
|
||||
for (auto const& decoder : decoders) {
|
||||
for (size_t i = 0; i < decoder->frame_count(); ++i) {
|
||||
auto frame = TRY(decoder->frame(i));
|
||||
|
||||
// FIXME: Make overridable, at least for single-frame inputs?
|
||||
auto frame_duration = frame.duration;
|
||||
|
||||
if (options.write_full_frames) {
|
||||
TRY(animation_writer->add_frame(*frame.image, frame_duration));
|
||||
} else {
|
||||
TRY(animation_writer->add_frame_relative_to_last_frame(*frame.image, frame_duration, last_frame, options.allow_inter_frame_compression));
|
||||
last_frame = frame.image;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user