Jump to content

How to write image data to binary PGM file format(P5)?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
tommy_chai

tommy_chai

    Newbie

  • Members
  • PipPip
  • 16 posts
Hi all,
I fail to write the image data to binary PGM format (P5).
Let's say i have to write a image data into PGM P5 format.
I have the information for width, height, Y_min, Y_max, XL, XR and the TEMPLATE1 (data matrix) ==>A region of interest from an image.
Y_min & Y_max = minimum and maximum of the height value.
XL & XR = min and max of the width value for the image.

First, i call the subroutine,
write_data_grey_binary(width1,height1,XL1,XR1,Y_min1,Y_max1,filename99[total],TEMPLATE1);

void write_data_grey_binary(width,height,XL,XR,Y_min,Y_max,filename,output)
int width,height,XL,XR,Y_min,Y_max;
char *filename;
unsigned char** output;
{
int i,j,nread;
FILE *fp;
unsigned char* temp;

if((fp = fopen(filename,"w")) == NULL){
printf("File 333 output data can not open\n");
exit(1);
}

fprintf(fp,"P5\n%d %d\n%d\n",width,height,(GRYSCL-1));

temp = (unsigned char*)calloc(height*width, sizeof(unsigned char));

for(i=Y_min;i<=Y_max;i++){
for(j=XL;j<=XR;j++){
temp[(i*width)+j]= (unsigned char)output[i][j];
}
}
nread = fwrite((void*)temp, sizeof(unsigned char), (height*width), fp);
free(temp);
fclose(fp);
return(0);
}

However , i have an error at the line " temp[(i*width)+j]= (unsigned char)output[i][j]; ".
However, this coding can be ran when i want to write the full image like:
write_data_grey_binary(width,height,0,width-1,0,height-1,filename99[total],inpgrey);

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
I think you have a few problems:

First of all, fwrite() is used when writing to binary files. Your file is currently opened as a text file for output. Change "w" to "wb" to fix this.

Second of all: fprintf() does NOT output binary data, but text data. Your file modes are inconsistent.

Your problem with the temp array: if width isn't equal to Y_max, then either you won't initialize the entire buffer or you'll overrun it. This might be the reason why the program crashes.

#3
tommy_chai

tommy_chai

    Newbie

  • Members
  • PipPip
  • 16 posts
thanks for your advices. It is really helpful.

#4
vinod08

vinod08

    Newbie

  • Members
  • Pip
  • 4 posts
hey dude.. can u plz post the write code for this..
thnx in advance...

#5
vinod08

vinod08

    Newbie

  • Members
  • Pip
  • 4 posts
wil the thing work if i make all the changes suggested by dargueta?

#6
nutario

nutario

    Newbie

  • Members
  • PipPip
  • 23 posts
for(i=Y_min;i<=Y_max;i++){

for(j=XL;j<=XR;j++){

temp[(i*width)+j]= (unsigned char)output[i][j];

}

}

With this code you probably don't get a linear arry of unsigned char, with no memory holes.
e.g:
width = 10; height = 5;
Situation: ouy are at the end of the first line:
i = 0 and j = 5 => temp[(0*10)+5] = temp[5]
The next char you'll put in the temp array will be at position:
i = 1 j = 0 => temp[(1*10)+0] = temp[10]

You should write it like this to get a well formed array:

int count = 0;

for( i = >_min; i <= Y_MAX; i++)

{

    for(j = XL; j <= XR; j++)

    {

       temp[count] = (unsigned char)output[i][j];  [COLOR="Green"]//or ... = output[i][j][/COLOR]

       count++;

     }

}