C**********************************************************************
C**********************************************************************
C** 
C**  File: readbinex2.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_EX2
      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
C
C     Variables
C
      INTEGER    NUM_PIX          ! Number of pixels
      PARAMETER (NUM_PIX = 625*576)
      INTEGER    BLOCKSZ          ! Size of the RAWDATA buffer
      PARAMETER (BLOCKSZ = 8192)
      INTEGER    DATA(NUM_PIX)    ! Converted data
      BYTE       RAWDATA(BLOCKSZ) ! Raw data as read from disc
      INTEGER    I, J             ! Loop counters
      INTEGER    NUM_BLOCKS       ! Number of blocks in file
      INTEGER    BYTESPERPIX      ! Bytes per pixel
      INTEGER    PIXREAD          ! Number of pixels read so far
      INTEGER    OFFSET           ! Byte offset in file to read from
      INTEGER    REMAINDER        ! Number of remaining bytes after
C                                   all blocks have been read
C
C     Open file
C
      IF (OPEN_BIN_FILE('example.xxx') .NE. 0) GOTO 901
C
C     Little-endian file. If the machine has a different endian than
C     the file then set SWAP to .TRUE.
C
      BIG_END = .FALSE.
      SWAP = (BIGEND_CPU() .XOR. BIG_END)
C     
C     Start at pixel zero. The data starts at byte 4100.
C     Read data in blocks of BLOCKSZ bytes.
C     
      BYTESPERPIX = 2
      PIXREAD     = 0
      OFFSET      = 4100
      NUM_BLOCKS  = NUM_PIX*BYTESPERPIX/BLOCKSZ
      DO I = 1, NUM_BLOCKS
C
         IF (READ_BIN_FILE(RAWDATA, OFFSET, 
     $                     (OFFSET+BLOCKSZ-1)) .NE. 0) 
     $        GOTO 902
C
         DO J = 1, BLOCKSZ, BYTESPERPIX
            PIXREAD = PIXREAD + 1
            DATA(PIXREAD) = READ_BIN_SHORT(RAWDATA(J))
         ENDDO
         OFFSET = OFFSET + BLOCKSZ
      ENDDO
C
C     Read the remainder of the data if any
C
      REMAINDER = MOD((NUM_PIX*BYTESPERPIX), BLOCKSZ)
      IF (REMAINDER .NE. 0) THEN
         IF (READ_BIN_FILE(RAWDATA, OFFSET, 
     $                     (OFFSET+REMAINDER-1)) .NE. 0) 
     $        GOTO 902
C
         DO J = 1, REMAINDER, BYTESPERPIX
            PIXREAD = PIXREAD + 1
            DATA(PIXREAD) = READ_BIN_SHORT(RAWDATA(J))
         ENDDO
      ENDIF
C
C     Close file
C
      CALL CLOSE_BIN_FILE()
C
C     Analysis can now be carried out on the data in DATA.
C
      RETURN
C
C     Error traps
C
 901  WRITE(*,*) '** Error opening file!'
      RETURN
 902  WRITE(*,*) '** Error reading file!'
      RETURN
      END
C

