Thread: Code to store PGM file as a workable array

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    5

    Code to store PGM file as a workable array

    I've been hacking my brais trying to figure this out and all the resources I keep using get me errors. I'm using Dev-C++ and I can't figure out the code to simply take a pgm file (let's call it test.pgm) and open it or read it and then store it as a 2D array. Is there a function like in MATLAB or something. In MATLAB it's rather simple. All you do is:
    Code:
    A = imread('test.pgm');
    This will put the image in matrix A, and then you can play arround with it from there. How does this work in Dev-C++?

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    it looks to me like you can skip (or take in) the first four lines, and then take in one line at a time into a two-dimensional char array... for example:

    Code:
    char matrix[90][90];
    int index=0;
    
    ifstream infile("something.pgm");
    
    infile.ignore(1000'\n');  //ignore the first line
    infile.ignore(1000'\n');  //these can also be changed to
    infile.ignore(1000'\n');  //infile.getline(...) so that you
    infile.ignore(1000'\n');  //can use the data if need be.
    
    while(!infile.eof())
    {
       infile.getline(matrix[index],90,'\n');
       index++;
    }
    
    infile.close();
    for ifstream, you'll need to #include <fstream>

    I don't know much (really anything) about .pgm files, but I used google to find this page... you may then want/need to go through this matrix and convert each char to an integer and put it in an integer matrix...
    Last edited by major_small; 11-09-2004 at 07:58 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    major, your code doens't work. You ignore the image resolution, and use a fixed size array, when the image can be any size.
    I have code to read the image in C, if you want I'll post it.
    Meanwhile
    Code:
    unsigned char* readPGMImage(const  char *filename, int* height, int* width, int* grayscale){
    	//open the file to read
    	//read 1st 2 chars: have to match P5
    	//then skip all lines (ended with '\n') started with #
    	//read 3 ints: width, height, grayscale
    	//start reading info which is width*height bytes big to a buffer
    	//close file and return buffer
    }

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    it works, but you have to modify it to make it do what you want... I'm not going to hand-code everything for everybody... and I already said I didn't know what the file was really for... the OP called for putting it into a matrix, so I put it into a matrix for them... and I provided for the first few lines of input... the size of the matrix need not be hardcoded, just don't allocate the space until after you read the first four lines...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    5
    Thanks everyone, yeah I found that resource too online, but the PGM files weren't in the format given in the text file you linked, I got some help with this and I'm banging throught the program now, thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Text file to 2D Array PLEASE HELP!
    By lostboy101 in forum C Programming
    Replies: 0
    Last Post: 03-26-2002, 10:51 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM