Skip to main content

Start to build out GIF support

ID
755bb1b
date
2021-11-29 21:49:44+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
1f79221
message
Start to build out GIF support
changed files
7 files, 54 additions, 1 deletion

Changed files

Cargo.lock (13035) → Cargo.lock (13465)

diff --git a/Cargo.lock b/Cargo.lock
index 5c05d98..237b58c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -173,6 +173,16 @@ dependencies = [
 ]
 
 [[package]]
+name = "gif"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c3a7187e78088aead22ceedeee99779455b23fc231fe13ec443f99bb71694e5b"
+dependencies = [
+ "color_quant",
+ "weezl",
+]
+
+[[package]]
 name = "hermit-abi"
 version = "0.1.19"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -190,6 +200,7 @@ dependencies = [
  "bytemuck",
  "byteorder",
  "color_quant",
+ "gif",
  "jpeg-decoder",
  "num-iter",
  "num-rational",
@@ -478,6 +489,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
 
 [[package]]
+name = "weezl"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d8b77fdfd5a253be4ab714e4ffa3c49caf146b4de743e97510c0656cf90f1e8e"
+
+[[package]]
 name = "winapi"
 version = "0.3.9"
 source = "registry+https://github.com/rust-lang/crates.io-index"

Cargo.toml (399) → Cargo.toml (406)

diff --git a/Cargo.toml b/Cargo.toml
index ceadd98..82a9812 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,7 +14,7 @@ default-features = false
 
 [dependencies.image]
 version = "0.23"
-features = ["jpeg", "png"]
+features = ["jpeg", "png", "gif"]
 default-features = false
 
 [dependencies.palette]

src/main.rs (7768) → src/main.rs (8579)

diff --git a/src/main.rs b/src/main.rs
index 4430cba..0326ada 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -67,6 +67,7 @@ fn main() {
     // of magnitude) than in debug mode.
     //
     // See https://docs.rs/image/0.23.14/image/imageops/enum.FilterType.html
+    println!("{:?}", img);
     let resized_img = img.resize(400, 400, FilterType::Nearest);
 
     let img_vec = resized_img.into_rgba8().into_raw();
@@ -131,6 +132,24 @@ mod tests {
     }
 
     #[test]
+    fn it_can_look_at_png_images() {
+        let output = get_success(&["./src/tests/red.png", "--max-colours=1"]);
+        assert_eq!(output.exit_code, 0);
+    }
+
+    #[test]
+    fn it_can_look_at_jpeg_images() {
+        let output = get_success(&["./src/tests/noise.jpg", "--max-colours=1"]);
+        assert_eq!(output.exit_code, 0);
+    }
+
+    #[test]
+    fn it_can_look_at_static_gif_images() {
+        let output = get_success(&["./src/tests/yellow.gif", "--max-colours=1"]);
+        assert_eq!(output.exit_code, 0);
+    }
+
+    #[test]
     fn it_omits_the_escape_codes_with_no_palette() {
         let output = get_success(&["./src/tests/red.png", "--max-colours=1"]);
 
@@ -160,6 +179,13 @@ mod tests {
     }
 
     #[test]
+    fn it_looks_at_multiple_frames_in_an_animated_gif() {
+        let output = get_success(&["./src/tests/animated_squares.gif"]);
+
+        assert_eq!(output.stdout.matches("\n").count(), 2, "stdout = {:?}", output.stdout);
+    }
+
+    #[test]
     fn it_fails_if_you_pass_an_invalid_max_colours() {
         let output = get_failure(&["./src/tests/red.png", "--max-colours=NaN"]);
 

src/tests/README.md (0) → src/tests/README.md (403)

diff --git a/src/tests/README.md b/src/tests/README.md
new file mode 100644
index 0000000..8835520
--- /dev/null
+++ b/src/tests/README.md
@@ -0,0 +1,10 @@
+# test images
+
+The animated GIF was created with the following command:
+
+```console
+$ convert -delay 200 -loop 10 -dispose previous red.png blue.png red.png blue.png red.png blue.png red.png blue.png animated_squares.gif
+```
+
+The 2-second delay is to avoid causing seizures from the flashing.
+I don't expect anyone else to be looking at the gif, but the delay is arbitrary so it's easy enough to avoid.

src/tests/animated_squares.gif (0) → src/tests/animated_squares.gif (703)

diff --git a/src/tests/animated_squares.gif b/src/tests/animated_squares.gif
new file mode 100644
index 0000000..b05a113
Binary files /dev/null and b/src/tests/animated_squares.gif differ

src/tests/blue.png (0) → src/tests/blue.png (774)

diff --git a/src/tests/blue.png b/src/tests/blue.png
new file mode 100644
index 0000000..e7137c6
Binary files /dev/null and b/src/tests/blue.png differ

src/tests/yellow.gif (0) → src/tests/yellow.gif (77)

diff --git a/src/tests/yellow.gif b/src/tests/yellow.gif
new file mode 100644
index 0000000..d036bdd
Binary files /dev/null and b/src/tests/yellow.gif differ