Merge pull request #47 from shssoichiro/feature/43-Extract-Presets
Allow using optimization presets from crate
This commit is contained in:
commit
723c0c29fb
6 changed files with 133 additions and 114 deletions
|
|
@ -26,6 +26,8 @@ matrix:
|
|||
rust: nightly
|
||||
env: TARGET=x86_64-unknown-linux-gnu
|
||||
cache: cargo
|
||||
allow_failures:
|
||||
- rust: nightly
|
||||
|
||||
before_install:
|
||||
- export PATH="$PATH:$HOME/.cargo/bin"
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
**Version 0.11.0 (unreleased)**
|
||||
- [SEMVER_MAJOR] Bump minimum rustc version to 1.8.0, required by dependencies
|
||||
- [SEMVER_MINOR] Allow calling optimization presets via crate using `Options::from_preset`
|
||||
|
||||
**Version 0.10.0**
|
||||
- [SEMVER_MINOR] Make clap and regex dependencies optional
|
||||
|
|
|
|||
12
Cargo.lock
generated
12
Cargo.lock
generated
|
|
@ -5,7 +5,7 @@ dependencies = [
|
|||
"bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clap 2.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clippy 0.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clippy 0.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"crc 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"image 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"itertools 0.4.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
@ -61,15 +61,15 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "clippy"
|
||||
version = "0.0.80"
|
||||
version = "0.0.81"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"clippy_lints 0.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clippy_lints 0.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clippy_lints"
|
||||
version = "0.0.80"
|
||||
version = "0.0.81"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
@ -423,8 +423,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
|
||||
"checksum byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855"
|
||||
"checksum clap 2.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6adb6a046b8155874daf331e6cb6f4a3edf3ea3cbc625809eb4077a384124761"
|
||||
"checksum clippy 0.0.80 (registry+https://github.com/rust-lang/crates.io-index)" = "e96469b413984c78285727f94f9c626a1f2006cecdcf813b5d6893c0c85df42f"
|
||||
"checksum clippy_lints 0.0.80 (registry+https://github.com/rust-lang/crates.io-index)" = "f11938c4b10c556903bb1c1e717eb038658324bf7197e4cfc159a16417327345"
|
||||
"checksum clippy 0.0.81 (registry+https://github.com/rust-lang/crates.io-index)" = "4eb4e734b771514b74b44e0d04e176e572d6e0637e32fe112331a0063a22ef39"
|
||||
"checksum clippy_lints 0.0.81 (registry+https://github.com/rust-lang/crates.io-index)" = "cd863204278a2011c4bf82417e7e8b869afd949410ce8d0dab14da2c79b96af9"
|
||||
"checksum crc 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2a3f9159e74024e2cdb6f574e9117cdc2e91523a890930a022823d17abedc90a"
|
||||
"checksum crossbeam 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "fb974f835e90390c5f9dfac00f05b06dc117299f5ea4e85fbc7bb443af4911cc"
|
||||
"checksum enum_primitive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f79eff5be92a4d7d5bddf7daa7d650717ea71628634efe6ca7bcda85b2183c23"
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ version = "^2.2.5"
|
|||
|
||||
[dependencies.clippy]
|
||||
optional = true
|
||||
version = "0.0.80"
|
||||
version = "0.0.81"
|
||||
|
||||
[dependencies.regex]
|
||||
optional = true
|
||||
|
|
|
|||
108
src/lib.rs
108
src/lib.rs
|
|
@ -112,6 +112,114 @@ pub struct Options {
|
|||
pub threads: usize,
|
||||
}
|
||||
|
||||
impl Options {
|
||||
pub fn from_preset(level: u8) -> Options {
|
||||
let mut opts = Options::default();
|
||||
match level {
|
||||
0 => {
|
||||
opts.idat_recoding = false;
|
||||
let mut compression = HashSet::new();
|
||||
compression.insert(3);
|
||||
opts.compression = compression;
|
||||
}
|
||||
1 => {
|
||||
let filter = HashSet::new();
|
||||
opts.filter = filter;
|
||||
let strategies = HashSet::new();
|
||||
opts.strategies = strategies;
|
||||
opts.use_heuristics = true;
|
||||
}
|
||||
// 2 is the default
|
||||
3 => {
|
||||
let mut filter = HashSet::new();
|
||||
filter.insert(0);
|
||||
filter.insert(5);
|
||||
opts.filter = filter;
|
||||
let mut compression = HashSet::new();
|
||||
compression.insert(9);
|
||||
opts.compression = compression;
|
||||
let mut memory = HashSet::new();
|
||||
for i in 8..10 {
|
||||
memory.insert(i);
|
||||
}
|
||||
opts.memory = memory;
|
||||
let mut strategies = HashSet::new();
|
||||
for i in 0..4 {
|
||||
strategies.insert(i);
|
||||
}
|
||||
opts.strategies = strategies;
|
||||
}
|
||||
4 => {
|
||||
let mut filter = HashSet::new();
|
||||
for i in 0..6 {
|
||||
filter.insert(i);
|
||||
}
|
||||
opts.filter = filter;
|
||||
let mut compression = HashSet::new();
|
||||
compression.insert(9);
|
||||
opts.compression = compression;
|
||||
let mut memory = HashSet::new();
|
||||
for i in 8..10 {
|
||||
memory.insert(i);
|
||||
}
|
||||
opts.memory = memory;
|
||||
let mut strategies = HashSet::new();
|
||||
for i in 0..4 {
|
||||
strategies.insert(i);
|
||||
}
|
||||
opts.strategies = strategies;
|
||||
}
|
||||
5 => {
|
||||
let mut filter = HashSet::new();
|
||||
for i in 0..6 {
|
||||
filter.insert(i);
|
||||
}
|
||||
opts.filter = filter;
|
||||
let mut compression = HashSet::new();
|
||||
for i in 3..10 {
|
||||
compression.insert(i);
|
||||
}
|
||||
opts.compression = compression;
|
||||
let mut memory = HashSet::new();
|
||||
for i in 8..10 {
|
||||
memory.insert(i);
|
||||
}
|
||||
opts.memory = memory;
|
||||
let mut strategies = HashSet::new();
|
||||
for i in 0..4 {
|
||||
strategies.insert(i);
|
||||
}
|
||||
opts.strategies = strategies;
|
||||
}
|
||||
// Level 6
|
||||
// If higher than 6, assume 6
|
||||
_ => {
|
||||
let mut filter = HashSet::new();
|
||||
for i in 0..6 {
|
||||
filter.insert(i);
|
||||
}
|
||||
opts.filter = filter;
|
||||
let mut compression = HashSet::new();
|
||||
for i in 1..10 {
|
||||
compression.insert(i);
|
||||
}
|
||||
opts.compression = compression;
|
||||
let mut memory = HashSet::new();
|
||||
for i in 7..10 {
|
||||
memory.insert(i);
|
||||
}
|
||||
opts.memory = memory;
|
||||
let mut strategies = HashSet::new();
|
||||
for i in 0..4 {
|
||||
strategies.insert(i);
|
||||
}
|
||||
opts.strategies = strategies;
|
||||
}
|
||||
}
|
||||
opts
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Options {
|
||||
fn default() -> Options {
|
||||
// Default settings based on -o 2 from the CLI interface
|
||||
|
|
|
|||
122
src/main.rs
122
src/main.rs
|
|
@ -4,6 +4,7 @@ extern crate regex;
|
|||
|
||||
use clap::{App, Arg, ArgMatches};
|
||||
use oxipng::headers::Headers;
|
||||
use oxipng::Options;
|
||||
use regex::Regex;
|
||||
use std::collections::HashSet;
|
||||
use std::fs::DirBuilder;
|
||||
|
|
@ -205,15 +206,13 @@ fn main() {
|
|||
regardless of the order you write the arguments.")
|
||||
.get_matches();
|
||||
|
||||
let mut opts = oxipng::Options::default();
|
||||
|
||||
match parse_opts_into_struct(&matches, &mut opts) {
|
||||
Ok(_) => (),
|
||||
let opts = match parse_opts_into_struct(&matches) {
|
||||
Ok(x) => x,
|
||||
Err(x) => {
|
||||
writeln!(&mut stderr(), "{}", x).ok();
|
||||
return ();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
handle_optimization(matches.values_of("files")
|
||||
.unwrap()
|
||||
|
|
@ -222,7 +221,7 @@ fn main() {
|
|||
opts);
|
||||
}
|
||||
|
||||
fn handle_optimization(inputs: Vec<PathBuf>, opts: oxipng::Options) {
|
||||
fn handle_optimization(inputs: Vec<PathBuf>, opts: Options) {
|
||||
for input in inputs {
|
||||
let mut current_opts = opts.clone();
|
||||
if input.is_dir() {
|
||||
|
|
@ -251,107 +250,16 @@ fn handle_optimization(inputs: Vec<PathBuf>, opts: oxipng::Options) {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_opts_into_struct(matches: &ArgMatches, opts: &mut oxipng::Options) -> Result<(), String> {
|
||||
match matches.value_of("optimization") {
|
||||
Some("0") => {
|
||||
opts.idat_recoding = false;
|
||||
let mut compression = HashSet::new();
|
||||
compression.insert(3);
|
||||
opts.compression = compression;
|
||||
fn parse_opts_into_struct(matches: &ArgMatches) -> Result<Options, String> {
|
||||
let mut opts = if let Some(x) = matches.value_of("optimization") {
|
||||
if let Ok(opt) = x.parse::<u8>() {
|
||||
Options::from_preset(opt)
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
Some("1") => {
|
||||
let filter = HashSet::new();
|
||||
opts.filter = filter;
|
||||
let strategies = HashSet::new();
|
||||
opts.strategies = strategies;
|
||||
opts.use_heuristics = true;
|
||||
}
|
||||
// 2 is the default
|
||||
Some("3") => {
|
||||
let mut filter = HashSet::new();
|
||||
filter.insert(0);
|
||||
filter.insert(5);
|
||||
opts.filter = filter;
|
||||
let mut compression = HashSet::new();
|
||||
compression.insert(9);
|
||||
opts.compression = compression;
|
||||
let mut memory = HashSet::new();
|
||||
for i in 8..10 {
|
||||
memory.insert(i);
|
||||
}
|
||||
opts.memory = memory;
|
||||
let mut strategies = HashSet::new();
|
||||
for i in 0..4 {
|
||||
strategies.insert(i);
|
||||
}
|
||||
opts.strategies = strategies;
|
||||
}
|
||||
Some("4") => {
|
||||
let mut filter = HashSet::new();
|
||||
for i in 0..6 {
|
||||
filter.insert(i);
|
||||
}
|
||||
opts.filter = filter;
|
||||
let mut compression = HashSet::new();
|
||||
compression.insert(9);
|
||||
opts.compression = compression;
|
||||
let mut memory = HashSet::new();
|
||||
for i in 8..10 {
|
||||
memory.insert(i);
|
||||
}
|
||||
opts.memory = memory;
|
||||
let mut strategies = HashSet::new();
|
||||
for i in 0..4 {
|
||||
strategies.insert(i);
|
||||
}
|
||||
opts.strategies = strategies;
|
||||
}
|
||||
Some("5") => {
|
||||
let mut filter = HashSet::new();
|
||||
for i in 0..6 {
|
||||
filter.insert(i);
|
||||
}
|
||||
opts.filter = filter;
|
||||
let mut compression = HashSet::new();
|
||||
for i in 3..10 {
|
||||
compression.insert(i);
|
||||
}
|
||||
opts.compression = compression;
|
||||
let mut memory = HashSet::new();
|
||||
for i in 8..10 {
|
||||
memory.insert(i);
|
||||
}
|
||||
opts.memory = memory;
|
||||
let mut strategies = HashSet::new();
|
||||
for i in 0..4 {
|
||||
strategies.insert(i);
|
||||
}
|
||||
opts.strategies = strategies;
|
||||
}
|
||||
Some("6") => {
|
||||
let mut filter = HashSet::new();
|
||||
for i in 0..6 {
|
||||
filter.insert(i);
|
||||
}
|
||||
opts.filter = filter;
|
||||
let mut compression = HashSet::new();
|
||||
for i in 1..10 {
|
||||
compression.insert(i);
|
||||
}
|
||||
opts.compression = compression;
|
||||
let mut memory = HashSet::new();
|
||||
for i in 7..10 {
|
||||
memory.insert(i);
|
||||
}
|
||||
opts.memory = memory;
|
||||
let mut strategies = HashSet::new();
|
||||
for i in 0..4 {
|
||||
strategies.insert(i);
|
||||
}
|
||||
opts.strategies = strategies;
|
||||
}
|
||||
_ => (), // Use default
|
||||
}
|
||||
} else {
|
||||
Options::default()
|
||||
};
|
||||
|
||||
if let Some(x) = matches.value_of("interlace") {
|
||||
opts.interlace = x.parse::<u8>().ok();
|
||||
|
|
@ -498,7 +406,7 @@ fn parse_opts_into_struct(matches: &ArgMatches, opts: &mut oxipng::Options) -> R
|
|||
opts.threads = x.parse::<usize>().unwrap();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(opts)
|
||||
}
|
||||
|
||||
fn parse_numeric_range_opts(input: &str,
|
||||
|
|
|
|||
Loading…
Reference in a new issue