Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I READ in an image file?

Status
Not open for further replies.

Mvpr

Technical User
May 9, 2001
4
0
0
CA
I am trying to read an image file of the following format:
header consisting of 1024 bytes
image data consisting of 256x256 16 bit pixels.

I'm having trouble with I/O errors, especially whether the READ statement should specify direct or sequential access, formatted or unformatted read, etc. Various combinations so far have got me no further than the header. Can someone help me out?

 
Open the file as a BINARY file.
Skip (read) the first 1024 bytes.
The pixel data should be 16-bit integers (not floating
point variables !) Read vectors of 256 16-bit words.
 
PAndersen,
Thanks for the quick response. I'm still at a loss though. I can read the header if I use a formatted read, but then it doesn't get to the data. Also, the low byte preceeds the high byte. Here's the relevant part of the code I have for it:

INTEGER(2) MAT(256,256), HEADER(512)
*
* Read input data
*
OPEN (UNIT=16,FILE='PVStudy',STATUS='OLD',ACCESS='DIRECT',
1 RECL=512,FORM='UNFORMATTED')
PRINT*, 'file read'
READ(16,*) HEADER
PRINT*, 'header read'
DO 10 I = 1, 256
DO 12 J = 1, 256
READ(16,*) MAT(I,J)
12 CONTINUE
10 CONTINUE
PRINT*, 'data read'
STOP

Can you give me some more specific help?

Monique
 
Can you confirme that the file is a sequence of data
with no delimiters (CR LF etc.) inserted ? - If so, the
length of the file should be
1024 + 256 * 256 * 2 .
Your Fortran , does it support FORM='BINARY' in the
OPEN-statement ? - Here is a proposal :
INTEGER * 2 MAT(256,256) , HEADER(512)
OPEN(16,FILE='PVstudy',STATUS='OLD',FORM='BINARY')
C SKIP HEADER
READ(16) HEADER
C READ PIXEL DATA
DO 10 I=1,256
DO 10 J=1,256
C READ ONE PIXEL :
READ(16,ERR=20,END=30)M
C SHIFT BYTES
MLOW=M/256
MHIGH=M-MLOW*256
10 MAT(I,J) = MLOW + MHIGH * 256
PRINT *,'Data read and converted'
STOP
20 WRITE(*,21) I,J
21 FORMAT(1H ,'ERROR , I = ',I4,' J = ',I4)
STOP
30 WRITE(*,31) I,J
31 FORMAT(1H ,'EOF ENCOUNTERED , I = 'I4,' J = ',I4)
END

If your Fortran supports INTEGER * 1 , the shifting can
be made more simple.

P.Andersen
 
Additional info :
The variable M must also be declared as INTEGER * 2
P.A.
 
PAnderson,
I'm getting some numbers out! It will be fairly simple to check whether they're the right numbers. Thanks for your help.

Mvpr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top