Hi everyone,
I’m new to rust (and heavily typed languages in general) coming from python. I’m interested in making an image processing tool for a side project, but I’m having some difficulty of coming up with the design of the structure in rust.
The outcome I want is to be able to define a tree of processes to perform on images, like applying histograms transformations, cropping, merging two images, averaging a collection of images, etc. So a user can make a workflow on their images. I guess the ultimate goal would be to have a UI where people can add nodes of processes and connect them, but I might be getting ahead of myself.
From the examples I can find online this would be okay if all the processes worked on the same data, say something like
trait Process {
fn process(&self, input: Image) -> Image;
}
Then I would be able to create a tree of nodes using a Vec and can keep track of the inputs and outputs. Maybe something like this:
struct Node {
process: Box<dyn Process>,
input: usize,
output: usize,
}
struct Tree {
nodes: Vec<Node>,
}
But my problem is that the I have processes which take different types of inputs, and numbers of inputs which also need to match up with the corresponding outputs of other nodes.
The types of inputs and outputs I want are greyscale images, rgb images, and collections of greyscale or rgb images.
Here’s some examples of processes I have:
- FileImage which takes no inputs and loads a file from disk to an image
- Histogram and cropping will take either a greyscale or rgb image and output a similar image
- ChannelCombine takes exactly three greyscale inputs and maps them to an rgb image
- some splitting which might take an image and output two images
- AverageCollection will take a collection of greyscale or rgb images (potentially 1000’s of them saved to disk and average them together) and output a single image
I’ve been trying to implement something myself around this and keeping track of the types of inputs and outputs and having lots of match statements and raising errors, but I feel I’m just fighting the type system constantly.
I appreciate this is a long question and very specific but I’m just getting myself quite lost with it now and how someone who knows the language would start to tackle this problem, so any pointers would be appreciated! I can also provide better code examples of what I’ve tried before.