use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.
the class BDReader method getTimestamp.
private long getTimestamp(String file) throws FormatException, IOException {
RandomAccessInputStream s = new RandomAccessInputStream(file, 16);
TiffParser parser = new TiffParser(s);
parser.setDoCaching(false);
IFD firstIFD = parser.getFirstIFD();
if (firstIFD != null) {
TiffIFDEntry timestamp = (TiffIFDEntry) firstIFD.get(IFD.DATE_TIME);
if (timestamp != null) {
String stamp = parser.getIFDValue(timestamp).toString();
s.close();
stamp = DateTools.formatDate(stamp, BaseTiffReader.DATE_FORMATS, ".");
Timestamp t = Timestamp.valueOf(stamp);
// NPE if invalid input.
return t.asInstant().getMillis();
}
}
s.close();
return new Location(file).lastModified();
}
use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.
the class TiffWriterTest method testSaveBytesTiling.
@Test(dataProvider = "tiling")
public void testSaveBytesTiling(int tileSize, boolean littleEndian, boolean interleaved, int rgbChannels, int seriesCount, int sizeT, String compression, int pixelType, boolean bigTiff) throws Exception {
if (percentageOfTilingTests == 0)
return;
File tmp = File.createTempFile("tiffWriterTest_Tiling", ".tiff");
tmp.deleteOnExit();
Plane originalPlane = WriterUtilities.writeImage(tmp, tileSize, littleEndian, interleaved, rgbChannels, seriesCount, sizeT, compression, pixelType, bigTiff);
TiffReader reader = new TiffReader();
reader.setId(tmp.getAbsolutePath());
int expectedTileSize = tileSize;
if (tileSize < TILE_GRANULARITY) {
expectedTileSize = TILE_GRANULARITY;
} else {
expectedTileSize = Math.round((float) tileSize / TILE_GRANULARITY) * TILE_GRANULARITY;
}
IFD tileIFd = reader.getIFDs().get(0);
assertEquals(tileIFd.getIFDIntValue(IFD.TILE_LENGTH), expectedTileSize);
assertEquals(tileIFd.getIFDIntValue(IFD.TILE_WIDTH), expectedTileSize);
WriterUtilities.checkImage(reader, originalPlane, interleaved, rgbChannels, seriesCount, sizeT, compression);
tmp.delete();
reader.close();
}
use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.
the class BaseTiffReaderTest method testCreationDateMultipleIFDs.
@Test
public void testCreationDateMultipleIFDs() {
ifd.put(IFD.DATE_TIME, "CreationDate1");
reader.addIFD(ifd);
IFD ifd2 = new IFD();
ifd2.put(IFD.DATE_TIME, "CreationDate2");
reader.addIFD(ifd2);
assertEquals("CreationDate1", reader.getCreationDate());
}
use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.
the class ImageConverter method convertTilePlane.
/**
* Convert the specified plane as a set of tiles, using the specified writer.
* @param writer the {@link loci.formats.IFormatWriter} to use for writing the plane
* @param index the index of the plane to convert in the input file
* @param outputIndex the index of the plane to convert in the output file
* @param currentFile the file name or pattern being written to
* @return the time at which conversion started, in milliseconds
* @throws FormatException
* @throws IOException
*/
private long convertTilePlane(IFormatWriter writer, int index, int outputIndex, String currentFile) throws FormatException, IOException {
int w = reader.getOptimalTileWidth();
int h = reader.getOptimalTileHeight();
if (saveTileWidth > 0 && saveTileWidth <= width) {
w = saveTileWidth;
}
if (saveTileHeight > 0 && saveTileHeight <= height) {
h = saveTileHeight;
}
if (firstTile) {
LOGGER.info("Tile size = {} x {}", w, h);
firstTile = false;
}
int nXTiles = width / w;
int nYTiles = height / h;
if (nXTiles * w != width) {
nXTiles++;
}
if (nYTiles * h != height) {
nYTiles++;
}
IFD ifd = new IFD();
ifd.put(IFD.TILE_WIDTH, w);
ifd.put(IFD.TILE_LENGTH, h);
Long m = null;
for (int y = 0; y < nYTiles; y++) {
for (int x = 0; x < nXTiles; x++) {
int tileX = xCoordinate + x * w;
int tileY = yCoordinate + y * h;
int tileWidth = x < nXTiles - 1 ? w : width - (w * x);
int tileHeight = y < nYTiles - 1 ? h : height - (h * y);
byte[] buf = reader.openBytes(index, tileX, tileY, tileWidth, tileHeight);
String tileName = FormatTools.getTileFilename(x, y, y * nXTiles + x, currentFile);
if (!currentFile.equals(tileName)) {
int nTileRows = getTileRows(currentFile);
int nTileCols = getTileColumns(currentFile);
int sizeX = nTileCols == 1 ? width : tileWidth;
int sizeY = nTileRows == 1 ? height : tileHeight;
MetadataRetrieve retrieve = writer.getMetadataRetrieve();
if (retrieve instanceof MetadataStore) {
((MetadataStore) retrieve).setPixelsSizeX(new PositiveInteger(sizeX), reader.getSeries());
((MetadataStore) retrieve).setPixelsSizeY(new PositiveInteger(sizeY), reader.getSeries());
}
writer.close();
writer.setMetadataRetrieve(retrieve);
writer.setId(tileName);
if (compression != null)
writer.setCompression(compression);
outputIndex = 0;
if (nextOutputIndex.containsKey(tileName)) {
outputIndex = nextOutputIndex.get(tileName);
}
nextOutputIndex.put(tileName, outputIndex + 1);
if (nTileRows > 1) {
tileY = 0;
}
if (nTileCols > 1) {
tileX = 0;
}
}
autoscalePlane(buf, index);
applyLUT(writer);
if (m == null) {
m = System.currentTimeMillis();
}
if (writer instanceof TiffWriter) {
((TiffWriter) writer).saveBytes(outputIndex, buf, ifd, tileX, tileY, tileWidth, tileHeight);
} else if (writer instanceof ImageWriter) {
IFormatWriter baseWriter = ((ImageWriter) writer).getWriter(out);
if (baseWriter instanceof TiffWriter) {
((TiffWriter) baseWriter).saveBytes(outputIndex, buf, ifd, tileX, tileY, tileWidth, tileHeight);
}
}
}
}
return m;
}
use of loci.formats.tiff.IFD 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();
}
Aggregations