File size: 5,176 Bytes
d5bfab8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
use super::Image;
use anyhow::Context;
/// Perform a 5x5 convolution the places where the mask is non-zero.
///
/// The `source` minimum size is 5x5. It must be the same size as `target`.
///
/// The `target` minimum size is 5x5. It must be the same size as `source`.
///
/// The `mask` size is equal to the `source` size minus 4 pixels.
/// - With `source.width = 8` then use `mask.width = 4`.
/// - With `source.height = 42` then use `mask.height = 38`.
///
/// The `color_for_unprocessed_pixels` is used the places where the mask is zero.
///
/// The `callback` is invoked where the mask is non-zero.
/// The callback is provided with two 5x5 images. One image for the `source` and another image for the `target`.
///
/// This function returns an `Image` with the same size as the `mask` image.
#[allow(dead_code)]
pub fn convolution5x5_special<F>(source: &Image, target: &Image, mask: &Image, color_for_unprocessed_pixels: u8, callback: F) -> anyhow::Result<Image>
where F: Fn(&Image, &Image) -> anyhow::Result<u8>
{
let width: u8 = source.width();
let height: u8 = source.height();
if width < 5 || height < 5 {
return Err(anyhow::anyhow!("too small image, must be 5x5 or bigger"));
}
if source.size() != target.size() {
return Err(anyhow::anyhow!("source.size and target.size must be the same"));
}
let invalid_width: bool = (width as u16) != (mask.width() as u16) + 4;
let invalid_height: bool = (height as u16) != (mask.height() as u16) + 4;
if invalid_width || invalid_height {
return Err(anyhow::anyhow!("Size constraint not satisfied. Expected image.width == mask.width+4 AND image.height == mask.height+4"));
}
let mut result_image = Image::color(width - 4, height - 4, color_for_unprocessed_pixels);
let mut conv_source = Image::zero(5, 5);
let mut conv_target = Image::zero(5, 5);
for self_y in 0..height-4 {
for self_x in 0..width-4 {
let mask_x: i32 = self_x as i32;
let mask_y: i32 = self_y as i32;
let mask_value = mask.get(mask_x, mask_y)
.ok_or_else(|| anyhow::anyhow!("mask.get({},{}) returned None", mask_x, mask_y))?;
// Ignore areas where the mask is zero
if mask_value == 0 {
continue;
}
// Perform 5x5 convolution
for conv_y in 0..5u8 {
for conv_x in 0..5u8 {
let get_x: i32 = (self_x as i32) + (conv_x as i32);
let get_y: i32 = (self_y as i32) + (conv_y as i32);
{
let source_value: u8 = source.get(get_x, get_y)
.ok_or_else(|| anyhow::anyhow!("source.get({},{}) returned None", get_x, get_y))?;
conv_source.set(conv_x as i32, conv_y as i32, source_value)
.ok_or_else(|| anyhow::anyhow!("conv_source.set({},{}) returned None", conv_x, conv_y))?;
}
{
let target_value: u8 = target.get(get_x, get_y)
.ok_or_else(|| anyhow::anyhow!("target.get({},{}) returned None", get_x, get_y))?;
conv_target.set(conv_x as i32, conv_y as i32, target_value)
.ok_or_else(|| anyhow::anyhow!("conv_target.set({},{}) returned None", conv_x, conv_y))?;
}
}
}
let computed_value: u8 = callback(&conv_source, &conv_target)
.with_context(|| format!("error in callback when computing ({},{})", self_x, self_y))?;
result_image.set(self_x as i32, self_y as i32, computed_value)
.ok_or_else(|| anyhow::anyhow!("result_image.set({},{}) returned None", self_x, self_y))?;
}
}
Ok(result_image)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::arc::ImageTryCreate;
fn conv5x5_doit(source: &Image, _target: &Image) -> anyhow::Result<u8> {
let center_pixel: u8 = match source.get(2, 2) {
Some(value) => value,
None => {
return Ok(255);
}
};
Ok(center_pixel)
}
#[test]
fn test_10000_sunshine_scenario() {
// Arrange
let source_pixels: Vec<u8> = vec![
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20,
21, 22, 23, 24, 25,
];
let input_source: Image = Image::try_create(5, 5, source_pixels).expect("image");
let input_target = Image::zero(5, 5);
let mask_pixels: Vec<u8> = vec![
1,
];
let input_mask: Image = Image::try_create(1, 1, mask_pixels).expect("image");
// Act
let output: Image = convolution5x5_special(&input_source, &input_target, &input_mask, 42, conv5x5_doit).expect("image");
// Assert
let expected_pixels: Vec<u8> = vec![
13,
];
let expected: Image = Image::try_create(1, 1, expected_pixels).expect("image");
assert_eq!(output, expected);
}
}
|