Mercurial > image-resizer
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 |
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 | 2 use log::info; |
3 use std::env; | |
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 | 7 use std::path::Path; |
8 use std::str::FromStr; | |
9 | |
10 fn main() -> Result<(), Box<dyn Error>> { | |
11 let args: Vec<_> = env::args().collect(); | |
12 if args.len() != 4 { | |
13 eprintln!("Usage: {} <size> <input> <output>", args[0]); | |
14 std::process::exit(1); | |
15 } | |
16 | |
17 let size = u32::from_str(&args[1])?; | |
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 | 26 } |
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 | 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 | 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 | 47 |
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 | 50 |
51 // Then save it to the output. | |
52 image.save_with_format(output, format)?; | |
53 | |
54 info!("Successfully resized!"); | |
55 Ok(()) | |
56 } |