4 import java.awt.image.*;
6 import java.awt.event.*;
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;
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";
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"};
47 public static ImagePlus createByteImage(String title,
int width,
int height,
int slices,
int fill) {
48 byte[] pixels =
new byte[width*height];
51 for (
int i=0; i<width*height; i++)
52 pixels[i] = (byte)255;
57 byte[] ramp =
new byte[width];
58 for (
int i=0; i<width; i++)
59 ramp[i] = (byte)(((i*256.0)/width)+0.5);
61 for (
int y=0; y<height; y++) {
63 for (
int x=0; x<width; x++)
64 pixels[offset++] = ramp[x];
68 ImageProcessor ip =
new ByteProcessor(width, height, pixels, null);
74 public static ImagePlus createRGBImage(String title,
int width,
int height,
int slices,
int fill) {
75 int[] pixels =
new int[width*height];
78 for (
int i=0; i<width*height; i++)
82 for (
int i=0; i<width*height; i++)
83 pixels[i] = 0xff000000;
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);
92 for (
int y=0; y<height; y++) {
94 for (
int x=0; x<width; x++)
95 pixels[offset++] = ramp[x];
107 short[] pixels =
new short[width*height];
109 case FILL_WHITE:
case FILL_BLACK:
112 short[] ramp =
new short[width];
113 for (
int i=0; i<width; i++)
114 ramp[i] = (
short)(((i*65536.0)/width)+0.5);
116 for (
int y=0; y<height; y++) {
118 for (
int x=0; x<width; x++)
119 pixels[offset++] = ramp[x];
123 ImageProcessor ip =
new ShortProcessor(width, height, pixels, null);
134 public static ImagePlus createFloatImage(String title,
int width,
int height,
int slices,
int fill) {
135 float[] pixels =
new float[width*height];
137 case FILL_WHITE:
case FILL_BLACK:
140 float[] ramp =
new float[width];
141 for (
int i=0; i<width; i++)
142 ramp[i] = (
float)((i*1.0)/width);
144 for (
int y=0; y<height; y++) {
146 for (
int x=0; x<width; x++)
147 pixels[offset++] = ramp[x];
151 ImageProcessor ip =
new FloatProcessor(width, height, pixels, null);
152 ImagePlus imp = createImagePlus();
153 imp.setProcessor(title, ip);
157 public static void open(String title,
int width,
int height,
int nSlices,
int type,
int fill) {
158 ImagePlus imp = null;
161 imp = createByteImage(title, width, height, nSlices, fill);
167 imp = createFloatImage(title, width, height, nSlices, fill);
170 imp = createRGBImage(title, width, height, nSlices, fill);
177 void showClipboard() {
178 ImagePlus clipboard = ImageCanvas.getClipboard();
182 IJ.error(
"The clipboard is empty.");
185 boolean showDialog() {
186 if (type<GRAY8|| type>RGB)
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,
"");
198 if (gd.wasCanceled())
200 name = gd.getNextString();
201 String s = gd.getNextChoice();
202 if (s.startsWith(
"8"))
204 else if (s.startsWith(
"16"))
206 else if (s.endsWith(
"RGB") || s.endsWith(
"rgb"))
210 fillWith = gd.getNextChoiceIndex();
211 width = (int)gd.getNextNumber();
212 height = (int)gd.getNextNumber();
213 slices = (int)gd.getNextNumber();
220 if (fillWith>FILL_RAMP)
221 {showClipboard();
return;}
222 try {open(name, width, height, slices, type, fillWith);}
223 catch(OutOfMemoryError e) {IJ.outOfMemory(
"New...");}
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));