/**
  * Zooms out by making the source rectangle (srcRect) larger and centering it on (x,y). If we
  * can't make it larger, then make the window smaller.
  */
 public void zoomOut(int x, int y) {
   if (magnification <= 0.03125) return;
   double oldMag = magnification;
   double newMag = getLowerZoomLevel(magnification);
   double srcRatio = (double) srcRect.width / srcRect.height;
   double imageRatio = (double) imageWidth / imageHeight;
   double initialMag = imp.getWindow().getInitialMagnification();
   if (Math.abs(srcRatio - imageRatio) > 0.05) {
     double scale = oldMag / newMag;
     int newSrcWidth = (int) Math.round(srcRect.width * scale);
     int newSrcHeight = (int) Math.round(srcRect.height * scale);
     if (newSrcWidth > imageWidth) newSrcWidth = imageWidth;
     if (newSrcHeight > imageHeight) newSrcHeight = imageHeight;
     int newSrcX = srcRect.x - (newSrcWidth - srcRect.width) / 2;
     int newSrcY = srcRect.y - (newSrcHeight - srcRect.height) / 2;
     if (newSrcX < 0) newSrcX = 0;
     if (newSrcY < 0) newSrcY = 0;
     srcRect = new Rectangle(newSrcX, newSrcY, newSrcWidth, newSrcHeight);
     // IJ.log(newMag+" "+srcRect+" "+dstWidth+" "+dstHeight);
     int newDstWidth = (int) (srcRect.width * newMag);
     int newDstHeight = (int) (srcRect.height * newMag);
     setMagnification(newMag);
     setMaxBounds();
     // IJ.log(newDstWidth+" "+dstWidth+" "+newDstHeight+" "+dstHeight);
     if (newDstWidth < dstWidth || newDstHeight < dstHeight) {
       // IJ.log("pack");
       setDrawingSize(newDstWidth, newDstHeight);
       imp.getWindow().pack();
     } else repaint();
     return;
   }
   if (imageWidth * newMag > dstWidth) {
     int w = (int) Math.round(dstWidth / newMag);
     if (w * newMag < dstWidth) w++;
     int h = (int) Math.round(dstHeight / newMag);
     if (h * newMag < dstHeight) h++;
     x = offScreenX(x);
     y = offScreenY(y);
     Rectangle r = new Rectangle(x - w / 2, y - h / 2, w, h);
     if (r.x < 0) r.x = 0;
     if (r.y < 0) r.y = 0;
     if (r.x + w > imageWidth) r.x = imageWidth - w;
     if (r.y + h > imageHeight) r.y = imageHeight - h;
     srcRect = r;
   } else {
     srcRect = new Rectangle(0, 0, imageWidth, imageHeight);
     setDrawingSize((int) (imageWidth * newMag), (int) (imageHeight * newMag));
     // setDrawingSize(dstWidth/2, dstHeight/2);
     imp.getWindow().pack();
   }
   // IJ.write(newMag + " " + srcRect.x+" "+srcRect.y+" "+srcRect.width+" "+srcRect.height+"
   // "+dstWidth + " " + dstHeight);
   setMagnification(newMag);
   // IJ.write(srcRect.x + " " + srcRect.width + " " + dstWidth);
   setMaxBounds();
   repaint();
 }
 /** Enlarge the canvas if the user enlarges the window. */
 void resizeCanvas(int width, int height) {
   ImageWindow win = imp.getWindow();
   // IJ.log("resizeCanvas: "+srcRect+" "+imageWidth+"  "+imageHeight+" "+width+"  "+height+"
   // "+dstWidth+"  "+dstHeight+" "+win.maxBounds);
   if (!maxBoundsReset
       && (width > dstWidth || height > dstHeight)
       && win != null
       && win.maxBounds != null
       && width != win.maxBounds.width - 10) {
     if (resetMaxBoundsCount != 0)
       resetMaxBounds(); // Works around problem that prevented window from being larger than
                         // maximized size
     resetMaxBoundsCount++;
   }
   if (IJ.altKeyDown()) {
     fitToWindow();
     return;
   }
   if (srcRect.width < imageWidth || srcRect.height < imageHeight) {
     if (width > imageWidth * magnification) width = (int) (imageWidth * magnification);
     if (height > imageHeight * magnification) height = (int) (imageHeight * magnification);
     setDrawingSize(width, height);
     srcRect.width = (int) (dstWidth / magnification);
     srcRect.height = (int) (dstHeight / magnification);
     if ((srcRect.x + srcRect.width) > imageWidth) srcRect.x = imageWidth - srcRect.width;
     if ((srcRect.y + srcRect.height) > imageHeight) srcRect.y = imageHeight - srcRect.height;
     repaint();
   }
   // IJ.log("resizeCanvas2: "+srcRect+" "+dstWidth+"  "+dstHeight+" "+width+"  "+height);
 }
 void adjustSourceRect(double newMag, int x, int y) {
   // IJ.log("adjustSourceRect1: "+newMag+" "+dstWidth+"  "+dstHeight);
   int w = (int) Math.round(dstWidth / newMag);
   if (w * newMag < dstWidth) w++;
   int h = (int) Math.round(dstHeight / newMag);
   if (h * newMag < dstHeight) h++;
   x = offScreenX(x);
   y = offScreenY(y);
   Rectangle r = new Rectangle(x - w / 2, y - h / 2, w, h);
   if (r.x < 0) r.x = 0;
   if (r.y < 0) r.y = 0;
   if (r.x + w > imageWidth) r.x = imageWidth - w;
   if (r.y + h > imageHeight) r.y = imageHeight - h;
   srcRect = r;
   setMagnification(newMag);
   // IJ.log("adjustSourceRect2: "+srcRect+" "+dstWidth+"  "+dstHeight);
 }
 protected void scroll(int sx, int sy) {
   int ox = xSrcStart + (int) (sx / magnification); // convert to offscreen coordinates
   int oy = ySrcStart + (int) (sy / magnification);
   // IJ.log("scroll: "+ox+" "+oy+" "+xMouseStart+" "+yMouseStart);
   int newx = xSrcStart + (xMouseStart - ox);
   int newy = ySrcStart + (yMouseStart - oy);
   if (newx < 0) newx = 0;
   if (newy < 0) newy = 0;
   if ((newx + srcRect.width) > imageWidth) newx = imageWidth - srcRect.width;
   if ((newy + srcRect.height) > imageHeight) newy = imageHeight - srcRect.height;
   srcRect.x = newx;
   srcRect.y = newy;
   // IJ.log(sx+"  "+sy+"  "+newx+"  "+newy+"  "+srcRect);
   imp.draw();
   Thread.yield();
 }