New image dialog box plus several static utility methods for creating images.
| 8 | |
| 9 | /** New image dialog box plus several static utility methods for creating images.*/ |
| 10 | public class NewImage { |
| 11 | |
| 12 | public static final int GRAY8=0, GRAY16=1, GRAY32=2, RGB=3; |
| 13 | public static final int FILL_BLACK=1, FILL_RAMP=2, FILL_NOISE=3, FILL_RANDOM=3, |
| 14 | FILL_WHITE=4, CHECK_AVAILABLE_MEMORY=8, SIGNED_INT=16; |
| 15 | private static final int OLD_FILL_WHITE=0; |
| 16 | |
| 17 | static final String TYPE = "new.type"; |
| 18 | static final String FILL = "new.fill"; |
| 19 | static final String WIDTH = "new.width"; |
| 20 | static final String HEIGHT = "new.height"; |
| 21 | static final String SLICES = "new.slices"; |
| 22 | |
| 23 | private static String name = "Untitled"; |
| 24 | private static int staticWidth = Prefs.getInt(WIDTH, 512); |
| 25 | private static int staticHeight = Prefs.getInt(HEIGHT, 512); |
| 26 | private static int staticSlices = Prefs.getInt(SLICES, 1); |
| 27 | private static int staticType = Prefs.getInt(TYPE, GRAY8); |
| 28 | private static int staticFillWith = Prefs.getInt(FILL, FILL_BLACK); |
| 29 | private static String[] types = {"8-bit", "16-bit", "32-bit", "RGB"}; |
| 30 | private static String[] fill = {"White", "Black", "Ramp", "Noise"}; |
| 31 | private int gwidth, gheight, gslices, gtype, gfill; |
| 32 | |
| 33 | public NewImage() { |
| 34 | openImage(); |
| 35 | } |
| 36 | |
| 37 | static boolean createStack(ImagePlus imp, ImageProcessor ip, int nSlices, int type, int options) { |
| 38 | int fill = getFill(options); |
| 39 | int width = imp.getWidth(); |
| 40 | int height = imp.getHeight(); |
| 41 | long bytesPerPixel = 1; |
| 42 | if (type==GRAY16) bytesPerPixel = 2; |
| 43 | else if (type==GRAY32||type==RGB) bytesPerPixel = 4; |
| 44 | long size = (long)width*height*nSlices*bytesPerPixel; |
| 45 | int sizeThreshold = fill==FILL_NOISE?10:250; |
| 46 | boolean bigStack = size/(1024*1024)>=sizeThreshold; |
| 47 | String size2 = size/(1024*1024)+"MB ("+width+"x"+height+"x"+nSlices+")"; |
| 48 | if ((options&CHECK_AVAILABLE_MEMORY)!=0) { |
| 49 | long max = IJ.maxMemory(); // - 100*1024*1024; |
| 50 | if (max>0) { |
| 51 | long inUse = IJ.currentMemory(); |
| 52 | long available = max - inUse; |
| 53 | if (size>available) |
| 54 | System.gc(); |
| 55 | inUse = IJ.currentMemory(); |
| 56 | available = max-inUse; |
| 57 | if (size>available) { |
| 58 | IJ.error("Insufficient Memory", "There is not enough free memory to allocate a \n" |
| 59 | + size2+" stack.\n \n" |
| 60 | + "Memory available: "+available/(1024*1024)+"MB\n" |
| 61 | + "Memory in use: "+IJ.freeMemory()+"\n \n" |
| 62 | + "More information can be found in the \"Memory\"\n" |
| 63 | + "sections of the ImageJ installation notes at\n" |
| 64 | + "\""+IJ.URL2+"/docs/install/\"."); |
| 65 | return false; |
| 66 | } |
| 67 | } |