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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//! `gamut convert` — decode an image and re-encode it with a gamut codec.
use std::path::PathBuf;
use clap::{Args, ValueEnum};
use gamut::avif::AvifEncoder;
use gamut::core::{EncodeImage, ImageRef, Rgb8, Rgba8};
use gamut::jpeg::{ChromaSubsampling as JpegChroma, JpegEncoder};
use gamut::jxl::{
Container as JxlContainer, Distance as JxlDistance, Effort as JxlEffort, JxlEncoder,
};
use gamut::png::{Level as PngLevel, PngEncoder};
use gamut::tiff::{Compression as TiffCompression, TiffEncoder};
use gamut::webp::WebpEncoder;
use crate::error::CliError;
use crate::input::{decode_rgb8, decode_rgba8};
/// Arguments for `gamut convert`.
#[derive(Args)]
pub(crate) struct ConvertArgs {
/// Input image (PNG, JPEG, PPM/P6, WebP, or JPEG XL). WebP and JPEG XL are decoded by gamut's
/// own decoders.
input: PathBuf,
/// Output file. The format is inferred from its extension unless `--format` is given.
output: PathBuf,
/// Output format. Defaults to the output file's extension.
#[arg(long, value_enum)]
format: Option<OutputFormat>,
/// AVIF mode selector: `0` keeps the lossless default; any nonzero value selects lossy AVIF at
/// `--quality` (the encoder now takes a `0..=100` quality rather than a raw `base_q_idx`).
#[arg(long, default_value_t = 0)]
qindex: u8,
/// Encode lossy (WebP VP8 intra) instead of lossless. For AVIF, select lossy with `--qindex`.
#[arg(long)]
lossy: bool,
/// Lossy quality, 0–100 (higher is better but larger). Used with WebP `--lossy`, lossy AVIF,
/// and JPEG (which is always lossy).
#[arg(long, default_value_t = 75)]
quality: u8,
/// JPEG chroma subsampling for colour input: `444` (none), `422` (halve horizontally), or `420`
/// (halve both, the default). Ignored for other output formats and for grayscale.
#[arg(long = "jpeg-subsampling", value_enum, default_value = "420")]
jpeg_subsampling: JpegSubsampling,
/// JPEG restart interval in MCUs: insert an RSTn marker every N MCUs so a decoder can resync
/// (`0`, the default, disables restarts). Ignored for other output formats.
#[arg(long = "jpeg-restart-interval", default_value_t = 0)]
jpeg_restart_interval: u16,
/// Encode JPEG output progressively (SOF2, spectral-band scans with successive approximation)
/// instead of baseline sequential. Ignored for other output formats.
#[arg(long = "jpeg-progressive")]
jpeg_progressive: bool,
/// Compress TIFF output with PackBits run-length encoding instead of storing it uncompressed.
#[arg(long)]
packbits: bool,
/// JPEG XL Butteraugli distance for lossy encoding (~1.0 = visually lossless, up to 25.0).
/// Supplying it selects lossy JXL; omitting it keeps the lossless default. Ignored for other
/// output formats.
#[arg(long)]
jxl_distance: Option<f32>,
/// JPEG XL encoder effort, 1 (fastest) to 10 (densest); libjxl's default is 7. Ignored for
/// other output formats.
#[arg(long, default_value_t = 7, value_parser = clap::value_parser!(u8).range(1..=10))]
jxl_effort: u8,
/// Emit JPEG XL in the ISO BMFF (`.jxl` box) container instead of a bare codestream. Ignored
/// for other output formats.
#[arg(long)]
jxl_container: bool,
}
/// Output container/codec for `gamut convert`.
#[derive(Clone, Copy, ValueEnum)]
pub(crate) enum OutputFormat {
/// AVIF (8-bit RGB; lossless or lossy intra via `--qindex`).
Avif,
/// WebP — lossless (VP8L) or lossy (VP8, with `--lossy`); transparency is preserved.
Webp,
/// TIFF (8-bit RGB; uncompressed, or PackBits with `--packbits`).
Tiff,
/// PNG — lossless; transparency preserved, with automatic lossless colour-type reduction.
Png,
/// JPEG XL — lossless by default, or lossy at `--jxl-distance`; transparency preserved.
Jxl,
/// JPEG (JPEG-1 baseline) — always lossy at `--quality`, YCbCr with `--jpeg-subsampling`.
Jpeg,
}
/// Chroma subsampling for JPEG YCbCr output.
#[derive(Clone, Copy, ValueEnum)]
pub(crate) enum JpegSubsampling {
/// 4:4:4 — no chroma subsampling (full-resolution Cb/Cr).
#[value(name = "444")]
S444,
/// 4:2:2 — halve chroma horizontally.
#[value(name = "422")]
S422,
/// 4:2:0 — halve chroma both horizontally and vertically.
#[value(name = "420")]
S420,
}
impl JpegSubsampling {
/// Maps the CLI choice onto the codec's [`JpegChroma`] enum.
fn to_codec(self) -> JpegChroma {
match self {
JpegSubsampling::S444 => JpegChroma::Ycbcr444,
JpegSubsampling::S422 => JpegChroma::Ycbcr422,
JpegSubsampling::S420 => JpegChroma::Ycbcr420,
}
}
}
/// Runs the `convert` command: decode the input, encode it, and report the result.
pub(crate) fn run(args: &ConvertArgs) -> Result<(), CliError> {
let format = resolve_format(args)?;
let mut out = Vec::new();
let (raw_len, dims) = match format {
OutputFormat::Avif => {
let (rgb, dims) = decode_rgb8(&args.input)?;
tracing::info!(
width = dims.width,
height = dims.height,
bytes = rgb.len(),
"decoded input"
);
// `AvifEncoder` migrated from a raw `base_q_idx` to a lossless()/lossy(quality) model;
// qindex 0 keeps the lossless default, any nonzero value selects lossy at --quality.
let encoder = if args.qindex == 0 {
AvifEncoder::lossless()
} else {
AvifEncoder::lossy(args.quality)
};
encoder.encode_image(ImageRef::<Rgb8>::new(&rgb, dims)?, &mut out)?;
(rgb.len(), dims)
}
OutputFormat::Webp => {
// RGBA so transparency survives; `encode_rgba8` emits a simple file when fully opaque.
let (rgba, dims) = decode_rgba8(&args.input)?;
tracing::info!(
width = dims.width,
height = dims.height,
bytes = rgba.len(),
"decoded input"
);
let encoder = if args.lossy {
WebpEncoder::lossy(args.quality)
} else {
WebpEncoder::lossless()
};
encoder.encode_image(ImageRef::<Rgba8>::new(&rgba, dims)?, &mut out)?;
(rgba.len(), dims)
}
OutputFormat::Tiff => {
let (rgb, dims) = decode_rgb8(&args.input)?;
tracing::info!(
width = dims.width,
height = dims.height,
bytes = rgb.len(),
"decoded input"
);
let compression = if args.packbits {
TiffCompression::PackBits
} else {
TiffCompression::None
};
let image = ImageRef::<Rgb8>::new(&rgb, dims)?;
TiffEncoder::new()
.with_compression(compression)
.encode_image(image, &mut out)?;
(rgb.len(), dims)
}
OutputFormat::Png => {
// RGBA so transparency survives; auto-reduce drops it (and chooses grey/palette) when
// that is lossless.
let (rgba, dims) = decode_rgba8(&args.input)?;
tracing::info!(
width = dims.width,
height = dims.height,
bytes = rgba.len(),
"decoded input"
);
PngEncoder::new()
.with_compression(PngLevel::Best)
.with_auto_reduce(true)
.encode_image(ImageRef::<Rgba8>::new(&rgba, dims)?, &mut out)?;
(rgba.len(), dims)
}
OutputFormat::Jxl => {
// RGBA so transparency survives, matching the PNG/WebP paths.
let (rgba, dims) = decode_rgba8(&args.input)?;
tracing::info!(
width = dims.width,
height = dims.height,
bytes = rgba.len(),
"decoded input"
);
// A `--jxl-distance` selects lossy; `Distance::new` validates the range and surfaces an
// out-of-range value as the codec's `InvalidInput` through `CliError::Codec`.
let encoder = match args.jxl_distance {
Some(distance) => JxlEncoder::lossy(JxlDistance::new(distance)?),
None => JxlEncoder::lossless(),
};
// The clap `1..=10` range guarantees `from_level` returns `Some`; fall back to the
// default effort (Squirrel/7) rather than unwrap so the path stays panic-free.
let effort = JxlEffort::from_level(args.jxl_effort).unwrap_or_default();
let container = if args.jxl_container {
JxlContainer::IsoBmff
} else {
JxlContainer::Codestream
};
encoder
.with_effort(effort)
.with_container(container)
.encode_image(ImageRef::<Rgba8>::new(&rgba, dims)?, &mut out)?;
(rgba.len(), dims)
}
OutputFormat::Jpeg => {
// JPEG has no alpha channel, so decode to RGB like the AVIF/TIFF paths; the input
// pipeline does not distinguish grayscale, so colour is always encoded as YCbCr.
let (rgb, dims) = decode_rgb8(&args.input)?;
tracing::info!(
width = dims.width,
height = dims.height,
bytes = rgb.len(),
"decoded input"
);
// JPEG-1 is inherently lossy; `--quality` (default 75) drives the quantization tables,
// `--jpeg-subsampling` (default 4:2:0) the chroma resolution, and `--jpeg-progressive`
// selects the SOF2 progressive process. A restart interval of 0 disables restarts, so
// only apply a nonzero one.
let mut encoder = JpegEncoder::new()
.with_quality(args.quality)
.with_subsampling(args.jpeg_subsampling.to_codec())
.with_progressive(args.jpeg_progressive);
if args.jpeg_restart_interval != 0 {
encoder = encoder.with_restart_interval(args.jpeg_restart_interval);
}
encoder.encode_image(ImageRef::<Rgb8>::new(&rgb, dims)?, &mut out)?;
(rgb.len(), dims)
}
};
tracing::info!(bytes = out.len(), lossy = args.lossy, "encoded output");
std::fs::write(&args.output, &out).map_err(|source| CliError::Io {
path: args.output.clone(),
source,
})?;
let ratio = if out.is_empty() {
0.0
} else {
raw_len as f64 / out.len() as f64
};
println!(
"wrote {} ({}x{}, {} bytes, {ratio:.2}x vs raw RGB)",
args.output.display(),
dims.width,
dims.height,
out.len(),
);
Ok(())
}
/// Picks the output format from `--format`, falling back to the output file's extension.
fn resolve_format(args: &ConvertArgs) -> Result<OutputFormat, CliError> {
if let Some(format) = args.format {
return Ok(format);
}
match args
.output
.extension()
.and_then(|ext| ext.to_str())
.map(str::to_ascii_lowercase)
.as_deref()
{
Some("avif") => Ok(OutputFormat::Avif),
Some("webp") => Ok(OutputFormat::Webp),
Some("tiff" | "tif") => Ok(OutputFormat::Tiff),
Some("png") => Ok(OutputFormat::Png),
Some("jxl") => Ok(OutputFormat::Jxl),
Some("jpg" | "jpeg") => Ok(OutputFormat::Jpeg),
Some(other) => Err(CliError::UnsupportedOutput(other.to_string())),
None => Err(CliError::UnsupportedOutput("<none>".to_string())),
}
}