×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

how do I READ in an image file?

how do I READ in an image file?

how do I READ in an image file?

(OP)
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?  

RE: how do I READ in an image file?

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.

RE: how do I READ in an image file?

(OP)
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

RE: how do I READ in an image file?

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

RE: how do I READ in an image file?

Additional info :
The variable M must also be declared as INTEGER * 2
P.A.

RE: how do I READ in an image file?

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close