!v1.1 29 December 2007
!this is the FORTRAN 90 version of the program that puts a pretty fractal 
!image into julia.ppm
!compile with:  f90 -o juliaf julia.f90
!or maybe with: f95 -o juliaf julia.f90
!or with Intel compiler on linux: ifort -o juliaf julia.f90
!or with g95 in Cygwin: g95 -o juliaf julia.f90
!note in Cygwin you must set the following in your .bashrc:
!G95_CR=0
!export G95_CR
!The contolling parameters are input in a namelist file, run juliaf with:
!juliaf < fractal.input
!Note that the use of argv and system is not part of standard fortran
!The namelist input is a very useful aspect of fortran
program julia
!the following means mrl=8, or double precision.  kind(1.0e0) is single 
integer, parameter :: mrl=kind(1.0d0)
integer itermax,magnify,hxres,hyres
real(mrl) :: breakout,cr,ci,x0,y0
namelist/params/itermax,breakout,magnify,cr,ci,x0,y0,&
  hxres,hyres,fname !note the use of & to continue this statement
real(mrl) x,xx,y,xl,yl,zsq,zm,rb
integer iter,hx,hy
integer red,green,blue
character fname*50

fname="fromfort.ppm" !default filename
read (*,nml=params) ! the * means read from standard input

!next write the header to the output file
open (unit=20,file=trim(fname),status="unknown",recl=3*hxres*hyres+10) 
!open (unit=20,file="julia.ppm",status="unknown") !for P3 
write(20,'(a)') "P6"  !edit to P3 if using P3
write(20,'("# magnify=",i6,"  itermax=",i6)') magnify,itermax !PPM comment line
write(20,*) hxres,hyres ! the * means let the compiler figure out the format
write(20,'(a)') "255" 

rb=sqrt(breakout)

do hy=1,hyres
  do hx=1,hxres
  y = (4.*hyres/hxres)*((hyres+1-hy-.5)/hyres-0.5)/magnify+y0
  x = 4*((hx-.5)/hxres-0.5)/magnify+x0
  zm=0
  do iter=1,itermax-1
      xl=x
      yl=y
      xx = x*x-y*y+cr
      y = 2.0*x*y+ci
      x = xx
      zsq=x*x+y*y
      if (zsq > zm) zm=zsq
      if (zsq>breakout) exit 
  end do           
  if (iter>=itermax) then 
      red=0
      green=255.*zm/breakout
      blue=255.*zm/breakout
   else
      red=255*(rb+xl)/(2*rb)
      green=0
      blue=.5*255*(rb+yl)/(2*rb)
   end if
  write(20,'(3a)',advance='no') char(red),char(green),char(blue)
!  write(20,'(i3,1x,i3,1x,i3)') red,green,blue !for P3
  end do
end do
close(20)
print *,  "wrote ",hxres,"by",hyres, "ppm file: ",trim(fname)
end program julia
