Re: Opening a PPM file Image
- From: Norbert Unterberg <nunterberg@xxxxxxxxxxxxxxxxx>
- Date: Tue, 17 Oct 2006 12:03:12 +0200
dacky wrote:
how can i read a PPM file by using CFile?
Just create a CFile object and open it in binary mode for reading, and then start reading it. There is no magic in image files compared to other files. Files are just files, you can open and read them. It is then your task to interpret the values "by hand".
- how can i access P6?
- how can i access the width and length?
It seems that you need to make your hands dirty and do the byte fiddling yourself. You could do it by reading the file byte by byte, take the white space as separators, and parse the data fields in between.
Something like this:
bool ReadNext(CFile& f, CString &value)
{
CStringA s;
char ch;
// Skip white space
do
{
if (f.Read(&ch, 1) != 1)
return false; // EOF
} while (isspace(ch));
// Read next field until we get a white space
do
{
// append char to string
s += ch;
// read next
if (f.Read(&ch, 1) != 1)
{
// eof: must not happen here
return false;
}
} while (!isspace(ch)); // white space: end of field
value = s;
return true;
}
bool LoadPPM(LPCTSTR filename)
{
CFile f(filename,
CFile::modeRead | CFile::typeBinary | CFile::shareDenyWrite);
CString val;
int width;
int height;
int maxval;
if (!ReadNext(f, val) || val != "P6")
return false; // bad magic number
if (!ReadNext(f, val) || (width = _ttoi(val)) == 0)
return false; // bad width
if (!ReadNext(f, val) || (height = _ttoi(val)) == 0)
return false; // bad height
if (!ReadNext(f, val) || (maxval = _ttoi(val)) == 0)
return false; // bad height
// now read all the pixel values.
int rowsize = width;
if (maxval >= 256)
rowsize *= 2;
BYTE* pRow = new BYTE[rowsize];
for (int row = 0; row < height; ++row)
{
f.Read(pRow, rowsize);
BYTE *p;
int col;
for (col = 0, p = pRow; col < width; ++col)
{
unsigned value = *p++;
if (maxval >= 256)
value = (value * 256) + *p++;
// ... handle row pixel values here
}
}
delete[] pRow;
return true;
}
Note that the ReadNext function needs to be enhanced to detect and handle the '#' comment sign.
To create a bitmap from the raw pixel values, read about the functions CreateCompatibleDC, CreateCompatibleBitmap and SetDIBits.
About color conversion, I have no clue, there you need to do your homework on your own. How about Googling after "ITU-R Recommendation BT.709" or "CCIR Recommendation 709"?
Good Luck!
Norbert
.
- Follow-Ups:
- Re: Opening a PPM file Image
- From: dacky
- Re: Opening a PPM file Image
- References:
- Opening a PPM file Image
- From: dacky
- Re: Opening a PPM file Image
- From: Tony Thomas
- Re: Opening a PPM file Image
- From: dacky
- Re: Opening a PPM file Image
- From: Ajay Kalra
- Re: Opening a PPM file Image
- From: dacky
- Re: Opening a PPM file Image
- From: Norbert Unterberg
- Re: Opening a PPM file Image
- From: dacky
- Opening a PPM file Image
- Prev by Date: How to get current File Pointers Physical Position on Drive/CD/DVD...
- Next by Date: Re: Opening a PPM file Image
- Previous by thread: Re: Opening a PPM file Image
- Next by thread: Re: Opening a PPM file Image
- Index(es):
Relevant Pages
|