Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::collections::HashMap;
// from external crate

// from local crate
use error::{RasterError, RasterResult};
use color::Color;
use error::{RasterError, RasterResult};

/// A struct for easily representing a raster image.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -64,12 +64,14 @@ impl<'a> Image {
/// assert_eq!(image.check_pixel(3, 3), false);
/// ```
pub fn check_pixel(&self, x: i32, y: i32) -> bool {
if y < 0 || y > self.height {
// TODO: check on actual vectors and not just width and height?
false
} else {
!(x < 0 || x > self.width)
}
// Step 1: check coordinate bounds
if y < 0 || y > self.height { // TODO: check on actual vectors and not just width and height? -> Done by Blob_01
return false;
}

// Step 2: check if bytes vector contains this pixel
let start = ((y * self.width + x) * 4) as usize; // safe now
start + 4 <= self.bytes.len()
}

/// Get the histogram of the image.
Expand Down
21 changes: 21 additions & 0 deletions tests/image_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extern crate raster;

use raster::Image;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_check_pixel() {
let mut img = Image::blank(10, 10);

// Shrink bytes manually to simulate corruption
img.bytes.truncate(50);
// Only pixels that fit within 50 bytes are valid
assert!(img.check_pixel(0, 0)); // start = 0..4
assert!(img.check_pixel(1, 1)); // start = 44..48
assert!(!img.check_pixel(2, 2)); // start = 88..92 → exceeds 50
assert!(!img.check_pixel(3, 3)); // start = 156..160 → exceeds 50
}
}