Skip to content

Commit

Permalink
Update crop to cut out a bounding box
Browse files Browse the repository at this point in the history
  • Loading branch information
bgins committed Apr 11, 2023
1 parent 86142a1 commit c455d93
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
Binary file modified homestar-guest-wasm/fixtures/synthcat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions homestar-guest-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ impl Homestar for Component {
blurred.into_bytes()
}

fn crop(data: Vec<u8>, x: u32, y: u32, width: u32, height: u32) -> Vec<u8> {
fn crop(data: Vec<u8>, x: u32, y: u32, target_width: u32, target_height: u32, width: u32, height: u32) -> Vec<u8> {
let img_buf= image::RgbImage::from_vec(width, height, data).unwrap();

// Crop the bottom right rectangle
let cropped = DynamicImage::ImageRgb8(img_buf).crop(x, y, width, height);
// Crop this image delimited by the bounding rectangle
let cropped = DynamicImage::ImageRgb8(img_buf).crop(x, y, target_width, target_height);
cropped.into_bytes()
}

Expand Down Expand Up @@ -105,10 +105,10 @@ mod test {
let (width, height) = (img.width(), img.height());
let img_vec = img.into_bytes();

// Call component to crop the image to the bottom right corner
let result = Component::crop(img_vec, 200, 200, width, height);
// Call component to crop the image to a 200x200 square
let result = Component::crop(img_vec, 150, 350, 400, 400, width, height);

let processed_buf= image::RgbImage::from_vec(width - 200, height - 200, result).unwrap();
let processed_buf= image::RgbImage::from_vec(400,400, result).unwrap();
let processed = DynamicImage::ImageRgb8(processed_buf);
processed.save("./out/cropped.jpg").expect("Failed to write cropped.jpg to filesystem");
}
Expand Down
2 changes: 1 addition & 1 deletion homestar-guest-wasm/wits/test.wit
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ default world homestar {
export append-string: func(a: string) -> string
export transpose: func(matrix: list<list<u8>>) -> list<list<u8>>
export blur: func(data: list<u8>, sigma: float32, width: u32, height: u32) -> list<u8>
export crop: func(data: list<u8>, x: u32, y: u32, width: u32, height: u32) -> list<u8>
export crop: func(data: list<u8>, x: u32, y: u32, target-width: u32, target-height: u32, width: u32, height: u32) -> list<u8>
export grayscale: func(data: list<u8>, width: u32, height: u32) -> list<u8>
export rotate90: func(data: list<u8>, width: u32, height: u32) -> list<u8>
}

0 comments on commit c455d93

Please sign in to comment.