use of ij.io.TiffEncoder in project TrakEM2 by trakem2.
the class Patch method writeAlphaMask.
private final synchronized boolean writeAlphaMask(final ByteProcessor bp, final long amID) {
RandomAccessFile ra = null;
try {
final File f = new File(createAlphaMaskFilePath(amID));
Utils.ensure(f);
ra = new RandomAccessFile(f, "rw");
final ByteArrayOutputStream ba = new ByteArrayOutputStream(bp.getWidth() * bp.getHeight());
final ZipOutputStream zos = new ZipOutputStream(ba);
// ImageJ looks for ".tif" extension in the ZipEntry
final ImagePlus imp = new ImagePlus("mask.tif", bp);
zos.putNextEntry(new ZipEntry(imp.getTitle()));
final TiffEncoder te = new TiffEncoder(imp.getFileInfo());
te.write(zos);
zos.flush();
zos.closeEntry();
zos.close();
ra.write((byte[]) ImageSaver.Bbuf.get(ba), 0, ba.size());
return true;
} catch (final Throwable e) {
IJError.print(e);
} finally {
try {
if (null != ra)
ra.close();
} catch (final Throwable t) {
IJError.print(t);
}
}
return false;
}
use of ij.io.TiffEncoder in project TrakEM2 by trakem2.
the class Loader method createZippedStream.
/**
*Returns the ImagePlus as a zipped InputStream of bytes; the InputStream has to be closed by whoever is calling this method.
*/
protected InputStream createZippedStream(final ImagePlus imp) throws Exception {
final FileInfo fi = imp.getFileInfo();
final Object info = imp.getProperty("Info");
if (info != null && (info instanceof String)) {
fi.info = (String) info;
}
if (null == fi.description) {
fi.description = new ij.io.FileSaver(imp).getDescriptionString();
}
// see whether this is a stack or not
/* //never the case in my program
if (fi.nImages > 1) {
IJ.log("saving a stack!");
//virtual stacks would be supported? I don't think so because the FileSaver.saveAsTiffStack(String path) doesn't.
if (fi.pixels == null && imp.getStack().isVirtual()) {
//don't save it!
IJ.showMessage("Virtual stacks not supported.");
return false;
}
//setup stack things as in FileSaver.saveAsTiffStack(String path)
fi.sliceLabels = imp.getStack().getSliceLabels();
}
*/
final TiffEncoder te = new TiffEncoder(fi);
ByteArrayInputStream i_stream = null;
ByteArrayOutputStream o_bytes = new ByteArrayOutputStream();
DataOutputStream o_stream = null;
try {
/* // works, but not significantly faster and breaks older databases (can't read zipped images properly)
byte[] bytes = null;
// compress in RAM
o_stream = new DataOutputStream(new BufferedOutputStream(o_bytes));
te.write(o_stream);
o_stream.flush();
o_stream.close();
Deflater defl = new Deflater();
byte[] unzipped_bytes = o_bytes.toByteArray();
defl.setInput(unzipped_bytes);
defl.finish();
bytes = new byte[unzipped_bytes.length]; // this length *should* be enough
int length = defl.deflate(bytes);
if (length < unzipped_bytes.length) {
byte[] bytes2 = new byte[length];
System.arraycopy(bytes, 0, bytes2, 0, length);
bytes = bytes2;
}
*/
// old, creates temp file
// clearing
o_bytes = new ByteArrayOutputStream();
final ZipOutputStream zos = new ZipOutputStream(o_bytes);
o_stream = new DataOutputStream(new BufferedOutputStream(zos));
zos.putNextEntry(new ZipEntry(imp.getTitle()));
te.write(o_stream);
// this was missing and was 1) truncating the Path images and 2) preventing the snapshots (which are very small) to be saved at all!!
o_stream.flush();
// this should ensure the flush above anyway. This can work because closing a ByteArrayOutputStream has no effect.
o_stream.close();
final byte[] bytes = o_bytes.toByteArray();
// Utils.showStatus("Zipping " + bytes.length + " bytes...", false);
// Utils.debug("Zipped ImagePlus byte array size = " + bytes.length);
i_stream = new ByteArrayInputStream(bytes);
} catch (final Exception e) {
Utils.log("Loader: ImagePlus NOT zipped! Problems at writing the ImagePlus using the TiffEncoder.write(dos) :\n " + e);
// attempt to cleanup:
try {
if (null != o_stream)
o_stream.close();
if (null != i_stream)
i_stream.close();
} catch (final IOException ioe) {
Utils.log("Loader: Attempt to clean up streams failed.");
IJError.print(ioe);
}
return null;
}
return i_stream;
}
use of ij.io.TiffEncoder in project TrakEM2 by trakem2.
the class ImageSaver method saveAsZip.
/**
* Returns true on success.
* <p>
* Core functionality adapted from {@code ij.io.FileSaver} class by Wayne Rasband.
* </p>
*/
public static final boolean saveAsZip(final ImagePlus imp, String path) {
// safety checks
if (null == imp) {
Utils.log("Null imp, can't saveAsZip");
return false;
}
if (!checkPath(path))
return false;
// ok, onward:
FileInfo fi = imp.getFileInfo();
if (!path.endsWith(".zip"))
path = path + ".zip";
String name = imp.getTitle();
if (name.endsWith(".zip"))
name = name.substring(0, name.length() - 4);
if (!name.endsWith(".tif"))
name = name + ".tif";
fi.description = ImageSaver.getDescriptionString(imp, fi);
Object info = imp.getProperty("Info");
if (info != null && (info instanceof String))
fi.info = (String) info;
fi.sliceLabels = imp.getStack().getSliceLabels();
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(path));
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(zos));
zos.putNextEntry(new ZipEntry(name));
TiffEncoder te = new TiffEncoder(fi);
te.write(out);
out.close();
} catch (IOException e) {
IJError.print(e);
return false;
}
return true;
}
use of ij.io.TiffEncoder in project GDSC-SMLM by aherbert.
the class TiffSeriesViewer method saveAsTiff.
private static void saveAsTiff(ImagePlus imp, String path) throws IOException {
final FileInfo fi = imp.getFileInfo();
fi.nImages = imp.getStackSize();
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(path))) {
new TiffEncoder(fi).write(out);
}
}
use of ij.io.TiffEncoder in project GDSC-SMLM by aherbert.
the class SeriesImageSourceTest method saveAsTiff.
private static void saveAsTiff(ImagePlus imp, String path, boolean intelByteOrder) throws IOException {
// IJ.saveAsTiff(imp, path);
final FileInfo fi = imp.getFileInfo();
fi.nImages = imp.getStackSize();
ij.Prefs.intelByteOrder = intelByteOrder;
try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)))) {
final TiffEncoder file = new TiffEncoder(fi);
file.write(out);
}
}
Aggregations