tiff adobe deflate / zip support

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

tiff adobe deflate / zip support

Jason Newton
Hi,

There was very little code needed to get this done given java.util.zip.  
It works for me as is, let me know if there's any problems.  I based it
off 1.4.5e and exported the patch via git format-patch.  I  couldn't get
the mailing list to take it as an attachment so I embedded it in the
email body.  Btw is there a more proper path to submitting a patch to
imagej?  I couldn't find squat.

-Jason Newton


 From 9bbe364c52d537c2364aa1b0625dc8f44c6c4712 Mon Sep 17 00:00:00 2001
From: Jason Newton <[hidden email]>
Date: Sat, 23 Apr 2011 03:38:35 -0700
Subject: [PATCH] add zip/deflate support to tif decoding

---
  ij/io/FileInfo.java    |    1 +
  ij/io/ImageReader.java |   24 +++++++++++++++++++++++-
  ij/io/TiffDecoder.java |    3 +++
  3 files changed, 27 insertions(+), 1 deletions(-)

diff --git a/ij/io/FileInfo.java b/ij/io/FileInfo.java
index 0dcf775..db34875 100644
--- a/ij/io/FileInfo.java
+++ b/ij/io/FileInfo.java
@@ -85,6 +85,7 @@ public class FileInfo implements Cloneable {
      public static final int LZW_WITH_DIFFERENCING = 3;
      public static final int JPEG = 4;
      public static final int PACK_BITS = 5;
+    public static final int ZIP = 6;

      /* File format (TIFF, GIF_OR_JPG, BMP, etc.). Used by the
File/Revert command */
      public int fileFormat;
diff --git a/ij/io/ImageReader.java b/ij/io/ImageReader.java
index 8d313b2..759847d 100644
--- a/ij/io/ImageReader.java
+++ b/ij/io/ImageReader.java
@@ -1,10 +1,13 @@
  package ij.io;
+import ij.util.Tools;
  import ij.*;
  import ij.process.*;
  import java.io.*;
  import java.net.*;
  import java.awt.image.BufferedImage;
  import javax.imageio.ImageIO;
+import java.util.zip.Inflater;
+import java.util.zip.DataFormatException;

  /** Reads raw 8-bit, 16-bit or 32-bit (float or RGB)
      images from a stream or URL. */
@@ -845,6 +848,8 @@ public class ImageReader {
              return packBitsUncompress(input,
fi.rowsPerStrip*fi.width*fi.getBytesPerPixel());
          else if (fi.compression==FileInfo.LZW ||
fi.compression==FileInfo.LZW_WITH_DIFFERENCING)
              return lzwUncompress(input);
+        else if (fi.compression==FileInfo.ZIP)
+             return zipUncompress(input);
          else
              return input;
      }
@@ -912,7 +917,24 @@ public class ImageReader {
          }
          return out.toByteArray();
      }
