mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Address CodeRabbit review: refuse a Bitmap Output size past 300,000 px
Keeping the print size at a higher Output resolution could ask for more than the 300,000 px document limit; the side was clamped, silently changing the print size. Refuse it instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
901e19d30b
commit
cd407f1a05
2 files changed
+21
-2
No files matched your search
@@ -431,8 +431,17 @@ fn bitmap(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (nw, nh) = if (output - input).abs() < 1e-6 {
|
||||
(ow, oh)
|
||||
} else {
|
||||
let side = |v: u32| (f64::from(v) * output / input).round().clamp(1.0, 300_000.0) as u32;
|
||||
(side(ow), side(oh))
|
||||
// Keeping the print size can ask for more pixels than a document may have: refuse that
|
||||
// rather than clamp, which would change the print size.
|
||||
let max = crate::document_preset_cmds::MAX_DIMENSION;
|
||||
let side = |v: u32| {
|
||||
let n = (f64::from(v) * output / input).round().max(1.0);
|
||||
if n > f64::from(max) {
|
||||
return Err(bad(CMD, format!("{n} px across at {output} pixels/inch is more than {max} px; choose a lower Output resolution")));
|
||||
}
|
||||
Ok(n as u32)
|
||||
};
|
||||
(side(ow)?, side(oh)?)
|
||||
};
|
||||
if (nw, nh) != (ow, oh) {
|
||||
// Besides the resampled document, the conversion holds an RGBA f32 composite (16 B/px; 32
|
||||
|
||||
@@ -485,6 +485,16 @@ fn bitmap_output_resolution_budgets_the_whole_conversion_not_just_the_resample()
|
||||
assert_eq!(s.active().unwrap().history.past_len(), before, "{depth}");
|
||||
let d = doc(&s);
|
||||
assert_eq!((d.mode, d.size.width, d.resolution_dpi), (ColorMode::Grayscale, 100, 1.0), "{depth}");
|
||||
// A side past the 300,000 px document limit is refused, not clamped (that would change
|
||||
// the print size): 20000 px at 72 ppi is 333,333 px at 1200 ppi.
|
||||
let mut wide = ramp(20_000, 1, depth, 72.0);
|
||||
let e = wide.execute("image.mode.bitmap", json!({"method": "threshold", "resolution": 1200})).unwrap_err().to_string();
|
||||
assert!(e.contains("300000 px"), "{depth}: {e}");
|
||||
assert_eq!((doc(&wide).mode, doc(&wide).size.width), (ColorMode::Grayscale, 20_000), "{depth}");
|
||||
// 18000 px to 300,000 px exactly is allowed.
|
||||
let mut edge = ramp(18_000, 1, depth, 72.0);
|
||||
edge.execute("image.mode.bitmap", json!({"method": "threshold", "resolution": 1200})).unwrap();
|
||||
assert_eq!(doc(&edge).size.width, 300_000, "{depth}");
|
||||
// A size that fits is still converted.
|
||||
s.execute("image.mode.bitmap", json!({"method": "diffusion", "resolution": 3})).unwrap();
|
||||
assert_eq!((doc(&s).size.width, doc(&s).mode), (300, ColorMode::Bitmap), "{depth}");
|
||||
|
||||
Reference in new issue
Block a user