Search in sources :

Example 56 with ImageReader

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

the class CacheConsole method main.

// -- Main method --
/**
 * Interactive interpreter for testing Bio-Formats caching implementation.
 */
public static void main(String[] args) throws FormatException, IOException {
    if (args.length < 1) {
        System.out.println("Please specify a filename containing image data.");
        System.exit(1);
    }
    ImageReader reader = new ImageReader();
    String id = args[0];
    System.out.println("Reading " + id);
    reader.setId(id);
    System.out.println("Initializing cache");
    final Cache cache = new Cache(new CrosshairStrategy(getLengths(reader)), new BufferedImageSource(reader), true);
    CacheListener l = new CacheListener() {

        @Override
        public void cacheUpdated(CacheEvent e) {
            int type = e.getType();
            int ndx = e.getIndex();
            int[] len, pos;
            switch(type) {
                case CacheEvent.SOURCE_CHANGED:
                    printSource("source ->", cache);
                    break;
                case CacheEvent.STRATEGY_CHANGED:
                    printStrategy("strategy ->", cache);
                    break;
                case CacheEvent.POSITION_CHANGED:
                    len = cache.getStrategy().getLengths();
                    pos = FormatTools.rasterToPosition(len, ndx);
                    printArray("pos ->", pos);
                    break;
                case CacheEvent.PRIORITIES_CHANGED:
                    printArray("priorities ->", cache.getStrategy().getPriorities());
                    break;
                case CacheEvent.ORDER_CHANGED:
                    printOrder("order ->", cache);
                    break;
                case CacheEvent.RANGE_CHANGED:
                    printArray("range ->", cache.getStrategy().getRange());
                    break;
                case CacheEvent.OBJECT_LOADED:
                    len = cache.getStrategy().getLengths();
                    pos = FormatTools.rasterToPosition(len, ndx);
                    printArray("loaded:", pos);
                    break;
                case CacheEvent.OBJECT_DROPPED:
                    len = cache.getStrategy().getLengths();
                    pos = FormatTools.rasterToPosition(len, ndx);
                    printArray("dropped:", pos);
                    break;
            }
        }
    };
    cache.addCacheListener(l);
    BufferedReader r = new BufferedReader(new InputStreamReader(System.in, Constants.ENCODING));
    System.out.println("Entering Bio-Formats caching test console");
    while (true) {
        System.out.print("> ");
        String cmd = r.readLine().trim();
        if (cmd.equals(""))
            continue;
        else if (cmd.startsWith("c")) {
            // cache
            cache.recache();
        } else if (// exit/quit
        cmd.startsWith("e") || cmd.startsWith("q"))
            // exit/quit
            break;
        else if (cmd.startsWith("g")) {
            // gui
            String[] axes = { "Z", "C", "T" };
            CacheComponent widget = new CacheComponent(cache, axes, id);
            widget.setBorder(new EmptyBorder(15, 15, 15, 15));
            JFrame frame = new JFrame("Cache controls");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setContentPane(widget);
            frame.pack();
            frame.setVisible(true);
        } else if (cmd.startsWith("h")) {
            // help
            System.out.println("Available commands:");
            System.out.println("  cache    -- begins loading planes into the cache");
            System.out.println("  gui      -- pops up a GUI to configure the cache");
            System.out.println("  info     -- displays the cache state");
            System.out.println("  position -- changes the current position");
            System.out.println("  strategy -- changes the cache strategy");
            System.out.println("  source   -- changes the cache source");
            System.out.println("  priority -- changes the cache priorities");
            System.out.println("  order    -- changes the cache order");
            System.out.println("  range    -- changes the cache ranges");
            System.out.println("  read     -- gets a plane from the cache");
            System.out.println("  exit     -- quits the interpreter");
        } else if (cmd.startsWith("i")) {
            // info
            // output dimensional position
            printArray("pos =", cache.getCurrentPos());
            // output source information
            ICacheSource source = cache.getSource();
            printSource("source =", cache);
            System.out.println("object count = " + source.getObjectCount());
            // output strategy information
            ICacheStrategy strategy = cache.getStrategy();
            printStrategy("strategy =", cache);
            printArray("priorities =", strategy.getPriorities());
            printOrder("order =", cache);
            printArray("range =", strategy.getRange());
            printArray("lengths =", strategy.getLengths());
        } else if (cmd.startsWith("o")) {
            // order
            System.out.println(ICacheStrategy.CENTERED_ORDER + " => centered");
            System.out.println(ICacheStrategy.FORWARD_ORDER + " => forward");
            System.out.println(ICacheStrategy.BACKWARD_ORDER + " => backward");
            System.out.print("Z: ");
            int z = Integer.parseInt(r.readLine().trim());
            System.out.print("C: ");
            int c = Integer.parseInt(r.readLine().trim());
            System.out.print("T: ");
            int t = Integer.parseInt(r.readLine().trim());
            ICacheStrategy strategy = cache.getStrategy();
            strategy.setOrder(z, 0);
            strategy.setOrder(c, 1);
            strategy.setOrder(t, 2);
        } else if (cmd.startsWith("po")) {
            // position
            System.out.print("Z: ");
            int z = Integer.parseInt(r.readLine().trim());
            System.out.print("C: ");
            int c = Integer.parseInt(r.readLine().trim());
            System.out.print("T: ");
            int t = Integer.parseInt(r.readLine().trim());
            cache.setCurrentPos(new int[] { z, c, t });
        } else if (cmd.startsWith("pr")) {
            // priority
            System.out.println(ICacheStrategy.MIN_PRIORITY + " => min priority");
            System.out.println(ICacheStrategy.LOW_PRIORITY + " => low priority");
            System.out.println(ICacheStrategy.NORMAL_PRIORITY + " => normal priority");
            System.out.println(ICacheStrategy.HIGH_PRIORITY + " => high priority");
            System.out.println(ICacheStrategy.MAX_PRIORITY + " => max priority");
            System.out.print("Z: ");
            int z = Integer.parseInt(r.readLine().trim());
            System.out.print("C: ");
            int c = Integer.parseInt(r.readLine().trim());
            System.out.print("T: ");
            int t = Integer.parseInt(r.readLine().trim());
            ICacheStrategy strategy = cache.getStrategy();
            strategy.setPriority(z, 0);
            strategy.setPriority(c, 1);
            strategy.setPriority(t, 2);
        } else if (cmd.startsWith("ra")) {
            // range
            System.out.print("Z: ");
            int z = Integer.parseInt(r.readLine().trim());
            System.out.print("C: ");
            int c = Integer.parseInt(r.readLine().trim());
            System.out.print("T: ");
            int t = Integer.parseInt(r.readLine().trim());
            cache.getStrategy().setRange(z, 0);
            cache.getStrategy().setRange(c, 1);
            cache.getStrategy().setRange(t, 2);
        } else if (cmd.startsWith("re")) {
            // read
            System.out.print("Z: ");
            int z = Integer.parseInt(r.readLine().trim());
            System.out.print("C: ");
            int c = Integer.parseInt(r.readLine().trim());
            System.out.print("T: ");
            int t = Integer.parseInt(r.readLine().trim());
            System.out.println("Retrieving Z" + z + "-C" + c + "-T" + t);
            Object o = cache.getObject(new int[] { z, c, t });
            System.out.println(o);
        } else if (cmd.startsWith("so")) {
            // source
            System.out.println("0: BufferedImage");
            System.out.println("1: byte array");
            System.out.print("> ");
            int n = Integer.parseInt(r.readLine().trim());
            switch(n) {
                case 0:
                    cache.setSource(new BufferedImageSource(reader));
                    break;
                case 1:
                    cache.setSource(new ByteArraySource(reader));
                    break;
                default:
                    System.out.println("Unknown source: " + n);
            }
        } else if (cmd.startsWith("st")) {
            // strategy
            System.out.println("0: crosshair");
            System.out.println("1: rectangle");
            System.out.print("> ");
            int n = Integer.parseInt(r.readLine().trim());
            int[] zct = getLengths(reader);
            ICacheStrategy strategy = null;
            switch(n) {
                case 0:
                    strategy = new CrosshairStrategy(zct);
                    break;
                case 1:
                    strategy = new RectangleStrategy(zct);
                    break;
                default:
                    System.out.println("Unknown strategy: " + n);
            }
            if (strategy != null) {
                ICacheStrategy old = cache.getStrategy();
                int[] priorities = old.getPriorities();
                int[] range = old.getRange();
                int[] order = old.getOrder();
                for (int i = 0; i < zct.length; i++) {
                    strategy.setPriority(priorities[i], i);
                    strategy.setRange(range[i], i);
                    strategy.setOrder(order[i], i);
                }
                cache.setStrategy(strategy);
            }
        } else
            System.out.println("Unknown command: " + cmd);
    }
    reader.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) ICacheStrategy(loci.formats.cache.ICacheStrategy) ICacheSource(loci.formats.cache.ICacheSource) BufferedImageSource(loci.formats.gui.BufferedImageSource) CacheListener(loci.formats.cache.CacheListener) RectangleStrategy(loci.formats.cache.RectangleStrategy) CrosshairStrategy(loci.formats.cache.CrosshairStrategy) JFrame(javax.swing.JFrame) CacheEvent(loci.formats.cache.CacheEvent) BufferedReader(java.io.BufferedReader) ImageReader(loci.formats.ImageReader) CacheComponent(loci.formats.gui.CacheComponent) EmptyBorder(javax.swing.border.EmptyBorder) Cache(loci.formats.cache.Cache) ByteArraySource(loci.formats.cache.ByteArraySource)

