Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mandelbrot demo: show the image #11

Closed
2 tasks done
certik opened this issue Aug 12, 2022 · 6 comments
Closed
2 tasks done

Mandelbrot demo: show the image #11

certik opened this issue Aug 12, 2022 · 6 comments
Assignees

Comments

@certik
Copy link
Contributor

certik commented Aug 12, 2022

TODO:

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.

@certik
Copy link
Contributor Author

certik commented Aug 12, 2022

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!

mandelbrot

@certik
Copy link
Contributor Author

certik commented Aug 12, 2022

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:

@certik certik changed the title Mandelbrot demo Mandelbrot demo: show the image Aug 12, 2022
@Shaikh-Ubaid
Copy link
Member

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.

@certik
Copy link
Contributor Author

certik commented Aug 12, 2022

@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.

@Shaikh-Ubaid
Copy link
Member

Please, could someone possibly share if this issue is complete?

@certik
Copy link
Contributor Author

certik commented Aug 14, 2022

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:

Screen Shot 2022-08-14 at 8 50 42 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants