-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmtpng.rs
270 lines (234 loc) · 8.72 KB
/
mtpng.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//
// mtpng - a multithreaded parallel PNG encoder in Rust
// mtpng.rs - CLI utility for testing and Rust API example
//
// Copyright (c) 2018-2024 Brooke Vibber
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
use std::convert::TryFrom;
use std::fs::File;
use std::io;
use std::io::{Error, ErrorKind, Write};
// CLI options
extern crate clap;
use clap::{Arg, ArgMatches, Command};
// For reading an existing file
extern crate png;
extern crate rayon;
use rayon::{ThreadPool, ThreadPoolBuilder};
// For timing!
extern crate time;
use time::OffsetDateTime;
// Hey that's us!
extern crate mtpng;
use mtpng::{ColorType, CompressionLevel, Header};
use mtpng::Mode::{Adaptive, Fixed};
use mtpng::encoder::{Encoder, Options};
use mtpng::Strategy;
use mtpng::Filter;
pub fn err(payload: &str) -> Error
{
Error::new(ErrorKind::Other, payload)
}
fn expand(src: &[u8]) -> io::Result<Vec<u8>>
{
let mut v = Vec::new();
v.write_all(src)?;
Ok(v)
}
struct Image {
header: Header,
data: Vec<u8>,
palette: Option<Vec<u8>>,
transparency: Option<Vec<u8>>,
}
fn read_png(filename: &str)
-> io::Result<Image>
{
use png::Decoder;
use png::Transformations;
let mut decoder = Decoder::new(File::open(filename)?);
decoder.set_transformations(Transformations::IDENTITY);
let mut reader = decoder.read_info()?;
let info = reader.info();
let mut header = Header::new();
header.set_size(info.width, info.height)?;
header.set_color(ColorType::try_from(info.color_type as u8)?,
info.bit_depth as u8)?;
let palette = match info.palette {
Some(ref cow) => Some(expand(&cow[..])?),
None => None,
};
let transparency = match info.trns {
Some(ref cow) => Some(expand(&cow[..])?),
None => None,
};
let mut data = vec![0u8; reader.output_buffer_size()];
reader.next_frame(&mut data)?;
Ok(Image {
header,
data,
palette,
transparency
})
}
fn write_png(pool: &ThreadPool,
args: &ArgMatches,
filename: &str,
image: &Image)
-> io::Result<()>
{
let writer = File::create(filename)?;
let mut options = Options::new();
// Encoding options
options.set_thread_pool(pool)?;
match args.value_of("chunk-size") {
None => {},
Some(s) => {
let n = s.parse::<usize>().map_err(|_e| err("Invalid chunk size"))?;
options.set_chunk_size(n)?;
},
}
match args.value_of("filter") {
None => {},
Some("adaptive") => options.set_filter_mode(Adaptive)?,
Some("none") => options.set_filter_mode(Fixed(Filter::None))?,
Some("up") => options.set_filter_mode(Fixed(Filter::Up))?,
Some("sub") => options.set_filter_mode(Fixed(Filter::Sub))?,
Some("average") => options.set_filter_mode(Fixed(Filter::Average))?,
Some("paeth") => options.set_filter_mode(Fixed(Filter::Paeth))?,
_ => return Err(err("Unsupported filter type")),
}
match args.value_of("level") {
None => {},
Some("default") => options.set_compression_level(CompressionLevel::Default)?,
Some("1") => options.set_compression_level(CompressionLevel::Fast)?,
Some("9") => options.set_compression_level(CompressionLevel::High)?,
_ => return Err(err("Unsupported compression level (try default, 1, or 9)")),
}
match args.value_of("strategy") {
None => {},
Some("auto") => options.set_strategy_mode(Adaptive)?,
Some("default") => options.set_strategy_mode(Fixed(Strategy::Default))?,
Some("filtered") => options.set_strategy_mode(Fixed(Strategy::Filtered))?,
Some("huffman") => options.set_strategy_mode(Fixed(Strategy::HuffmanOnly))?,
Some("rle") => options.set_strategy_mode(Fixed(Strategy::Rle))?,
Some("fixed") => options.set_strategy_mode(Fixed(Strategy::Fixed))?,
_ => return Err(err("Invalid compression strategy mode"))?,
}
match args.value_of("streaming") {
None => {},
Some("yes") => options.set_streaming(true)?,
Some("no") => options.set_streaming(false)?,
_ => return Err(err("Invalid streaming mode, try yes or no."))
}
let mut encoder = Encoder::new(writer, &options);
// Image data
encoder.write_header(&image.header)?;
match &image.palette {
Some(v) => encoder.write_palette(v)?,
None => {},
}
match &image.transparency {
Some(v) => encoder.write_transparency(v)?,
None => {},
}
encoder.write_image_rows(&image.data)?;
encoder.finish()?;
Ok(())
}
fn doit(args: ArgMatches) -> io::Result<()> {
let threads = match args.value_of("threads") {
None => 0, // Means default
Some(s) => {
s.parse::<usize>().map_err(|_e| err("invalid threads"))?
},
};
let pool = ThreadPoolBuilder::new().num_threads(threads)
.build()
.map_err(|e| err(&e.to_string()))?;
eprintln!("Using {} threads", pool.current_num_threads());
let reps = match args.value_of("repeat") {
Some(s) => {
s.parse::<usize>().map_err(|_e| err("invalid repeat"))?
},
None => 1,
};
// input and output are guaranteed to be present
let infile = args.value_of("input").unwrap();
let outfile = args.value_of("output").unwrap();
println!("{} -> {}", infile, outfile);
let image = read_png(infile)?;
for _i in 0 .. reps {
let start_time = OffsetDateTime::now_utc();
write_png(&pool, &args, outfile, &image)?;
let delta = OffsetDateTime::now_utc() - start_time;
println!("Done in {} ms", (delta.as_seconds_f64() * 1000.0).round());
}
Ok(())
}
pub fn main() {
let matches = Command::new("mtpng parallel PNG encoder")
.version("0.4.1")
.author("Brooke Vibber <[email protected]>")
.about("Re-encodes PNG images using multiple CPU cores to exercise the mtpng library.")
.arg(Arg::new("chunk-size")
.long("chunk-size")
.value_name("bytes")
.help("Divide image into chunks of at least this given size.")
.takes_value(true))
.arg(Arg::new("filter")
.long("filter")
.value_name("filter")
.help("Set a fixed filter: one of none, sub, up, average, or paeth."))
.arg(Arg::new("level")
.long("level")
.value_name("level")
.help("Set deflate compression level, from 1-9."))
.arg(Arg::new("strategy")
.long("strategy")
.value_name("strategy")
.help("Deflate strategy: one of filtered, huffman, rle, or fixed."))
.arg(Arg::new("streaming")
.long("streaming")
.value_name("streaming")
.help("Use streaming output mode; trades off file size for lower latency and memory usage"))
.arg(Arg::new("threads")
.long("threads")
.value_name("threads")
.help("Override default number of threads."))
.arg(Arg::new("repeat")
.long("repeat")
.value_name("n")
.help("Run conversion n times, as load benchmarking helper."))
.arg(Arg::new("input")
.help("Input filename, must be another PNG.")
.required(true)
.index(1))
.arg(Arg::new("output")
.help("Output filename.")
.required(true)
.index(2))
.get_matches();
match doit(matches) {
Ok(()) => {},
Err(e) => eprintln!("Error: {}", e),
}
}