Re: Fixes to PNM/PGM/... reader/writer

Posted by dscho on
URL: http://imagej.273.s1.nabble.com/Fixes-to-PNM-PGM-reader-writer-tp3700640p3700647.html

Hi,

On Thu, 11 Jan 2007, Johannes Schindelin wrote:

> On Wed, 10 Jan 2007, Spencer Eugene Olson wrote:
>
> > I don't think that my changes were too complicated, but that is just
> > my opinion (especially in the writer).
>
> What I wanted to say is that your patch does not introduce the minimal
> change to do what it tries to do. Maybe I did a poor job explaining
> that.

FWIW here is what I consider a concise, easy-to-review patch for the
reader part:

---

 ij/plugin/PGM_Reader.java |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/ij/plugin/PGM_Reader.java b/ij/plugin/PGM_Reader.java
index 5a86806..6eaacf0 100644
--- a/ij/plugin/PGM_Reader.java
+++ b/ij/plugin/PGM_Reader.java
@@ -68,6 +68,7 @@ public class PGM_Reader extends ImagePlus implements PlugIn {
     private boolean isColor;
     private boolean isBlackWhite;
     private int maxValue;
+    private ImageStack stack;
 
     public void run(String arg) {
         OpenDialog od = new OpenDialog("PBM/PGM/PPM Reader...", arg);
@@ -112,18 +113,28 @@ public class PGM_Reader extends ImagePlus implements PlugIn {
         tok.parseNumbers();
         tok.eolIsSignificant(true);
         tok.commentChar('#');
+ try {
+ while (true)
+ readSlice(tok, is);
+ } catch(IOException e) { /* end of file */ }
+ return stack;
+    }
+
+    private ImageStack readSlice(StreamTokenizer tok, InputStream is)
+    throws IOException {
         openHeader(tok);
+ if (stack == null)
+ stack = new ImageStack(width, height);
+
  //IJ.log("PGM_Reader: w="+width+",h="+height+",raw="+rawBits+",16bits="+sixteenBits+",color="+isColor+",b&w="+isBlackWhite+",max="+maxValue);
         
         if (!isColor && sixteenBits) { // 16-bit grayscale
             if (rawBits) {
                 ImageProcessor ip = open16bitRawImage(is, width, height);
-                ImageStack stack = new ImageStack(width, height);
                 stack.addSlice("", ip);
             return stack;
             } else {
                 ImageProcessor ip = open16bitAsciiImage(tok, width, height);
-                ImageStack stack = new ImageStack(width, height);
                 stack.addSlice("", ip);
             return stack;
             }
@@ -149,7 +160,6 @@ public class PGM_Reader extends ImagePlus implements PlugIn {
  } else
  pixels[i] = (byte) (0xff & (255 * (int) (0xff & pixels[i]) / maxValue));
  }
- ImageStack stack = new ImageStack(width, height);
  stack.addSlice("", ip);
  return stack;
  }
@@ -171,7 +181,6 @@ public class PGM_Reader extends ImagePlus implements PlugIn {
  b = (b * 255 / maxValue);
  pixels[i] = 0xFF000000 | r | g | b;
  }
- ImageStack stack = new ImageStack(width, height);
  stack.addSlice("", ip);
  return stack;
  }
@@ -203,7 +212,6 @@ public class PGM_Reader extends ImagePlus implements PlugIn {
  blue[i] = (short)(pixels[i*3+2]&0xffffff);
  }
  }
- ImageStack stack = new ImageStack(width, height);
  stack.addSlice("red", new ShortProcessor(width, height, red, null));
  stack.addSlice("green", new ShortProcessor(width, height, green, null));
  stack.addSlice("blue", new ShortProcessor(width, height, blue, null));