FileDocCategorySizeDatePackage
PPMImageDecoder.javaAPI DocExample7531Mon Mar 24 01:59:38 GMT 1997None

PPMImageDecoder

public class PPMImageDecoder extends Object implements ImageProducer

Fields Summary
private ImageConsumer
consumer
boolean
loadError
int
width
int
height
int[]
store
Hashtable
props
private static int
PpmHints
Constructors Summary
Methods Summary
public synchronized voidaddConsumer(java.awt.image.ImageConsumer ic)


/* There is only a single consumer. When it registers, produce image. */
/* On error, notify consumer */

          
        consumer = ic;
        try {
            produce();
        } catch (Exception e) {
            if (consumer != null)
                consumer.imageComplete (ImageConsumer.IMAGEERROR);
        }
        consumer = null;
    
public synchronized booleanisConsumer(java.awt.image.ImageConsumer ic)

        return (ic == consumer);
    
private voidproduce()

        ColorModel cm = ColorModel.getRGBdefault();
        if (consumer != null) {
            if (loadError) {
                consumer.imageComplete (ImageConsumer.IMAGEERROR);
            } else {
                consumer.setDimensions (width, height);
                consumer.setProperties (props);
                consumer.setColorModel (cm);
                consumer.setHints (PpmHints);
                for (int j=0;j<height;j++)
                    consumer.setPixels (0, j, width, 1, cm, store[j], 0, width);
                consumer.imageComplete (ImageConsumer.STATICIMAGEDONE);
            }
        }
    
public voidreadImage(byte[] b)

        readImage (new ByteArrayInputStream (b));
    
public voidreadImage(java.io.InputStream is)

        long tm = System.currentTimeMillis();
        boolean raw=false;
        DataInputStream dis = null;
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream (is);
            dis = new DataInputStream (bis);
            String word;
            word = readWord (dis);
            if ("P6".equals (word)) {
                raw = true;
            } else if ("P3".equals (word)) {
                raw = false;
            } else {
                throw (new AWTException ("Invalid Format " + word));
            }
            width = Integer.parseInt (readWord (dis));
            height = Integer.parseInt (readWord (dis));
            // Could put comments in props - makes readWord more complex
            int maxColors = Integer.parseInt (readWord (dis));
            if ((maxColors < 0) || (maxColors > 255)) {
                throw (new AWTException ("Invalid Colors " + maxColors));
            }
            store = new int[height][width];
            if (raw) {
                byte row[] = new byte [width*3];
                for (int i=0;i<height;i++){
                    dis.readFully (row);
                    for (int j=0,k=0;j<width;j++,k+=3) {
                        int red = row[k];
                        int green = row[k+1];
                        int blue = row[k+2];
                        if (red < 0)
                            red +=256;
                        if (green < 0)
                            green +=256;
                        if (blue < 0)
                            blue +=256;
                        store[i][j] = (0xff<< 24) | (red << 16) | (green << 8) | blue;
                    }
                }
            } else {
                for (int i=0;i<height;i++) {
                    for (int j=0;j<width;j++) {
                        int red = Integer.parseInt (readWord (dis));
                        int green = Integer.parseInt (readWord (dis));
                        int blue = Integer.parseInt (readWord (dis));
                        store[i][j] = (0xff<< 24) | (red << 16) | (green << 8) | blue;
                    }
                }
            }
        } catch (IOException io) {
            loadError = true;
            System.out.println ("IO Exception " + io.getMessage());
        } catch (AWTException awt) {
            loadError = true;
            System.out.println ("AWT Exception " + awt.getMessage());
        } catch (NoSuchElementException nse) {
            loadError = true;
            System.out.println ("No Such Element Exception " + nse.getMessage());
        } finally {
            try {
                if (dis != null)
                    dis.close();
                if (bis != null)
                    bis.close();
                if (is != null)
                    is.close();
            } catch (IOException io) {
                System.out.println ("IO Exception " + io.getMessage());
            }
        }
        System.out.println ("Done in " + (System.currentTimeMillis() - tm) + " ms");
    
private java.lang.StringreadWord(java.io.InputStream is)

        StringBuffer buf = new StringBuffer();
        int b;
        do { // get rid of leading whitespace
            if ((b=is.read()) == -1)
                throw new EOFException();
            if ((char)b == '#") {  // read to end of line - ppm comment
                DataInputStream dis = new DataInputStream (is);
                dis.readLine();
                b = ' ";  // ensure more reading
            }
        } while (Character.isSpace ((char)b));
        do {
            buf.append ((char)(b));
            if ((b=is.read()) == -1)
                throw new EOFException();
        } while (!Character.isSpace ((char)b));  // reads first space
        return buf.toString();
    
public synchronized voidremoveConsumer(java.awt.image.ImageConsumer ic)

        if (consumer == ic)
            consumer = null;
    
public voidrequestTopDownLeftRightResend(java.awt.image.ImageConsumer ic)

        // Not needed.  The data is always in this format.
    
public voidstartProduction(java.awt.image.ImageConsumer ic)

        addConsumer (ic);