;+
; NAME:
;  Read_IPLAB
; 
; PURPOSE:
;  This function returns an IPLAB format image. Overlays and 
;    registration marks cannot be read.  Overlays are in PICT2
;    vector format.   See Reference  below.
; 
; CALLING SEQUENCE:
;  Result = READ_IPLAB(file)
;
; ARGUMENTS:
;  File   Set this argument equal to the full file/path for the
;    IPLAB format image.  
;    If not provided, the user is prompted by Dialog_PickFile
;    If the user cancels, then -1 is returned.
;
; KEYWORDS:
;   HDR  Set this keyword equal to a named variable to return
;      the file header structure which describes the file contents.
;      Below is a sample of a header structure showing the field values.
;      Note that even though there is an OVERLAYINFILE it is not read.
;
;  VERSION         STRING    '3.1a'
;	IPLABFORMAT     BYTE         0
;	DATATYPE        BYTE         0
;	WIDTH           LONG               640
;	HEIGHT          LONG               480
;	RESERVED1       BYTE         0
;	OVERLAYINFILE   BYTE         1
;	RESERVED2       LONG          16842753
;	NFRAMES         INT              5
;	RESERVED3       BYTE      Array[25]
;	NREGMARKS       BYTE         0
;	RESERVED4       BYTE      Array[24]
;	CLUT            BYTE      Array[2048]
; 
; ALPHA  Set this keyword to return a 32 bit image with the
;	first image plane [0] equal to the alpha channel.
;
; REFERENCE:
;   IPLAB Spectrum,  Scientific Imaging Software for the MacIntosh.
;   Signal Analytics Corporation, 1996, User's Guide, v 3.1 , rev. 5.8.
; 
; REQUIREMENTS:
;		QUERY_IPLAB is called.
;  
; MODIFICATION HISTORY:
;  Written by Ben Tuppper, Fall, 1999 
;   Bigelow Lab for Ocean Sciences
;   tupper@seadas.bigelow.org
;   pemaquidriver@tidewater.net
;
;  Added Documentation and cleaned up.  29JUNE2000 BT
;  Changed BytesPerPixel to 1L for a Integer Image 
;  Added call to QUERY_IPLAB. 27JULY2000 BT
;-

Function Read_IPLab, File , $
	Alpha = Alpha, Hdr = Hdr, Group = group
	
	;make sure that the type conforms to IPLAB image data, get the header
Flag = QUERY_IPLAB(File, HDR = HDR, Group = Group)

If Flag LT 1 Then Return, -1

    ;prep the image aray
Case Hdr.DataType of
	0: 	Begin
	    	BytesPerPixel = 1L	;byte
	    	Type = 1
		END	

	1: 	BEGIN
		BytesPerPixel = 1L	;int
		Type = 2
		END	

	2: 	BEGIN
		BytesPerPixel = 4L	;long
		Type = 3
		END

	3:	BEGIN
		BytesPerPixel = 4L	;Float
		Type = 4
		END

	4:	BEGIN
		BytesPerPixel = 2L	;16 bit
		Type = 1
		END

	5:	BEGIN
		BytesPerPixel = 4L	;24 bit
		Type = 1
		END

	6:	BEGIN
		BytesPerPixel = 2L	; 16 bit unsigned
		Type = 1
		END

EndCase

Image= Make_Array( Hdr.Width* Hdr.Height*BytesPerPixel*Hdr.nFrames,Type = Type)

   ;open the file
OpenR,U,File,/get_lun, /Binary

	;advance the file pointer past the header info
Point_LUN, U, 2120    

   ;read the image
ReadU,u, Image

   ; clean the image formatting
If Hdr.nFrames EQ 1 Then Begin

 Case 1 of 
  BytesPerPixel EQ 1 : Image = Reform(Image,Hdr.Width,Hdr.Height) 
  BytesPerPixel GT 1 : Image = Reform(Image, BytesPerPixel,Hdr.Width,Hdr.Height) 
 EndCase

EndIf Else Image = Reform(Image,  Hdr.Width, Hdr.Height,Hdr.nFrames)

   ;check for alpha
If Not(KeyWord_Set(Alpha)) Then $
  If BytesPerPixel GT 1 Then Image = Image[1:3,*,*]

Free_LUN,u

Return, Image	
End

