Viewing contents of file '../idllib/uit/pro/czoom.pro'
pro czoom, inc = inc, interp = interp
;+
; NAME:	
;	CZOOM
; PURPOSE:
;	Display part of an image (or graphics) from the current window
;	expanded in another window.
;	The cursor is used to mark the center of the zoom.
;
; CALLING SEQUENCE:
;	Czoom, [INC = , /INTERP ]
;
; INPUTS:
;	None - All input parameters are keywords.
;
; KEYWORD INPUTS:
;	Inc = zoom expansion factor, default = 2, maximum = 20.
;	Interp = 1 or set to interpolate, otherwise pixel replication is used.
;
; OUTPUTS:
;	No explicit outputs. 
;
; COMMON BLOCKS:
;	Current image plane read from common block TV
;	Left hand corner of image and window size read from common block IMAGES
;
; SIDE EFFECTS:
;	A window is created.
;	A pixmap window is created and destroyed.
;
; RESTRICTIONS:
;	Only works with color systems.
;	Maximum size of zoom window is 512x512
;
; PROCEDURE:
;	Left mouse button decrements zoom by inc,
;	Middle mouse button increments zoom by inc,
;	Right button exits.
;	Zoom window is always active except before first zoom and after
;	total unzoom.
;
; MODIFICATION HISTORY:
;	Written by K. Bhagat, ST Systems Corp., 	August 1990
;	Revised	   J. D. Offenberg,       STX.,		June 1991
;       Added kluge for case where offset applied to original image
;                                                 W.Landsman   July, 1992
;-
;
COMMON IMAGES, x00,y00,xsize,ysize
COMMON TV, chan,zoom,xroam,yroam
;
on_error,2              ;Return to caller if an error occurs
;
X_OrigSize = !D.X_size
Y_OrigSize = !D.Y_size

if N_elements(inc) le 0 then inc=2 
if inc gt 20 then begin
	message,'Zoom increment too large, truncated to 20.', /inform
	inc = 20
   end
if inc lt 2 then begin
	message,'Zoom increment too small, has been reset to 2.', /inform
	inc = 2
   end
;
zms = 0			;count number of zooms
orig_w = chan 		;original image window
print,'Please wait, reading original image...' 
a = tvrd(0,0,x_OrigSize,y_OrigSize,chan)	;read original window
;
;set up pixmap of original window
;
window, /free,xsize=X_OrigSize,ysize=Y_OrigSize,retain=2, /pixmap
wbox = !d.window
device,copy = [0,0,X_OrigSize,y_origSize,0,0,orig_w]
wset,orig_w
;
;find available zoom window 
;
device,window_state=opn
for i=0,15 do if opn(i) eq 0 then zoom_w = i
zoom(zoom_w) = zoom(orig_w)
;
tvcrs,1			;enable cursor
print,'If zoom window is shown, it is active, otherwise original window'
print,'is active.'
print,'Middle button to zoom, Left to unzoom, Right to exit.'

if ( zoom(orig_w) EQ 1) and ( (x00(orig_w) LT 0) or (y00(orig_w) LT 0) ) $
       then begin         ;Kluge to cover case of image offset
       x0save = x00(orig_w)   & y0save = y00(orig_w)
       x00(orig_w)=0   & y00(orig_w)=0
       im_offset = 1
endif else im_offset = 0
;
again:
	cursor,x,y,3,/dev	;Wait for user to click mouse button
	case !err of
4:	goto, done
2: begin					;Zoom
	zms = zms + 1
	xsize(zoom_w) = 512<x_OrigSize
	ysize(zoom_w) = 512<Y_origSize
	if zms eq 1 then begin			;Translate cursor position
		x = x - x00(!d.window) 		;to original window, and
       		y = y - y00(!d.window)		;increment zoom counter,	
		zoom(zoom_w) = inc		;depending on zms
	endif else begin   
		x = x/(inc*(zms-1)) - x00(!d.window)
        	y = y/(inc*(zms-1)) - y00(!d.window)
		zoom(zoom_w) = zoom(zoom_w)+inc
	endelse
   end
1: begin					;Unzoom		
        if zms eq 0 then goto,nouz              ;cannot unzoom original image
        if zms eq 1 then begin
                wdelete,zoom_w                  ;delete zoom window, make
                wset,orig_w			;original image current window
                chan = orig_w
                zms = 0
                zoom(zoom_w) = zoom(orig_w)
                goto,again
        endif
        x = x/(inc*zms) - x00(!d.window)	;Translate cursor position
        y = y/(inc*zms) - y00(!d.window)	;to original window
	zms = zms - 1				;Decrement zoom counters
	zoom(zoom_w) = zoom(zoom_w)-inc
   end
endcase
p = xsize(Zoom_w)/(inc*zms*2)			;maximum xsize of zoom area
q = ysize(zoom_w)/(inc*zms*2)			;maximum ysize of zoom area
x0 = 0>(x-p)					;actual bottom left hand-
y0 = 0>(y-q)					;corner of zoom area
if (x lt p) then lft = (p-x) else lft = 0	;theoretical bottom left-
if (y lt q) then btm = (q-y) else btm = 0	;hand corner of zoom area
nx = (x_Origsize-1-x0)<(2*p-lft)		;actual xsize of zoom area
ny = (y_Origsize-1-y0)<(2*q-btm)		;actual ysize of zoom area
rt = nx+lft-1					;translated xsize of zoom area
top = ny+btm-1					;translated ysize of zoom area
b = a(x0:x0+nx-1, y0:y0+ny-1)                   ;extract zoom area
bim = bytarr(2*p,2*q) 				;make sure zoom center is
bim(lft:rt,btm:top) = b				;at center of zoomed image
;
;box zoom area on original image
;	
wset,orig_w
device, copy=[0,0,x_Origsize,y_Origsize,0,0,wbox]
plots,[x0,x0+nx],[y0,y0],/dev	
plots,[x0,x0+nx],[y0+ny,y0+ny],/dev
plots,[x0,x0],[y0,y0+ny],/dev
plots,[x0+nx,x0+nx],[y0,y0+ny],/dev
;	
 ;open zoom window
;
ttl = 'IDL ' + strtrim(zoom_w,2) + '   Zoomed Image #' + strtrim(zms,2) + $
      '   Zoom Factor: ' + strtrim(inc*zms,2)      
device,window_state=opn
if opn(zoom_w) then wset,zoom_w else $
   window,zoom_w,xsize=xsize(zoom_w),ysize=ysize(zoom_w), $
              title = ttl 

;
;put zoomed image in new window
;
xss = p*(inc*zms*2)
yss = q*(inc*zms*2)
tv,rebin(bim,xss,yss,sample=1-keyword_set(interp))
;
;update common block
;
if (lft eq 0) then x00(zoom_w) = -x0 else x00(zoom_w) = -(x0-lft-1)
if (btm eq 0) then y00(zoom_w) = -y0 else y00(zoom_w) = -(y0-btm-1)
chan = zoom_w
goto,again
;
nouz:
message,string(7b) + 'Cannot unzoom original image!',/inform
chan = orig_w
goto,again
;
done:
if im_offset EQ 1 then begin
       x00(orig_w)=x0save   & y00(orig_w)=y0save
       x00(zoom_w) = x00(zoom_w) + x0save
       y00(zoom_w) = y00(zoom_w) + y0save
       im_offset = 1
endif else im_offset = 0

w_save = !D.WINDOW
wset,orig_w
device, copy=[0,0,x_Origsize,y_origsize,0,0,wbox]
wset,w_save
wdelete,wbox			;delete pixmap of original window
return
end