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);
How to write image data to binary PGM file format(P5)?
Started by tommy_chai, Oct 08 2007 04:24 AM
5 replies to this topic
#1
Posted 08 October 2007 - 04:24 AM
|
|
|
#2
Posted 09 October 2007 - 05:34 PM
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.
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
Posted 18 October 2007 - 06:08 AM
thanks for your advices. It is really helpful.
#4
Posted 28 March 2008 - 10:25 AM
hey dude.. can u plz post the write code for this..
thnx in advance...
thnx in advance...
#5
Posted 29 March 2008 - 03:29 AM
wil the thing work if i make all the changes suggested by dargueta?
#6
Posted 30 March 2008 - 02:47 AM
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++;
}
}


Sign In
Create Account


Back to top









