Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
NewImage.java
1 package ij.gui;
2 
3 import java.awt.*;
4 import java.awt.image.*;
5 import java.io.*;
6 import java.awt.event.*;
7 import java.util.*;
8 import ij.*;
9 import ij.process.*;
10 
11 
13 public class NewImage {
14 
15  public static final int GRAY8=0, GRAY16=1, GRAY32=2, RGB=3;
16  public static final int FILL_WHITE=0, FILL_BLACK=1, FILL_RAMP=2;
17 
18  static final String NAME = "new.name";
19  static final String TYPE = "new.type";
20  static final String FILL = "new.fill";
21  static final String WIDTH = "new.width";
22  static final String HEIGHT = "new.height";
23  static final String SLICES = "new.slices";
24 
25  private static String name = Prefs.getString(NAME, "Untitled");
26  private static int width = Prefs.getInt(WIDTH, 400);
27  private static int height = Prefs.getInt(HEIGHT, 400);
28  private static int slices = Prefs.getInt(SLICES, 1);
29  private static int type = Prefs.getInt(TYPE, GRAY8);
30  private static int fillWith = Prefs.getInt(FILL, FILL_WHITE);
31  private static String[] types = {"8-bit", "16-bit", "32-bit", "RGB"};
32  private static String[] fill = {"White", "Black", "Ramp", "Clipboard"};
33 
34 
35  public NewImage() {
36  openImage();
37  }
38 
39  static ImagePlus createImagePlus() {
40  //ImagePlus imp = IJ.getInstance().getImagePlus();
41  //if (imp!=null)
42  // return imp.createImagePlus();
43  //else
44  return new ImagePlus();
45  }
46 
47  public static ImagePlus createByteImage(String title, int width, int height, int slices, int fill) {
48  byte[] pixels = new byte[width*height];
49  switch (fill) {
50  case FILL_WHITE:
51  for (int i=0; i<width*height; i++)
52  pixels[i] = (byte)255;
53  break;
54  case FILL_BLACK:
55  break;
56  case FILL_RAMP:
57  byte[] ramp = new byte[width];
58  for (int i=0; i<width; i++)
59  ramp[i] = (byte)(((i*256.0)/width)+0.5);
60  int offset;
61  for (int y=0; y<height; y++) {
62  offset = y*width;
63  for (int x=0; x<width; x++)
64  pixels[offset++] = ramp[x];
65  }
66  break;
67  }
68  ImageProcessor ip = new ByteProcessor(width, height, pixels, null);
69  ImagePlus imp = createImagePlus();
70  imp.setProcessor(title, ip);
71  return imp;
72  }
73 
74  public static ImagePlus createRGBImage(String title, int width, int height, int slices, int fill) {
75  int[] pixels = new int[width*height];
76  switch (fill) {
77  case FILL_WHITE:
78  for (int i=0; i<width*height; i++)
79  pixels[i] = -1;
80  break;
81  case FILL_BLACK:
82  for (int i=0; i<width*height; i++)
83  pixels[i] = 0xff000000;
84  break;
85  case FILL_RAMP:
86  int r,g,b,offset;
87  int[] ramp = new int[width];
88  for (int i=0; i<width; i++) {
89  r = g = b = (byte)(((i*256.0)/width)+0.5);
90  ramp[i] = 0xff000000 | ((r<<16)&0xff0000) | ((g<<8)&0xff00) | (b&0xff);
91  }
92  for (int y=0; y<height; y++) {
93  offset = y*width;
94  for (int x=0; x<width; x++)
95  pixels[offset++] = ramp[x];
96  }
97  break;
98  }
99  ImageProcessor ip = new ColorProcessor(width, height, pixels);
100  ImagePlus imp = createImagePlus();
101  imp.setProcessor(title, ip);
102  return imp;
103  }
104 
106  public static ImagePlus createShortImage(String title, int width, int height, int slices, int fill) {
107  short[] pixels = new short[width*height];
108  switch (fill) {
109  case FILL_WHITE: case FILL_BLACK:
110  break;
111  case FILL_RAMP:
112  short[] ramp = new short[width];
113  for (int i=0; i<width; i++)
114  ramp[i] = (short)(((i*65536.0)/width)+0.5);
115  int offset;
116  for (int y=0; y<height; y++) {
117  offset = y*width;
118  for (int x=0; x<width; x++)
119  pixels[offset++] = ramp[x];
120  }
121  break;
122  }
123  ImageProcessor ip = new ShortProcessor(width, height, pixels, null);
124  ImagePlus imp = createImagePlus();
125  imp.setProcessor(title, ip);
126  return imp;
127  }
128 
130  public static ImagePlus createUnsignedShortImage(String title, int width, int height, int slices, int fill) {
131  return createShortImage(title, width, height, slices, fill);
132  }
133 
134  public static ImagePlus createFloatImage(String title, int width, int height, int slices, int fill) {
135  float[] pixels = new float[width*height];
136  switch (fill) {
137  case FILL_WHITE: case FILL_BLACK:
138  break;
139  case FILL_RAMP:
140  float[] ramp = new float[width];
141  for (int i=0; i<width; i++)
142  ramp[i] = (float)((i*1.0)/width);
143  int offset;
144  for (int y=0; y<height; y++) {
145  offset = y*width;
146  for (int x=0; x<width; x++)
147  pixels[offset++] = ramp[x];
148  }
149  break;
150  }
151  ImageProcessor ip = new FloatProcessor(width, height, pixels, null);
152  ImagePlus imp = createImagePlus();
153  imp.setProcessor(title, ip);
154  return imp;
155  }
156 
157  public static void open(String title, int width, int height, int nSlices, int type, int fill) {
158  ImagePlus imp = null;
159  switch (type) {
160  case GRAY8:
161  imp = createByteImage(title, width, height, nSlices, fill);
162  break;
163  case GRAY16:
164  imp = createShortImage(title, width, height, nSlices, fill);
165  break;
166  case GRAY32:
167  imp = createFloatImage(title, width, height, nSlices, fill);
168  break;
169  case RGB:
170  imp = createRGBImage(title, width, height, nSlices, fill);
171  break;
172  }
173  if (imp!=null)
174  imp.show();
175  }
176 
177  void showClipboard() {
178  ImagePlus clipboard = ImageCanvas.getClipboard();
179  if (clipboard!=null)
180  clipboard.show();
181  else
182  IJ.error("The clipboard is empty.");
183  }
184 
185  boolean showDialog() {
186  if (type<GRAY8|| type>RGB)
187  type = GRAY8;
188  if (fillWith<FILL_WHITE||fillWith>FILL_RAMP)
189  fillWith = FILL_WHITE;
190  GenericDialog gd = new GenericDialog("New...", IJ.getInstance());
191  gd.addStringField("Name:", name, 12);
192  gd.addChoice("Type:", types, types[type]);
193  gd.addChoice("Fill With:", fill, fill[fillWith]);
194  gd.addNumericField("Width:", width, 0, 5, "pixels");
195  gd.addNumericField("Height:", height, 0, 5, "pixels");
196  gd.addNumericField("Slices:", slices, 0, 5, "");
197  gd.showDialog();
198  if (gd.wasCanceled())
199  return false;
200  name = gd.getNextString();
201  String s = gd.getNextChoice();
202  if (s.startsWith("8"))
203  type = GRAY8;
204  else if (s.startsWith("16"))
205  type = GRAY16;
206  else if (s.endsWith("RGB") || s.endsWith("rgb"))
207  type = RGB;
208  else
209  type = GRAY32;
210  fillWith = gd.getNextChoiceIndex();
211  width = (int)gd.getNextNumber();
212  height = (int)gd.getNextNumber();
213  slices = (int)gd.getNextNumber();
214  return true;
215  }
216 
217  void openImage() {
218  if (!showDialog())
219  return;
220  if (fillWith>FILL_RAMP)
221  {showClipboard(); return;}
222  try {open(name, width, height, slices, type, fillWith);}
223  catch(OutOfMemoryError e) {IJ.outOfMemory("New...");}
224  }
225 
227  public static void savePreferences(Properties prefs) {
228  prefs.put(NAME, name);
229  prefs.put(TYPE, Integer.toString(type));
230  prefs.put(FILL, Integer.toString(fillWith));
231  prefs.put(WIDTH, Integer.toString(width));
232  prefs.put(HEIGHT, Integer.toString(height));
233  prefs.put(SLICES, Integer.toString(slices));
234  }
235 
236 }