Logo Search packages:      
Sourcecode: josm-plugins version File versions  Download package

org::openstreetmap::josm::plugins::ywms::PPM::PPM ( String  filename  )  throws IOException, UnsupportedEncodingException [inline]

Creates a PPM image from a file

The stream must contain a binary PPM (type P6 in the header) of 255 colors

Parameters:
filename The name of the file to read the PPM from
Exceptions:
IOException when failing to read the file
UnsupportedEncodingException if file is not a binary(P6) PPM of 255 colors

Definition at line 32 of file PPM.java.

References data, dim, and rgb().

      {
            FileInputStream fis = new FileInputStream(filename);
            
            // XXX: The source data to the StreamTokenizer can't buffered, since we
            // only need a few lines of the beginning of the file, and all remain
            // data must be processed later. Even a InputStreamReader does some
            // internal buffering, so we must use a deprecated constructor, create
            // our own Reader or create some methods to simulate a StreamTokenizer.
            // Easy way chosen :-)
            StreamTokenizer st = new StreamTokenizer(fis);
            st.commentChar('#');

            /* PPM file format:
             * 
             * #                --> Comments allowed anywere before binary data
             * P3|P6          --> ASCII/Binary
             * WIDTH          --> image width, in ascii
             * HEIGHT         --> image height, in ascii
             * COLORS           --> num colors, in ascii
             * [data]           --> if P6, data in binary, 3 RGB bytes per pixel 
             */

            st.nextToken();
            if( !st.sval.equals("P6") )
                  throw new UnsupportedEncodingException("Not a P6 (binary) PPM");
            
            st.nextToken();
            dim.width = (int) Math.round(st.nval);
            st.nextToken();
            dim.height = (int) Math.round(st.nval);
            data = new int[dim.width * dim.height];

            st.nextToken(); 
            int maxVal = (int) Math.round(st.nval); 
            if( maxVal != 255 )
                  throw new UnsupportedEncodingException("Not a 255 color PPM");
                        
            // Binary data cann be buffered
            InputStream in = new BufferedInputStream(fis);
            int numPixels = dim.width * dim.height;
            for (int i = 0; i < numPixels; i++)
            {
                  int r = in.read();
                  int g = in.read();
                  int b = in.read();
                  if( r== -1 || g == -1 || b == -1)
                        throw new IOException("EOF:" + r + " " + g + " " + b);
                  
                  data[i] = rgb(r, g, b);
            }
            in.close();       
      }


Generated by  Doxygen 1.6.0   Back to index