Problem
I'm trying to make a basic image (bitmap) writter in Elixir but I stuck on a point.
I tried to make a function for set a pixel into a binary. I use pattern matching but my function is clearly too slow (more than 10 min to set all pixels to a picture with a size of 1024 * 768).
Currently, I've a binary with a size equals to width * height. Like you can see on the following code, my function take x and y as params and have to modify an int at this location.
Current code
# Function
def replace_by_test(output, width, x, y) do
out_offset = y * width + x
<<
o_before :: binary-size(out_offset),
_ :: binary-size(4),
o_after :: binary
>> = output
<< o_before :: binary, "TEST" :: binary, o_after :: binary >>
end
# Test on a 1024 * 768 resolution image
out_size = 1024 * 768 * 8
output = << 0 :: size(out_size) >>
for x <- 0..(1024*768-1), do: replace_by_test(output, 1024, 0, 0)
Goal
Make this code faster. If possible, run it in less than 10 seconds.
x=0 y=0thenx=0 y=1thenx=0 y=3and so on? or can they be in random order?