467,075 Members | 1,006 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,075 developers. It's quick & easy.

Outputing ASCII PPM files

I am lost on how to do this. I have not worked with C much at all but
can understand the basic properties of this language. Could someone
please show me or explain to me how to write a function that outputs
ASCII PPM files. I need to output a 512 x 512 image containing random
noise with the top-most row of pixels red and the left-most pixels
blue( instead of the random). Thank you so much for your help it will
greatly greatly be appreciated and it will allow me to understand this
type of format a lot more. Again thanks.

Nov 15 '05 #1
  • viewed: 6094
Share:
4 Replies
George wrote:
I am lost on how to do this. I have not worked with C much at all but
can understand the basic properties of this language. Could someone
please show me or explain to me how to write a function that outputs
ASCII PPM files. I need to output a 512 x 512 image containing random
noise with the top-most row of pixels red and the left-most pixels
blue( instead of the random). Thank you so much for your help it will
greatly greatly be appreciated and it will allow me to understand this
type of format a lot more. Again thanks.


You can create a struct that holds the picture

struct pixel {
unsigned char r;
unsigned char g;
unsigned char b;
};

struct pixel image[512][512];

and then fill it with anything you need.

For the output, check this out:
http://netpbm.sourceforge.net/doc/ppm.html
--
one's freedom stops where others' begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 15 '05 #2
Giannis Papadopoulos wrote:
George wrote:
I am lost on how to do this. I have not worked with C much at all but
can understand the basic properties of this language. Could someone
please show me or explain to me how to write a function that outputs
ASCII PPM files. I need to output a 512 x 512 image containing random
noise with the top-most row of pixels red and the left-most pixels
blue( instead of the random). Thank you so much for your help it will
greatly greatly be appreciated and it will allow me to understand this
type of format a lot more. Again thanks.


You can create a struct that holds the picture

struct pixel {
unsigned char r;
unsigned char g;
unsigned char b;
};

struct pixel image[512][512];

and then fill it with anything you need.

For the output, check this out:
http://netpbm.sourceforge.net/doc/ppm.html


Assuming that char is 8bit long...
On a second thought, maybe you should use

struct pixel {
unsigned int r:8;
unsigned int g:8;
unsigned int b:8;
};

--
one's freedom stops where others' begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 15 '05 #3
On 27 Aug 2005 14:31:55 -0700, "George" <bu*******@hotmail.com> wrote
in comp.lang.c:
I am lost on how to do this. I have not worked with C much at all but
can understand the basic properties of this language. Could someone
please show me or explain to me how to write a function that outputs
ASCII PPM files. I need to output a 512 x 512 image containing random
noise with the top-most row of pixels red and the left-most pixels
blue( instead of the random). Thank you so much for your help it will
greatly greatly be appreciated and it will allow me to understand this
type of format a lot more. Again thanks.


Your request is not very clear. You have posted a statement of what
it is that you have to do, but you haven't identified the part that it
causing you a problem. You have also used an abbreviation or acronym,
"PPM", that you haven't identified, apparently because you assume
everyone knows what it means.

Assuming that your system uses the ASCII character set, which is not
required by C but almost a certainly, the most convenient way to
generate a text (ASCII) file is to use the fopen() function with a
mode argument of "w". The easiest way to write text data to such a
file is to use the fprintf() function.

On the other hand, if your problem is with generating the image data,
that's not really C related at all, although the srand() and rand()
functions can be helpful in generating "white noise" or "random" data.

Finally if you don't understand the format of the image or the output
file, that's not a C language issue at all. You need to do some
research. http://www.wotsit.org is an excellent site to look for the
format of many different types of files.

And of course, there's Google.

When I did a Google search for the phrase "ASCII PPM" I found a large
number of hits. Interestingly enough, some of the links appeared to
be homework assignments for CS classes. People here are not willing
to do someone's homework assignment for them, although they are
willing to help.

If you understand how to generate your data and the file format, but
you have a problem writing correct code to work in C, then post the
problem code here and explain your problem with it, and people will be
happy to offer advice.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #4
George wrote:
I am lost on how to do this. I have not worked with C much at all but
can understand the basic properties of this language. Could someone
please show me or explain to me how to write a function that outputs
ASCII PPM files. I need to output a 512 x 512 image containing random
noise with the top-most row of pixels red and the left-most pixels
blue( instead of the random). Thank you so much for your help it will
greatly greatly be appreciated and it will allow me to understand this
type of format a lot more. Again thanks.


The PPM file format starts with P3 (for ASCII format) or P6 (for raw
format). Following are the width and height of the image, then the
maximum value that any red, green or blue component will take. Typically
this is 255, giving a range from 0 (no colour) to 255 (brightest colour).

In the ASCII format, after the header above, for each pixel in the
image, three decimal numbers are given, each from 0 up to the maximum
value specified. First the red value, then the green value, then the
blue value.

You can also represent a colour as a single number with the red, green
and blue components taking up a set number of bits in the binary
representation of the number. Often, the least significant 8 bits
represent blue, the next 8 bits represent green, and the next 8 bits
represent red. This gives a number from 0 (black) to 16777215 (white).
In this system, 255 represents blue, and 16711680 represents red.

The following program attempts to do what you have specified. If the y
coordinate is zero then the colour is 16711680, else if the x coordinate
is zero then the colour is 255, and if neither are zero then the colour
is a random number from 0 to 16777215.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
long x,y,c;
printf("P3 512 512 255\n");
for(y=0;y<512;y++) for(x=0;x<512;x++) {
c = x ? y ? rand()%16777216 : 16711680 : 255;
printf("%ld %ld %ld\n", c>>16, c>>8&255, c&255);
}
return 0;
}

--
Simon.
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Sebastian Krause | last post: by
8 posts views Thread by W. de Jonge | last post: by
18 posts views Thread by John | last post: by
12 posts views Thread by bg_ie@yahoo.com | last post: by
399 posts views Thread by =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.