Outputing ASCII PPM files

G

George

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.
 
G

Giannis Papadopoulos

George said:
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.
 
G

Giannis Papadopoulos

Giannis said:
George said:
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.
 
J

Jack Klein

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.
 
S

Simon Biber

George said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top