-
+
+    public byte[] zipUncompress(byte[] input){
+        ByteArrayOutputStream imageBuffer = new ByteArrayOutputStream();
+        byte[] buffer = new byte[1024];
+        Inflater decompressor = new Inflater();
+        decompressor.setInput(input);
+        try{
+            while(!decompressor.finished()){
+                int rlen = decompressor.inflate(buffer);
+                imageBuffer.write(buffer, 0, rlen);
+            }
+        }catch(DataFormatException e){
+            IJ.log(e.toString());
+        }
+        decompressor.end();
+        return imageBuffer.toByteArray();
+    }
+
      /** Based on the Bio-Formats PackbitsCodec written by Melissa
Linkert. */
      public byte[] packBitsUncompress(byte[] input, int expected) {
          if (expected==0) expected = Integer.MAX_VALUE;
diff --git a/ij/io/TiffDecoder.java b/ij/io/TiffDecoder.java
index 6bdb5c7..3bb3dec 100644
--- a/ij/io/TiffDecoder.java
+++ b/ij/io/TiffDecoder.java
@@ -1,4 +1,5 @@
  package ij.io;
+import ij.*;
  import ij.util.Tools;
  import java.io.*;
  import java.util.*;
@@ -483,6 +484,8 @@ public class TiffDecoder {
                          fi.compression = FileInfo.LZW;
                      else if (value==32773)  // PackBits compression
                          fi.compression = FileInfo.PACK_BITS;
+                    else if (value == 32946 || value == 8)
+                        fi.compression = FileInfo.ZIP;
                      else if (value!=1 && value!=0 &&
!(value==7&&fi.width<500)) {
                          // don't abort with Spot camera compressed (7)
thumbnails
                          // otherwise, this is an unknown compression type
--
1.7.4.2
Reply | Threaded
Open this post in threaded view
|

Re: tiff adobe deflate / zip support (V2)

Jason Newton
Hi,

I removed a few unneeded imports and re-exported the patch.

-Jason Newton



 From 437b851a071a8050e0b897f1d9e3ba14af107963 Mon Sep 17 00:00:00 2001
From: Jason Newton <[hidden email]>
Date: Sat, 23 Apr 2011 03:38:35 -0700
Subject: [PATCH] add zip/deflate support to tif decoding

---
  ij/io/FileInfo.java    |    1 +
  ij/io/ImageReader.java |   23 ++++++++++++++++++++++-
  ij/io/TiffDecoder.java |    2 ++
  3 files changed, 25 insertions(+), 1 deletions(-)

diff --git a/ij/io/FileInfo.java b/ij/io/FileInfo.java
index 0dcf775..db34875 100644
--- a/ij/io/FileInfo.java
+++ b/ij/io/FileInfo.java
@@ -85,6 +85,7 @@ public class FileInfo implements Cloneable {
      public static final int LZW_WITH_DIFFERENCING = 3;
      public static final int JPEG = 4;
      public static final int PACK_BITS = 5;
+    public static final int ZIP = 6;

      /* File format (TIFF, GIF_OR_JPG, BMP, etc.). Used by the
File/Revert command */
      public int fileFormat;
diff --git a/ij/io/ImageReader.java b/ij/io/ImageReader.java
index 8d313b2..8fafed1 100644
--- a/ij/io/ImageReader.java
+++ b/ij/io/ImageReader.java
@@ -5,6 +5,8 @@ import java.io.*;
  import java.net.*;
  import java.awt.image.BufferedImage;
  import javax.imageio.ImageIO;
+import java.util.zip.Inflater;
+import java.util.zip.DataFormatException;

  /** Reads raw 8-bit, 16-bit or 32-bit (float or RGB)
      images from a stream or URL. */
@@ -845,6 +847,8 @@ public class ImageReader {
              return packBitsUncompress(input,
fi.rowsPerStrip*fi.width*fi.getBytesPerPixel());
          else if (fi.compression==FileInfo.LZW ||
fi.compression==FileInfo.LZW_WITH_DIFFERENCING)
              return lzwUncompress(input);
+        else if (fi.compression==FileInfo.ZIP)
+             return zipUncompress(input);
          else
              return input;
      }
@@ -912,7 +916,24 @@ public class ImageReader {
          }
          return out.toByteArray();
      }
-
+
+    public byte[] zipUncompress(byte[] input){
+        ByteArrayOutputStream imageBuffer = new ByteArrayOutputStream();
+        byte[] buffer = new byte[1024];
+        Inflater decompressor = new Inflater();
+        decompressor.setInput(input);
+        try{
+            while(!decompressor.finished()){
+                int rlen = decompressor.inflate(buffer);
+                imageBuffer.write(buffer, 0, rlen);
+            }
+        }catch(DataFormatException e){
+            IJ.log(e.toString());
+        }
+        decompressor.end();
+        return imageBuffer.toByteArray();
+    }
+
      /** Based on the Bio-Formats PackbitsCodec written by Melissa
Linkert. */
      public byte[] packBitsUncompress(byte[] input, int expected) {
          if (expected==0) expected = Integer.MAX_VALUE;
diff --git a/ij/io/TiffDecoder.java b/ij/io/TiffDecoder.java
index 6bdb5c7..488c1b7 100644
--- a/ij/io/TiffDecoder.java
+++ b/ij/io/TiffDecoder.java
@@ -483,6 +483,8 @@ public class TiffDecoder {
                          fi.compression = FileInfo.LZW;
                      else if (value==32773)  // PackBits compression
                          fi.compression = FileInfo.PACK_BITS;
+                    else if (value == 32946 || value == 8)
+                        fi.compression = FileInfo.ZIP;
                      else if (value!=1 && value!=0 &&
!(value==7&&fi.width<500)) {
                          // don't abort with Spot camera compressed (7)
thumbnails
                          // otherwise, this is an unknown compression type
--
1.7.4.2
Reply | Threaded
Open this post in threaded view
|

Re: tiff adobe deflate / zip support (V2)

dscho
Hi Jason,

On Sat, 23 Apr 2011, Jason Newton wrote:

> I removed a few unneeded imports and re-exported the patch.

It was still whitespace-damaged, so I "applied" it by copy-pasting the
relevant parts. While at it, I also fixed some style issues to reflect the
coding style surrounding the code you added.

http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=ImageJA.git;a=commitdiff;h=d0c480346f467dec560c4b91a8d22f798d0954a8;hb=refs/heads/master

Would you have a small example TIFF, too?

Thank you,
Johannes
Reply | Threaded
Open this post in threaded view
|

Re: tiff adobe deflate / zip support (V2)

Jason Newton
On 04/23/2011 08:46 AM, Johannes Schindelin wrote:
> Hi Jason,
>
> It was still whitespace-damaged, so I "applied" it by copy-pasting the
> relevant parts. While at it, I also fixed some style issues to reflect the
> coding style surrounding the code you added.

Ah, sorry, I tried undoing tabexpand in my vim to match your whitespace
so it looked the same on my end as far as I could tell.
> http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=ImageJA.git;a=commitdiff;h=d0c480346f467dec560c4b91a8d22f798d0954a8;hb=refs/heads/master
>
> Would you have a small example TIFF, too?

Sure, go bananas.
> Thank you,
> Johannes
>


bananas.tif (24K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: tiff adobe deflate / zip support (V2)

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by Jason Newton
On Apr 23, 2011, at 7:38 AM, Jason Newton wrote:

> Hi,
>
> I removed a few unneeded imports and re-exported the patch.
>
> -Jason Newton

This patch is in the 1.45g1 daily build.

-wayne

> From 437b851a071a8050e0b897f1d9e3ba14af107963 Mon Sep 17 00:00:00 2001
> From: Jason Newton <[hidden email]>
> Date: Sat, 23 Apr 2011 03:38:35 -0700
> Subject: [PATCH] add zip/deflate support to tif decoding
>
> ---
>  ij/io/FileInfo.java    |    1 +
>  ij/io/ImageReader.java |   23 ++++++++++++++++++++++-
>  ij/io/TiffDecoder.java |    2 ++
>  3 files changed, 25 insertions(+), 1 deletions(-)
>
> diff --git a/ij/io/FileInfo.java b/ij/io/FileInfo.java
> index 0dcf775..db34875 100644
> --- a/ij/io/FileInfo.java
> +++ b/ij/io/FileInfo.java
> @@ -85,6 +85,7 @@ public class FileInfo implements Cloneable {
>      public static final int LZW_WITH_DIFFERENCING = 3;
>      public static final int JPEG = 4;
>      public static final int PACK_BITS = 5;
> +    public static final int ZIP = 6;
>
>      /* File format (TIFF, GIF_OR_JPG, BMP, etc.). Used by the
> File/Revert command */
>      public int fileFormat;
> diff --git a/ij/io/ImageReader.java b/ij/io/ImageReader.java
> index 8d313b2..8fafed1 100644
> --- a/ij/io/ImageReader.java
> +++ b/ij/io/ImageReader.java
> @@ -5,6 +5,8 @@ import java.io.*;
>  import java.net.*;
>  import java.awt.image.BufferedImage;
>  import javax.imageio.ImageIO;
> +import java.util.zip.Inflater;
> +import java.util.zip.DataFormatException;
>
>  /** Reads raw 8-bit, 16-bit or 32-bit (float or RGB)
>      images from a stream or URL. */
> @@ -845,6 +847,8 @@ public class ImageReader {
>              return packBitsUncompress(input,
> fi.rowsPerStrip*fi.width*fi.getBytesPerPixel());
>          else if (fi.compression==FileInfo.LZW ||
> fi.compression==FileInfo.LZW_WITH_DIFFERENCING)
>              return lzwUncompress(input);
> +        else if (fi.compression==FileInfo.ZIP)
> +             return zipUncompress(input);
>          else
>              return input;
>      }
> @@ -912,7 +916,24 @@ public class ImageReader {
>          }
>          return out.toByteArray();
>      }
> -
> +
> +    public byte[] zipUncompress(byte[] input){
> +        ByteArrayOutputStream imageBuffer = new ByteArrayOutputStream();
> +        byte[] buffer = new byte[1024];
> +        Inflater decompressor = new Inflater();
> +        decompressor.setInput(input);
> +        try{
> +            while(!decompressor.finished()){
> +                int rlen = decompressor.inflate(buffer);
> +                imageBuffer.write(buffer, 0, rlen);
> +            }
> +        }catch(DataFormatException e){
> +            IJ.log(e.toString());
> +        }
> +        decompressor.end();
> +        return imageBuffer.toByteArray();
> +    }
> +
>      /** Based on the Bio-Formats PackbitsCodec written by Melissa
> Linkert. */
>      public byte[] packBitsUncompress(byte[] input, int expected) {
>          if (expected==0) expected = Integer.MAX_VALUE;
> diff --git a/ij/io/TiffDecoder.java b/ij/io/TiffDecoder.java
> index 6bdb5c7..488c1b7 100644
> --- a/ij/io/TiffDecoder.java
> +++ b/ij/io/TiffDecoder.java
> @@ -483,6 +483,8 @@ public class TiffDecoder {
>                          fi.compression = FileInfo.LZW;
>                      else if (value==32773)  // PackBits compression
>                          fi.compression = FileInfo.PACK_BITS;
> +                    else if (value == 32946 || value == 8)
> +                        fi.compression = FileInfo.ZIP;
>                      else if (value!=1 && value!=0 &&
> !(value==7&&fi.width<500)) {
>                          // don't abort with Spot camera compressed (7)
> thumbnails
>                          // otherwise, this is an unknown compression type
> --
> 1.7.4.2