0

I'm attempting to read an image into an array to apply the sobel filter to it. Here is my code:

program edges
implicit none
integer, dimension(:,:), allocatable :: inp, outim, GX, GY
integer, parameter :: dp = selected_real_kind(15,300)
integer :: ky, kx, x, y, out_unit = 10, M, N, sx, sy, i, j, P2, W, H, mxgr
real(kind=dp) :: G
M = 5
N = 5
W = 50
H = 50
mxgr = 1

allocate(inp(M,N))
allocate(outim(M-2,N-2))
allocate(GX(3,3))
allocate(GY(3,3))

open(file = 'clown.pgm',unit=out_unit,status= 'unknown') !opening file to     write to inp
read (out_unit,11) P2      !pgm magic number
read (out_unit,12) W,H     !width, height
read (out_unit,13) mxgr      !max gray value
do M=-25,25
    do N=-25,25
        read (out_unit,*) inp(M,N)
    end do
end do
11 format(a2)
12 format(i3,1x,i3)
13 format(i5)

open(file = 'outclown.pgm',unit=out_unit,status= 'unknown') !opening file to         write to inp
write (out_unit,15) P2      !pgm magic number
write (out_unit,16) W,H     !width, height
write (out_unit,17) mxgr      !max gray value
do M=-25,25
    do N=-25,25
        write (out_unit,*) imout(M,N)
    end do
end do
close (unit=out_unit)
15 format(a2)
16 format(i3,1x,i3)
17 format(i5)

Which spits out the following error:

At line 43 of file sobel.f90 (unit = 10, file = 'clown.pgm')
Fortran runtime error: End of file

I'm fairly new to working with images in fortran. How can I fix this error? Am I attempting to read the image into the array correctly?

  • Please compile with boundary checking (-Wall) and also have a look at https://stackoverflow.com/questions/49070617/fortran-is-reading-beyond-endfile-record also follow the advise in respect to names from your previous question and the comment from @agentp – albert Mar 03 '18 at 19:36
  • Your program is very basic and I doubt it would read in any .pgm image correctly. Right from the beginning, you are reading in the 'magic number' into your variable P2, an integer. The file format says this magic number is 2 ASCII characters. [link](https://en.wikipedia.org/wiki/Netpbm_format) . From there, you read W and H but don't use them. You assume the image is 51x51 pixels. Also, is the data ASCII or binary. This file format allows both. – Dan Sp. Mar 03 '18 at 20:45
  • Possible duplicate of [Reading an image into an array?](https://stackoverflow.com/questions/49087084/reading-an-image-into-an-array) – agentp Mar 04 '18 at 00:42
  • Not really a duplicate of https://stackoverflow.com/questions/49087084/reading-an-image-into-an-array. But this code still shows a number of serious errors pointed out in commentary on that previous question and OP needs to fix those. – High Performance Mark Mar 04 '18 at 13:55
  • well I hope he doesn't post a new question with every incremental attempted fix. – agentp Mar 04 '18 at 17:52

0 Answers0