Tiny PGM file reading bug and proposed fix.

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

Tiny PGM file reading bug and proposed fix.

Harry Parker
I get Java exceptions when trying to open tiny PGM test image files. This is with the latest ImageJ, v. 1.41i.

The exception is:

java.lang.ArithmeticException: / by zero
    at ij.plugin.PGM_Reader.openAsciiImage(PGM_Reader.java:261)
    at ij.plugin.PGM_Reader.openFile(PGM_Reader.java:134)
    at ij.plugin.PGM_Reader.run(PGM_Reader.java:83)
    at ij.IJ.runPlugIn(IJ.java:142)
    at ij.IJ.runPlugIn(IJ.java:124)
    at ij.io.Opener.openImage(Opener.java:213)
    at ij.io.Opener.openImage(Opener.java:253)
    at ij.io.Opener.open(Opener.java:117)
    at ij.io.Opener.openAndAddToRecent(Opener.java:177)
    at ij.plugin.DragAndDrop.openFile(DragAndDrop.java:101)
    at ij.plugin.DragAndDrop.run(DragAndDrop.java:87)
    at java.lang.Thread.run(Thread.java:619)


I found the problem in PGM_Reader's function, openAsciiImage(). This is the function:

public void openAsciiImage(StreamTokenizer tok, int size, byte[] pixels) throws IOException {
   int i = 0;
   int inc = size / 20;
   while (tok.nextToken() != tok.TT_EOF) {
      if (tok.ttype == tok.TT_NUMBER) {
          pixels[i++] = (byte) (((int) tok.nval) & 255);
          if (i % inc == 0)
              IJ.showProgress(0.5 + ((double) i / size) / 2.0);
      }
   }
   IJ.showProgress(1.0);
}
The problem is that my tiny, hand-crafted test images are less than 20 pixels in size, so "inc" becomes zero and the "i % inc" throws the divide by zero exception. (This problem also occurs in the  open16bitAsciiImage function in the same file.)

Would it be possible to correct this corner case?  It cost me several days to track it down, as I assumed the problem was in my code, not ImageJ's.

One simple solution is just skip the progress indication code if the size is less than some reasonable value, e.g. 10000 pixels.


Thanks.

--
Harry Parker
Senior Imaging Systems Engineer
Digital Imaging Systems, Inc.



     
Reply | Threaded
Open this post in threaded view
|

Re: Tiny PGM file reading bug and proposed fix.

dscho
Hi Harry,

sorry it took me so long, my inbox is only now starting to look better...

On Thu, 31 Jul 2008, Harry Parker wrote:

> I get Java exceptions when trying to open tiny PGM test image files.
> This is with the latest ImageJ, v. 1.41i.

How does this look?

-- snipsnap --
[PATCH] PGM_Reader: fix division by zero

When reading .pgm files which have a size smaller than 20, there was
a division by zero.

Noticed by Harry Parker.

Signed-off-by: Johannes Schindelin <[hidden email]>
---
 ij/plugin/PGM_Reader.java |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/ij/plugin/PGM_Reader.java b/ij/plugin/PGM_Reader.java
index c0caee7..378c6f7 100644
--- a/ij/plugin/PGM_Reader.java
+++ b/ij/plugin/PGM_Reader.java
@@ -259,7 +259,7 @@ public class PGM_Reader extends ImagePlus implements PlugIn {
         while (tok.nextToken() != tok.TT_EOF) {
             if (tok.ttype == tok.TT_NUMBER) {
                 pixels[i++] = (byte) (((int) tok.nval) & 255);
-                if (i % inc == 0)
+                if (inc != 0 && (i % inc == 0))
                     IJ.showProgress(0.5 + ((double) i / size) / 2.0);
             }
         }
@@ -293,7 +293,7 @@ public class PGM_Reader extends ImagePlus implements PlugIn {
         while (tok.nextToken() != tok.TT_EOF) {
             if (tok.ttype == tok.TT_NUMBER) {
                 pixels[i++] = (short) (((int) tok.nval) & 65535);
-                if (i % inc == 0)
+                if (int != 0 && (i % inc == 0))
                     IJ.showProgress(0.5 + ((double) i / size) / 2.0);
             }
         }
--
1.6.2.rc0.344.gecd284
Reply | Threaded
Open this post in threaded view
|

Re: Tiny PGM file reading bug and proposed fix.

Harry Parker
Hi Johannes,

Thanks for the effort, but Wayne fixed this last August in version 1.41j.

--
Harry Parker
Senior Imaging Systems Engineer
Currently available for hire in NJ and Eastern PA or thru the 'net.






________________________________
From: Johannes Schindelin <[hidden email]>
To: Harry Parker <[hidden email]>
Cc: [hidden email]
Sent: Thursday, February 12, 2009 9:10:34 PM
Subject: Re: Tiny PGM file reading bug and proposed fix.

Hi Harry,

sorry it took me so long, my inbox is only now starting to look better...

On Thu, 31 Jul 2008, Harry Parker wrote:

> I get Java exceptions when trying to open tiny PGM test image files.
> This is with the latest ImageJ, v. 1.41i.

How does this look?

-- snipsnap --
[PATCH] PGM_Reader: fix division by zero

When reading .pgm files which have a size smaller than 20, there was
a division by zero.

Noticed by Harry Parker.

Signed-off-by: Johannes Schindelin <[hidden email]>
---
ij/plugin/PGM_Reader.java |    4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/ij/plugin/PGM_Reader.java b/ij/plugin/PGM_Reader.java
index c0caee7..378c6f7 100644
--- a/ij/plugin/PGM_Reader.java
+++ b/ij/plugin/PGM_Reader.java
@@ -259,7 +259,7 @@ public class PGM_Reader extends ImagePlus implements PlugIn {
         while (tok.nextToken() != tok.TT_EOF) {
             if (tok.ttype == tok.TT_NUMBER) {
                 pixels[i++] = (byte) (((int) tok.nval) & 255);
-                if (i % inc == 0)
+                if (inc != 0 && (i % inc == 0))
                     IJ.showProgress(0.5 + ((double) i / size) / 2.0);
             }
         }
@@ -293,7 +293,7 @@ public class PGM_Reader extends ImagePlus implements PlugIn {
         while (tok.nextToken() != tok.TT_EOF) {
             if (tok.ttype == tok.TT_NUMBER) {
                 pixels[i++] = (short) (((int) tok.nval) & 65535);
-                if (i % inc == 0)
+                if (int != 0 && (i % inc == 0))
                     IJ.showProgress(0.5 + ((double) i / size) / 2.0);
             }
         }
--
1.6.2.rc0.344.gecd284