Skip to content

Commit

Permalink
* added API methods for retrieving thumbnails:
Browse files Browse the repository at this point in the history
   - BufferedImage openThumbImage(String, int)
   - byte[] openThumbBytes(String, int)
   - int getThumbSizeX(String)
   - int getThumbSizeY(String)

* fixed a bug in DataTools
* tweaked Zeiss ZVI readers (adapted from Michel Boudinot's changes)
  • Loading branch information
melissalinkert committed Aug 30, 2006
1 parent f10429b commit 650b0f4
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 23 deletions.
6 changes: 3 additions & 3 deletions loci/formats/DataTools.java
Expand Up @@ -319,9 +319,9 @@ public static long bytesToLong(byte[] bytes, int off, int len,
{
if (bytes.length - off < len) len = bytes.length - off;
long total = 0;
for (int i=0; i<len; i++) {
total |= (bytes[i] < 0 ? 256L + bytes[i] :
(long) bytes[i]) << ((little ? i : len - i - 1) * 8);
for (int i=0, ndx=off; i<len; i++, ndx++) {
total |= (bytes[ndx] < 0 ? 256L + bytes[ndx] :
(long) bytes[ndx]) << ((little ? i : len - i - 1) * 8);
}
return total;
}
Expand Down
40 changes: 40 additions & 0 deletions loci/formats/FormatReader.java
Expand Up @@ -44,6 +44,9 @@ public abstract class FormatReader extends FormatHandler
/** Debugging level. 1=basic, 2=extended, 3=everything. */
protected static final int DEBUG_LEVEL = 1;

/** Default thumbnail width and height. */
protected static final int THUMBNAIL_DIMENSION = 128;

// -- Fields --

/** Hashtable containing metadata key/value pairs. */
Expand Down Expand Up @@ -99,6 +102,16 @@ public abstract boolean isRGB(String id)
/** Get the size of the T dimension. */
public abstract int getSizeT(String id) throws FormatException, IOException;

/** Get the size of the X dimension for the thumbnail. */
public int getThumbSizeX(String id) throws FormatException, IOException {
return THUMBNAIL_DIMENSION;
}

/** Get the size of the Y dimension for the thumbnail. */
public int getThumbSizeY(String id) throws FormatException, IOException {
return THUMBNAIL_DIMENSION;
}

/** Return true if the data is in little-endian format. */
public abstract boolean isLittleEndian(String id)
throws FormatException, IOException;
Expand All @@ -120,6 +133,33 @@ public abstract BufferedImage openImage(String id, int no)
public abstract byte[] openBytes(String id, int no)
throws FormatException, IOException;

/** Obtains a thumbnail for the specified image from the given file. */
public BufferedImage openThumbImage(String id, int no)
throws FormatException, IOException
{
return ImageTools.scale(openImage(id, no),
getThumbSizeX(id), getThumbSizeY(id));
}

/**
* Obtains a thumbnail for the specified image from the given file,
* as a byte array. We assume that the thumbnail has the same number of
* channels as the original image. If there is more than one channel, then
* the resulting byte array will be of the format "RRR...BBB...GGG...".
*/
public byte[] openThumbBytes(String id, int no)
throws FormatException, IOException
{
BufferedImage img = openThumbImage(id, no);
byte[][] bytes = ImageTools.getBytes(img);
if (bytes.length == 1) return bytes[0];
byte[] rtn = new byte[bytes.length * bytes[0].length];
for (int i=0; i<bytes.length; i++) {
System.arraycopy(bytes[i], 0, rtn, bytes[0].length * i, bytes[i].length);
}
return rtn;
}

/** Closes the currently open file. */
public abstract void close() throws FormatException, IOException;

Expand Down
17 changes: 10 additions & 7 deletions loci/formats/IFormatReader.java
Expand Up @@ -28,9 +28,6 @@
import java.io.IOException;
import java.util.Hashtable;




/** Abstract superclass of all biological file format readers. */
public interface IFormatReader extends IFormatHandler {
/** Checks if the given block is a valid header for this file format. */
Expand Down Expand Up @@ -59,6 +56,12 @@ boolean isRGB(String id)
/** Get the size of the T dimension. */
int getSizeT(String id) throws FormatException, IOException;

/** Get the size of the X dimension for the thumbnail. */
int getThumbSizeX(String id) throws FormatException, IOException;

/** Get the size of the Y dimension for the thumbnail. */
int getThumbSizeY(String id) throws FormatException, IOException;

/** Return true if the data is in little-endian format. */
boolean isLittleEndian(String id)
throws FormatException, IOException;
Expand All @@ -81,15 +84,15 @@ byte[] openBytes(String id, int no)
throws FormatException, IOException;

/** Obtains a thumbnail for the specified image from the given file. */
//BufferedImage openThumbImage(String id, int no)
// throws FormatException, IOException;
BufferedImage openThumbImage(String id, int no)
throws FormatException, IOException;

/**
* Obtains a thumbnail for the specified image from the given file,
* as a byte array.
*/
//byte[] openThumbBytes(String id, int no)
// throws FormatException, IOException;
byte[] openThumbBytes(String id, int no)
throws FormatException, IOException;

/** Closes the currently open file. */
void close() throws FormatException, IOException;
Expand Down
33 changes: 33 additions & 0 deletions loci/formats/ImageReader.java
Expand Up @@ -230,6 +230,18 @@ public int getSizeT(String id) throws FormatException, IOException {
return readers[index].getSizeT(id);
}

/** Get the size of the X dimension for the thumbnail. */
public int getThumbSizeX(String id) throws FormatException, IOException {
if (!id.equals(currentId)) initFile(id);
return readers[index].getThumbSizeX(id);
}

/** Get the size of the Y dimension for the thumbnail. */
public int getThumbSizeY(String id) throws FormatException, IOException {
if (!id.equals(currentId)) initFile(id);
return readers[index].getThumbSizeY(id);
}

/** Return true if the data is in little-endian format. */
public boolean isLittleEndian(String id) throws FormatException, IOException
{
Expand Down Expand Up @@ -292,6 +304,27 @@ public BufferedImage openImage(String id, int no)
return readers[index].openImage(id, no);
}

/** Obtains a thumbnail for the specified image from the given file. */
public BufferedImage openThumbImage(String id, int no)
throws FormatException, IOException
{
if (!id.equals(currentId)) initFile(id);
return readers[index].openThumbImage(id, no);
}

/**
* Obtains a thumbnail for the specified image from the given file,
* as a byte array. We assume that the thumbnail has the same number of
* channels as the original image. If there is more than one channel, then
* the resulting byte array will be of the format "RRR...BBB...GGG...".
*/
public byte[] openThumbBytes(String id, int no)
throws FormatException, IOException
{
if (!id.equals(currentId)) initFile(id);
return readers[index].openThumbBytes(id, no);
}

/** Closes any open files. */
public void close() throws FormatException, IOException {
for (int i=0; i<readers.length; i++) readers[i].close();
Expand Down
1 change: 1 addition & 0 deletions loci/formats/in/LegacyZVIReader.java
Expand Up @@ -35,6 +35,7 @@
*
* @author Curtis Rueden ctrueden at wisc.edu
* @author Melissa Linkert linkert at cs.wisc.edu
* @author Michel Boudinot Michel dot boudinot at iaf.cnrs-gif.fr
*/
public class LegacyZVIReader extends FormatReader {

Expand Down
54 changes: 44 additions & 10 deletions loci/formats/in/ZeissZVIReader.java
Expand Up @@ -35,6 +35,7 @@
*
* @author Melissa Linkert linkert at cs.wisc.edu
* @author Curtis Rueden ctrueden at wisc.edu
* @author Michel Boudinot Michel dot boudinot at iaf.cnrs-gif.fr
*/
public class ZeissZVIReader extends FormatReader {

Expand Down Expand Up @@ -309,12 +310,18 @@ public BufferedImage openImage(String id, int no)
throws FormatException, IOException
{
if (needLegacy) return legacy.openImage(id, no);
byte[] data = openBytes(id, no);
int bpp = bitsPerSample / 8;
if (bpp == 0) bpp = bytesPerPixel;
if (bpp > 4) bpp /= 3;
return ImageTools.makeImage(data, width, height,
(!isRGB(id) || separated) ? 1 : 3, true, bpp, false);
try {
byte[] data = openBytes(id, no);
int bpp = bitsPerSample / 8;
if (bpp == 0) bpp = bytesPerPixel;
if (bpp > 4) bpp /= 3;
return ImageTools.makeImage(data, width, height,
(!isRGB(id) || separated) ? 1 : 3, true, bpp, false);
}
catch (Exception e) {
needLegacy = true;
}
return openImage(id, no);
}

/** Closes any open files. */
Expand Down Expand Up @@ -736,6 +743,11 @@ protected void initMetadata() throws FormatException, IOException {

try {
switch (type) {
case 0: // VT_EMPTY: nothing
case 1: // VT_NUL: nothing
data = null;
pt += 2;
break;
case 2: // VT_I2: 16 bit integer
data = new Integer(DataTools.bytesToInt(tag, pt, 2, true));
pt += 2;
Expand Down Expand Up @@ -765,10 +777,14 @@ protected void initMetadata() throws FormatException, IOException {
data = new String(tag, pt, length);
pt += length;
break;
case 9: // VT_DISPATCH: 16 bytes
data = new String(tag, pt, 16);
pt += 16;
break;
case 11: // VT_BOOL: 16 bit integer (true if !0)
int temp = DataTools.bytesToInt(tag, pt, 4, true);
int temp = DataTools.bytesToInt(tag, pt, 2, true);
data = new Boolean(temp != 0);
pt += 4;
pt += 2;
break;
case 16: // VT_I1: 8 bit integer
data = new Integer(DataTools.bytesToInt(tag, pt, 1, true));
Expand Down Expand Up @@ -858,14 +874,19 @@ protected void initMetadata() throws FormatException, IOException {
case 266: metadata.put("ImageRelativeTime2", data); break;
case 267: metadata.put("ImageRelativeTime3", data); break;
case 268: metadata.put("ImageRelativeTime4", data); break;
case 333: metadata.put("RelFocusPosition1", data); break;
case 334: metadata.put("RelFocusPosition2", data); break;
case 513: metadata.put("tagID_513", data); break;
case 515: metadata.put("ImageWidth", data); break;
case 516: metadata.put("ImageHeight", data); break;
case 517: metadata.put("tagID_517", data); break;
//case 518: metadata.put("PixelType", data); break;
case 519: metadata.put("NumberOfRawImages", data); break;
case 520: metadata.put("ImageSize", data); break;
case 523: metadata.put("Acquisition pause annotation", data); break;
case 530: metadata.put("Document Subtype", data); break;
case 531: metadata.put("Acquisition Bit Depth", data); break;
case 532: metadata.put("Image Memory Usage (RAM)", data); break;
case 534: metadata.put("Z-Stack single representative", data);
break;
case 769: metadata.put("Scale Factor for X", data); break;
Expand All @@ -882,6 +903,7 @@ protected void initMetadata() throws FormatException, IOException {
case 1002: metadata.put("code", data); break;
case 1003: metadata.put("Source", data); break;
case 1004: metadata.put("Message", data); break;
case 1025: metadata.put("Acquisition Date", data); break;
case 1026: metadata.put("8-bit acquisition", data); break;
case 1027: metadata.put("Camera Bit Depth", data); break;
case 1029: metadata.put("MonoReferenceLow", data); break;
Expand Down Expand Up @@ -952,6 +974,7 @@ protected void initMetadata() throws FormatException, IOException {
case 2068: metadata.put("Fluorescence Lamp Level", data); break;
case 2069: metadata.put("Fluorescence Lamp Intensity", data); break;
case 2070: metadata.put("LightManagerEnabled", data); break;
case 2071: metadata.put("tag_ID_2071", data); break;
case 2072: metadata.put("Focus Position", data); break;
case 2073: metadata.put("Stage Position X", data);break;
case 2074: metadata.put("Stage Position Y", data); break;
Expand Down Expand Up @@ -1166,6 +1189,8 @@ protected void initMetadata() throws FormatException, IOException {
case 2239:
metadata.put("Fluorescence Attenuator Position", data);
break;
case 2261: metadata.put("Objective ID", data); break;
case 2262: metadata.put("Reflector ID", data); break;
case 2307: metadata.put("Camera Framestart Left", data); break;
case 2308: metadata.put("Camera Framestart Top", data); break;
case 2309: metadata.put("Camera Frame Width", data); break;
Expand Down Expand Up @@ -1223,8 +1248,12 @@ protected void initMetadata() throws FormatException, IOException {
case 2602: metadata.put("CameraLiveGainValue", data); break;
case 2603: metadata.put("CameraLiveExposureTimeValue", data); break;
case 2604: metadata.put("CameraLiveScalingFactor", data); break;
case 2819: metadata.put("Image Index Z", data); break;
case 2820: metadata.put("Image Channel Index", data); break;
case 2821: metadata.put("Image Index T", data); break;
case 2822: metadata.put("ImageTile Index", data); break;
case 2823: metadata.put("Image acquisition Index", data); break;
case 2827: metadata.put("Image IndexS", data); break;
case 2841: metadata.put("Original Stage Position X", data); break;
case 2842: metadata.put("Original Stage Position Y", data); break;
case 3088: metadata.put("LayerDrawFlags", data); break;
Expand Down Expand Up @@ -1253,6 +1282,7 @@ protected void initMetadata() throws FormatException, IOException {
case 8198:
metadata.put("Autofocus Current Calibration Item", data);
break;
case 20478: metadata.put("tag_ID_20478", data); break;
case 65537: metadata.put("CameraFrameFullWidth", data); break;
case 65538: metadata.put("CameraFrameFullHeight", data); break;
case 65541: metadata.put("AxioCam Shutter Signal", data); break;
Expand Down Expand Up @@ -1361,6 +1391,9 @@ protected void initMetadata() throws FormatException, IOException {
case 65638: metadata.put("DeepView DoF", data); break;
case 65639: metadata.put("DeepView EDoF", data); break;
case 65643: metadata.put("DeepView Slider Name", data); break;
case 65655: metadata.put("DeepView Slider Name", data); break;
case 16777488: metadata.put("Excitation Wavelength", data); break;
case 16777489: metadata.put("Emission Wavelength", data); break;
}
}
}
Expand Down Expand Up @@ -1471,11 +1504,12 @@ private void populateStageLabel() throws FormatException, IOException {

// Stage Label
try {
int xPos = Integer.parseInt((String) metadata.get("Stage Position X"));
int yPos = Integer.parseInt((String) metadata.get("Stage Position Y"));
int xPos = Integer.parseInt(metadata.get("Stage Position X").toString());
int yPos = Integer.parseInt(metadata.get("Stage Position Y").toString());
store.setStageLabel(null, new Float(xPos), new Float(yPos), null, null);
}
catch (NumberFormatException n) { }
catch (NullPointerException npe) { }
}

// -- Main method --
Expand Down
5 changes: 2 additions & 3 deletions loci/plugins/browser/ImagePreview.java
Expand Up @@ -61,10 +61,9 @@ public void loadImage() {

ImageReader ir = new ImageReader();
FormatReader fr = (FormatReader) ir.getReader(file.getAbsolutePath());
BufferedImage image = fr.openImage(file.getAbsolutePath(),0);
BufferedImage image = fr.openThumbImage(file.getAbsolutePath(),0);

thumbnail =
new ImageIcon(image.getScaledInstance(90, -1, Image.SCALE_DEFAULT));
thumbnail = new ImageIcon(image);
}
catch (Exception e) { e.printStackTrace(); }
}
Expand Down

0 comments on commit 650b0f4

Please sign in to comment.