Example 57 with ImageReader

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

the class BaseModelReaderTest method testSetId.

@Test
public void testSetId() throws Exception {
    reader = new MinMaxCalculator(new ChannelSeparator(new ChannelFiller(new ImageReader())));
    metadata = new OMEXMLMetadataImpl();
    reader.setMetadataStore(metadata);
    reader.setId(temporaryFile.getAbsolutePath());
}
Also used : MinMaxCalculator(loci.formats.MinMaxCalculator) ChannelFiller(loci.formats.ChannelFiller) ImageReader(loci.formats.ImageReader) ChannelSeparator(loci.formats.ChannelSeparator) OMEXMLMetadataImpl(loci.formats.ome.OMEXMLMetadataImpl) Test(org.testng.annotations.Test)

Example 58 with ImageReader

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

the class ConversionTest method testCompressDecompress.

private void testCompressDecompress(String ext, String compression, boolean lossy, int rgbChannels, int seriesCount, String pixelType, boolean littleEndian) throws Exception {
    File tmp = File.createTempFile("conversionTest", ext);
    tmp.deleteOnExit();
    ImageWriter writer = new ImageWriter();
    writer.setMetadataRetrieve(createMetadata(pixelType, rgbChannels, seriesCount, littleEndian));
    writer.setId(tmp.getAbsolutePath());
    int bytes = FormatTools.getBytesPerPixel(pixelType);
    byte[] plane = getPlane(WIDTH, HEIGHT, bytes * rgbChannels);
    Plane originalPlane = new Plane(plane, littleEndian, !writer.isInterleaved(), rgbChannels, pixelType);
    for (int s = 0; s < seriesCount; s++) {
        writer.setSeries(s);
        writer.saveBytes(0, plane);
    }
    writer.close();
    ImageReader reader = new ImageReader();
    reader.setId(tmp.getAbsolutePath());
    assertEquals(reader.getSeriesCount(), seriesCount);
    for (int s = 0; s < seriesCount; s++) {
        reader.setSeries(s);
        assertEquals(reader.getSizeC(), rgbChannels);
        assertTrue(reader.getImageCount() == rgbChannels || reader.isRGB());
        byte[] readPlane = reader.openBytes(0);
        if (!lossy) {
            Plane newPlane = new Plane(readPlane, reader.isLittleEndian(), !reader.isInterleaved(), reader.getRGBChannelCount(), FormatTools.getPixelTypeString(reader.getPixelType()));
            assertTrue(originalPlane.equals(newPlane), tmp.getAbsolutePath());
        }
    }
    reader.close();
}
Also used : ImageWriter(loci.formats.ImageWriter) ImageReader(loci.formats.ImageReader) File(java.io.File)

