PRO DefineZone, image, xAxis, yAxis, xValues, yValues, $
	XPIXELS = xRange, YPIXELS = yRange, $
	TEXT = text, SIZES_ON = sizes_on, $
	WINNR = winNr

  IF !d.name NE 'X' THEN BEGIN
    Message, 'This procedure needs an x-windows terminal', $
	/INFO, /CONT
  ENDIF
  IF Keyword_Set(WINNR) THEN BEGIN
    oldWin = !d.window
    !d.window = winNr
  ENDIF
  imgWinNr = !d.window
  IF imgWinNr EQ -1 THEN BEGIN
    Message, 'No window is open', /INFO, /CONT
    RETURN
  ENDIF

  nx = N_Elements( image( *, 0 ) )
  ny = N_Elements( image( 0, * ) )
  nPar = N_Params()
  CASE nPar OF
    1: BEGIN 
           xAxis=LIndGen(nx)
           yAxis=LIndGen(ny)
         END
    2: xAxis = LIndGen(nx)
    3:
    ELSE: IF (nPar GT 5) OR (nPar LT 1) THEN BEGIN
          Message, 'Usage: DefineZone, image ' $
	+ '[, xAxis, yAxis [, xValues, yValues ] ]', /INFO, /CONT
          RETURN
        ENDIF
  ENDCASE

  IF NOT Keyword_Set( TEXT ) THEN $
    text = 'Click and drag the mouse to define the region'

  GetImageSizes, xPos, yPos, width, height
  hms = GetHMS()
  GetBreaks, xBreaks, yBreaks
  GetOffsets, d, offset

  win_orig = !d.window

  xSize = !d.x_vSize
  ySize = !d.y_vSize
; Create a pixmap copy of the window
    window, /free, XSIZE=xSize, YSIZE=ySize, /PIXMAP
    win_copy = !d.window
    device,copy=[0,0,xsize,ysize,0,0,win_orig]

; Get the beginning point (xb,yb)
    msgWinNr = WMesg( $
                text, 'Define image region',100,600)

; Set the displayed window as current
    wset,win_orig

    cursor, x, y, /device, /down
    xb = x
    yb = y
    xe = x
    ye = y

; Loop until user lets up on button
    while !err gt 0 do begin

; If position changes, update display
      while xe ne x and ye ne y do begin

; Calculate lower left and upper right points for last line drawn
; (device,copy requires the coordinates to be in this order)
        x1 = min([xb,xe])
        y1 = min([yb,ye])
        x2 = abs(xe-xb)
        y2 = abs(ye-yb)

; If length > 0, restore that part of the image from the pixmap
        if x2 gt 0 and y2 gt 0 then begin
          device,copy=[x1-5,y1-5,x2+10,y2+10,x1-5,y1-5,win_copy]
        endif

; Draw the new line and update the endpoint
        plots, [xb,xb], [yb,y], /device, color=255
	plots, [x,x],[yb,y],/device,color=255
	plots, [xb,x],[y,y],/device,color=255
	plots, [xb,x],[yb,yb],/device,color=255
        empty
        xe = x
        ye = y

      endwhile 

; Check cursor
      cursor, x, y, /device, /nowait
    endwhile

    WDelete, msgWinNr
    x1 = DevToPix( xb,  xPos, width, xBreaks, offset, nx )
    x2 = DevToPix( xe,  xPos, width, xBreaks, offset, nx )
    y1 = DevToPix( yb,  yPos, height, yBreaks, offset, ny )
    y2 = DevToPix( ye,  yPos, height, yBreaks, offset, ny )
    xValues = [ xAxis(x1), xAxis(x2) ]
    yValues = [ yAxis(y1), yAxis(y2) ]
    xRange = [x1,x2]
    yRange = [y1, y2]
    IF Keyword_Set(WINNR) THEN !d.window = oldWin

END
