pro plotimage, filename, centre, rayon, inclin, frame, LASER=laser, $
		CADRE=cadre, ANNOTATION=annotation, LIMB=limb, IMAGE=image, $
		NOERASE=noerase, SCALE=scale, message
;
; Plot an image of the Sun in a given field
;
; Parameters:
;		filename	Name of the file containing the image
;		centre		Array: (horiz., vert.) position of disk centre
;				[pixels]
;		rayon		Radius of the solar disk [pixels]
;		inclin		Inclination of the solar south-north direction
;				to the vertical dir. on the plot (deg, N->E->S)
;		frame		Array [x0,y0,x1,y1] of lower left [x0,y0] and
;				upper right [x1,y1] border of the field 
;				[solar radii]
;		limb		Overplot the solar limb on the image
;		image		Positive (1) or negative (2) image
;		scale		Plot using TV (0) or TVSCL (1)
;		laser		Output written on X and PS
;		message (output)	Error message
;
; MODIFICATION HISTORY: (bonmartin@obspm.fr)
;	13/04/99 (Ludwig.klein@obspm.fr):
;		ajout lecture de fichiers .jpg and .fts


plotphot, frame, CADRE=cadre, LIMB=limb, ANNOTATION=annotation, $
		/NOERASE

type = STRLOWCASE(nrh_fileext(filename))
			SZ = SIZE(FINDFILE(Filename))
		IF SZ(0) EQ 0 THEN BEGIN
		message='File ' + filename + ' does not exist'
		RETURN
ENDIF
 
IF type EQ 'png' THEN BEGIN
		read_png, filename, imtab, r, g, b
ENDIF ELSE IF type EQ 'jpg' THEN BEGIN
			read_jpeg, filename, imtab
ENDIF ELSE IF type EQ 'ras' THEN BEGIN
			read_srf, filename, imtab, r, g, b
ENDIF ELSE IF type EQ 'bmp' THEN BEGIN
			imtab = read_bmp(filename, r, g, b)
ENDIF ELSE IF type EQ 'fts' OR type EQ 'fits'  THEN BEGIN
			imtab = mrdfits(filename, 0, hdr)
ENDIF ELSE BEGIN
		message='File type ' + type + ' cannot be read'
		RETURN
ENDELSE
	
message='File read: ' + filename
	
; ---------------------------------------------------------------------------
; Rotation de l'image: puisque la procedure ROT produit un tableau de sortie
; ayant les memes dimensions que le tableau d'entree, il faut preparer l'image
; pour qu'il n'y ait pas de perte de points lors de la rotation. Nous evaluons
; les abscisses et ordonnees des coins du tableau apres rotation, creons un 
; nouveau tableau, test, suffisamment large pour qu'aucun point de l'image
; d'origine ne soit perdu.
;
; Il y a encore des problemes pour des angles eleves, mais <90 deg, et pour des
; angles negatifs (essayer alors 360 deg - angle)	21/2/98 KLK

