Skip to content

Commit 19a9e99

Browse files
committed
Simplify the decode example's argument parsing
The first argument is now the input jpeg file and the second argument the output png file. The output file is no longer optional and must not be specified with the --output option.
1 parent 07b0081 commit 19a9e99

File tree

2 files changed

+28
-48
lines changed

2 files changed

+28
-48
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ byteorder = "1.0"
1515
rayon = { version = "1.0", optional = true }
1616

1717
[dev-dependencies]
18-
docopt = "0.7"
1918
png = "0.5"
2019
walkdir = "1.0"
2120

examples/decode.rs

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,44 @@
1-
extern crate docopt;
21
extern crate jpeg_decoder as jpeg;
32
extern crate png;
43

5-
use docopt::Docopt;
64
use png::HasParameters;
75
use std::env;
86
use std::fs::File;
9-
use std::io::BufReader;
7+
use std::io::{self, BufReader, Write};
108
use std::process;
119

12-
const USAGE: &'static str = "
13-
Usage: decode <input> [--output=<file>]
14-
decode -h | --help
15-
16-
Options:
17-
-h --help Show this screen.
18-
-o <file>, --output=<file> Output PNG file.
19-
";
10+
fn usage() -> ! {
11+
write!(io::stderr(), "usage: decode image.jpg image.png").unwrap();
12+
process::exit(1)
13+
}
2014

2115
fn main() {
22-
let args = &Docopt::new(USAGE)
23-
.and_then(|d| d.argv(env::args()).parse())
24-
.unwrap_or_else(|e| e.exit());
25-
let input = args.get_str("<input>");
26-
let output = args.get_str("-o");
27-
let file = match File::open(input) {
28-
Ok(file) => file,
29-
Err(error) => {
30-
println!("The specified input could not be opened: {}", error);
31-
process::exit(1);
32-
},
33-
};
34-
let mut decoder = jpeg::Decoder::new(BufReader::new(file));
35-
let mut data = match decoder.decode() {
36-
Ok(data) => data,
37-
Err(error) => {
38-
println!("The image could not be decoded: {}", error);
39-
println!("If other software can decode this image successfully then it's likely that this is a bug.");
40-
process::exit(1);
41-
}
42-
};
16+
let mut args = env::args().skip(1);
17+
let input_path = args.next().unwrap_or_else(|| usage());
18+
let output_path = args.next().unwrap_or_else(|| usage());
4319

44-
if !output.is_empty() {
45-
let output_file = File::create(output).unwrap();
46-
let info = decoder.info().unwrap();
47-
let mut encoder = png::Encoder::new(output_file, info.width as u32, info.height as u32);
48-
encoder.set(png::BitDepth::Eight);
20+
let input_file = File::open(input_path).expect("The specified input file could not be opened");
21+
let mut decoder = jpeg::Decoder::new(BufReader::new(input_file));
22+
let mut data = decoder.decode().expect("Decoding failed. If other software can successfully decode the specified JPEG image, then it's likely that there is a bug in jpeg-decoder");
23+
let info = decoder.info().unwrap();
4924

50-
match info.pixel_format {
51-
jpeg::PixelFormat::L8 => encoder.set(png::ColorType::Grayscale),
52-
jpeg::PixelFormat::RGB24 => encoder.set(png::ColorType::RGB),
53-
jpeg::PixelFormat::CMYK32 => {
54-
data = cmyk_to_rgb(&mut data);
55-
encoder.set(png::ColorType::RGB)
56-
},
57-
};
25+
let output_file = File::create(output_path).unwrap();
26+
let mut encoder = png::Encoder::new(output_file, info.width as u32, info.height as u32);
27+
encoder.set(png::BitDepth::Eight);
5828

59-
encoder.write_header().expect("writing png header failed").write_image_data(&data).expect("png encoding failed");
60-
}
29+
match info.pixel_format {
30+
jpeg::PixelFormat::L8 => encoder.set(png::ColorType::Grayscale),
31+
jpeg::PixelFormat::RGB24 => encoder.set(png::ColorType::RGB),
32+
jpeg::PixelFormat::CMYK32 => {
33+
data = cmyk_to_rgb(&mut data);
34+
encoder.set(png::ColorType::RGB)
35+
},
36+
};
37+
38+
encoder.write_header()
39+
.expect("writing png header failed")
40+
.write_image_data(&data)
41+
.expect("png encoding failed");
6142
}
6243

6344
fn cmyk_to_rgb(input: &[u8]) -> Vec<u8> {

0 commit comments

Comments
 (0)