Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Resizer.java
1 package ij.plugin.filter;
2 import ij.*;
3 import ij.gui.*;
4 import ij.process.*;
5 import ij.measure.*;
6 import java.awt.*;
7 
9 public class Resizer implements PlugInFilter {
10  ImagePlus imp;
11  private boolean crop;
12  private static int newWidth = 100;
13  private static int newHeight = 100;
14  private static boolean constrain = true;
15  private static boolean interpolate = true;
16 
17  public int setup(String arg, ImagePlus imp) {
18  crop = arg.equals("crop");
19  this.imp = imp;
20  IJ.register(Resizer.class);
21  if (crop)
23  else
24  return DOES_ALL+NO_CHANGES;
25  }
26 
27  public void run(ImageProcessor ip) {
28  Roi roi = imp.getRoi();
29  if (roi!=null && roi.getType()>=Roi.LINE && roi.getType()<=Roi.FREELINE) {
30  IJ.error("The Crop and Adjust->Size commands\ndo not work with line selections.");
31  return;
32  }
33  boolean sizeToHeight=false;
34  if (crop) {
35  Rectangle bounds = roi.getBounds();
36  newWidth = bounds.width;
37  newHeight = bounds.height;
38  interpolate = false;
39  } else {
40  GenericDialog gd = new GenericDialog("Resize", IJ.getInstance());
41  gd.addNumericField("Width (pixels):", newWidth, 0);
42  gd.addNumericField("Height (pixels):", newHeight, 0);
43  gd.addCheckbox("Constrain Aspect Ratio", constrain);
44  gd.addCheckbox("Interpolate", interpolate);
45  gd.addMessage("NOTE: Undo is not available");
46  gd.showDialog();
47  if (gd.wasCanceled())
48  return;
49  newWidth = (int)gd.getNextNumber();
50  newHeight = (int)gd.getNextNumber();
51  if (gd.invalidNumber()) {
52  IJ.error("Width or height are invalid.");
53  return;
54  }
55  constrain = gd.getNextBoolean();
56  interpolate = gd.getNextBoolean();
57  sizeToHeight = constrain && newWidth==0;
58  if (newWidth<=0.0 && !constrain) newWidth = 50;
59  if (newHeight<=0.0) newHeight = 50;
60  }
61 
62  Rectangle r = ip.getRoi();
63  double oldWidth = r.width;;
64  double oldHeight = r.height;
65  if (!crop && constrain) {
66  if (sizeToHeight)
67  newWidth = (int)(newHeight*(oldWidth/oldHeight));
68  else
69  newHeight = (int)(newWidth*(oldHeight/oldWidth));
70  }
71  ip.setInterpolate(interpolate);
72 
73  try {
74  ip = ip.resize(newWidth, newHeight);
75  ImagePlus imp2 = new ImagePlus(imp.getTitle(), ip);
76  imp2.show();
77 
78  } catch(OutOfMemoryError o) {
79  IJ.outOfMemory("Resize");
80  }
81  }
82 
83 }