Search in sources :

Example 6 with IFormatWriter

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

the class SixteenBitLosslessJPEG2000Test method testLosslessPixels.

@Test
public void testLosslessPixels() throws Exception {
    int failureCount = 0;
    for (int v = Short.MIN_VALUE; v < Short.MAX_VALUE; v += increment) {
        int index = v + Short.MAX_VALUE + 1;
        byte[] pixels = DataTools.shortToBytes((short) v, false);
        String file = index + ".jp2";
        ByteArrayHandle tmpFile = new ByteArrayHandle(1);
        Location.mapFile(file, tmpFile);
        IMetadata metadata16;
        try {
            ServiceFactory factory = new ServiceFactory();
            OMEXMLService service = factory.getInstance(OMEXMLService.class);
            metadata16 = service.createOMEXMLMetadata();
        } catch (DependencyException exc) {
            throw new FormatException("Could not create OME-XML store.", exc);
        } catch (ServiceException exc) {
            throw new FormatException("Could not create OME-XML store.", exc);
        }
        MetadataTools.populateMetadata(metadata16, 0, "foo", false, "XYCZT", "uint16", 1, 1, 1, 1, 1, 1);
        IFormatWriter writer16 = new JPEG2000Writer();
        writer16.setMetadataRetrieve(metadata16);
        writer16.setId(file);
        writer16.saveBytes(0, pixels);
        writer16.close();
        byte[] buf = tmpFile.getBytes();
        byte[] realData = new byte[(int) tmpFile.length()];
        System.arraycopy(buf, 0, realData, 0, realData.length);
        tmpFile.close();
        tmpFile = new ByteArrayHandle(realData);
        Location.mapFile(file, tmpFile);
        ImageReader reader = new ImageReader();
        reader.setId(file);
        byte[] plane = reader.openBytes(0);
        for (int q = 0; q < plane.length; q++) {
            if (plane[q] != pixels[q]) {
                LOGGER.debug("FAILED on {}", DataTools.bytesToShort(pixels, false));
                failureCount++;
                break;
            }
        }
        reader.close();
        tmpFile.close();
        Location.mapFile(file, null);
    }
    assertEquals(failureCount, 0);
}
Also used : JPEG2000Writer(loci.formats.out.JPEG2000Writer) ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException) IFormatWriter(loci.formats.IFormatWriter) IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) ByteArrayHandle(loci.common.ByteArrayHandle) ImageReader(loci.formats.ImageReader) Test(org.testng.annotations.Test)

Example 7 with IFormatWriter

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

the class EightBitLosslessJPEG2000Test method setUp.

@BeforeMethod
public void setUp() throws Exception {
    for (byte v = Byte.MIN_VALUE; v < Byte.MAX_VALUE; v++) {
        int index = v + Byte.MAX_VALUE + 1;
        pixels[index][0] = v;
        String file = index + ".jp2";
        File tempFile = File.createTempFile("test", ".jp2");
        tempFile.deleteOnExit();
        Location.mapId(file, tempFile.getAbsolutePath());
        files.add(file);
        IMetadata metadata;
        try {
            ServiceFactory factory = new ServiceFactory();
            OMEXMLService service = factory.getInstance(OMEXMLService.class);
            metadata = service.createOMEXMLMetadata();
        } catch (DependencyException exc) {
            throw new FormatException("Could not create OME-XML store.", exc);
        } catch (ServiceException exc) {
            throw new FormatException("Could not create OME-XML store.", exc);
        }
        MetadataTools.populateMetadata(metadata, 0, "foo", false, "XYCZT", "uint8", 1, 1, 1, 1, 1, 1);
        IFormatWriter writer = new JPEG2000Writer();
        writer.setMetadataRetrieve(metadata);
        writer.setId(file);
        writer.saveBytes(0, pixels[index]);
        writer.close();
    }
}
Also used : IFormatWriter(loci.formats.IFormatWriter) JPEG2000Writer(loci.formats.out.JPEG2000Writer) IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) File(java.io.File) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 8 with IFormatWriter

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

the class FormatWriterTest method getWriterList.

// -- Data provider --
@DataProvider(name = "getWriterList")
public Object[][] getWriterList() {
    IFormatWriter[] writers = new ImageWriter().getWriters();
    List tmp = new ArrayList();
    for (int i = 0; i < writers.length; i++) {
        String[] compressionTypes = writers[i].getCompressionTypes();
        if (compressionTypes == null) {
            try {
                IFormatWriter w = (IFormatWriter) writers[i].getClass().newInstance();
                tmp.add(w);
            } catch (InstantiationException ie) {
            } catch (IllegalAccessException iae) {
            }
            continue;
        }
        for (int q = 0; q < compressionTypes.length; q++) {
            try {
                IFormatWriter w = (IFormatWriter) writers[i].getClass().newInstance();
                if (DataTools.containsValue(w.getPixelTypes(compressionTypes[q]), reader.getPixelType())) {
                    w.setCompression(compressionTypes[q]);
                    tmp.add(w);
                }
            } catch (FormatException fe) {
            } catch (InstantiationException ie) {
            } catch (IllegalAccessException iae) {
            }
        }
    }
    IFormatWriter[][] writersToUse = new IFormatWriter[tmp.size()][1];
    for (int i = 0; i < tmp.size(); i++) {
        writersToUse[i][0] = (IFormatWriter) tmp.get(i);
    }
    return writersToUse;
}
Also used : IFormatWriter(loci.formats.IFormatWriter) ImageWriter(loci.formats.ImageWriter) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) FormatException(loci.formats.FormatException) DataProvider(org.testng.annotations.DataProvider)

Aggregations

IFormatWriter (loci.formats.IFormatWriter)8 FormatException (loci.formats.FormatException)6 DependencyException (loci.common.services.DependencyException)5 ServiceException (loci.common.services.ServiceException)5 ServiceFactory (loci.common.services.ServiceFactory)5 ImageWriter (loci.formats.ImageWriter)5 IMetadata (loci.formats.meta.IMetadata)5 OMEXMLService (loci.formats.services.OMEXMLService)5 File (java.io.File)3 ImageReader (loci.formats.ImageReader)3 JPEG2000Writer (loci.formats.out.JPEG2000Writer)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)2 MetadataStore (loci.formats.meta.MetadataStore)2 TiffWriter (loci.formats.out.TiffWriter)2 OMEXMLMetadataRoot (ome.xml.meta.OMEXMLMetadataRoot)2 PositiveInteger (ome.xml.model.primitives.PositiveInteger)2 CompositeImage (ij.CompositeImage)1 ImagePlus (ij.ImagePlus)1