Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Vote on your favorite hash algorithm here!

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-08-2007, 07:24 AM
tommy_chai tommy_chai is offline
Newbie
 
Join Date: Oct 2007
Posts: 16
Credits: 0
Rep Power: 4
tommy_chai is on a distinguished road
Default How to write image data to binary PGM file format(P5)?

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_mi n1,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);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 10-09-2007, 08:34 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 606
Last Blog:
Programs Under the Hoo...
Credits: 193
Rep Power: 11
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

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.

Last edited by dargueta; 10-09-2007 at 08:40 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-18-2007, 09:08 AM
tommy_chai tommy_chai is offline
Newbie
 
Join Date: Oct 2007
Posts: 16
Credits: 0
Rep Power: 4
tommy_chai is on a distinguished road
Default

thanks for your advices. It is really helpful.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-28-2008, 01:25 PM
vinod08 vinod08 is offline
Newbie
 
Join Date: Mar 2008
Posts: 4
Credits: 0
Rep Power: 0
vinod08 is on a distinguished road
Default Re: How to write image data to binary PGM file format(P5)?

hey dude.. can u plz post the write code for this..
thnx in advance...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-29-2008, 06:29 AM
vinod08 vinod08 is offline
Newbie
 
Join Date: Mar 2008
Posts: 4
Credits: 0
Rep Power: 0
vinod08 is on a distinguished road
Default Re: How to write image data to binary PGM file format(P5)?

wil the thing work if i make all the changes suggested by dargueta?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 03-30-2008, 05:47 AM
nutario's Avatar   
nutario nutario is offline
Newbie
 
Join Date: Mar 2008
Location: Germany
Posts: 23
Credits: 0
Rep Power: 3
nutario is on a distinguished road
Send a message via ICQ to nutario Send a message via Skype™ to nutario
Default Re: How to write image data to binary PGM file format(P5)?

Code:
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:
Code:
int count = 0;
for( i = >_min; i <= Y_MAX; i++)
{
    for(j = XL; j <= XR; j++)
    {
       temp[count] = (unsigned char)output[i][j];  //or ... = output[i][j]
       count++;
     }
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving data from a listbox to a txt file! pds2ect Visual Basic Programming 5 10-03-2007 03:59 PM
Program to pass data from text file to table. sania21 Java Help 3 05-28-2007 08:32 AM
RAW image code / data mpcode General Programming 1 04-30-2007 11:01 AM
Writing to binary file paul. Pascal/Delphi 4 04-13-2007 08:23 PM
Determine if a file is binary Nightracer C# Programming 5 09-16-2006 02:26 PM


All times are GMT -5. The time now is 10:50 PM.

Contest Stats

Xav ........ 1357.94
MeTh0Dz|Reb0rn ........ 1077.71
WingedPanther ........ 919.18
marwex89 ........ 906.86
morefood2001 ........ 900.18
John ........ 890.77
Brandon W ........ 770.65
chili5 ........ 312.39
Steve.L ........ 264.99
dcs ........ 234.34

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 83%

Ads