Advertisement
Guest User

main.f90

a guest
Oct 31st, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module ppm
  2.     implicit none
  3.     public loadppm, saveppm, blurppm
  4.  
  5.     type ppmfile
  6.         integer              :: depth, width, height
  7.         integer, allocatable :: data( :, :, : )
  8.  
  9.         contains
  10.             procedure, public :: load => loadppm
  11.             procedure, public :: save => saveppm
  12.             procedure, public :: blur => blurppm
  13.     end type ppmfile
  14.  
  15.     contains
  16.         subroutine loadppm( this, filename )
  17.             class( ppmfile )                   :: this
  18.             character( len = * ), intent( in ) :: filename
  19.  
  20.             integer              :: funit, cdepth, cwidth, cheight, x, y, cr, cg, cb, reason
  21.             character( 2 )       :: header
  22.  
  23.             open( newunit = funit, file = filename, status = 'old', action = 'read', access = 'stream', form = 'formatted' )
  24.  
  25.             read( funit, '(a2)' ) header
  26.             if ( header /= 'P3' ) then
  27.                 print *, "Invalid file type detected."
  28.                 stop
  29.             end if
  30.                
  31.             read( funit, '(2i4)' ) cwidth, cheight
  32.             read( funit, '(1i4)' ) cdepth
  33.                
  34.             print *, 'cwidth  = ', cwidth
  35.             print *, 'cheight = ', cheight
  36.             print *, 'cdepth  = ', cdepth
  37.            
  38.             if ( cdepth /= 255 ) then
  39.                 print *, "Invalid colour depth detected."
  40.                 stop
  41.             end if
  42.  
  43.             this%width  = cwidth
  44.             this%height = cheight
  45.             this%depth  = cdepth
  46.  
  47.             print *, 'Width, height, and depth assigned, reading in colour values...'
  48.  
  49.             allocate( this%data( 3, this%width, this%height ) )
  50.  
  51.             do y = 1, this%width
  52.                 do x = 1, this%height
  53.                     read( funit, *, IOSTAT = reason ) cr, cg, cb
  54.  
  55.                     if ( reason < 0 ) then
  56.                         ! EOF reached
  57.                         exit
  58.                         exit
  59.                     end if
  60.  
  61.                     this%data( 1, y, x ) = cr
  62.                     this%data( 2, y, x ) = cg
  63.                     this%data( 3, y, x ) = cb
  64.                 end do
  65.             end do
  66.  
  67.             close( funit )
  68.         end subroutine loadppm
  69.  
  70.         subroutine saveppm( this, filename )
  71.             class( ppmfile ),     intent( in ) :: this
  72.             character( len = * ), intent( in ) :: filename
  73.  
  74.             integer :: funit, x, y, r, g, b, w, h
  75.  
  76.             open( newunit = funit, file = filename, status = 'replace', action = 'write', access = 'stream', form = 'formatted' )
  77.  
  78.             write( funit, '(a2)'        ) "P3"
  79.             write( funit, '(i0, a, i0)' ) this%width, ' ', this%height
  80.             write( funit, '(i0)'        ) this%depth
  81.  
  82.             w = this%width
  83.             h = this%height
  84.  
  85.             print *, 'Width  = ', w
  86.             print *, 'Height = ', h
  87.  
  88.             do y = 1, w
  89.                 do x = 1, h
  90.                     r = this%data( 1, y, x )
  91.                     g = this%data( 2, y, x )
  92.                     b = this%data( 3, y, x )
  93.  
  94.                     if ( r > 255 .or. g > 255 .or. b > 255 ) then
  95.                         print *, "There's incorrect values in the data!"
  96.                         print *, r, g, b
  97.                         call exit( -1 )
  98.                     end if
  99.  
  100.                     write( funit, '(i0, a, i0, a, i0, a)', advance = 'no' ) r, ' ', g, ' ', b, ' '
  101.                 end do
  102.             end do
  103.  
  104.             flush( funit )
  105.             close( funit )
  106.         end subroutine saveppm
  107.  
  108.         subroutine blurppm( this )
  109.             class( ppmfile ), intent( in ) :: this
  110.         end subroutine blurppm
  111. end module ppm
  112.  
  113. program main
  114.     use ppm
  115.     implicit none
  116.     type( ppmfile ) :: ppmf
  117.  
  118.     print *, 'Testing ppm reading...'
  119.  
  120.     call ppmf%load( filename = "eagle.ppm" )
  121.  
  122.     print *, 'PPM file read in.'
  123.     print *, ' Width  = ', ppmf%width
  124.     print *, ' Height = ', ppmf%height
  125.     print *, ' Depth  = ', ppmf%depth
  126.  
  127.     print *, 'Testing saving...'
  128.  
  129.     call ppmf%save( filename = "eagle-new.ppm" )
  130. end program main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement