Read PPM images as BufferedImage

Discussion in 'Java' started by hz010c1877, Aug 14, 2003.

  1. hz010c1877

    hz010c1877 Guest

    Hi,

    I've got PPM image files stored in raw bytes rather than ACSII which magic
    identifiers are P6 and P3 respectively. I want to read them into memory and
    return a BufferedImage instance. I know how to do it for ASCII PPM image
    files but have no idea for raw bytes ones. Does anyone have any suggestions?
    Any help will be appreciated.

    Here is the mothed for reading ASCII PPM image as BufferedImage:

    // some codes are not list here for simplicity
    // width and height is the width and heigh of a image
    // readHeader() reads the header of PPM image
    // The following is supposed to run before the parsePPm() method

    // private StreamTokenizer parser;
    // reader = new BufferedReader(new InputStreamReader(in));
    // parser = new StreamTokenizer(reader);
    // parser.commentChar('#');
    // readHeader();

    private BufferedImage parsePPM() throws IOException, PPMDecoderException {
    BufferedImage img =
    new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
    WritableRaster raster = img.getRaster();
    for (int y = 0; y < height; ++y)
    for (int x = 0; x < width; ++x)
    for (int i = 0; i < 3; ++i) {
    parser.nextToken();
    if (parser.ttype == StreamTokenizer.TT_EOF)
    throw new EOFException("image appears to be truncated");
    if (parser.ttype != StreamTokenizer.TT_NUMBER)
    throw new PPMDecoderException("non-numeric value for sample "
    + i + " of pixel at (" + x + "," + y + ")");
    raster.setSample(x, y, i, (int) parser.nval);
    }
    return img;
    }
     
    hz010c1877, Aug 14, 2003
    #1
    1. Advertising

  2. hz010c1877 wrote:
    > Hi,
    >
    > I've got PPM image files stored in raw bytes rather than ACSII which magic
    > identifiers are P6 and P3 respectively. I want to read them into memory and
    > return a BufferedImage instance. I know how to do it for ASCII PPM image
    > files but have no idea for raw bytes ones. Does anyone have any suggestions?
    > Any help will be appreciated.
    >
    > Here is the mothed for reading ASCII PPM image as BufferedImage:
    >
    > // some codes are not list here for simplicity
    > // width and height is the width and heigh of a image
    > // readHeader() reads the header of PPM image
    > // The following is supposed to run before the parsePPm() method
    >
    > // private StreamTokenizer parser;
    > // reader = new BufferedReader(new InputStreamReader(in));
    > // parser = new StreamTokenizer(reader);
    > // parser.commentChar('#');
    > // readHeader();
    >
    > private BufferedImage parsePPM() throws IOException, PPMDecoderException {
    > BufferedImage img =
    > new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
    > WritableRaster raster = img.getRaster();
    > for (int y = 0; y < height; ++y)
    > for (int x = 0; x < width; ++x)
    > for (int i = 0; i < 3; ++i) {
    > parser.nextToken();
    > if (parser.ttype == StreamTokenizer.TT_EOF)
    > throw new EOFException("image appears to be truncated");
    > if (parser.ttype != StreamTokenizer.TT_NUMBER)
    > throw new PPMDecoderException("non-numeric value for sample "
    > + i + " of pixel at (" + x + "," + y + ")");
    > raster.setSample(x, y, i, (int) parser.nval);
    > }
    > return img;
    > }
    >
    >


    Magic number P3 is used by the (putatively rare) "plain" PPM format,
    which appears to be exactly what the code above is supposed to handle.
    (Modulo possible differences in the header.) All header information is
    ASCII, with all numeric values represented in decimal.

    Magic number P6 designates the more common "raw" PPM format. The header
    for raw PPM is the same as for plain PPM except for the magic number.
    The difference is that the pixel data is in machine format with no
    intervening whitespace. There is one byte per color component if the
    header specifies the maximimum color value as less than 256, or two
    bytes per color component otherwise, most significant byte first. Also,
    it is allowed for multiple images in that format to be concatenated
    together into one file; this is not allowed for the plain format.

    Do note that according to the docs the color values in a standard PPM
    file are non-linear, proportional to the CIE Rec. 709 color values and
    adjusted by the CIE Rec. 708n gamma transfer function. (Whatever that
    means -- I'm just reading the docs to you for this bit.) The docs do
    note that a common variant of the format uses linear color values,
    however, so I guess you should just look to see whether the result looks
    good.



    John Bollinger
     
    John C. Bollinger, Aug 14, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Irwin Friedman
    Replies:
    1
    Views:
    2,214
    John Bokma
    Sep 6, 2003
  2. WS
    Replies:
    0
    Views:
    687
  3. alex

    PPM and Proxy

    alex, Jun 18, 2004, in forum: Perl
    Replies:
    0
    Views:
    653
  4. Read pixels from a PGM/PPM Image

    , May 23, 2005, in forum: C Programming
    Replies:
    2
    Views:
    1,674
    Malcolm
    May 24, 2005
  5. Marko Faldix

    ppm: ppm install Spreadsheet::ParseExcel

    Marko Faldix, Jan 26, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    443
    James Willmore
    Jan 27, 2004
Loading...

Share This Page