	PROGRAM SAODEMO7
C
C  Note:  This is the UNIX version.
C
C	This program demonstrates the control of SAOimage from a
C  standalone program.  Here we download a custom look-up table
C  and then send random rectangular areas which have colors corresponding
C  to increasing indexes in the look-up table.  There are two possible 
C  approaches to sending multiple areas.  We could repeatedly load an 
C  array with sub areas and then repeatedly load the entire array into
C  SAOimage.  This can be quite fast for a small frame buffer, e.g., the
C  simulated video in SAODEMO3, but becomes slow when the frame buffer is
C  large.  Another approach is to repeatedly load small areas into 
C  SAOimage with MODE=1 in SAOLOAD.  This is always slow but doesn't slow
C  down when large frame buffers are used.  This is the approach taken here,
C  but we have to in addition use dynamic memory since we are changing the
C  size of the area on each load.
C
C			Donald H. Gudehus, October 29, 1994
C			Version 1U, D. Gudehus, October 30, 1994
C			Modified code to work under UNIX
C			Version 2, D. Gudehus, August 29, 2006
C			Incorporated changes from saofacts.for and
C			added call to GET_ENDIAN
C
	INCLUDE 'saofacts.for'
C
	PARAMETER NCOLS=256,NROWS=256,NIMAGES=200
C
	CHARACTER*40 ARRAY_NAME
	INTEGER*4 BLUE(200),GREEN(200),RED(200),ADDRESS
C
	CALL GET_ENDIAN
C  Don't prompt the user for the size of the buffer, just use a big
C  enough one.
	IFB_SAO=12 !  Gives a 256 by 256 buffer
	CALL SAOSTART
	CALL SAOFIRST
C
C  Create a custom look-up table
	ISEED=12345
	DO I=1,200
	  RED(I)=RAN(ISEED)*255
	  GREEN(I)=RAN(ISEED)*255
	  BLUE(I)=RAN(ISEED)*255
	END DO
C  Make the lowest location black
	RED(1)=0
	GREEN(1)=0
	BLUE(1)=0
C  Now load it into SAOimage
	CALL SAO_LOAD_VLT(BLUE,GREEN,RED)
C
	BRIGHTNESS_MIN=0.0
	BRIGHTNESS_MAX=199.0
	ARRAY_NAME='Look-up Table Demonstration'
	CALL MESSAGE('We will load 1 patch for each color in the look-up'//
	1' table, i.e., 200')
	DO K=1,NIMAGES
C  Calculate where the images will be placed in the frame.  The 
C  coordinates below are the where the lower left of each image will be 
C  placed in the frame buffer.  The frame has (0,0) in the upper left;  
C  any image has (1,1) in the lower left.
	  IX=(NCOLS-1)*RAN(ISEED)
	  IY=(NROWS-1)*RAN(ISEED)
	  IWX=MIN(1.0*NCOLS-IX,MAX(1.0,20*RAN(ISEED)))
	  IWY=MIN(IY+1.0,MAX(1.0,20*RAN(ISEED)))
	  CALL DYNAMIC(ADDRESS,IWX*IWY,'  R4',.TRUE.)
	  CALL SAODEMO7_1(%VAL(ADDRESS),IWX,IWY,IX,IY,BRIGHTNESS_MIN,
	1 BRIGHTNESS_MAX,ARRAY_NAME,K)
	  CALL DYNAMIC(ADDRESS,IWX*IWY,'  R4',.FALSE.)
	END DO
	END
C
	SUBROUTINE SAODEMO7_1(ARRAY,IWX,IWY,IX,IY,BRIGHTNESS_MIN,
	1BRIGHTNESS_MAX,ARRAY_NAME,K)
C
	DIMENSION ARRAY(IWX,IWY)
	CHARACTER*40 ARRAY_NAME
C
	DO J=1,IWY
	  DO I=1,IWX
	    ARRAY(I,J)=K-1 !  Assumes that K does not go beyond allowable
			   !  range
	  END DO
	END DO
C  NPICTURE is set to 1.  We sacrifice easy cursor readback of 
C  coordinates with this approach however.
C  MODE is set to 1 to allow loading several pictures into frame
	CALL SAOLOAD(ARRAY,IWX,IWY,IX,IY,
	1BRIGHTNESS_MIN,BRIGHTNESS_MAX,ARRAY_NAME,1,1)
	RETURN
C
	END
