Advertisement
Guest User

main.f90

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