Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added .dax reader/writer plugins for ImageJ #25

Merged
merged 1 commit into from
Aug 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ build/
*.eggs
dist/
.cache/
.DS_STORE
storm_analysis/test/output/
*__pycache__*

Expand Down
Binary file not shown.
148 changes: 148 additions & 0 deletions storm_analysis/imagej_plugins/dax_file_io/Dax_Reader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//ImageJ reader plugin for Zhuang lab .dax files
//Translated from the Python versions in storm-analysis/sa_library and
//based on sample IJ reader/writer plugins by Albert Cardona at
//http://albert.rierol.net/imagej_programming_tutorials.html.
//Evan Heller, 10/2015

import java.io.*;
import ij.*;
import ij.io.*;
import ij.plugin.PlugIn;
import ij.plugin.FileInfoVirtualStack;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Dax_Reader extends ImagePlus implements PlugIn {

public void run(String arg) {
String path = getPath(arg);
if (null == path) return;
if (!parse(path)) return;
if (null == arg || 0 == arg.trim().length()) this.show(); // was opened by direct call to the plugin
// not via HandleExtraFileTypes which would
// have given a non-null arg.
}

private String getPath(String arg) {
if (null != arg) {
if (0 == arg.indexOf("http://") || new File(arg).exists()) return arg;
}
// else, ask:
OpenDialog od = new OpenDialog("Choose a .dax file", null);
String dir = od.getDirectory();
if (null == dir) return null; // dialog was canceled
dir = dir.replace('\\', '/'); // Windows safe
if (!dir.endsWith("/")) dir += "/";
return dir + od.getFileName();
}

private boolean parse(String path) {
File mydax = new File(path);
String dirname = mydax.getParent();
String filename = mydax.getName();
if (dirname.length() > 0) dirname += "/";

String inf_file = dirname + filename.split("\\.")[0] + ".inf";

//Image parameters
int image_height = 256;
int image_width = 256;
int number_frames = 0, scalemax=0, scalemin=0;
boolean bigendian = false;
float lock_target, stage_x = 0, stage_y = 0;

//Extract the movie information from the associated inf file
Pattern size_re = Pattern.compile("frame dimensions = ([\\d]+) x ([\\d]+)");
Pattern length_re = Pattern.compile("number of frames = ([\\d]+)");
Pattern endian_re = Pattern.compile(" (big|little) endian");
Pattern stagex_re = Pattern.compile("Stage X = ([\\d\\.\\-]+)");
Pattern stagey_re = Pattern.compile("Stage Y = ([\\d\\.\\-]+)");
Pattern lock_target_re = Pattern.compile("Lock Target = ([\\d\\.\\-]+)");
Pattern scalemax_re = Pattern.compile("scalemax = ([\\d\\.\\-]+)");
Pattern scalemin_re = Pattern.compile("scalemin = ([\\d\\.\\-]+)");

try {
BufferedReader br = new BufferedReader(new FileReader(inf_file));
String line;
Matcher m;

while ( (line = br.readLine()) != null) {
m = size_re.matcher(line);
if (m.find()) {
image_width = Integer.parseInt(m.group(1));
image_height = Integer.parseInt(m.group(2));
}

m = length_re.matcher(line);
if ( m.find() ) number_frames = Integer.parseInt(m.group(1));

m = endian_re.matcher(line);
if ( m.find() ) {
if (m.group(1).equals("big")) bigendian = true;
}

m = stagex_re.matcher(line);
if ( m.find() ) stage_x = Float.parseFloat(m.group(1));

m = stagey_re.matcher(line);
if ( m.find() ) stage_y = Float.parseFloat(m.group(1));

m = lock_target_re.matcher(line);
if (m.find() ) lock_target = Float.parseFloat(m.group(1));

m = scalemax_re.matcher(line);
if (m.find() ) scalemax = Integer.parseInt(m.group(1));

m = scalemin_re.matcher(line);
if ( m.find() ) scalemin = Integer.parseInt(m.group(1));
}

br.close();

}
catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
return false;
}

FileInfo fi = new FileInfo();
fi.fileType=2;
fi.fileFormat=fi.TIFF;
fi.directory=dirname;
fi.fileName=filename.split("\\.")[0] + ".dax";
fi.width=image_width;
fi.height=image_height;
fi.nImages=number_frames;
fi.gapBetweenImages = 0;
fi.intelByteOrder = !bigendian;
fi.whiteIsZero = false;
fi.longOffset = fi.offset = 0;

try {
ImagePlus imp;

//Open regular stack if small enough; otherwise, virtual
if (number_frames <= 500) {
FileOpener fo = new FileOpener(fi);
imp = fo.open(false);
}
else {
FileInfoVirtualStack fv = new FileInfoVirtualStack(fi, false);
imp = new ImagePlus("",fv);
}

this.setStack(imp.getTitle(), imp.getStack());
this.setCalibration(imp.getCalibration());
Object obinfo = imp.getProperty("Info");
if (null != obinfo) this.setProperty("Info", obinfo);
this.setFileInfo(imp.getOriginalFileInfo());

}
catch (Exception e) {
e.printStackTrace();
return false;
}

return true;
}
}
Binary file not shown.
78 changes: 78 additions & 0 deletions storm_analysis/imagej_plugins/dax_file_io/Dax_Writer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//ImageJ writer plugin for Zhuang lab .dax files
//Translated from the Python versions in storm-analysis/sa_library and
//based on sample IJ reader/writer plugins by Albert Cardona at
//http://albert.rierol.net/imagej_programming_tutorials.html.
//Evan Heller, 10/2015

import java.io.*;
import ij.*;
import ij.io.*;
import ij.plugin.PlugIn;
import ij.process.ImageProcessor;
import ij.process.ImageConverter;

public class Dax_Writer implements PlugIn {

public void run(String arg) {
ImagePlus imp = WindowManager.getCurrentImage();
if (null == imp) return;
SaveDialog sd = new SaveDialog("Save Dax", "untitled", null);
String dir = sd.getDirectory();
if (null == dir) return; // user canceled dialog
dir = dir.replace('\\', '/'); // Windows safe
if (!dir.endsWith("/")) dir += "/";
saveDax(imp, dir + sd.getFileName());
}

static public void saveDax(ImagePlus imp, String path) {
File file = new File(path);
DataOutputStream dos = null;

try {
dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

// read data:
FileInfo fi = imp.getFileInfo();
String inf_file = path.split("\\.")[0] + ".inf";

FileWriter fw = new FileWriter(inf_file);

//Write a rudimentary .inf file
fw.write("binning = 1 x 1\n");
fw.write("data type = 16 bit integers (binary, big endian)\n");
fw.write("frame dimensions = " + fi.width + " x " + fi.height + "\n");
fw.write("number of frames = " + fi.nImages + "\n");
fw.write("Lock Target = 0.0\n");
fw.write("x_start = 1\n");
fw.write("x_end = " + fi.width + "\n");
fw.write("y_start = 1\n");
fw.write("y_end = " + fi.height + "\n");
fw.flush();
fw.close();

ImageProcessor ip;

//Covert to 16-bit grayscale for writing to .dax
if (imp.getBitDepth() != 16) {
ImageConverter ic = new ImageConverter(imp);
ic.convertToGray16();
}

//Write the data to file
for (int i=0; i<imp.getNSlices(); i++) {
imp.setSlice(i);
ip= imp.getProcessor(); //Get ip of current slice
short[] px = (short[])ip.getPixels();

for (int j=0; j<px.length; j++)
dos.writeShort(px[j]);

}
dos.flush();
dos.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}
Binary file not shown.
Loading