Skip to content

Commit

Permalink
replace -reverse with negative -speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Pennebaker committed May 10, 2023
1 parent 561b3b6 commit 08210ba
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
30 changes: 8 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Often, animations appear to accelerate when frame are removed. This is not alway

## Trim Start / End

The `-trimStart <n>` / `-trimEnd <n>` options drop `n` frames from the start and/or end of the original sequence.
The `-trimStart <n>` / `-trimEnd <n>` options drop `n` frames from the start and/or end of the original sequence. Zero indicates no trimming.

For brevity, we will now assume the None transition and elide the successive sequence repetitions.

Expand Down Expand Up @@ -181,7 +181,7 @@ With `-trimStart 1` and `-trimEnd 1`:

## Trim Edges

For convenience, we provide a similar option `-trimEdges <n>`. This drops `n` frames from both sides of the original sequence.
For convenience, we provide a similar option `-trimEdges <n>`. This drops `n` frames from both sides of the original sequence. Zero indicates no trimming.

### Before

Expand All @@ -199,7 +199,7 @@ With `-trimEdges 1`:

## Window

The `-window <n>` option truncates the original sequence to a fixed frame count. This is helpful for cutting down long animations.
The `-window <n>` option truncates the original sequence to a fixed frame count. This is helpful for cutting down long animations. Zero indicates no truncation.

### Before

Expand All @@ -225,7 +225,7 @@ With `-window 3` and `-trimStart 1`:

The `-shift <offset>` option performs a circular, leftward shift on the original sequence. This is useful for fine tuning how the GIF's very first cycle presents, before entering successive loops.

A negative offset indicates rightward shift. Zero is the neutral shift.
Zero is the neutral shift. A negative offset indicates rightward shift.

### Before

Expand All @@ -247,25 +247,11 @@ With `-shift -1`:
3 1 2
```

# REVERSE

The `-reverse` option reorders the original sequence backwards.

### Before

```text
1 2 3
```

### After

```text:
3 2 1
```

# SPEED

The `-speed <factor>` option adjusts animation speed. Speed is expressed as a factor relative to the original GIF frame delay. We recommend using values in the range `0.2` (slow) to `2.0` (fast).
The `-speed <factor>` option adjusts animation speed. Speed is expressed as a factor relative to the original GIF frame delay. We recommend using magnitudes between `0.2` (slow) and `2.0` (fast).

`1` = `1.0` is the neutral factor.

We can diagram this in terms of the frame delays, expressed in centiseconds. That is, `4cs` indicates 4 centisec = 4/100 sec between advancing to the next frame.

Expand All @@ -289,7 +275,7 @@ With `-speed 0.5`:
8cs 8cs 8cs
```

`1.0` is the neutral factor. Speed cannot be zero or negative.
A negative speed plays the original sequence in reverse order.

### Quirks, Quickly

Expand Down
18 changes: 10 additions & 8 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (

// Config models a set of animation editing manipulations.
type Config struct {
// Reverse plays the incoming sequence backwards (Default false).
Reverse bool

// TrimEdges removes frames from the start and end of the incoming sequence (Default zero).
TrimEdges int

Expand All @@ -38,7 +35,9 @@ type Config struct {
// Stitch denotes a loop continuity transition (Default Mirror).
Stitch Stitch

// Speed applies a factor to each frame delay (Default 1.0)
// Speed applies a factor to each frame delay (Default 1.0).
//
// Negative speed reverses the incoming sequence.
Speed float64
}

Expand Down Expand Up @@ -68,8 +67,8 @@ func (o Config) Validate() error {
return errors.New("window cannot be negative")
}

if o.Speed <= 0 {
return errors.New("speed must be positive")
if o.Speed == 0 {
return errors.New("speed cannot be zero")
}

return nil
Expand Down Expand Up @@ -104,9 +103,12 @@ func (o Config) Edit(destPth string, sourceGif *gif.GIF) error {
clonePaletteds[i] = clonePaletted
}

if o.Reverse {
speed := o.Speed

if o.Speed < 0 {
ReverseSlice(clonePaletteds)
ReverseSlice(sourceDelays)
speed *= -1.0
}

clonePaletteds = clonePaletteds[o.TrimStart:]
Expand Down Expand Up @@ -158,7 +160,7 @@ func (o Config) Edit(destPth string, sourceGif *gif.GIF) error {

butteryPaletteds[i] = paletted
sourceDelay := sourceDelays[r]
butteryDelays[i] = int(math.Max(2.0, float64(sourceDelay)/o.Speed))
butteryDelays[i] = int(math.Max(2.0, float64(sourceDelay)/speed))

if o.Stitch == Mirror && i >= clonePalettedsLen-1 {
r--
Expand Down
4 changes: 1 addition & 3 deletions src/cmd/buttery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ var flagTrimStart = flag.Int("trimStart", 0, "drop frames from start of the inpu
var flagTrimEnd = flag.Int("trimEnd", 0, "drop frames from end of the input GIF")
var flagWindow = flag.Int("window", 0, "set fixed sequence length")
var flagStitch = flag.String("stitch", "Mirror", "stitching strategy (None/Mirror/FlipH/FlipV)")
var flagReverse = flag.Bool("reverse", false, "reverse original sequence")
var flagShift = flag.Int("shift", 0, "rotate sequence left")
var flagSpeed = flag.Float64("speed", 1.0, "speed factor (highly sensitive)")
var flagSpeed = flag.Float64("speed", 1.0, "animation speed factor")
var flagVersion = flag.Bool("version", false, "show version information")
var flagHelp = flag.Bool("help", false, "show usage information")

Expand Down Expand Up @@ -81,7 +80,6 @@ func main() {
}

config := buttery.NewConfig()
config.Reverse = *flagReverse
config.TrimStart = *flagTrimStart + trimEdges
config.TrimEnd = *flagTrimEnd + trimEdges
config.Window = *flagWindow
Expand Down

0 comments on commit 08210ba

Please sign in to comment.