demo_ppm Program

Uses

  • program~~demo_ppm~~UsesGraph program~demo_ppm demo_ppm module~forimage forimage program~demo_ppm->module~forimage module~forcolor forcolor module~forimage->module~forcolor module~forimage_parameters forimage_parameters module~forimage->module~forimage_parameters module~lut lut module~forimage->module~lut module~pnm pnm module~forimage->module~pnm module~forcolor->module~forimage_parameters module~forcolor->module~pnm iso_fortran_env iso_fortran_env module~forimage_parameters->iso_fortran_env module~pnm->module~forimage_parameters

Calls

program~~demo_ppm~~CallsGraph program~demo_ppm demo_ppm proc~brighten format_pnm%brighten program~demo_ppm->proc~brighten proc~crop format_pnm%crop program~demo_ppm->proc~crop proc~deallocate_pnm format_pnm%deallocate_pnm program~demo_ppm->proc~deallocate_pnm proc~export_pnm format_pnm%export_pnm program~demo_ppm->proc~export_pnm proc~flip_horizontal format_pnm%flip_horizontal program~demo_ppm->proc~flip_horizontal proc~flip_vertical format_pnm%flip_vertical program~demo_ppm->proc~flip_vertical proc~greyscale format_pnm%greyscale program~demo_ppm->proc~greyscale proc~import_pnm format_pnm%import_pnm program~demo_ppm->proc~import_pnm proc~mandelbrot mandelbrot program~demo_ppm->proc~mandelbrot proc~negative format_pnm%negative program~demo_ppm->proc~negative proc~print_info format_pnm%print_info program~demo_ppm->proc~print_info proc~remove_channels format_pnm%remove_channels program~demo_ppm->proc~remove_channels proc~resize format_pnm%resize program~demo_ppm->proc~resize proc~rotate format_pnm%rotate program~demo_ppm->proc~rotate proc~set_pnm format_pnm%set_pnm program~demo_ppm->proc~set_pnm proc~swap_channels format_pnm%swap_channels program~demo_ppm->proc~swap_channels proc~set_pixels format_pnm%set_pixels proc~brighten->proc~set_pixels proc~allocate_pixels format_pnm%allocate_pixels proc~crop->proc~allocate_pixels proc~set_height format_pnm%set_height proc~crop->proc~set_height proc~crop->proc~set_pixels proc~set_width format_pnm%set_width proc~crop->proc~set_width proc~set_format format_pnm%set_format proc~export_pnm->proc~set_format proc~write_header write_header proc~export_pnm->proc~write_header proc~check_pixel_range format_pnm%check_pixel_range proc~flip_horizontal->proc~check_pixel_range proc~flip_horizontal->proc~set_height proc~flip_horizontal->proc~set_width proc~flip_vertical->proc~check_pixel_range proc~flip_vertical->proc~set_height proc~flip_vertical->proc~set_width proc~greyscale->proc~check_pixel_range proc~import_pnm->proc~allocate_pixels proc~import_pnm->proc~check_pixel_range proc~read_header read_header proc~import_pnm->proc~read_header proc~set_file_format format_pnm%set_file_format proc~import_pnm->proc~set_file_format proc~import_pnm->proc~set_format proc~negative->proc~set_pixels proc~aspect_ratio aspect_ratio proc~print_info->proc~aspect_ratio proc~average_colors average_colors proc~print_info->proc~average_colors proc~pixel_size pixel_size proc~print_info->proc~pixel_size proc~remove_channels->proc~check_pixel_range proc~resize->proc~allocate_pixels proc~resize->proc~set_height proc~resize->proc~set_pixels proc~resize->proc~set_width proc~rotate->proc~allocate_pixels proc~rotate->proc~set_height proc~rotate->proc~set_pixels proc~rotate->proc~set_width proc~set_pnm->proc~allocate_pixels proc~set_pnm->proc~set_file_format proc~set_pnm->proc~set_format proc~set_header format_pnm%set_header proc~set_pnm->proc~set_header proc~set_pnm->proc~set_pixels proc~swap_channels->proc~check_pixel_range proc~set_header->proc~set_height proc~set_header->proc~set_width proc~set_comment format_pnm%set_comment proc~set_header->proc~set_comment proc~set_magicnumber format_pnm%set_magicnumber proc~set_header->proc~set_magicnumber proc~set_max_color format_pnm%set_max_color proc~set_header->proc~set_max_color proc~set_pixels->proc~check_pixel_range

Variables

Type Attributes Name Initial
type(format_pnm) :: copy_image
integer, parameter :: height = 400
type(format_pnm) :: image
integer(kind=ik) :: px(height,3*width)
integer, parameter :: width = 400

Functions

pure function mandelbrot(h, w) result(pixels)

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: h
integer, intent(in) :: w

Return Value integer(kind=ik), (h,3*w)


Source Code

