File size: 4,753 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 127 128 129 130 131 132 133 134 135 136 137 |
use super::{Image, ImageToNumber, convolution3x3};
use anyhow::Context;
use loda_rust_core::execute::{NodeLoopLimit, ProgramCache, ProgramRunner, RegisterValue, RunMode};
use loda_rust_core::execute::NodeRegisterLimit;
use num_bigint::{BigInt, BigUint};
use num_bigint::ToBigInt;
pub trait ConvolutionWithProgram {
fn conv3x3_program(&self, program_runner: &ProgramRunner) -> anyhow::Result<Image>;
}
impl ConvolutionWithProgram for Image {
fn conv3x3_program(&self, program_runner: &ProgramRunner) -> anyhow::Result<Image> {
// let mut cache = ProgramCache::new();
// let step_count_limit: u64 = 1000000000;
// let mut step_count: u64 = 0;
// let step_count_limit: u64 = 1000000000;
// let mut cache = ProgramCache::new();
// let mut step_count: u64 = 0;
// let input_raw_int: BigInt = 42u32.to_bigint().unwrap();
// let input = RegisterValue(input_raw_int);
// let result_run = program_runner.run(
// &input,
// RunMode::Silent,
// &mut step_count,
// step_count_limit,
// NodeRegisterLimit::Unlimited,
// NodeLoopLimit::Unlimited,
// &mut cache
// );
let result = convolution3x3(&self, |bm| {
let step_count_limit: u64 = 1000000000;
let mut cache = ProgramCache::new();
let mut step_count: u64 = 0;
let input_raw_uint: BigUint = bm.to_number()?;
let input_raw_int: BigInt = match input_raw_uint.to_bigint() {
Some(value) => value,
None => {
return Err(anyhow::anyhow!("Integrity error. Couldn't convert BigUint to BigInt. input {}", input_raw_uint));
}
};
let input_registervalue = RegisterValue(input_raw_int);
let result_run = program_runner.run(
input_registervalue,
RunMode::Silent,
&mut step_count,
step_count_limit,
NodeRegisterLimit::Unlimited,
NodeLoopLimit::Unlimited,
&mut cache
);
let output: RegisterValue = result_run
.with_context(|| format!("run failed for input {}", input_raw_uint))?;
let output_i64: i64 = match output.try_to_i64() {
Some(value) => value,
None => {
return Err(anyhow::anyhow!("output value {} is out of range i64 when computing term for input {}", output, input_raw_uint));
}
};
if output_i64 < 0 || output_i64 > 255 {
return Err(anyhow::anyhow!("output value {} is out of range [0..255] when computing term for input {}", output, input_raw_uint));
}
let output: u8 = output_i64 as u8;
Ok(output)
});
result
}
}
#[cfg(test)]
mod tests {
use super::*;
use loda_rust_core::execute::ProgramId;
use crate::arc::ImageTryCreate;
use loda_rust_core::control::{DependencyManager,DependencyManagerFileSystemMode};
use loda_rust_core::unofficial_function::UnofficialFunctionRegistry;
use std::path::PathBuf;
#[test]
fn test_10000_callback() {
// Arrange
let pixels: Vec<u8> = vec![
1,2,3,4,
5,6,7,8,
9,10,11,12,
13,14,15,16,
];
let input: Image = Image::try_create(4, 4, pixels).expect("image");
let program: &str = "
mov $1,$0
mod $1,256 ; width
div $0,256
mov $2,$0
mod $2,256 ; height
div $0,256
mov $3,$1
mul $3,$2 ; number of pixels
mov $5,255 ; inital value
lpb $3
mov $6,$0
mod $6,256
min $5,$6 ; pick the lowest pixel value
div $0,256
sub $3,1
lpe
mov $0,$5
";
let mut dm = DependencyManager::new(
DependencyManagerFileSystemMode::Virtual,
PathBuf::from("non-existing-dir"),
UnofficialFunctionRegistry::new(),
);
let result_parse = dm.parse(ProgramId::ProgramWithoutId, program);
let program_runner: ProgramRunner = result_parse.expect("ProgramRunner");
// Act
let output: Image = input.conv3x3_program(&program_runner).expect("image");
// Assert
assert_eq!(output.width(), 2);
assert_eq!(output.height(), 2);
assert_eq!(output.get(0, 0), Some(1));
assert_eq!(output.get(1, 0), Some(2));
assert_eq!(output.get(0, 1), Some(5));
assert_eq!(output.get(1, 1), Some(6));
}
}
|