IF inclin NE 0. THEN  BEGIN
	phirad = !DTOR*ABS(inclin)
	oldxdim = (SIZE(imtab))(1)
	oldydim = (SIZE(imtab))(2)
	xedge = FLOAT([oldxdim-centre(0),oldxdim-centre(0),-centre(0), -centre(0)])
	yedge = FLOAT([-centre(1),oldydim-centre(1),oldydim-centre(1), -centre(1)] )
	phi0 = FLTARR(4)
	FOR i=0,3 DO BEGIN
		IF xedge(i) GT 0. THEN phi0(i) = ATAN( yedge(i) / xedge(i) )
		IF xedge(i) LT 0. THEN phi0(i) = ATAN( yedge(i) / xedge(i) ) + !PI
		IF xedge(i) EQ 0. THEN BEGIN
		IF yedge(i) GT 0. THEN phi0(i) = 0.5*!PI ELSE $
				phi0(i) = 1.5*!PI
		ENDIF
	ENDFOR
	dist = SQRT( xedge^2. + yedge^2. )
	xedge = dist * COS( phi0 + phirad )
	yedge = dist * SIN( phi0 + phirad )
	maxx =  MAX(xedge, MIN=minx)
	maxy =  MAX(yedge, MIN=miny)
	minx = LONG(minx-1.) 
	maxx = LONG(maxx+1.)
	miny = LONG(miny-1.)
	maxy = LONG(maxy+1.)
	newcentre = -[minx, miny]
	newxdim = newcentre(0) + MAX([maxx, oldxdim])
	newydim = newcentre(1) + MAX([maxy, oldydim])
	offset = newcentre - centre
	index = WHERE(offset LT 0)
	IF (SIZE(index))(0) GT 0 THEN BEGIN
		newcentre(index) = newcentre(index) - offset(index)
		offset(index) = 0
	ENDIF
	centre = newcentre
	test = BYTARR(newxdim, newydim)
	test(offset(0):offset(0)+oldxdim-1,$
			offset(1):offset(1)+oldydim-1) = imtab
	imtab=ROT(test,-inclin, 1., newcentre(0), newcentre(1), $
			/PIVOT, MISSING=0, CUBIC=-0.5)
ENDIF
; ---------------------------------------------------------------------------

	IF KEYWORD_SET(image) THEN BEGIN
		IF image EQ 2 THEN BEGIN
			top = MAX(imtab)
			imtab = top - imtab 
		ENDIF
	ENDIF

	isz = SIZE(imtab)
	isz = [ 0, 0, isz(1:2)]
	npix = ROUND( [centre, centre] + frame*rayon)
	FOR i=2,3 DO IF npix(i) GE isz(i) THEN npix(i)=isz(i)-1
	FOR i=0,1 DO IF npix(i) LT 0 THEN npix(i) = 0
; Bords inferieur gauche et superieur droit du trace
	bordinf = FLOAT( npix(0:1)-centre ) / rayon
	bordsup = FLOAT( npix(2:3)-centre ) / rayon
	bordinf_d = CONVERT_COORD(bordinf, /DATA, /TO_DEVICE)
	bordsup_d = CONVERT_COORD(bordsup, /DATA, /TO_DEVICE)
	sx = bordsup_d - bordinf_d
; Trace sur ecran X:	
	IF npix(0) LT npix(2) AND npix(1) LT npix(3) THEN BEGIN
		IF KEYWORD_SET(scale) THEN $
			TVSCL, congrid(imtab(npix(0):npix(2),npix(1):npix(3)),$
				sx(0), sx(1)), bordinf_d(0), bordinf_d(1) $
		ELSE $
			TV, congrid(imtab(npix(0):npix(2),npix(1):npix(3)),$
				sx(0), sx(1)), bordinf_d(0), bordinf_d(1)
	ENDIF
	plotphot, frame, CADRE=cadre, LIMB=limb, ANNOTATION=annotation, $
		/NOERASE
; Trace POSTSCRIPT
	IF KEYWORD_SET(laser) THEN BEGIN		
		nrh1_postscript, /ON
		plotphot, frame, NOERASE=noerase
;		TVLCT, r, g, b
		IF KEYWORD_SET(scale) THEN $
			TVSCL, imtab(npix(0):npix(2),npix(1):npix(3)),$
				bordinf(0), bordinf(1), $
				xsize = bordsup(0)-bordinf(0),$
				ysize = bordsup(1)-bordinf(1), /DATA $
		ELSE $
			TV, imtab(npix(0):npix(2),npix(1):npix(3)),$
				bordinf(0), bordinf(1), $
				xsize = bordsup(0)-bordinf(0),$
				ysize = bordsup(1)-bordinf(1), /DATA
		plotphot, frame, CADRE=cadre, LIMB=limb, $
			ANNOTATION=annotation, /NOERASE
		nrh1_postscript, /OFF
	ENDIF
END
