Skip to content

Commit 5936626

Browse files
fix(ops): validate brightness and widen blur coordinates
1 parent a8dabf1 commit 5936626

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

‎crates/pixl-ops/src/lib.rs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ fn luma(r: u8, g: u8, b: u8) -> u8 {
296296

297297
/// adjusts brightness by adding `amount` to each color channel.
298298
pub fn brightness(image: &Image, amount: i16, limits: Limits) -> Result<Image, Error> {
299+
if !(-255..=255).contains(&amount) {
300+
return Err(Error::InvalidArgument("brightness must be -255..255".into()));
301+
}
299302
map_channels(image, limits, |v, _| {
300303
clamp_i16(i32::from(v) + i32::from(amount))
301304
})
@@ -394,7 +397,8 @@ pub fn gaussian_blur(image: &Image, sigma: f64, limits: Limits) -> Result<Image,
394397
for x in 0..w as i32 {
395398
let mut acc = [0.0; 4];
396399
for (k, weight) in weights.iter().enumerate() {
397-
let sx = (x + k as i32 - radius).clamp(0, w as i32 - 1) as usize;
400+
let sx = (i64::from(x) + k as i64 - i64::from(radius))
401+
.clamp(0, i64::from(w) - 1) as usize;
398402
let offset = sx * 4;
399403
let alpha = f64::from(src_row[offset + 3]) / 255.0;
400404
for c in 0..3 {
@@ -417,7 +421,8 @@ pub fn gaussian_blur(image: &Image, sigma: f64, limits: Limits) -> Result<Image,
417421
for x in 0..w as usize {
418422
let mut acc = [0.0; 4];
419423
for (k, weight) in weights.iter().enumerate() {
420-
let sy = (y as i32 + k as i32 - radius).clamp(0, h as i32 - 1) as usize;
424+
let sy = (y as i64 + k as i64 - i64::from(radius))
425+
.clamp(0, i64::from(h) - 1) as usize;
421426
let offset = sy * w as usize * 4 + x * 4;
422427
for c in 0..4 {
423428
acc[c] += f64::from(horizontal[offset + c]) * weight;
@@ -709,6 +714,13 @@ mod tests {
709714
assert_eq!(px[3], 128);
710715
}
711716

717+
#[test]
718+
fn brightness_rejects_out_of_range_amounts() {
719+
let img = solid(1, 1, [0, 0, 0, 255]);
720+
assert!(brightness(&img, 256, Limits::default()).is_err());
721+
assert!(brightness(&img, -256, Limits::default()).is_err());
722+
}
723+
712724
#[test]
713725
fn brightness_clamps() {
714726
let img = solid(2, 1, [250, 5, 100, 255]);

0 commit comments

Comments
 (0)