Read pixels from a PGM/PPM Image

Q

querypk

Can some one help me to read PGM/PPM Images in C/C++. I am sure there
should be a library or a simple routine to read the pixels of an Image
and return a 2D array of pixel values...
 
C

CBFalconer

Can some one help me to read PGM/PPM Images in C/C++. I am sure
there should be a library or a simple routine to read the pixels
of an Image and return a 2D array of pixel values...

Please tell us where you find any definition of PGM, PPM, pixel,
Image, or C++ in the C standard. In other words this is entirely
off topic for c.l.c, where we deal with the portable C language
(not C++) as defined in the C standard. You probably want a
newsgroup that deals with your particular system.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
M

Malcolm

Can some one help me to read PGM/PPM Images in C/C++. I am sure there
should be a library or a simple routine to read the pixels of an Image
and return a 2D array of pixel values...
It is so easy that you don't need a library routine.

First look up the format on wotsit.org. Basically it consists of width,
height, and raw pixel data, though there might be a few comments or colour
channel data in there.

I cannot remember offhand whether the width and height is big-endian or
little endian. Whichever, it may not match your machine. So write functions
that will read and write 16 and 32 bit integers in big or little endian
format, using the shift and AND and OR operators to construct the integer
portably.

Once you've done this, write a routine to load the header information, then
call malloc(), and then call fgetc() iteratively to read the pixel data.
The function should look something like this

unsigned char *readppm(const char *fname, int *width, int *height)

return NULL on any error, such as the file not being openable or there not
being enoguh memory.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top