|
1 | | -extern crate docopt; |
2 | 1 | extern crate jpeg_decoder as jpeg; |
3 | 2 | extern crate png; |
4 | 3 |
|
5 | | -use docopt::Docopt; |
6 | 4 | use png::HasParameters; |
7 | 5 | use std::env; |
8 | 6 | use std::fs::File; |
9 | | -use std::io::BufReader; |
| 7 | +use std::io::{self, BufReader, Write}; |
10 | 8 | use std::process; |
11 | 9 |
|
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 | +} |
20 | 14 |
|
21 | 15 | 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()); |
43 | 19 |
|
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(); |
49 | 24 |
|
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); |
58 | 28 |
|
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"); |
61 | 42 | } |
62 | 43 |
|
63 | 44 | fn cmyk_to_rgb(input: &[u8]) -> Vec<u8> { |
|
0 commit comments