/**
  * 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();
 }
 void drawRoiLabel(Graphics g, int index, Roi roi) {
   Rectangle r = roi.getBounds();
   int x = screenX(r.x);
   int y = screenY(r.y);
   double mag = getMagnification();
   int width = (int) (r.width * mag);
   int height = (int) (r.height * mag);
   int size = width > 40 && height > 40 ? 12 : 9;
   if (font != null) {
     g.setFont(font);
     size = font.getSize();
   } else if (size == 12) g.setFont(largeFont);
   else g.setFont(smallFont);
   boolean drawingList = index >= LIST_OFFSET;
   if (drawingList) index -= LIST_OFFSET;
   String label = "" + (index + 1);
   if (drawNames && roi.getName() != null) label = roi.getName();
   FontMetrics metrics = g.getFontMetrics();
   int w = metrics.stringWidth(label);
   x = x + width / 2 - w / 2;
   y = y + height / 2 + Math.max(size / 2, 6);
   int h = metrics.getAscent() + metrics.getDescent();
   if (bgColor != null) {
     g.setColor(bgColor);
     g.fillRoundRect(x - 1, y - h + 2, w + 1, h - 3, 5, 5);
   }
   if (!drawingList && labelRects != null && index < labelRects.length)
     labelRects[index] = new Rectangle(x - 1, y - h + 2, w + 1, h);
   g.setColor(labelColor);
   g.drawString(label, x, y - 2);
   g.setColor(defaultColor);
 }
 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);
 }
 public void fitToWindow() {
   ImageWindow win = imp.getWindow();
   if (win == null) return;
   Rectangle bounds = win.getBounds();
   Insets insets = win.getInsets();
   int sliderHeight = (win instanceof StackWindow) ? 20 : 0;
   double xmag = (double) (bounds.width - 10) / srcRect.width;
   double ymag = (double) (bounds.height - (10 + insets.top + sliderHeight)) / srcRect.height;
   setMagnification(Math.min(xmag, ymag));
   int width = (int) (imageWidth * magnification);
   int height = (int) (imageHeight * magnification);
   if (width == dstWidth && height == dstHeight) return;
   srcRect = new Rectangle(0, 0, imageWidth, imageHeight);
   setDrawingSize(width, height);
   getParent().doLayout();
 }