Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Outputing ASCII PPM files

Reply
Thread Tools

Outputing ASCII PPM files

 
 
George
Guest
Posts: n/a
 
      08-27-2005
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.

 
Reply With Quote
 
 
 
 
Giannis Papadopoulos
Guest
Posts: n/a
 
      08-27-2005
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.
 
Reply With Quote
 
 
 
 
Giannis Papadopoulos
Guest
Posts: n/a
 
      08-27-2005
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.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-27-2005
On 27 Aug 2005 14:31:55 -0700, "George" <> 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
 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      08-28-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Outputing a .pdf stream to current window Dan Kimhi ASP .Net 2 02-08-2005 04:00 PM
Collecting Data on one form and outputing it on another form Michael Mitchell via DotNetMonster.com ASP .Net 0 01-23-2005 03:14 PM
Outputing Xml from ASPX =?Utf-8?B?QWxleCBNYWdoZW4=?= ASP .Net 2 11-25-2004 07:55 AM
Error outputing results of transformation Mark Goldin ASP .Net 0 02-22-2004 10:55 PM
ppm: ppm install Spreadsheet::ParseExcel Marko Faldix Perl Misc 3 01-27-2004 05:47 AM



Advertisments