473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

20
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);
Oct 8 '07 #1
1 6063
Your code seems a bit confusing ... try using the code tags.

Also, to write a binary file you can do the following:

Expand|Select|Wrap|Line Numbers
  1. std::ofstream myfile;
  2. myfile.open ("/path/to/file", std::ios::out | std::ios::app | std::ios::binary);
  3. myfile << data;
  4. myfile.close();

hopefully that helps
Oct 8 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jerry | last post by:
I've inherited a system w/5 projects - I'll refer to them as P1 thru P5 - P5 being the executable... P1 has no dependents. P2 is dependant upon P1 P3 has no dependents. P4 is dependant upon P1,...
4
by: Dean Card | last post by:
Okay, so here is the situation. I have need to do some on-the-fly image creation. I have everything working great except for the last part of it, applying a perspective type transform to the...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
3
by: Howler | last post by:
Hello all, I am having a hard time seeing what I am doing wrong with a program I am having to write that converts pbm monochrome images into a similar pgm file. The problem I am having is...
0
by: crazyyellowguy | last post by:
hi, This is my first post in this forum and I just wanted to thank you for even taking a look at my post. The class that I am writing the C code for is my first computer class and my knowledge is...
0
by: OmarcheO | last post by:
Hi guys, i have a question here. If i have to convert a ppm image file to pgm image file. Both are in P5 and P6 format which will be binary file. So far i can open a binary stream read and...
2
by: Adam Teale | last post by:
hey guys Is there a builtin/standard install method in python for retrieving or finding out an image's dimensions? A quick google found me this:...
0
by: robykeys82 | last post by:
Hi! I'm an Italian user of PostGres and I'm having some trouble! I hope you can help me! I'm trying to migrate the db content from SQL SERVER 2005 to PostGreSQL. Everything works ok except for the...
1
by: wilson | last post by:
i converted an 8bit rgb .jpg file into .pgm using adobe photoshop and a plugin from http://photoshop.pluginsworld.com/plugins/adobe/362/richard-rosenman/portable-pixmap-importer-exporter.html I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.