I want to make a lazy loading of image pixels to the 3 dimensional array of integers. For example in simple way it looks like this:
for i=0 to Width
What do you mean by lazy?
An array is not a lazy data type, which means that if you want to use arrays, you need to load all pixels during the initialization. If we were using single-dimensional array, an alternative would be to use seq<_>
which is lazy (but you can access elements only sequentially). There is nothing like seq<_>
for multi-dimensional arrays, so you'll need to use something else.
Probably the closest option would be to use three-dimensional array of lazy values (Lazy
). This is an array of delayed thunks that access pixels and are evaluated only when you actually read the value at the location. You could initialize it like this:
for i=0 to Width
for j=0 to Height
let point = lazy image.GetPixel(i,j)
pixels.[0,i,j] <- lazy point.Value.R
pixels.[1,i,j] <- lazy point.Value.G
pixels.[2,i,j] <- lazy point.Value.B
The snippet creates a lazy value that reads the pixel (point
) and then three lazy values to get the individual color components. When accessing color component, the point
value is evaluated (by accessing Value
).
The only difference in the rest of your code is that you'll need to call Value
(e.g. pixels.[0,10,10].Value
to get the actual color component of the pixel.
You could define more complex data structures (such as your own type that supports indexing and is lazy), but I think that array of lazy values should be a good starting point.