Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Export As: a scale that changes only the height is applied (#1353)

ragtuxa7587f62701c
diff --git a/crates/ui-egui/src/export_dialog.rs b/crates/ui-egui/src/export_dialog.rs
index e88d6c3a8..5897132c2 100644
--- a/crates/ui-egui/src/export_dialog.rs
+++ b/crates/ui-egui/src/export_dialog.rs
@@ -74,16 +74,19 @@ fn n(f: &Map<String, Value>, k: &str, d: f64) -> f64 {
fn export_document(doc: &Document, f: &Map<String, Value>, max_side: Option<u32>) -> Result<Document, String> {
let mut s = photocraft_engine::Session::new();
s.add_document(doc.clone(), None);
- let scale = n(f, "scale", 100.0) / 100.0;
- let mut w = (doc.size.width as f64 * scale).round().max(1.0);
+ let mut scale = n(f, "scale", 100.0) / 100.0;
if let Some(m) = max_side {
let long = doc.size.width.max(doc.size.height) as f64 * scale;
if long > m as f64 {
- w = (w * m as f64 / long).round().max(1.0);
+ scale *= m as f64 / long;
}
}
- if (w - doc.size.width as f64).abs() >= 1.0 {
- s.execute("image.imageSize", json!({"width": w, "resample": if max_side.is_some() { "bilinear" } else { "bicubic" }})).map_err(|e| e.to_string())?;
+ // Both dimensions are sent: a scale can leave a 1 px width unchanged while halving the
+ // height, and the engine would otherwise keep the aspect ratio from the width alone.
+ let (w, h) = scaled_size(doc.size.width as f64, doc.size.height as f64, scale);
+ if (w - doc.size.width as f64).abs() >= 1.0 || (h - doc.size.height as f64).abs() >= 1.0 {
+ s.execute("image.imageSize", json!({"width": w, "height": h, "resample": if max_side.is_some() { "bilinear" } else { "bicubic" }}))
+ .map_err(|e| e.to_string())?;
}
let fmt = s_fmt(f);
if !f.get("transparency").and_then(Value::as_bool).unwrap_or(true) || fmt == "jpg" {
@@ -92,6 +95,12 @@ fn export_document(doc: &Document, f: &Map<String, Value>, max_side: Option<u32>
s.active().map(|d| (*d.doc).clone()).ok_or_else(|| "export failed".into())
}
+/// The exported pixel size: each side scaled and rounded on its own, never below 1 px. The
+/// dialog's size label and `export_document` share it so the label names the file's real size.
+fn scaled_size(w: f64, h: f64, scale: f64) -> (f64, f64) {
+ ((w * scale).round().max(1.0), (h * scale).round().max(1.0))
+}
+
/// Export As already supports lossy WebP; reuse the Quick Export defaults when
/// choosing that format so both flows expose the same quality and lossless options.
fn set_format_defaults(f: &mut Map<String, Value>, fmt: &str, prefs: &photocraft_engine::prefs::Export) {
@@ -188,8 +197,8 @@ pub fn body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<String, Valu
crate::widgets::value_field(ui, &mut sc, 1.0..=1000.0, "%", 70.0);
});
f.insert("scale".into(), json!(sc.round()));
- let (bw, bh) = (n(f, "__w", f64::from(doc.size.width)) as f32, n(f, "__h", f64::from(doc.size.height)) as f32);
- let (w, h) = ((bw * sc / 100.0).round(), (bh * sc / 100.0).round());
+ let (bw, bh) = (n(f, "__w", f64::from(doc.size.width)), n(f, "__h", f64::from(doc.size.height)));
+ let (w, h) = scaled_size(bw, bh, f64::from(sc) / 100.0);
ui.label(egui::RichText::new(format!("{w} × {h} px")).color(t.text_dim).size(11.5));
});
ui.add_space(12.0);
@@ -303,6 +312,33 @@ mod tests {
assert_eq!(settings(&f).jpeg_quality, Some(85));
}
+ /// #1353: a scale that changes only the height (the width rounds back to the original)
+ /// used to be skipped, so the exported file kept the original size while the dialog
+ /// labelled it as scaled.
+ #[test]
+ fn export_document_scales_when_only_the_height_changes() {
+ let mut f = Map::new();
+ f.insert("format".into(), json!("png"));
+ f.insert("scale".into(), json!(50));
+ for (src_h, want_h) in [(100u32, 50u32), (200, 100)] {
+ let doc = Document::with_background(
+ "x",
+ photocraft_doc::Size::new(1, src_h),
+ photocraft_doc::ColorMode::Rgb,
+ photocraft_doc::SampleType::U8,
+ photocraft_doc::Color::WHITE,
+ );
+ let out = export_document(&doc, &f, None).unwrap();
+ assert_eq!((out.size.width, out.size.height), (1, want_h), "1 × {src_h} at 50%");
+ // The preview proxy takes the same path.
+ let proxy = export_document(&doc, &f, Some(40)).unwrap();
+ assert_eq!((proxy.size.width, proxy.size.height), (1, 40), "proxy of 1 × {src_h}");
+ }
+ // The label and the export agree, and neither side drops below 1 px.
+ assert_eq!(scaled_size(1.0, 100.0, 0.5), (1.0, 50.0));
+ assert_eq!(scaled_size(10.0, 300.0, 0.01), (1.0, 3.0));
+ }
+
#[test]
fn switching_export_formats_uses_independent_quality_preferences() {
let prefs = photocraft_engine::prefs::Export { jpeg_quality: 43, webp_quality: 72, webp_lossless: false, ..Default::default() };