changeset 973:55572a4bbf68

DocuImage implementation using Bio-Formats library (doesn't work yet).
author robcast
date Thu, 26 Jan 2012 15:02:29 +0100
parents 2ad5ba781e88
children 678313a989a9
files .hgignore common-bioformats/pom.xml common-bioformats/src/main/java/digilib/image/BioFormatsDocuImage.java
diffstat 3 files changed, 231 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Thu Jan 26 15:00:13 2012 +0100
+++ b/.hgignore	Thu Jan 26 15:02:29 2012 +0100
@@ -81,4 +81,12 @@
 syntax: regexp
 ^common-jai/\.classpath$
 syntax: regexp
-^common-jai/\.project$
\ No newline at end of file
+^common-jai/\.project$
+syntax: regexp
+^common-bioformats/\.classpath$
+syntax: regexp
+^common-bioformats/\.project$
+syntax: regexp
+^common-bioformats/target$
+syntax: regexp
+^common-bioformats/\.settings$
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common-bioformats/pom.xml	Thu Jan 26 15:02:29 2012 +0100
@@ -0,0 +1,32 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <artifactId>digilib</artifactId>
+    <groupId>digilib</groupId>
+    <version>2.1-SNAPSHOT</version>
+    <relativePath>..</relativePath>
+  </parent>
+  <artifactId>digilib-common-bioformats</artifactId>
+  <name>digilib-common-bioformats</name>
+  <description>digilib codec implementation using Bio-Formats (http://loci.wisc.edu/software/bio-formats) library.</description>
+	<repositories>
+		<!--  we currently use ImageJ snapshot repo -->
+		<repository>
+			<id>imagej.snapshots</id>
+			<url>http://code.imagej.net/maven2/snapshots</url>
+			<snapshots/>
+		</repository>
+	</repositories>
+
+  <dependencies>
+  	<dependency>
+  		<groupId>digilib</groupId>
+  		<artifactId>digilib-common</artifactId>
+  	</dependency>
+  	<dependency>
+  		<groupId>loci</groupId>
+  		<artifactId>bio-formats</artifactId>
+  		<version>4.3.3-SNAPSHOT</version>
+  	</dependency>
+  </dependencies>
+</project>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common-bioformats/src/main/java/digilib/image/BioFormatsDocuImage.java	Thu Jan 26 15:02:29 2012 +0100
@@ -0,0 +1,190 @@
+/**
+ * 
+ */
+package digilib.image;
+
+import java.awt.RenderingHints;
+import java.awt.geom.AffineTransform;
+import java.awt.image.AffineTransformOp;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import loci.formats.FormatException;
+import loci.formats.IFormatWriter;
+import loci.formats.ImageReader;
+import loci.formats.ImageWriter;
+import loci.formats.gui.BufferedImageReader;
+import loci.formats.gui.BufferedImageWriter;
+import loci.formats.meta.DummyMetadata;
+import loci.formats.meta.MetadataRetrieve;
+
+import digilib.io.FileOpException;
+import digilib.io.ImageInput;
+import digilib.util.ImageSize;
+
+/**
+ * @author casties
+ * 
+ */
+public class BioFormatsDocuImage extends DocuImageImpl {
+
+    private BufferedImage img;
+    private Object imageSize;
+    private RenderingHints renderHint;
+    private ImageReader reader;
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see digilib.image.DocuImageImpl#identify(digilib.io.ImageInput)
+     */
+    @Override
+    public ImageInput identify(ImageInput ii) throws IOException {
+        ImageReader reader = new ImageReader();
+        try {
+            reader.setId(ii.getFile().getAbsolutePath());
+        } catch (FormatException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        int width = reader.getSizeX();
+        int height = reader.getSizeY();
+        String fmt = reader.getFormat();
+
+        String mt = "";
+        if (fmt.equalsIgnoreCase("Tagged Image File Format")) {
+            mt = "image/tiff";
+        } else if (fmt.equalsIgnoreCase("JPEG")) {
+            mt = "image/jpeg";
+        }
+
+        logger.debug("BioFormats identify: width=" + width + " height=" + height + " format=" + fmt + " mimetype=" + mt);
+        ii.setSize(new ImageSize(width, height));
+        ii.setMimetype(mt);
+        return ii;
+    }
+
+    @Override
+    public void loadImage(ImageInput ii) throws FileOpException {
+        logger.debug("loadImage: " + ii);
+        this.input = ii;
+        reader = new ImageReader();
+        try {
+            reader.setId(ii.getFile().getAbsolutePath());
+            BufferedImageReader biReader = BufferedImageReader.makeBufferedImageReader(reader);
+            img = biReader.openImage(0);
+            logger.debug("image loaded: " + img);
+        } catch (FormatException e) {
+            throw new FileOpException("Unable to load image format: " + e);
+        } catch (IOException e) {
+            throw new FileOpException("Unable to load image file: " + e);
+        }
+    }
+
+    @Override
+    public void scale(double scaleX, double scaleY) throws ImageOpException {
+        logger.debug("scale: " + scaleX);
+        /* for downscaling in high quality the image is blurred first */
+        if ((scaleX <= 0.5) && (quality > 1)) {
+            int bl = (int) Math.floor(1 / scaleX);
+            // blur(bl);
+        }
+        /* then scaled */
+        AffineTransformOp scaleOp = new AffineTransformOp(AffineTransform.getScaleInstance(scaleX, scaleY), renderHint);
+        img = scaleOp.filter(img, null);
+        logger.debug("scaled to " + img.getWidth() + "x" + img.getHeight() + " img=" + img);
+        // invalidate image size
+        imageSize = null;
+    }
+
+    public void setQuality(int qual) {
+        quality = qual;
+        renderHint = new RenderingHints(null);
+        // hint.put(RenderingHints.KEY_ANTIALIASING,
+        // RenderingHints.VALUE_ANTIALIAS_OFF);
+        // setup interpolation quality
+        if (qual > 0) {
+            logger.debug("quality q1");
+            renderHint.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
+        } else {
+            logger.debug("quality q0");
+            renderHint.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
+        }
+    }
+
+    public void crop(int x_off, int y_off, int width, int height) throws ImageOpException {
+        // setup Crop
+        img = img.getSubimage(x_off, y_off, width, height);
+        logger.debug("CROP:" + img.getWidth() + "x" + img.getHeight());
+        // invalidate image size
+        imageSize = null;
+    }
+
+    @Override
+    public void writeImage(String mt, OutputStream ostream) throws ImageOpException, FileOpException {
+        logger.debug("writeImage");
+        File outFile;
+        String filext = ".jpg";
+        if (mt.equals("image/png")) {
+            filext = ".png";
+        }
+        try {
+            outFile = File.createTempFile("biof_temp", filext);
+        } catch (IOException e) {
+            throw new FileOpException(e.toString());
+        }
+        // save image to file
+        ImageWriter iw = new ImageWriter();
+        /*
+         * try { //iw.setMetadataRetrieve(new DummyMetadata());
+         * //iw.setSeries(0); //iw.setId(outFile.getAbsolutePath());
+         * //logger.debug("writer="+iw); } catch (FormatException e) { throw new
+         * FileOpException(e.toString()); } catch (IOException e) { // TODO
+         * Auto-generated catch block e.printStackTrace(); } /* if
+         * (mt.endsWith("png")) { iw = }
+         */
+        BufferedImageWriter writer = BufferedImageWriter.makeBufferedImageWriter(iw);
+        try {
+            logger.debug("setting metadata");
+            iw.setMetadataRetrieve((MetadataRetrieve) reader.getMetadataStore());
+            logger.debug("writing to file  " + outFile);
+            writer.setId(outFile.getAbsolutePath());
+            logger.debug("saving image " + img);
+            writer.saveImage(0, img);
+            logger.debug("closing file");
+            writer.close();
+        } catch (FormatException e1) {
+            // TODO Auto-generated catch block
+            e1.printStackTrace();
+        } catch (IOException e1) {
+            // TODO Auto-generated catch block
+            e1.printStackTrace();
+        }
+        // now send file
+        FileInputStream inFile = null;
+        try {
+            inFile = new FileInputStream(outFile);
+            byte dataBuffer[] = new byte[4096];
+            int len;
+            while ((len = inFile.read(dataBuffer)) != -1) {
+                // copy out file
+                ostream.write(dataBuffer, 0, len);
+            }
+        } catch (IOException e) {
+            throw new FileOpException(e.toString());
+        } finally {
+            try {
+                if (inFile != null) {
+                    inFile.close();
+                }
+            } catch (IOException e) {
+                // nothing to do
+            }
+        }
+
+    }
+
+}