Skip to content

Commit

Permalink
Quick'n'dirty bounce option
Browse files Browse the repository at this point in the history
Fixes #332
  • Loading branch information
kornelski committed Jun 5, 2024
1 parent 95f824d commit ca82163
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Note that `*` is a special wildcard character, and it won't work when placed ins

You can also resize frames (with `-W <width in pixels>` option). If the input was ever encoded using a lossy video codec it's recommended to at least halve size of the frames to hide compression artefacts and counter chroma subsampling that was done by the video codec.

See `gifski -h` for more options.
See `gifski --help` for more options.

### Tips for smaller GIF files

Expand Down
16 changes: 15 additions & 1 deletion src/bin/gifski.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ fn bin_main() -> BinResult<()> {
.num_args(1)
.value_parser(value_parser!(i16))
.value_name("num"))
.arg(Arg::new("bounce")
.long("bounce")
.num_args(0)
.action(ArgAction::SetTrue)
.hide_short_help(true)
.help("Make animation play forwards then backwards"))
.arg(Arg::new("fixed-color")
.long("fixed-color")
.help("Always include this color in the palette")
Expand All @@ -182,10 +188,11 @@ fn bin_main() -> BinResult<()> {
});

let mut frames: Vec<&str> = matches.get_many::<String>("FILES").ok_or("?")?.map(|s| s.as_str()).collect();
let bounce = matches.get_flag("bounce");
if !matches.get_flag("nosort") && frames.len() > 1 {
frames.sort_by(|a, b| natord::compare(a, b));
}
let frames: Vec<_> = frames.into_iter().map(PathBuf::from).collect();
let mut frames: Vec<_> = frames.into_iter().map(PathBuf::from).collect();

let output_path = DestPath::new(matches.get_one::<PathBuf>("output").ok_or("?")?);
let width = matches.get_one::<u32>("width").copied();
Expand Down Expand Up @@ -268,6 +275,9 @@ fn bin_main() -> BinResult<()> {

let decode_thread = thread::Builder::new().name("decode".into()).spawn_scoped(scope, move || {
let mut decoder = if let [path] = &frames[..] {
if bounce {
eprintln!("warning: the bounce flag is supported only for individual files, not pipe or video");
}
let mut src = if path.as_os_str() == "-" {
let fd = stdin().lock();
if fd.is_terminal() {
Expand All @@ -291,6 +301,10 @@ fn bin_main() -> BinResult<()> {
other_type => get_video_decoder(other_type, src, rate, settings)?,
}
} else {
if bounce {
let mut extra: Vec<_> = frames.iter().skip(1).rev().cloned().collect();
frames.append(&mut extra);
}
if speed != 1.0 {
eprintln!("warning: --fast-forward option is for videos. It doesn't make sense for images. Use --fps only.");
}
Expand Down

0 comments on commit ca82163

Please sign in to comment.