Example 59 with ImageReader

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

the class MetadataConfigurableTest method setUp.

@BeforeClass
public void setUp() {
    pixelsOnly = new ImageReader();
    pixelsOnly.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.MINIMUM));
    all = new ImageReader();
    all.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.ALL));
    noOverlays = new ImageReader();
    noOverlays.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.NO_OVERLAYS));
    id = getProperty(FILENAME_PROPERTY);
    if (null == id) {
        LOGGER.error(SKIP_MESSAGE);
        throw new SkipException(SKIP_MESSAGE);
    }
}
Also used : SkipException(org.testng.SkipException) ImageReader(loci.formats.ImageReader) DefaultMetadataOptions(loci.formats.in.DefaultMetadataOptions) BeforeClass(org.testng.annotations.BeforeClass)

Example 60 with ImageReader

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

the class SubResolutionTest method init.

@Parameters({ "id" })
@BeforeClass
public void init(String id) throws FormatException, IOException {
    this.id = id;
    reader = new ImageReader();
    reader.setFlattenedResolutions(false);
    reader.setId(id);
}
Also used : ImageReader(loci.formats.ImageReader) BeforeClass(org.testng.annotations.BeforeClass) Parameters(org.testng.annotations.Parameters)

Aggregations

ImageReader (loci.formats.ImageReader)71 ServiceFactory (loci.common.services.ServiceFactory)23 OMEXMLService (loci.formats.services.OMEXMLService)23 FormatException (loci.formats.FormatException)20 IFormatReader (loci.formats.IFormatReader)20 IMetadata (loci.formats.meta.IMetadata)19 Test (org.testng.annotations.Test)15 IOException (java.io.IOException)11 ChannelFiller (loci.formats.ChannelFiller)11 DependencyException (loci.common.services.DependencyException)10 ServiceException (loci.common.services.ServiceException)10 ChannelSeparator (loci.formats.ChannelSeparator)10 MinMaxCalculator (loci.formats.MinMaxCalculator)10 BufferedImageReader (loci.formats.gui.BufferedImageReader)8 ImageWriter (loci.formats.ImageWriter)7 File (java.io.File)6 FileStitcher (loci.formats.FileStitcher)6 OMETiffWriter (loci.formats.out.OMETiffWriter)6 Location (loci.common.Location)5 DimensionSwapper (loci.formats.DimensionSwapper)5