Reading a PGM file.

I am trying to work on an assignment that will load a PGM file, put the corresponding gray level values in a 2D integer array, and be able to manipulate the gray levels according to method (mean and median) and the selected window size, which will then alter the image as either "smoothed" or "oil painted".

Anyways, I'm trying to figure out the best way to grab the data from the PGM file to put into the 2D array. When you open the PGM in a text editor, the rows and columns in the PGM file do not match up to the specified rows and columns in the header and are messy. Therefore, I obviously cannot read each line through BufferedReader and grab each gray level value through substring in a nested for loop to add to the 2D array. To add to that, each value can be 1, 2, or 3 digits long.

My professor has not really covered this at all yet, and its pretty much the start of the program. It's due in a week and I figured I should really get a start on it now. If anybody can point me in the right direction, I'd greatly appreciate it. Thanks.

Comments

  • : I am trying to work on an assignment that will load a PGM file, put
    : the corresponding gray level values in a 2D integer array, and be
    : able to manipulate the gray levels according to method (mean and
    : median) and the selected window size, which will then alter the
    : image as either "smoothed" or "oil painted".
    :
    : Anyways, I'm trying to figure out the best way to grab the data from
    : the PGM file to put into the 2D array. When you open the PGM in a
    : text editor, the rows and columns in the PGM file do not match up to
    : the specified rows and columns in the header and are messy.
    : Therefore, I obviously cannot read each line through BufferedReader
    : and grab each gray level value through substring in a nested for
    : loop to add to the 2D array. To add to that, each value can be 1, 2,
    : or 3 digits long.
    :
    : My professor has not really covered this at all yet, and its pretty
    : much the start of the program. It's due in a week and I figured I
    : should really get a start on it now. If anybody can point me in the
    : right direction, I'd greatly appreciate it. Thanks.
    :
    If the numbers are split by some character, then the best way to read the file is to use a Scanner object. See http://java.sun.com/javase/6/docs/api/java/util/Scanner.html
    To read the file: simply make the 2D array with the specified rows and columns, and loop through the array, while grabbing each number with the Scanner. Also think about how your program should react if the file contains not enough gray values or too many gray values.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion