Mandelbrot demo: show the image
certik opened this issue · comments
TODO:
- Get the image out of Fortran and into JavaScript using the approach described at: https://gitlab.com/lfortran/lfortran/-/issues/761#note_1056724358
- Show the image on a canvas from JavaScript
Old:
Here is a code that compiles to WAT at dev.lfortran.org: https://gitlab.com/lfortran/lfortran/-/issues/702#note_1059822597, but the execution window says:
ERROR: The code could not be executed. Either there is a runtime error or there is an issue at our end.
So we need to investigate what is going on.
I had to reduce the image size. This works:
program mandelbrot
implicit none
integer , parameter :: rk = 8
integer , parameter :: i_max = 80
integer , parameter :: j_max = 60
integer , parameter :: n_max = 100
real (rk), parameter :: x_centre = -0.5_rk
real (rk), parameter :: y_centre = 0.0_rk
real (rk), parameter :: width = 4.0_rk
real (rk), parameter :: height = 3.0_rk
real (rk), parameter :: dx_di = width / i_max
real (rk), parameter :: dy_dj = -height / j_max
real (rk), parameter :: x_offset = x_centre - 0.5_rk * (i_max + 1) * dx_di
real (rk), parameter :: y_offset = y_centre - 0.5_rk * (j_max + 1) * dy_dj
integer :: image(i_max, j_max)
integer :: i
integer :: j
integer :: n
real (rk) :: x
real (rk) :: y
real (rk) :: x_0
real (rk) :: y_0
real (rk) :: x_sqr
real (rk) :: y_sqr
do j = 1, j_max
y_0 = y_offset + dy_dj * j
do i = 1, i_max
x_0 = x_offset + dx_di * i
x = 0.0_rk
y = 0.0_rk
n = 0
do
x_sqr = x * x
y_sqr = y * y
if (x_sqr + y_sqr > 4.0_rk) then
image(i,j) = 255
exit
end if
if (n == n_max) then
image(i,j) = 0
exit
end if
y = y_0 + 2.0_rk * x * y
x = x_0 + x_sqr - y_sqr
n = n + 1
end do
end do
end do
print '(a)', 'P2'
print '(i0, 1x, i0)', i_max, j_max
print '(i0)', 255
do j = 1, j_max
do i = 1, i_max
print '(i0)', image(i,j)
end do
end do
end program mandelbrot
And produces the correct output. Just copy & paste the output into a file mandelbrot.pbm
and open it in some image program and it works!
The last missing piece is how to show the image in the webpage itself as an image. The approach how to do it is described here:
It seems the initial example code failed due to insufficient memory. We recently increased the memory limit in lfortran/lfortran#126. Probably, the current lfortran.wasm
at dev.lfortran.org was compiled prior to the increase in memory and I guess, therefore the error. I will update the lfortran.wasm
(and lfortran.js
) at dev.lfortran.org with the latest one soon.
@Shaikh-Ubaid I think that's right. Even the pow
that we fixed didn't work, so you can see I worked around it too. These are both quite easy to fix.
Please, could someone possibly share if this issue is complete?
This is fixed! Thank you so much @Shaikh-Ubaid for implementing it. Here is how it looks like for me now at dev.lfortran.org: