annotate src/main.rs @ 1:1a362692ed76 default tip

Add native support for stdin/stdout.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 11 Jan 2022 16:25:21 +0100
parents 9b4be43ea74f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
1 use image::{imageops::FilterType, GenericImageView};
0
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 use log::info;
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 use std::env;
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 use std::error::Error;
1
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
5 use std::fs::File;
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
6 use std::io::{self, BufReader, Read};
0
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 use std::path::Path;
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 use std::str::FromStr;
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 fn main() -> Result<(), Box<dyn Error>> {
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 let args: Vec<_> = env::args().collect();
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 if args.len() != 4 {
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 eprintln!("Usage: {} <size> <input> <output>", args[0]);
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 std::process::exit(1);
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 }
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 let size = u32::from_str(&args[1])?;
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18
1
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
19 let mut input = Vec::new();
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
20 if args[2] == "-" {
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
21 io::stdin().read_to_end(&mut input)?;
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
22 } else {
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
23 let file = File::open(&args[2])?;
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
24 let mut reader = BufReader::new(file);
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
25 reader.read_to_end(&mut input)?;
0
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 }
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27
1
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
28 let output = if args[3] == "-" {
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
29 Path::new("/dev/stdout")
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
30 } else {
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
31 Path::new(&args[3])
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
32 };
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
33
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
34 // Guess the format of the input file.
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
35 let format = image::guess_format(&input)?;
0
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36
1
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
37 // Decode the image file.
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
38 let image = image::load_from_memory(&input)?;
0
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
1
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
40 let (mut width, mut height) = image.dimensions();
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
41 if width > size {
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
42 width = size;
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
43 }
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
44 if height > size {
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
45 height = size;
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
46 }
0
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 // Resize it to the requested size.
1
1a362692ed76 Add native support for stdin/stdout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
49 let image = image.resize(width, height, FilterType::Triangle);
0
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 // Then save it to the output.
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 image.save_with_format(output, format)?;
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 info!("Successfully resized!");
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 Ok(())
9b4be43ea74f Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 }