通过Rust中的非静态函数数组传递图像

通过Rust中的非静态函数数组传递图像,rust,Rust,我正在尝试重新设计一个Photoshop调整层风格的过程,其中图像处理功能由堆栈中的层表示,在堆栈中,上/第一个过程的结果被输入下一个过程。 这可以表示为一个处理函数数组,其中每个函数的输出被馈送到下一个函数 [Change Brightness(img)->Change Saturation(img)->Change Levels(img)] 我如何从Rust中的非静态函数数组中实现这种值的“传递” fn op_a(mut a:Vec,p1:i32)->Vec{ //处理图像 p

我正在尝试重新设计一个Photoshop调整层风格的过程,其中图像处理功能由堆栈中的层表示,在堆栈中,上/第一个过程的结果被输入下一个过程。 这可以表示为一个处理函数数组,其中每个函数的输出被馈送到下一个函数

[Change Brightness(img)->Change Saturation(img)->Change Levels(img)]
我如何从Rust中的非静态函数数组中实现这种值的“传递”

fn op_a(mut a:Vec,p1:i32)->Vec{
//处理图像
println!(“对大小为{},参数为1 val:{}的图像的操作A”,A.len(),p1);
a[0]=10;
A.
}
fn op_b(mut a:Vec,p1:i32,p2:i32)->Vec{
//处理图像
println!(“大小为{},参数1 val:{},参数2 val:{}的图像上的操作B”,a.len(),p1,p2);
a[0]+=2;
A.
}
fn op_c(mut a:Vec,p1:i32,p2:i32,p3:i32)->Vec{
//处理图像
println!(“大小为{},参数1 val:{},参数2 val:{},参数3 val:{}的图像上的操作C”,a.len(),p1,p2,p3);
a[0]*=2;
A.
}
fn管道功能数组(初始图像:ImageBuffer,Vec)->ImageBuffer{
//执行一个函数数组,将每个函数传递给数组中的下一个函数,初始输入值为initial_image
}
fn main(){
让我的_开始_值=*一些向量*;
设my_funcs=*一个op_a、op_b和op_c*的数组;
设final_image=pipe_function_数组(vec![0],my_funcs);
}

一旦修复了明显的语法错误,代码基本上就能正常工作

fn op_a(mut a: Vec<u8>) -> Vec<u8> {
    //process image
    println!("Operation A on image with size: {}", a.len());
    a[0] = 10;
    a
}

fn op_b(mut a: Vec<u8>) -> Vec<u8> {
    //process image
    println!("Operation B on image with size: {}", a.len());
    a[0] += 2;
    a
}

fn op_c(mut a: Vec<u8>) -> Vec<u8> {
    //process image
    println!("Operation C on image with size: {}", a.len());
    a[0] *= 2;
    a
}

fn pipe_function_array(initial_image: Vec<u8>, a: Vec<fn(Vec<u8>) -> Vec<u8>>) -> Vec<u8> {
    //Using fold here, but you can easily just use a for loop if you prefer.
    a.iter()
        .fold(initial_image, |image, operation| (operation)(image))
}

fn main() {
    // You have to define the type of the Vec here, 
    // otherwise the compiler will throw some ambiguous errors.
    let my_funcs: Vec<fn(Vec<u8>) -> Vec<u8>> = vec![op_a, op_b, op_c];

    let final_image = pipe_function_array(vec![0], my_funcs);
    dbg!(final_image);
}
fn op_a(mut a:Vec)->Vec{
//处理图像
println!(“对大小为{}的图像的操作A”,A.len();
a[0]=10;
A.
}
fn op_b(mut a:Vec)->Vec{
//处理图像
println!(“大小为{}的图像上的操作B”,a.len();
a[0]+=2;
A.
}
fn op_c(mut a:Vec)->Vec{
//处理图像
println!(“大小为{}的图像上的操作C”,a.len();
a[0]*=2;
A.
}
fn管道功能数组(初始图像:Vec,a:Vec Vec>)->Vec{
//在这里使用折叠,但如果您愿意,您可以轻松地使用for循环。
a、 iter()
.fold(初始图像,|图像,操作|(操作)(图像))
}
fn main(){
//您必须在此处定义Vec的类型,
//否则编译器将抛出一些不明确的错误。
让我的函数:Vec Vec>=Vec![op_a,op_b,op_c];
设final_image=pipe_function_数组(vec![0],my_funcs);
dbg!(最终图像);
}


但是,与直接传递函数指针不同,我建议采用不同的方法使用枚举数组

enum Operations {
    Saturation {
        config: SaturationConfig,
    },
    Contrast {
        config: ContrastConfig,
    }
}

fn saturation(mut a: Vec<u8>, config: SaturationConfig) -> Vec<u8> {
    //process image
    a
}
fn contrast(mut a: Vec<u8>, config: ContrastConfig) -> Vec<u8> {
    //process image
    a
}

fn pipe_function_array(mut initial_image: Vec<u8>, a: Vec<Operations>) -> Vec<u8> {
    a.iter()
        .fold(initial_image, |image, operation| match operation {
            Operations::Saturation { config } => saturation(image, config),
            Operations::Contrast { config } => contrast(image, config),
        })
}
enum操作{
饱和{
配置:SaturationConfig,
},
对比度{
config:config,
}
}
fn饱和(mut a:Vec,配置:饱和配置)->Vec{
//处理图像
A.
}
fn对比度(mut a:Vec,config:ContrastConfig)->Vec{
//处理图像
A.
}
fn管道功能数组(mut初始图像:Vec,a:Vec)->Vec{
a、 iter()
.fold(初始|图像、|图像、操作|匹配操作{
操作::饱和度{config}=>Saturation(图像,配置),
操作::对比度{config}=>对比度(图像,配置),
})
}

太好了,谢谢!如果我想在这些阶段中的每一个阶段添加额外的参数来控制那些op_uu函数,那么我如何在每个阶段中也传递这些参数?也就是说,一个阈值函数的值为5,然后饱和函数的值为20等等。@ANimator120不是直接传递函数指针的向量,我可能会用一个包含每个操作不同变量的
enum
替换函数指针,加上每个特定操作的附加值。如果没有添加或删除VEC中的元素,请考虑使用一个可变的切片,即和MUT[U8] < /代码>。