program demo_ppm

   use forimage, only: format_pnm, rk, ik
   implicit none

   ! Declare format_pnm object and parameters for image dimensions
   type(format_pnm)    :: image, copy_image
   integer, parameter  :: height = 400
   integer, parameter  :: width  = 400
   integer(ik)         :: px(height, 3*width)

   ! Generate Mandelbrot fractal and assign pixel values
   px = mandelbrot(height, width)

   ! Set the properties of the format_pnm object (encoding, file format, width, height, max_color, comment and pixels)
   call image%set_pnm(&
      encoding    = 'binary', &
      file_format = 'ppm', &
      width       = width, &
      height      = height, &
      max_color   = 255, &
      comment     = 'demo: mandelbrot', &
      pixels      = px &
      )

   ! Print information about the image
   call image%print_info()

   ! Export the image to a PPM file
   call image%export_pnm('pnm_files/mandelbrot_binary')

   ! Export the image to a PPM file with ascii encoding
   call image%export_pnm('pnm_files/mandelbrot_ascii', 'ascii')

   ! Export the image to a PPM file with a different encoding
   copy_image = image ! Copy the format_pnm object
   call copy_image%negative()
   call copy_image%export_pnm('pnm_files/mandelbrot_binary_negative', 'binary')
   call copy_image%finalize()

   ! Brighten the image
   copy_image = image ! Copy the format_pnm object
   call copy_image%brighten(100)
   call copy_image%export_pnm('pnm_files/mandelbrot_binary_brighten', 'binary')
   call copy_image%finalize()

   ! Swap the red and blue channels
   copy_image = image ! Copy the format_pnm object
   call copy_image%swap_channels('rb')
   call copy_image%export_pnm('pnm_files/mandelbrot_binary_swap', 'binary')
   call copy_image%finalize()

   ! Remove the blue channel
   copy_image = image ! Copy the format_pnm object
   call copy_image%remove_channels(remove_b=.true.)
   call copy_image%export_pnm('pnm_files/mandelbrot_binary_remove', 'binary')
   call copy_image%finalize()

   ! Convert the image to greyscale
   copy_image = image ! Copy the format_pnm object
   call copy_image%greyscale()
   call copy_image%export_pnm('pnm_files/mandelbrot_binary_greyscale', 'binary')
   call copy_image%finalize()

   ! Rotate the image 90 degrees clockwise
   copy_image = image ! Copy the format_pnm object
   call copy_image%rotate(-90)
   call copy_image%export_pnm('pnm_files/mandelbrot_binary_rotate', 'binary')
   call copy_image%finalize()

   ! Flip the image horizontally
   copy_image = image ! Copy the format_pnm object
   call copy_image%flip_horizontal()
   call copy_image%export_pnm('pnm_files/mandelbrot_binary_flip_horizontal', 'binary')
   call copy_image%finalize()

   ! Flip the image vertically
   copy_image = image ! Copy the format_pnm object
   call copy_image%flip_vertical()
   call copy_image%export_pnm('pnm_files/mandelbrot_binary_flip_vertical', 'binary')
   call copy_image%finalize()

   ! Crop the image
   copy_image = image ! Copy the format_pnm object
   call copy_image%crop(100,200, 50, 300)
   call copy_image%export_pnm('pnm_files/mandelbrot_binary_crop', 'binary')
   call copy_image%finalize()

   ! Resize the image
   copy_image = image ! Copy the format_pnm object
   call copy_image%resize(800,200)
   call copy_image%export_pnm('pnm_files/mandelbrot_binary_resize', 'binary')
   call copy_image%finalize()

   ! Finalize the format_pnm object to release resources
   call image%finalize()

   ! Import a PPM file with binary encoding and export it with ascii encoding
   call image%import_pnm('pnm_files/mandelbrot_binary', 'ppm', 'binary')
   call image%export_pnm('pnm_files/mandelbrot_ascii_ex', 'ascii')
   call image%finalize()

   ! Import a PPM file with ascii encoding and export it with binary encoding
   call image%import_pnm('pnm_files/mandelbrot_ascii', 'ppm', 'ascii')
   call image%export_pnm('pnm_files/mandelbrot_binary_ex', 'binary')
   call image%finalize()

contains

   ! Function to generate Mandelbrot fractal
   pure function mandelbrot(h, w) result(pixels)
      integer, intent(in)    :: w, h
      integer(ik)            :: pixels(h, 3*w)
      integer(ik), parameter :: max_iter = 256_ik
      real(rk),    parameter :: x_min = -2.0_rk
      real(rk),    parameter :: x_max = 1.0_rk
      real(rk),    parameter :: y_min = -1.5_rk
      real(rk),    parameter :: y_max = 1.5_rk
      real(rk)               :: x, y, x_temp, zx, zy, scale_x, scale_y
      integer(ik)            :: iter
      integer                :: i, j
      ! Calculate scale factors for mapping pixel coordinates to Mandelbrot coordinates
      scale_x = (x_max - x_min) / real(w, kind=rk)
      scale_y = (y_max - y_min) / real(h, kind=rk)
      ! Generate Mandelbrot fractal
      do i = 1, h
         do j = 1, w
            x = x_min + real(j - 1, kind=rk) * scale_x
            y = y_max - real(i - 1, kind=rk) * scale_y
            zx = 0.0_rk
            zy = 0.0_rk
            iter = 0_ik
            do while (iter < max_iter .and. zx * zx + zy * zy < 4.0_rk)
               x_temp = zx * zx - zy * zy + x
               zy = 2.0_rk * zx * zy + y
               zx = x_temp
               iter = iter + 1_ik
            end do
            ! Assign colors
            pixels(i, 3*j-2) = int(mod(iter*7_ik, 256_ik), kind=ik)     ! Red channel
            pixels(i, 3*j-1) = int(mod(iter*4_ik, 256_ik), kind=ik)     ! Green channel
            pixels(i, 3*j)   = int(mod(iter*10_ik, 256_ik), kind=ik)    ! Blue channel
         end do
      end do
   end function

end program demo_ppm