// // IPWReader.java // /* OME Bio-Formats package for reading and converting biological file formats. Copyright (C) 2005-@year@ UW-Madison LOCI and Glencoe Software, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package loci.formats.in; import java.io.File; import java.io.IOException; import java.util.Hashtable; import java.util.StringTokenizer; import java.util.Vector; import loci.common.DataTools; import loci.common.DateTools; import loci.common.Location; import loci.common.RandomAccessInputStream; import loci.common.services.DependencyException; import loci.common.services.ServiceFactory; import loci.formats.FormatException; import loci.formats.FormatReader; import loci.formats.FormatTools; import loci.formats.MetadataTools; import loci.formats.MissingLibraryException; import loci.formats.meta.MetadataStore; import loci.formats.services.POIService; import loci.formats.tiff.IFD; import loci.formats.tiff.IFDList; import loci.formats.tiff.PhotoInterp; import loci.formats.tiff.TiffParser; /** * IPWReader is the file format reader for Image-Pro Workspace (IPW) files. * * <dl><dt><b>Source code:</b></dt> * <dd><a href="http://trac.openmicroscopy.org.uk/ome/browser/bioformats.git/components/bio-formats/src/loci/formats/in/IPWReader.java">Trac</a>, * <a href="http://git.openmicroscopy.org/?p=bioformats.git;a=blob;f=components/bio-formats/src/loci/formats/in/IPWReader.java;hb=HEAD">Gitweb</a></dd></dl> * * @author Melissa Linkert melissa at glencoesoftware.com */ public class IPWReader extends FormatReader { // -- Constants -- public static final int IPW_MAGIC_BYTES = 0xd0cf11e0; // -- Fields -- /** List of embedded image file names (one per image plane). */ private Hashtable<Integer, String> imageFiles; /** Helper reader - parses embedded files from the OLE document. */ private POIService poi; // -- Constructor -- /** Constructs a new IPW reader. */ public IPWReader() { super("Image-Pro Workspace", "ipw"); domains = new String[] {FormatTools.UNKNOWN_DOMAIN}; } // -- IFormatReader API methods -- /* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */ public boolean isThisType(RandomAccessInputStream stream) throws IOException { final int blockLen = 4; if (!FormatTools.validStream(stream, blockLen, false)) return false; return stream.readInt() == IPW_MAGIC_BYTES; } /* @see loci.formats.IFormatReader#get8BitLookupTable() */ public byte[][] get8BitLookupTable() throws FormatException, IOException { FormatTools.assertId(currentId, true, 1); RandomAccessInputStream stream = poi.getDocumentStream(imageFiles.get(0)); TiffParser tp = new TiffParser(stream); IFD firstIFD = tp.getFirstIFD(); int[] bits = firstIFD.getBitsPerSample(); if (bits[0] <= 8) { int[] colorMap = (int[]) firstIFD.getIFDValue(IFD.COLOR_MAP); if (colorMap == null) return null; byte[][] table = new byte[3][colorMap.length / 3]; int next = 0; for (int j=0; j<table.length; j++) { for (int i=0; i<table[0].length; i++) { table[j][i] = (byte) (colorMap[next++] >> 8); } } return table; } return null; } /* @see loci.formats.IFormatReader#getOptimalTileWidth() */ public int getOptimalTileWidth() { FormatTools.assertId(currentId, true, 1); try { RandomAccessInputStream stream = poi.getDocumentStream(imageFiles.get(0)); TiffParser tp = new TiffParser(stream); IFD ifd = tp.getFirstIFD(); stream.close(); return (int) ifd.getTileWidth(); } catch (FormatException e) { LOGGER.debug("Could not retrieve tile width", e); } catch (IOException e) { LOGGER.debug("Could not retrieve tile height", e); } return super.getOptimalTileWidth(); } /* @see loci.formats.IFormatReader#getOptimalTileHeight() */ public int getOptimalTileHeight() { FormatTools.assertId(currentId, true, 1); try { RandomAccessInputStream stream = poi.getDocumentStream(imageFiles.get(0)); TiffParser tp = new TiffParser(stream); IFD ifd = tp.getFirstIFD(); stream.close(); return (int) ifd.getTileLength(); } catch (FormatException e) { LOGGER.debug("Could not retrieve tile height", e); } catch (IOException e) { LOGGER.debug("Could not retrieve tile length", e); } return super.getOptimalTileHeight(); } /** * @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int) */ public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException { FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h); RandomAccessInputStream stream = poi.getDocumentStream(imageFiles.get(no)); TiffParser tp = new TiffParser(stream); IFD ifd = tp.getFirstIFD(); tp.getSamples(ifd, buf, x, y, w, h); stream.close(); return buf; } /* @see loci.formats.IFormatReader#close(boolean) */ public void close(boolean fileOnly) throws IOException { super.close(fileOnly); if (!fileOnly) { if (poi != null) poi.close(); poi = null; imageFiles = null; } } // -- Internal FormatReader API methods -- /* @see loci.formats.FormatReader#initFile(String) */ protected void initFile(String id) throws FormatException, IOException { super.initFile(id); in = new RandomAccessInputStream(id); try { ServiceFactory factory = new ServiceFactory(); poi = factory.getInstance(POIService.class); } catch (DependencyException de) { throw new MissingLibraryException("POI library not found", de); } poi.initialize(Location.getMappedId(currentId)); imageFiles = new Hashtable<Integer, String>(); Vector<String> fileList = poi.getDocumentList(); String description = null, creationDate = null; for (String name : fileList) { String relativePath = name.substring(name.lastIndexOf(File.separator) + 1); if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) { if (relativePath.equals("CONTENTS")) { addGlobalMeta("Version", new String(poi.getDocumentBytes(name)).trim()); } else if (relativePath.equals("FrameRate")) { byte[] b = poi.getDocumentBytes(name, 4); addGlobalMeta("Frame Rate", DataTools.bytesToInt(b, true)); } else if (relativePath.equals("FrameInfo")) { RandomAccessInputStream s = poi.getDocumentStream(name); s.order(true); for (int q=0; q<s.length()/2; q++) { addGlobalMeta("FrameInfo " + q, s.readShort()); } s.close(); } } if (relativePath.equals("ImageInfo")) { description = new String(poi.getDocumentBytes(name)).trim(); addGlobalMeta("Image Description", description); String timestamp = null; // parse the description to get channels/slices/times where applicable // basically the same as in SEQReader if (description != null) { String[] tokens = description.split("\n"); for (String token : tokens) { String label = "Timestamp"; String data = token.trim(); if (token.indexOf("=") != -1) { label = token.substring(0, token.indexOf("=")).trim(); data = token.substring(token.indexOf("=") + 1).trim(); } addGlobalMeta(label, data); if (label.equals("frames")) core[0].sizeT = Integer.parseInt(data); else if (label.equals("slices")) { core[0].sizeZ = Integer.parseInt(data); } else if (label.equals("channels")) { core[0].sizeC = Integer.parseInt(data); } else if (label.equals("Timestamp")) timestamp = data; } } if (timestamp != null) { if (timestamp.length() > 26) { timestamp = timestamp.substring(timestamp.length() - 26); } creationDate = DateTools.formatDate(timestamp, "MM/dd/yyyy HH:mm:ss.SSS aa"); } } else if (relativePath.equals("ImageTIFF")) { // pixel data String idx = "0"; if (!name.substring(0, name.lastIndexOf(File.separator)).equals("Root Entry")) { idx = name.substring(21, name.indexOf(File.separator, 22)); } imageFiles.put(new Integer(idx), name); } } LOGGER.info("Populating metadata"); core[0].imageCount = imageFiles.size(); RandomAccessInputStream stream = poi.getDocumentStream(imageFiles.get(0)); TiffParser tp = new TiffParser(stream); IFD firstIFD = tp.getFirstIFD(); stream.close(); core[0].rgb = firstIFD.getSamplesPerPixel() > 1; if (!isRGB()) { core[0].indexed = firstIFD.getPhotometricInterpretation() == PhotoInterp.RGB_PALETTE; } if (isIndexed()) { core[0].sizeC = 1; core[0].rgb = false; } core[0].littleEndian = firstIFD.isLittleEndian(); // retrieve axis sizes addGlobalMeta("slices", "1"); addGlobalMeta("channels", "1"); addGlobalMeta("frames", getImageCount()); core[0].sizeX = (int) firstIFD.getImageWidth(); core[0].sizeY = (int) firstIFD.getImageLength(); core[0].dimensionOrder = isRGB() ? "XYCZT" : "XYZCT"; if (getSizeZ() == 0) core[0].sizeZ = 1; if (getSizeC() == 0) core[0].sizeC = 1; if (getSizeT() == 0) core[0].sizeT = 1; if (getSizeZ() * getSizeC() * getSizeT() == 1 && getImageCount() != 1) { core[0].sizeZ = getImageCount(); } if (isRGB()) core[0].sizeC *= 3; int bitsPerSample = firstIFD.getBitsPerSample()[0]; core[0].pixelType = firstIFD.getPixelType(); MetadataStore store = makeFilterMetadata(); MetadataTools.populatePixels(store, this); store.setImageDescription(description, 0); if (creationDate != null) { store.setImageAcquiredDate(creationDate, 0); } else MetadataTools.setDefaultCreationDate(store, id, 0); } }