C**********************************************************************
C**********************************************************************
C** 
C**  File: readbinex1.for
C**
C**  Description: An example of how to use the readbin routines.
C**
C**  Date: 8th January 2004
C**
C**********************************************************************
C**
C** Copyright (c) 2004 Scott A. Belmonte
C** All rights reserved.
C**
C** Redistribution and use in source and binary forms, with or without 
C** modification, are permitted provided that the following conditions
C** are met:
C**
C** Redistributions of source code must retain the above copyright
C** notice, this list of conditions and the following disclaimer.
C**
C** Redistributions in binary form must reproduce the above copyright
C** notice, this list of conditions and the following disclaimer in
C** the documentation and/or other materials provided with the
C** distribution.
C**
C** Neither the name of the copyright holder nor the names of any
C** contributors may be used to endorse or promote products derived
C** from this software without specific prior written permission.
C**
C** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
C** CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
C** INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
C** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
C** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
C** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
C** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
C** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
C** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
C** ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
C** TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
C** THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
C** SUCH DAMAGE.
C**
C**********************************************************************
C**********************************************************************
C
      PROGRAM READ_BIN_EX1
      IMPLICIT NONE
C     
      INCLUDE 'readbin.inc'
C
C     Functions
C
      INTEGER OPEN_BIN_FILE
      INTEGER READ_BIN_FILE
      LOGICAL BIGEND_CPU
      INTEGER READ_BIN_SHORT
      INTEGER READ_BIN_LONG
C
C     Variables
C
      BYTE      HEADER(8)       ! Store the 8-byte TIFF header
      INTEGER   MAGIC           ! TIFF magic num. Should be 42
      INTEGER   IFD1_OFFSET     ! The byte offset of the first IFD
C
C     Open TIFF file
C
      IF (OPEN_BIN_FILE('example.tif') .NE. 0) GOTO 901
C
C     Read first 8 bytes of the file into HEADER.
C     N.B. Remember that the start and end bytes are zero-offset
C     meaning that the first byte in the file is byte 0.
C
      IF (READ_BIN_FILE(HEADER, 0, 7) .NE. 0) GOTO 902
C
C     The first two bytes of a TIFF header should be II or MM
C     = 73, 73 or = 77, 77. II means the file is little-endian.
C     MM means the file is big-endian.
C
      IF (HEADER(1) .EQ. 73 .AND. HEADER(2) .EQ. 73) THEN
         BIG_END = .FALSE.
      ELSE IF (HEADER(1) .EQ. 77 .AND. HEADER(2) .EQ. 77) THEN
         BIG_END = .TRUE.
      ELSE
         GOTO 903
      ENDIF
C
C     If the machine has a different endian than the file
C     then SWAP is set to .TRUE.
C
      SWAP = (BIGEND_CPU() .XOR. BIG_END)
      WRITE(*,*) 'BIG_END = ', BIG_END, ' SWAP = ', SWAP
C
C     Check magic number.
C
      MAGIC = READ_BIN_SHORT(HEADER(3))
      WRITE(*,*) 'MAGIC = ', MAGIC
      IF (MAGIC .NE. 42) GOTO 904
C
C     Read the first IFD offset.
C
      IFD1_OFFSET = READ_BIN_LONG(HEADER(5))
      WRITE(*,*) 'IFD1_OFFSET = ', IFD1_OFFSET
C
      CALL CLOSE_BIN_FILE()
      RETURN
C
C     Error traps
C
 901  WRITE(*,*) '** Error opening TIFF file!'
      RETURN
 902  WRITE(*,*) '** Error reading TIFF header!'
      RETURN
 903  WRITE(*,*) '** Error bad endian marker!'
      RETURN 
 904  WRITE(*,*) '** Error bad magic number!'
      RETURN 
      END
C
