Save for Web: no matte by default, as Photoshop's scripted export (#4115) (#4578)

Co-authored-by: Brandon Thomas <[email protected]>
This commit is contained in:
F4LCONandBrandon Thomas authored and GitHub committed 2026-10-12 23:52:16 -04:00
1 parent 68c624003a
commit 25dd3b4794
2 files changed
+46 -2

No files matched your search

+8 -2
View File
@@ -136,8 +136,13 @@ fn hex3(s: &str) -> Option<[f32; 3]> {
impl WebSettings {
/// Settings from command parameters (missing keys keep the defaults; Photoshop's preset
/// names such as "GIF 128 Dithered" or "JPEG High" are accepted as `"preset"`).
///
/// Without a preset there is no matte, as in Photoshop's scripted Save for Web
/// (`ExportOptionsSaveForWeb` left at its defaults): a GIF or PNG-8 keeps the colour of its
/// partly transparent pixels instead of lightening them towards white (#4115). Formats
/// without transparency still go over white.
pub fn from_params(p: &Value, cmd: &str) -> Result<Self> {
let mut st = WebSettings::default();
let mut st = WebSettings { matte: None, ..WebSettings::default() };
if let Some(preset) = p.get("preset").and_then(Value::as_str) {
st = preset_settings(preset).ok_or_else(|| bad(cmd, format!("unknown preset `{preset}`")))?;
}
@@ -1054,6 +1059,7 @@ fn quick_export(s: &mut Session, p: &Value) -> Result<Value> {
);
o.insert("convertToSrgb".into(), json!(prefs.convert_to_srgb));
o.insert("transparency".into(), json!(true));
o.insert("matte".into(), json!("#ffffff"));
match fmt.as_str() {
"jpg" => {
o.insert("format".into(), json!("jpeg"));
@@ -1462,7 +1468,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Save for Web (Legacy)…",
&["File", "Export"],
Some("Cmd+Alt+Shift+S"),
r##"{"preset":"GIF 128 Dithered|JPEG High|PNG-24|…"?,"format":"gif|png8|png24|jpeg|wbmp"="png24","palette":"perceptual|selective|adaptive|restrictive|exact|systemMac|systemWindows|uniform"="selective","colors":2..256=256,"colorTable":["#rrggbb",…]? (fixed opaque source palette, 1–256 entries; 255 with transparency),"colorTableEdits":{"0":"#rrggbb",…}? (replace entries after indexing; requires colorTable),"dither":"none|diffusion|pattern|noise"="diffusion","ditherAmount":0..100=88,"transparency":bool=true,"matte":"#rrggbb|none"="#ffffff","interlaced":bool=false,"webSnap":0..100=0,"quality":0..100=60,"progressive":bool=false,"optimized":bool=true,"embedIcc":bool=false,"metadata":"none|copyright|copyrightAndContact|all"="copyright","convertToSrgb":bool=true,"width"|"height"|"percent"? (image size),"resample":"bicubic|bilinear|nearest"?,"path":file? (whole image),"dir":folder? (one file per slice in images/),"imagesFolder":relative-folder?="images","html":bool=false,"trustedSliceHtml":bool=false (only for trusted documents; emit raw no-image cell HTML),"slices":"all|user"="all","numbers":[n]?} → no path/dir: {bytes,width,height,colors,colorTable,transparentIndex} estimate; else {files, html, bytes}"##,
r##"{"preset":"GIF 128 Dithered|JPEG High|PNG-24|…"?,"format":"gif|png8|png24|jpeg|wbmp"="png24","palette":"perceptual|selective|adaptive|restrictive|exact|systemMac|systemWindows|uniform"="selective","colors":2..256=256,"colorTable":["#rrggbb",…]? (fixed opaque source palette, 1–256 entries; 255 with transparency),"colorTableEdits":{"0":"#rrggbb",…}? (replace entries after indexing; requires colorTable),"dither":"none|diffusion|pattern|noise"="diffusion","ditherAmount":0..100=88,"transparency":bool=true,"matte":"#rrggbb|none"="none" (a preset's own: white; formats without transparency go over white),"interlaced":bool=false,"webSnap":0..100=0,"quality":0..100=60,"progressive":bool=false,"optimized":bool=true,"embedIcc":bool=false,"metadata":"none|copyright|copyrightAndContact|all"="copyright","convertToSrgb":bool=true,"width"|"height"|"percent"? (image size),"resample":"bicubic|bilinear|nearest"?,"path":file? (whole image),"dir":folder? (one file per slice in images/),"imagesFolder":relative-folder?="images","html":bool=false,"trustedSliceHtml":bool=false (only for trusted documents; emit raw no-image cell HTML),"slices":"all|user"="all","numbers":[n]?} → no path/dir: {bytes,width,height,colors,colorTable,transparentIndex} estimate; else {files, html, bytes}"##,
has_doc,
save_for_web
),
+38
View File
@@ -230,6 +230,44 @@ fn gif_and_png8_keep_transparency_and_palette() {
assert_eq!(&img.data()[..4], &[0, 255, 0, 255]);
}
/// #4115: Photoshop's scripted Save for Web has no matte unless one is asked for, so a soft black
/// shadow exported to PNG-8 stays black (alpha only 0 or 255) instead of turning grey. A preset
/// keeps its white matte, and a matte asked for still applies.
#[test]
fn png8_has_no_matte_by_default() {
let dir = tmp("matte");
let mut s = Session::new();
s.execute("file.new", json!({"width": 256, "height": 8, "background": "transparent"})).unwrap();
s.edit("paint", |doc, active| {
let fmt = doc.pixel_format();
let mut l = Layer::raster("Shadow", fmt);
let surf = l.surface_mut().unwrap();
for x in 0..256 {
surf.fill_rect(Rect::new(x, 0, x + 1, 8), &photocraft_raster::from_rgba(&fmt, [0.0, 0.0, 0.0, 1.0 - x as f32 / 255.0]));
}
*active = Some(doc.insert_above(None, l));
Ok(())
})
.unwrap();
let export = |s: &mut Session, extra: Value| {
let path = format!("{dir}/{}.png", s.journal.len());
let mut p = json!({"format": "png8", "path": path});
p.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
s.execute("file.export.saveForWebLegacy", p).unwrap();
let img = decode(&path).convert(photocraft_codecs::ChannelLayout::Rgba, photocraft_codecs::SampleType::U8);
(0..256).map(|x| img.data()[x * 4..x * 4 + 4].to_vec()).collect::<Vec<_>>()
};
let plain = export(&mut s, json!({}));
assert!(plain.iter().all(|p| p == &[0, 0, 0, 255] || p[3] == 0), "black or transparent: {:?}", &plain[60..70]);
assert_eq!(plain[64], [0, 0, 0, 255], "alpha 191 stays black");
assert_eq!(plain[200][3], 0);
// A matte asked for, or a preset's, lightens the soft pixels as before.
let white = export(&mut s, json!({"matte": "#ffffff"}));
assert!(white[64][0] > 40 && white[64][3] == 255, "{:?}", white[64]);
let preset = WebSettings::from_params(&json!({"preset": "PNG-8 128 Dithered"}), "x").unwrap();
assert_eq!(preset.matte, Some([1.0, 1.0, 1.0]));
}
#[test]
fn jpeg_quality_progressive_and_metadata() {
let dir = tmp("jpeg");