Skip to content

Commit 0825ebd

Browse files
committed
Fixes #654, adds full test coverage
1 parent 07a3e12 commit 0825ebd

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

imageflow_core/src/flow/nodes/clone_crop_fill_expand.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,8 @@ impl NodeDefOneInputExpand for CropWhitespaceDef {
511511
if rect.x2 <= rect.x1 || rect.y2 <= rect.y1 {
512512
return Err(nerror!(crate::ErrorKind::InvalidState, "Whitespace detection returned invalid rectangle"));
513513
}
514-
let padding = (percent_padding * (rect.x2 - rect.x1 + rect.y2 - rect.y1) as f32 / 2f32).ceil() as i64;
514+
let padding = (percent_padding / 100f32 * (rect.x2 - rect.x1 + rect.y2 - rect.y1) as f32 / 2f32).ceil() as i64;
515+
//eprintln!("Detected {}x{} whitespace rect: {:?} within {}x{}, padding: {}", rect.x2 - rect.x1, rect.y2 - rect.y1, rect, frame.w, frame.h, padding);
515516
Ok((cmp::max(0, rect.x1 as i64 - padding) as u32, cmp::max(0, rect.y1 as i64 - padding) as u32,
516517
cmp::min(bitmap.w() as i64, rect.x2 as i64 + padding) as u32, cmp::min(bitmap.h() as i64, rect.y2 as i64 + padding) as u32))
517518
} else {
@@ -521,6 +522,7 @@ impl NodeDefOneInputExpand for CropWhitespaceDef {
521522
},
522523
other => { Err(nerror!(crate::ErrorKind::InvalidOperation, "Cannot CropWhitespace without a parent bitmap; got {:?}", other)) }
523524
}?;
525+
//eprintln!("Whitespace cropping to {}x{} of {}x{}: x1={}, y1={}, x2={}, y2={}", x2-x1, y2-y1, b.w, b.h, x1, y1, x2, y2);
524526
ctx.replace_node(ix, vec![
525527
Node::n(&CROP,
526528
NodeParams::Json(s::Node::Crop { x1, y1, x2, y2 }))

imageflow_core/src/graphics/whitespace.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ const SCAN_FULL: ScanRegion = ScanRegion {
7171
y_2_percent: 1f32,
7272
};
7373

74+
#[derive(Copy, Clone, Debug)]
7475
pub struct RectCorners {
7576
pub x1: u32,
7677
pub y1: u32,
7778
pub x2: u32,
7879
pub y2: u32,
7980
}
8081

82+
#[derive(Copy, Clone, Debug)]
8183
struct Rect {
8284
x: u32,
8385
y: u32,

imageflow_core/tests/visuals.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,100 @@ fn test_trim_whitespace() {
6767
}
6868

6969

70+
#[test]
71+
fn test_trim_whitespace_with_padding() {
72+
73+
compare_encoded(
74+
Some(IoTestEnum::Url("https://s3-us-west-2.amazonaws.com/imageflow-resources/test_inputs/whitespace-issue.png".to_owned())),
75+
"trim_whitespace_with_padding",
76+
POPULATE_CHECKSUMS,
77+
DEBUG_GRAPH,
78+
Constraints {
79+
similarity: Similarity::AllowDssimMatch(0.0, 0.002),
80+
max_file_size: None
81+
},
82+
vec![
83+
Node::CommandString{
84+
kind: CommandStringKind::ImageResizer4,
85+
value: "trim.threshold=20&trim.percentpadding=0.5&bgcolor=gray".to_owned(),
86+
decode: Some(0),
87+
encode: Some(1),
88+
watermarks: None
89+
}
90+
]
91+
);
92+
}
93+
#[test]
94+
fn test_trim_resize_whitespace_with_padding() {
95+
96+
compare_encoded(
97+
Some(IoTestEnum::Url("https://s3-us-west-2.amazonaws.com/imageflow-resources/test_inputs/whitespace-issue.png".to_owned())),
98+
"trim_resize_whitespace_with_padding",
99+
POPULATE_CHECKSUMS,
100+
DEBUG_GRAPH,
101+
Constraints {
102+
similarity: Similarity::AllowDssimMatch(0.0, 0.002),
103+
max_file_size: None
104+
},
105+
vec![
106+
Node::CommandString{
107+
kind: CommandStringKind::ImageResizer4,
108+
value: "w=450&h=450&scale=both&trim.threshold=20&trim.percentpadding=10&bgcolor=gray".to_owned(),
109+
decode: Some(0),
110+
encode: Some(1),
111+
watermarks: None
112+
}
113+
]
114+
);
115+
}
116+
#[test]
117+
fn test_trim_resize_whitespace_without_padding() {
118+
119+
compare_encoded(
120+
Some(IoTestEnum::Url("https://s3-us-west-2.amazonaws.com/imageflow-resources/test_inputs/whitespace-issue.png".to_owned())),
121+
"trim_resize_whitespace_without_padding",
122+
POPULATE_CHECKSUMS,
123+
DEBUG_GRAPH,
124+
Constraints {
125+
similarity: Similarity::AllowDssimMatch(0.0, 0.002),
126+
max_file_size: None
127+
},
128+
vec![
129+
Node::CommandString{
130+
kind: CommandStringKind::ImageResizer4,
131+
value: "w=450&h=450&scale=both&trim.threshold=20&bgcolor=gray".to_owned(),
132+
decode: Some(0),
133+
encode: Some(1),
134+
watermarks: None
135+
}
136+
]
137+
);
138+
}
139+
#[test]
140+
fn test_trim_whitespace_with_padding_no_resize() {
141+
142+
compare_encoded(
143+
Some(IoTestEnum::Url("https://s3-us-west-2.amazonaws.com/imageflow-resources/test_inputs/whitespace-issue.png".to_owned())),
144+
"trim_whitespace_with_padding_no_resize",
145+
POPULATE_CHECKSUMS,
146+
DEBUG_GRAPH,
147+
Constraints {
148+
similarity: Similarity::AllowDssimMatch(0.0, 0.002),
149+
max_file_size: None
150+
},
151+
vec![
152+
Node::CommandString{
153+
kind: CommandStringKind::ImageResizer4,
154+
value: "trim.threshold=20&trim.percentpadding=0.5&bgcolor=gray".to_owned(),
155+
decode: Some(0),
156+
encode: Some(1),
157+
watermarks: None
158+
}
159+
]
160+
);
161+
}
162+
163+
70164
#[test]
71165
fn test_transparent_png_to_png() {
72166
compare_encoded(

imageflow_core/tests/visuals/checksums.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@
9090
"transparent_png_to_png_round_corners": "070F860839693FDC7.png",
9191
"transparent_trim_whitespace": "0FBC6A5C3930ADEF7.png",
9292
"transparent_webp_to_webp": "0026D28C52CC20F72.webp",
93+
"trim_resize_whitespace_with_padding": "0C98752E6DE58648A.png",
94+
"trim_resize_whitespace_without_padding": "094AD8123D38FF811.png",
95+
"trim_whitespace_with_padding": "0BEBDEBEADEFEF409.png",
96+
"trim_whitespace_with_padding_no_resize": "0BEBDEBEADEFEF409.png",
9397
"watermark_jpeg_over_pnga": "04C0143A72CB4EBAA_073B7BE0C1ABF189F",
9498
"webp_lossless_alpha_decode_and_scale": "057FA4CBAC9D0E267_0172196390B512E97",
9599
"webp_lossy_alpha_decode_and_scale": "09F54D063249F69C8_0172196390B512E97",

0 commit comments

Comments
 (0)