Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,47 @@ itself after the last export or a quit; a browser that refuses says so on the
page instead. Leave the framing untouched in the app; any adjustment there
reintroduces the resample.

## Lend an export its RAW's detail

A photograph smaller than the delivery width, such as an edit exported at
2048, can take the detail it lacks from its RAW. ig-prep looks for a RAW with
the same stem (RAF, DNG, ARW, CR2, CR3, NEF and the rest the macOS RAW engine
reads) in the photograph's folder, then in each `--raw` folder:

```sh
ig-prep --raw /Volumes/CARD/DCIM/100_FUJI -o ig ~/Pictures/exports
```

The photograph stays the authority on the look. A cubic fitted on thumbnails
maps the RAW's rendering onto the photograph's colour. The photograph is
enlarged whole, and the mapped RAW adds only detail above the photograph's
sample rate, tile by tile, in proportion to how well the RAW's next octave
down agrees with the photograph's. Each pixel stays inside the photograph's
local range. A camera file that already covers the width is delivered byte for
byte as before, and its RAW is never decoded, because Apple's RAW render
carries a fifth of a Fujifilm HIF's fine-detail energy and would only swap
the camera's sharpening for its own.

In November 2026, five X-T50 HIFs were exported at 2048, rebuilt at 3072 from
their RAFs, and scored against each HIF's own 3072 output. Scores are
SSIMULACRA2 / Butteraugli max norm, from fast-ssim2 0.9.0 and imazen's
butteraugli 0.9.3:

| Film simulation | Enlarged | With the RAW |
| --------------- | ----------- | ------------ |
| Nostalgic Neg | 74.9 / 4.33 | 77.6 / 3.15 |
| Nostalgic Neg | 76.1 / 2.64 | 76.4 / 2.55 |
| Classic Chrome | 83.5 / 3.17 | 83.5 / 2.36 |
| Provia | 66.7 / 4.34 | 75.9 / 4.82 |
| Eterna | 80.6 / 3.47 | 79.5 / 4.34 |

Eterna loses because its glitter comes out of Apple's demosaic as different
sparkles, so the RAW is worth a try on an export and worth a look after. A RAW
is refused when its capture time, its frame shape or its fine structure
disagrees with the photograph's, so a cropped edit is delivered from the
export alone, and the line says why. `--no-raw` turns the search off.
`--pick`, `--variants` and `diagnose` do not use RAWs yet.

## JPEG encoder

The encoder is **ZenJPEG 0.8.4**, using the site's JPEG stack with a
Expand Down
9 changes: 9 additions & 0 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ pub struct Decoded {
}

pub fn open(path: &Path) -> Result<Decoded, String> {
// A RAW goes by extension: DNG, NEF and ARW are TIFF inside and CR3 is
// ISO BMFF, so their magic would send them down the wrong branch. sips
// renders them through the macOS RAW engine and leaves orientation alone.
if crate::raw::is_raw(path) {
return Ok(Decoded {
img: via_converter(path, false)?,
orientation_applied: false,
});
}
let bytes = std::fs::read(path).map_err(|e| e.to_string())?;
let (img, orientation_applied) = if bytes.starts_with(&[0xff, 0xd8]) {
(jpeg(&bytes)?, false)
Expand Down
15 changes: 12 additions & 3 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,18 @@ impl Rgb {

/// Lanczos3 operates directly on linear-light samples, without an intermediate copy.
pub fn resize(src: &Rgb, w: u32, h: u32) -> Result<Rgb, String> {
use fast_image_resize::images::{Image, ImageRef};
use fast_image_resize::{FilterType, PixelType, ResizeAlg, ResizeOptions, Resizer};
if (src.w, src.h) == (w, h) {
return Ok(Rgb::new(w, h, src.px.clone()));
}
resize_window(src, [0.0, 0.0, src.w as f64, src.h as f64], w, h)
}

/// Resize the window `[left, top, width, height]` of `src`, in fractional
/// source pixels, so a window drawn on one frame lands exactly on another of
/// a different size instead of rounding to the nearer whole pixel.
pub fn resize_window(src: &Rgb, window: [f64; 4], w: u32, h: u32) -> Result<Rgb, String> {
use fast_image_resize::images::{Image, ImageRef};
use fast_image_resize::{FilterType, PixelType, ResizeAlg, ResizeOptions, Resizer};
let source = ImageRef::new(
src.w,
src.h,
Expand All @@ -104,7 +111,9 @@ pub fn resize(src: &Rgb, w: u32, h: u32) -> Result<Rgb, String> {
.resize(
&source,
&mut target,
&ResizeOptions::new().resize_alg(ResizeAlg::Convolution(FilterType::Lanczos3)),
&ResizeOptions::new()
.resize_alg(ResizeAlg::Convolution(FilterType::Lanczos3))
.crop(window[0], window[1], window[2], window[3]),
)
.map_err(|e| format!("resize: {e}"))?;
Ok(Rgb::new(w, h, pixels))
Expand Down
125 changes: 120 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod heif;
mod image;
mod mcp;
mod pick;
mod raw;
mod simulate;

use encode::Chroma;
Expand Down Expand Up @@ -50,6 +51,18 @@ OPTIONS
about half the bytes, faster, and slightly ahead of q99
after the servers in local models. Cannot combine with
-q, the chroma flags or --dither.
--raw <dir> also look in <dir> for each photograph's RAW (same
stem, any RAW extension). Repeatable. Its own folder
is always searched first.
--no-raw never use a RAW

RAW
A photograph smaller than the delivery width, such as an edit exported
at 2048, takes the detail it lacks from its RAW: the photograph keeps
every frequency it holds and the RAW, mapped onto its look, fills in
above them. A camera file that already covers the width is delivered as
before and its RAW is never decoded. A RAW is refused when its capture
time, its frame or its structure disagrees with the photograph's.
--variants write eight variants (1080/1440, q95/99, 444/420),
references and a manifest to a NEW --out directory;
use --crop or --pad for out-of-band sources
Expand Down Expand Up @@ -161,6 +174,8 @@ fn main() {
let mut match_tables = false;
let mut out_dir = PathBuf::from("ig");
let mut pad = [255u8, 255, 255];
let mut raw_dirs = Vec::new();
let mut no_raw = false;

while let Some(a) = args.next() {
match a.as_str() {
Expand All @@ -187,6 +202,18 @@ fn main() {
"-w" | "--width" => width = args.next().and_then(|v| v.parse().ok()).unwrap_or(width),
"-q" => quality = args.next().and_then(|v| v.parse().ok()).unwrap_or(quality),
"-o" | "--out" => out_dir = args.next().map(PathBuf::from).unwrap_or(out_dir),
"--no-raw" => no_raw = true,
"--raw" => match args.next() {
Some(d) if Path::new(&d).is_dir() => raw_dirs.push(PathBuf::from(d)),
Some(d) => {
eprintln!("ig-prep: --raw needs a directory, got {d}");
std::process::exit(2);
}
None => {
eprintln!("ig-prep: --raw needs a directory");
std::process::exit(2);
}
},
"--pad-color" => {
if let Some(h) = args.next() {
let h = h.trim_start_matches('#');
Expand Down Expand Up @@ -237,6 +264,12 @@ fn main() {
eprintln!("ig-prep: {MATCH_CONFLICT}");
std::process::exit(2);
}
if !raw_dirs.is_empty() && (no_raw || pick || variants || check) {
eprintln!(
"ig-prep: --raw applies to an ordinary conversion; it cannot combine with --no-raw, --pick, --variants or --check"
);
std::process::exit(2);
}
if pick && (check || dry || variants) {
eprintln!(
"ig-prep: --pick chooses the framing interactively; it cannot combine with --check, --dry-run or --variants"
Expand Down Expand Up @@ -270,6 +303,11 @@ fn main() {
dry,
pad,
out_dir: out_dir.clone(),
raw: if no_raw {
Raw::Off
} else {
Raw::Search(raw_dirs)
},
};

if pick {
Expand Down Expand Up @@ -543,6 +581,16 @@ pub struct Opts {
pub dry: bool,
pad: [u8; 3],
pub out_dir: PathBuf,
pub raw: Raw,
}

/// Whether a camera RAW may lend a photograph the detail it lacks at the
/// delivery size.
#[derive(Clone)]
pub enum Raw {
Off,
/// Look beside each photograph, then in these folders.
Search(Vec<PathBuf>),
}

impl Opts {
Expand All @@ -560,6 +608,7 @@ impl Opts {
dry: false,
pad: [255, 255, 255],
out_dir: PathBuf::from("ig"),
raw: Raw::Search(Vec::new()),
}
}

Expand Down Expand Up @@ -606,6 +655,9 @@ pub struct Report {
pub chroma: &'static str,
pub error: Option<String>,
note: &'static str,
/// What a RAW found beside the photograph did, or why it did nothing.
/// Absent when no RAW was found or the search was off.
pub raw: Option<String>,
}

impl Report {
Expand All @@ -621,6 +673,7 @@ impl Report {
chroma: "",
error: Some(e),
note: "",
raw: None,
}
}

Expand All @@ -632,7 +685,10 @@ impl Report {
}
let (fw, fh) = self.from.unwrap_or((0, 0));
let (tw, th) = self.to.unwrap_or((0, 0));
let summary = format!("{}: {fw}x{fh} -> {tw}x{th}{}", self.name, self.note);
let mut summary = format!("{}: {fw}x{fh} -> {tw}x{th}{}", self.name, self.note);
if let Some(raw) = &self.raw {
summary.push_str(&format!(" [{raw}]"));
}
match self.bytes {
Some(b) => format!("{summary} {} {} KB", self.chroma, b / 1024),
None => summary,
Expand All @@ -652,7 +708,16 @@ pub fn one(path: &Path, o: &Opts) -> Report {
Err(e) => return Report::failed(path, name, e),
};

let plan = geometry::plan(img.w, img.h, o.fit, o.gravity, o.width);
let own = geometry::plan(img.w, img.h, o.fit, o.gravity, o.width);
let raw_path = match &o.raw {
Raw::Off => None,
Raw::Search(dirs) => raw::find(path, dirs),
};
let (lent, raw_note) = match &raw_path {
Some(rp) => lend(&img, &own, path, rp, o),
None => (None, None),
};
let plan = lent.as_ref().map_or(own, |l| l.plan);
let note = if plan.outside_band {
match o.fit {
Fit::Full => " (outside Instagram's band, crop it in the app)",
Expand All @@ -673,12 +738,18 @@ pub fn one(path: &Path, o: &Opts) -> Report {
chroma: o.encode_label(),
error: None,
note,
raw: raw_note,
};
if o.dry {
return report;
}

let final_img = match render(&img, &plan, o.pad) {
let rendered = match &lent {
Some(l) => raw::merge(&img, &l.img, &l.look, plan.crop, plan.scale)
.map(|scaled| padded(scaled, &plan, o.pad)),
None => render(&img, &plan, o.pad),
};
let final_img = match rendered {
Ok(img) => img,
Err(e) => return Report::failed(path, report.name, e),
};
Expand All @@ -700,6 +771,46 @@ pub fn one(path: &Path, o: &Opts) -> Report {
}
}

/// A RAW that passed every check, and the plan its larger frame allows.
struct Lent {
img: image::Rgb,
look: raw::Look,
plan: geometry::Plan,
}

/// Decide whether the RAW at `rp` lends the photograph detail, and say why
/// in the report either way. The checks run cheapest first, so a full-size
/// camera file never pays for decoding its RAW.
fn lend(
img: &image::Rgb,
own: &geometry::Plan,
path: &Path,
rp: &Path,
o: &Opts,
) -> (Option<Lent>, Option<String>) {
let rname = rp.file_name().unwrap_or_default().to_string_lossy();
// A photograph that is downscaled already holds every frequency the
// output can show, so the RAW would only replace the camera's detail
// with the RAW engine's.
if own.scale.0 < own.crop.2 {
return (None, Some(format!("{rname} not needed")));
}
if let Some(why) = raw::mismatch(path, rp) {
return (None, Some(format!("{rname} not used: {why}")));
}
let paired = load_oriented(rp).and_then(|r| raw::pair(img, &r).map(|look| (r, look)));
let (r, look) = match paired {
Ok(p) => p,
Err(why) => return (None, Some(format!("{rname} not used: {why}"))),
};
let plan = geometry::plan(r.w, r.h, o.fit, o.gravity, o.width);
if plan.scale.0 <= own.scale.0 {
return (None, Some(format!("{rname} not needed")));
}
let note = format!("detail above {}x{} from {rname}", own.scale.0, own.scale.1);
(Some(Lent { img: r, look, plan }), Some(note))
}

fn load_oriented(path: &Path) -> Result<image::Rgb, String> {
// Orientation comes from the metadata reader rather than from the decoder,
// because a HEIF's rotation lives in its EXIF and sips does not apply it.
Expand Down Expand Up @@ -745,7 +856,11 @@ fn render(img: &image::Rgb, plan: &geometry::Plan, pad: [u8; 3]) -> Result<image
&cropped
};
let scaled = image::resize(src, plan.scale.0, plan.scale.1)?;
Ok(if plan.canvas != plan.scale {
Ok(padded(scaled, plan, pad))
}

fn padded(scaled: image::Rgb, plan: &geometry::Plan, pad: [u8; 3]) -> image::Rgb {
if plan.canvas != plan.scale {
scaled.pad_onto(
plan.canvas.0,
plan.canvas.1,
Expand All @@ -755,7 +870,7 @@ fn render(img: &image::Rgb, plan: &geometry::Plan, pad: [u8; 3]) -> Result<image
)
} else {
scaled
})
}
}

const EXTS: [&str; 8] = ["jpg", "jpeg", "heif", "heic", "hif", "jxl", "png", "avif"];
Expand Down
Loading
Loading