Skip to content

Instantly share code, notes, and snippets.

@okthatsneat
Created June 25, 2012 09:23
Show Gist options
  • Save okthatsneat/2987604 to your computer and use it in GitHub Desktop.
Save okthatsneat/2987604 to your computer and use it in GitHub Desktop.
LoadPGM
unsigned short LoadPGM(FILE * inFile, Image * inImg) {
/********************************************************************************************/
// parse binary pgn file to validate format, extract width, height, and greyvalue data array.
// read file bytewise
int position = 1;
int c;
// parse first line. Expecting P5, abort if not. P is 80, 5 is 53, linebreak is 10
while ((c = fgetc(inFile)) != LINEBREAK) {
if( position == 1 && !(c == CHAR_P) ) {
printf("argument is not binary PGM file> fail\n");
return 1;
}
if( position == 2 && !(c == CHAR_5) ) {
printf("argument is not binary PGM file> fail\n");
return 1;
}
inImg->pgmType[position-1] = c;
position ++;
}
/********************************************************************************************/
// skip the next comment lines
// if char is #, then skip the line
printf("c is: %u\n", c);
if ((c = fgetc(inFile)) == CHAR_COMMENT) {
do
{
while ((c = fgetc(inFile)) != LINEBREAK){}
}
while ((c = fgetc(inFile)) == CHAR_COMMENT);
}
/********************************************************************************************/
// now we're at the width. 32 is space, so read till 32 for width, and then till newline(=10) for height.
// construct the byte array that is the width byte by byte.
position=0;
// dirty fix> have to figure out how to determine the size of the bytearray sometime. this will only work if i know the values in advance...
char soonAnInt[STRINGBUF];
memset(&soonAnInt[0], 0, sizeof(soonAnInt));
do {
soonAnInt[position] = c;
position++;
if(position > STRINGBUF) {
//freak out
}
} while ((c = fgetc(inFile)) != SPACE);
inImg->width = atoi(soonAnInt);
/********************************************************************************************/
// now the height
// reset position and char array helpers
position=0;
memset(&soonAnInt[0], 0, sizeof(soonAnInt));
// and again, only here till line break
while ((c = fgetc(inFile)) != LINEBREAK){
soonAnInt[position] = c;
position++;
}
inImg->height = atoi(soonAnInt);
/********************************************************************************************/
// now we're getting the depth info, 255 / ignore till next newline
position=0;
memset(&soonAnInt[0], 0, sizeof(soonAnInt));
while ((c = fgetc(inFile)) != LINEBREAK){
soonAnInt[position] = c;
position++;
}
inImg->depth = atoi(soonAnInt);
/********************************************************************************************/
/********************************************************************************************/
// allocate memory for inImg bytes
int i=0,j=0;
// Allocation
inImg->data = (byte **) malloc(inImg->height*sizeof(byte*));
for(i=0;i<inImg->height;i++){
inImg->data[i]=(byte *) malloc(inImg->width*sizeof(byte));
}
/********************************************************************************************/
// fill data with values
for(i=0;i<inImg->height;i++) {
//runs once for every line
for(j=0;j<inImg->width;j++) {
//runs once for every pixel inside that line, for each line.
if ((c = fgetc(inFile)) != EOF) {
inImg->data[i][j] = c;
} else {
inImg->data[i][j] = 0;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment