473,395 Members | 2,437 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,395 software developers and data experts.

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
4 6640
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Sebastian Krause | last post by:
Hello, I tried to read in some large ascii files (200MB-2GB) in Python using scipy.io.read_array, but it did not work as I expected. The whole idea was to find a fast Python routine to read in...
8
by: W. de Jonge | last post by:
Who can help me? I want to create a link(href) which opens an .doc or an ..xls directly in MS Word or MS Excell and not in IE so that I don't have to save the document first en open it from...
4
by: wob | last post by:
Many thanks for those who responded to my question of "putting greek char into C string". In searching for an solution, I noticed that there are more than one version of "Extended ASCII...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
12
by: IamIan | last post by:
I searched the archives but couldn't find anyone else with this problem. Basically I'm grabbing all ASCII files in a directory and doing geoprocessing on them. I need to calculate a z-factor based...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
1
by: JanaB | last post by:
I am writing a code that needs to read in a binary file. At the moment I don't have to do anything with the data, I just need to view the contents to compare its structure with another binary file....
12
by: bg_ie | last post by:
Hi, I'm updating my program to Python 2.5, but I keep running into encoding problems. I have no ecodings defined at the start of any of my scripts. What I'd like to do is scan a directory and...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.