use of loci.formats.out.TiffWriter in project bioformats by openmicroscopy.
the class TiffWriterTest method initializeWriter.
/**
* Initializes the writer.
* @param output The file where to write the compressed data.
* @param compression The compression to use.
* @param bigTiff Pass <code>true</code> to set the <code>bigTiff</code> flag,
* <code>false</code> otherwise.
* @return See above.
* @throws Exception Thrown if an error occurred.
*/
private TiffWriter initializeWriter(String output, String compression, boolean bigTiff) throws Exception {
IMetadata newMetadata = service.createOMEXMLMetadata();
MetadataConverter.convertMetadata(metadata, newMetadata);
TiffWriter writer = new TiffWriter();
writer.setMetadataRetrieve(newMetadata);
writer.setCompression(compression);
writer.setWriteSequentially(true);
writer.setInterleaved(false);
writer.setBigTiff(bigTiff);
writer.setId(output);
return writer;
}
use of loci.formats.out.TiffWriter in project bioformats by openmicroscopy.
the class TiffWriterTest method assertTiles.
/**
* Tests the writing of the tiles.
* @param output The output where to write the data.
* @param compression The compression to use.
* @param n The value by which to divide the width of the image.
* @param m The value by which to divide the height of the image.
* @param bigTiff Pass <code>true</code> to set the <code>bigTiff</code> flag,
* <code>false</code> otherwise.
*/
private void assertTiles(String output, String compression, int n, int m, boolean bigTiff) throws Exception {
TiffWriter writer = initializeWriter(output, compression, bigTiff);
int x, y;
byte[] tile;
long[] rowPerStrip;
int w, h;
IFD ifd;
int count;
int series = reader.getSeriesCount();
String[][][] tileMD5s = new String[series][][];
for (int s = 0; s < series; s++) {
reader.setSeries(s);
w = reader.getSizeX() / n;
h = reader.getSizeY() / m;
rowPerStrip = new long[1];
rowPerStrip[0] = h;
count = reader.getImageCount();
tileMD5s[s] = new String[count][m * n];
for (int k = 0; k < count; k++) {
ifd = new IFD();
ifd.put(IFD.TILE_WIDTH, w);
ifd.put(IFD.TILE_LENGTH, h);
ifd.put(IFD.ROWS_PER_STRIP, rowPerStrip);
for (int i = 0; i < m; i++) {
y = h * i;
for (int j = 0; j < n; j++) {
x = w * j;
tile = reader.openBytes(k, x, y, w, h);
tileMD5s[s][k][(i * n) + j] = TestTools.md5(tile);
writer.saveBytes(k, tile, ifd, x, y, w, h);
}
}
}
}
writer.close();
// Now going to read the output.
TiffReader outputReader = new TiffReader();
outputReader.setId(output);
// first series.
String writtenDigest;
String readDigest;
for (int s = 0; s < series; s++) {
outputReader.setSeries(s);
count = outputReader.getImageCount();
h = outputReader.getSizeY() / m;
w = outputReader.getSizeX() / n;
for (int k = 0; k < count; k++) {
for (int i = 0; i < m; i++) {
y = h * i;
for (int j = 0; j < n; j++) {
x = w * j;
tile = outputReader.openBytes(k, x, y, w, h);
writtenDigest = tileMD5s[s][k][(i * n) + j];
readDigest = TestTools.md5(tile);
if (!writtenDigest.equals(readDigest)) {
fail(String.format("Compression:%s MD5:%d;%d;%d;%d;%d; %s != %s", compression, k, x, y, w, h, writtenDigest, readDigest));
}
}
}
}
}
outputReader.close();
}
use of loci.formats.out.TiffWriter in project bioformats by openmicroscopy.
the class WriterUtilities method writeImage.
public static Plane writeImage(File file, int tileSize, boolean littleEndian, boolean interleaved, int rgbChannels, int seriesCount, int sizeT, String compression, int pixelType, boolean bigTiff) throws Exception {
TiffWriter writer = new TiffWriter();
String pixelTypeString = FormatTools.getPixelTypeString(pixelType);
writer.setMetadataRetrieve(createMetadata(pixelTypeString, rgbChannels, seriesCount, littleEndian, sizeT));
writer.setCompression(compression);
writer.setInterleaved(interleaved);
writer.setBigTiff(bigTiff);
if (tileSize != PLANE_WIDTH) {
writer.setTileSizeX(tileSize);
writer.setTileSizeY(tileSize);
}
writer.setId(file.getAbsolutePath());
int bytes = FormatTools.getBytesPerPixel(pixelType);
byte[] plane = getPlane(PLANE_WIDTH, PLANE_HEIGHT, bytes * rgbChannels);
Plane originalPlane = new Plane(plane, littleEndian, !writer.isInterleaved(), rgbChannels, FormatTools.getPixelTypeString(pixelType));
for (int s = 0; s < seriesCount; s++) {
writer.setSeries(s);
for (int t = 0; t < sizeT; t++) {
writer.saveBytes(t, plane);
}
}
writer.close();
return originalPlane;
}
use of loci.formats.out.TiffWriter in project bioformats by openmicroscopy.
the class MakeLZW method main.
public static void main(String[] args) throws Exception {
ImageReader reader = new ImageReader();
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
IMetadata omexmlMeta = service.createOMEXMLMetadata();
reader.setMetadataStore(omexmlMeta);
TiffWriter writer = new TiffWriter();
for (int i = 0; i < args.length; i++) {
String inFile = args[i];
String outFile = "lzw-" + inFile;
System.out.print("Converting " + inFile + " to " + outFile);
reader.setId(inFile);
writer.setMetadataRetrieve(omexmlMeta);
writer.setCompression("LZW");
writer.setId(outFile);
int planeCount = reader.getImageCount();
for (int p = 0; p < planeCount; p++) {
System.out.print(".");
byte[] plane = reader.openBytes(p);
writer.saveBytes(p, plane);
}
System.out.println(" [done]");
}
}
use of loci.formats.out.TiffWriter in project bioformats by openmicroscopy.
the class TiffWriterTest method assertUnevenTiles.
/**
* Tests the writing of the tiles.
* @param output The output where to write the data.
* @param compression The compression to use.
* @param blockWidth The width of block to write.
* @param blockHeight The height of block to write.
* @param bigTiff Pass <code>true</code> to set the <code>bigTiff</code> flag,
* <code>false</code> otherwise.
*/
private void assertUnevenTiles(String output, String compression, int blockWidth, int blockHeight, boolean bigTiff) throws Exception {
TiffWriter writer = initializeWriter(output, compression, bigTiff);
int x, y;
byte[] tile;
long[] rowPerStrip;
int w, h;
IFD ifd;
int count;
int sizeX, sizeY;
int n, m;
int diffWidth, diffHeight;
int series = reader.getSeriesCount();
String[][][] tileMD5s = new String[series][][];
for (int s = 0; s < series; s++) {
reader.setSeries(s);
sizeX = reader.getSizeX();
sizeY = reader.getSizeY();
if (blockWidth <= 0)
blockWidth = sizeX;
if (blockHeight <= 0)
blockHeight = sizeY;
n = sizeX / blockWidth;
m = sizeY / blockHeight;
if (n == 0) {
blockWidth = sizeX;
n = 1;
}
if (m == 0) {
blockHeight = sizeY;
m = 1;
}
diffWidth = sizeX - n * blockWidth;
diffHeight = sizeY - m * blockHeight;
if (diffWidth > 0)
n++;
if (diffHeight > 0)
m++;
rowPerStrip = new long[1];
rowPerStrip[0] = blockHeight;
count = reader.getImageCount();
tileMD5s[s] = new String[count][m * n];
for (int k = 0; k < count; k++) {
x = 0;
y = 0;
ifd = new IFD();
ifd.put(IFD.TILE_WIDTH, blockWidth);
ifd.put(IFD.TILE_LENGTH, blockHeight);
ifd.put(IFD.ROWS_PER_STRIP, rowPerStrip);
for (int i = 0; i < m; i++) {
if (diffHeight > 0 && i == (m - 1)) {
y = sizeY - diffHeight;
h = diffHeight;
} else {
y = blockHeight * i;
h = blockHeight;
}
for (int j = 0; j < n; j++) {
if (diffWidth > 0 && j == (n - 1)) {
x = sizeX - diffWidth;
w = diffWidth;
} else {
x = blockWidth * j;
w = blockWidth;
}
tile = reader.openBytes(k, x, y, w, h);
tileMD5s[s][k][(i * n) + j] = TestTools.md5(tile);
writer.saveBytes(0, tile, ifd, x, y, w, h);
}
}
}
}
writer.close();
// Now going to read the output.
TiffReader outputReader = new TiffReader();
outputReader.setId(output);
// first series.
String writtenDigest;
String readDigest;
for (int s = 0; s < series; s++) {
outputReader.setSeries(s);
count = outputReader.getImageCount();
for (int k = 0; k < count; k++) {
sizeX = outputReader.getSizeX();
sizeY = outputReader.getSizeY();
n = sizeX / blockWidth;
m = sizeY / blockHeight;
diffWidth = sizeX - n * blockWidth;
diffHeight = sizeY - m * blockHeight;
if (diffWidth > 0)
n++;
if (diffHeight > 0)
m++;
for (int i = 0; i < m; i++) {
if (diffHeight > 0 && i == (m - 1)) {
y = sizeY - diffHeight;
h = diffHeight;
} else {
y = blockHeight * i;
h = blockHeight;
}
for (int j = 0; j < n; j++) {
if (diffWidth > 0 && j == (n - 1)) {
x = sizeX - diffWidth;
w = diffWidth;
} else {
x = blockWidth * j;
w = blockWidth;
}
tile = outputReader.openBytes(k, x, y, w, h);
writtenDigest = tileMD5s[s][k][(i * n) + j];
readDigest = TestTools.md5(tile);
if (!writtenDigest.equals(readDigest)) {
fail(String.format("Compression:%s MD5:%d;%d;%d;%d;%d; %s != %s", compression, k, x, y, w, h, writtenDigest, readDigest));
}
}
}
}
}
outputReader.close();
}
Aggregations