Search in sources :

Example 6 with TiffReader

use of loci.formats.in.TiffReader in project bioformats by openmicroscopy.

the class TiffWriterTest method testSaveBytes.

@Test(dataProvider = "nonTiling")
public void testSaveBytes(int tileSize, boolean littleEndian, boolean interleaved, int rgbChannels, int seriesCount, int sizeT, String compression, int pixelType, boolean bigTiff) throws Exception {
    if (percentageOfSaveBytesTests == 0)
        return;
    File tmp = File.createTempFile("tiffWriterTest", ".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());
    WriterUtilities.checkImage(reader, originalPlane, interleaved, rgbChannels, seriesCount, sizeT, compression);
    tmp.delete();
    reader.close();
}
Also used : TiffReader(loci.formats.in.TiffReader) File(java.io.File) Test(org.testng.annotations.Test)

Example 7 with TiffReader

use of loci.formats.in.TiffReader in project bioformats by openmicroscopy.

the class OMETiffWriterBigTiffLargeImageWidthTest method testImageWidthWrittenCorrectly.

@Test
public void testImageWidthWrittenCorrectly() throws Exception {
    OMETiffWriter writer = new OMETiffWriter();
    writer.setBigTiff(true);
    writer.setMetadataRetrieve(ms);
    writer.setId(target.getAbsolutePath());
    writer.saveBytes(0, buf, 0, 0, buf.length, 1);
    writer.close();
    TiffReader reader = new TiffReader();
    reader.setId(target.getAbsolutePath());
    assertEquals(SIZE_X, reader.getSizeX());
    assertEquals(SIZE_Y, reader.getSizeY());
}
Also used : TiffReader(loci.formats.in.TiffReader) OMETiffWriter(loci.formats.out.OMETiffWriter) Test(org.testng.annotations.Test)

Example 8 with TiffReader

use of loci.formats.in.TiffReader 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();
}
Also used : TiffReader(loci.formats.in.TiffReader) TiffWriter(loci.formats.out.TiffWriter) IFD(loci.formats.tiff.IFD)

Example 9 with TiffReader

use of loci.formats.in.TiffReader in project bioformats by openmicroscopy.

the class SewTiffs method main.

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        System.out.println("Usage: java SewTiffs base_name channel_num [time_count]");
        System.exit(1);
    }
    String base = args[0];
    int c = Integer.parseInt(args[1]);
    int num;
    if (args.length < 3) {
        FilePattern fp = new FilePattern(new Location(base + "_C" + c + "_TP1.tiff"));
        int[] count = fp.getCount();
        num = count[count.length - 1];
    } else
        num = Integer.parseInt(args[2]);
    System.out.println("Fixing " + base + "_C" + c + "_TP<1-" + num + ">.tiff");
    TiffReader in = new TiffReader();
    TiffWriter out = new TiffWriter();
    String outId = base + "_C" + c + ".tiff";
    System.out.println("Writing " + outId);
    out.setId(outId);
    System.out.print("   ");
    boolean comment = false;
    for (int t = 0; t < num; t++) {
        String inId = base + "_C" + c + "_TP" + (t + 1) + ".tiff";
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        IMetadata meta = service.createOMEXMLMetadata();
        in.setMetadataStore(meta);
        in.setId(inId);
        out.setMetadataRetrieve(meta);
        // read first image plane
        byte[] image = in.openBytes(0);
        in.close();
        if (t == 0) {
            // read first IFD
            RandomAccessInputStream ras = new RandomAccessInputStream(inId);
            TiffParser parser = new TiffParser(ras);
            IFD ifd = parser.getFirstIFD();
            ras.close();
            // preserve TIFF comment
            String desc = ifd.getComment();
            if (desc != null) {
                ifd = new IFD();
                ifd.putIFDValue(IFD.IMAGE_DESCRIPTION, desc);
                comment = true;
                out.saveBytes(t, image, ifd);
                System.out.print(".");
                continue;
            }
        }
        // write image plane
        out.saveBytes(t, image);
        // update status
        System.out.print(".");
        if (t % DOTS == DOTS - 1) {
            System.out.println(" " + (t + 1));
            System.out.print("   ");
        }
    }
    System.out.println();
    if (comment)
        System.out.println("OME-TIFF comment saved.");
    else
        System.out.println("No OME-TIFF comment found.");
}
Also used : TiffWriter(loci.formats.out.TiffWriter) ServiceFactory(loci.common.services.ServiceFactory) IFD(loci.formats.tiff.IFD) OMEXMLService(loci.formats.services.OMEXMLService) TiffReader(loci.formats.in.TiffReader) IMetadata(loci.formats.meta.IMetadata) TiffParser(loci.formats.tiff.TiffParser) FilePattern(loci.formats.FilePattern) RandomAccessInputStream(loci.common.RandomAccessInputStream) Location(loci.common.Location)

Aggregations

TiffReader (loci.formats.in.TiffReader)9 Test (org.testng.annotations.Test)6 IFD (loci.formats.tiff.IFD)5 File (java.io.File)4 TiffWriter (loci.formats.out.TiffWriter)3 OMETiffReader (loci.formats.in.OMETiffReader)2 Location (loci.common.Location)1 RandomAccessInputStream (loci.common.RandomAccessInputStream)1 ServiceFactory (loci.common.services.ServiceFactory)1 FilePattern (loci.formats.FilePattern)1 ImageWriter (loci.formats.ImageWriter)1 IMetadata (loci.formats.meta.IMetadata)1 OMETiffWriter (loci.formats.out.OMETiffWriter)1 OMEXMLService (loci.formats.services.OMEXMLService)1 TiffParser (loci.formats.tiff.TiffParser)1