Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ImportDialog.java
1 package ij.io;
2 
3 import java.awt.*;
4 import java.awt.image.*;
5 import java.io.*;
6 import java.util.*;
7 import ij.*;
8 import ij.gui.*;
9 import ij.process.*;
10 import ij.util.StringSorter;
11 import ij.plugin.frame.Recorder;
12 
13 
15 public class ImportDialog {
16  private String fileName;
17  private String directory;
18  static final String TYPE = "raw.type";
19  static final String WIDTH = "raw.width";
20  static final String HEIGHT = "raw.height";
21  static final String OFFSET = "raw.offset";
22  static final String N = "raw.n";
23  static final String GAP = "raw.gap";
24  static final String OPTIONS = "raw.options";
25  static final int WHITE_IS_ZERO = 1;
26  static final int INTEL_BYTE_ORDER = 2;
27  static final int OPEN_ALL = 4;
28 
29  // default settings
30  private static int choiceSelection = Prefs.getInt(TYPE,0);
31  private static int width = Prefs.getInt(WIDTH,512);
32  private static int height = Prefs.getInt(HEIGHT,512);
33  private static long offset = Prefs.getInt(OFFSET,0);
34  private static int nImages = Prefs.getInt(N,1);
35  private static int gapBetweenImages = Prefs.getInt(GAP,0);
36  private static int options;
37  private static boolean whiteIsZero,intelByteOrder;
38  private static String[] types = {"8-bit", "16-bit Signed", "16-bit Unsigned",
39  "32-bit Signed", "32-bit Unsigned", "32-bit Real", "24-bit RGB",
40  "24-bit RGB Planar", "24-bit BGR", "32-bit ARGB", "1-bit Bitmap"};
41 
42  static {
43  options = Prefs.getInt(OPTIONS,0);
44  whiteIsZero = (options&WHITE_IS_ZERO)!=0;
45  intelByteOrder = (options&INTEL_BYTE_ORDER)!=0;
46  }
47 
48  public ImportDialog(String fileName, String directory) {
49  this.fileName = fileName;
50  this.directory = directory;
51  IJ.showStatus("Importing: " + fileName);
52  }
53 
54  public ImportDialog() {
55  }
56 
57  boolean showDialog() {
58  if (choiceSelection>=types.length)
59  choiceSelection = 0;
60  GenericDialog gd = new GenericDialog("Import...", IJ.getInstance());
61  gd.addChoice("Image Type:", types, types[choiceSelection]);
62  gd.addNumericField("Width:", width, 0, 6, "pixels");
63  gd.addNumericField("Height:", height, 0, 6, "pixels");
64  gd.addNumericField("Offset to First Image:", offset, 0, 6, "bytes");
65  gd.addNumericField("Number of Images:", nImages, 0, 6, null);
66  gd.addNumericField("Gap Between Images:", gapBetweenImages, 0, 6, "bytes");
67  gd.addCheckbox("White is Zero", whiteIsZero);
68  gd.addCheckbox("Little-Endian Byte Order", intelByteOrder);
69  gd.showDialog();
70  if (gd.wasCanceled())
71  return false;
72  choiceSelection = gd.getNextChoiceIndex();
73  width = (int)gd.getNextNumber();
74  height = (int)gd.getNextNumber();
75  offset = (long)gd.getNextNumber();
76  nImages = (int)gd.getNextNumber();
77  gapBetweenImages = (int)gd.getNextNumber();
78  whiteIsZero = gd.getNextBoolean();
79  intelByteOrder = gd.getNextBoolean();
80  IJ.register(ImportDialog.class);
81  return true;
82  }
83 
84 
87  public void openImage() {
88  FileInfo fi = getFileInfo();
89  if (fi==null)
90  return;
91  FileOpener fo = new FileOpener(fi);
92  fo.open();
93  }
94 
98  public FileInfo getFileInfo() {
99  if (!showDialog())
100  return null;
101  String imageType = types[choiceSelection];
102  FileInfo fi = new FileInfo();
103  fi.fileFormat = fi.RAW;
104  fi.fileName = fileName;
105  fi.directory = directory;
106  fi.width = width;
107  fi.height = height;
108  if (offset>2147483647)
109  fi.longOffset = offset;
110  else
111  fi.offset = (int)offset;
112  fi.nImages = nImages;
113  fi.gapBetweenImages = gapBetweenImages;
114  fi.intelByteOrder = intelByteOrder;
115  fi.whiteIsZero = whiteIsZero;
116  if (imageType.equals("8-bit"))
117  fi.fileType = FileInfo.GRAY8;
118  else if (imageType.equals("16-bit Signed"))
119  fi.fileType = FileInfo.GRAY16_SIGNED;
120  else if (imageType.equals("16-bit Unsigned"))
121  fi.fileType = FileInfo.GRAY16_UNSIGNED;
122  else if (imageType.equals("32-bit Signed"))
123  fi.fileType = FileInfo.GRAY32_INT;
124  else if (imageType.equals("32-bit Unsigned"))
125  fi.fileType = FileInfo.GRAY32_UNSIGNED;
126  else if (imageType.equals("32-bit Real"))
127  fi.fileType = FileInfo.GRAY32_FLOAT;
128  else if (imageType.equals("24-bit RGB"))
129  fi.fileType = FileInfo.RGB;
130  else if (imageType.equals("24-bit RGB Planar"))
131  fi.fileType = FileInfo.RGB_PLANAR;
132  else if (imageType.equals("24-bit BGR"))
133  fi.fileType = FileInfo.BGR;
134  else if (imageType.equals("32-bit ARGB"))
135  fi.fileType = FileInfo.ARGB;
136  else if (imageType.equals("1-bit Bitmap"))
137  fi.fileType = FileInfo.BITMAP;
138  else
139  fi.fileType = FileInfo.GRAY8;
140  if (IJ.debugMode) IJ.log("ImportDialog: "+fi);
141  return fi;
142  }
143 
145  public static void savePreferences(Properties prefs) {
146  prefs.put(TYPE, Integer.toString(choiceSelection));
147  prefs.put(WIDTH, Integer.toString(width));
148  prefs.put(HEIGHT, Integer.toString(height));
149  prefs.put(OFFSET, Integer.toString(offset>2147483647?0:(int)offset));
150  prefs.put(N, Integer.toString(nImages));
151  prefs.put(GAP, Integer.toString(gapBetweenImages));
152  int options = 0;
153  if (whiteIsZero)
154  options |= WHITE_IS_ZERO;
155  if (intelByteOrder)
156  options |= INTEL_BYTE_ORDER;
157  prefs.put(OPTIONS, Integer.toString(options));
158  }
159 
160 }