Bitmap/Fortran: Difference between revisions

Content added Content deleted
(rewrote everything. just one type using type-bound procedures)
(overwrote dim. now uses variable name: n)
Line 27: Line 27:
!! k=1: red, k=2: green, k=3: blue
!! k=1: red, k=2: green, k=3: blue


integer :: dim(2) = 0
integer :: n(2) = 0
!! image dimensions: [height, width]
!! image dimensions: [height, width]


Line 51: Line 51:
integer, intent(in) :: height, width
integer, intent(in) :: height, width


this%dim = [height, width]
this%n = [height, width]
allocate (this%rgb(height,width,3), source=0)
allocate (this%rgb(height,width,3), source=0)
end subroutine
end subroutine
Line 64: Line 64:


! always check that dimensions match
! always check that dimensions match
rgbimage_valid = ( all(this%dim > 0) .and. &
rgbimage_valid = ( all(this%n > 0) .and. &
& (size(this%rgb, dim=1) == this%dim(1)) .and. &
& (size(this%rgb, dim=1) == this%n(1)) .and. &
& (size(this%rgb, dim=2) == this%dim(2)) .and. &
& (size(this%rgb, dim=2) == this%n(2)) .and. &
& (size(this%rgb, dim=3) == 3) )
& (size(this%rgb, dim=3) == 3) )


! optionally: check if rgb values are in allowed range
! optionally: check if rgb values are in allowed range
Line 85: Line 85:
integer, intent(in) :: x, y
integer, intent(in) :: x, y


rgbimage_inside = ((x > 0) .and. (x <= this%dim(1)) .and. (y > 0) .and. (y <= this%dim(2)))
rgbimage_inside = ((x > 0) .and. (x <= this%n(1)) .and. (y > 0) .and. (y <= this%n(2)))
end function
end function


Line 162: Line 162:
! write header
! write header
write (iounit, '(A)') 'P6'
write (iounit, '(A)') 'P6'
write (iounit, '(I0, A, I0)') this%dim(1), " ", this%dim(2)
write (iounit, '(I0, A, I0)') this%n(1), " ", this%n(2)
write (iounit, '(A)') '255'
write (iounit, '(A)') '255'


do i = 1, this%dim(1)
do i = 1, this%n(1)
do j = 1, this%dim(2)
do j = 1, this%n(2)
write (iounit, '(3A1)', advance='no') [(achar(this%rgb(i,j,k)), k=1,3)]
write (iounit, '(3A1)', advance='no') [(achar(this%rgb(i,j,k)), k=1,3)]
end do
end do