Newbie read image help

J

james

there are many resource on the internet. the better way is through google.
www.itk.org is one of them. All your concern are possible. I suppose you are
running at windows platform. and plannig to display. you need to recognize
the format of BMP(orDIB). If you'd like to read JPEG file visit
http://www.jpeg.org .
typedef struct tagRGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;
typedef RGBQUAD* LPRGBQUAD;


typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;
typedef struct tagBITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO, *LPBITMAPINFO, *PBITMAPINFO;

typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER;

//source code for read 8 bit image
BOOL CDib::Load(LPCSTR lpszDibFile)
{
//openfile
ifstream tfile(lpszDibFile,ios::binary|ios::nocreate);
if (tfile) //open sucessful
{
//file header
tfile.read((char*)&(m_bmfHeader.bfType),sizeof(m_bmfHeader.bfType));
tfile.read((char*)&(m_bmfHeader.bfSize),sizeof(m_bmfHeader.bfSize));

tfile.read((char*)&(m_bmfHeader.bfReserved1),sizeof(m_bmfHeader.bfReserved1)
);

tfile.read((char*)&(m_bmfHeader.bfReserved2),sizeof(m_bmfHeader.bfReserved2)
);
tfile.read((char*)&(m_bmfHeader.bfOffBits),sizeof(m_bmfHeader.bfOffBits));
//bitmap info header
tfile.read((char*)&(m_bmiHeader.biSize),sizeof(m_bmiHeader.biSize));
tfile.read((char*)&(m_bmiHeader.biWidth),sizeof(m_bmiHeader.biWidth));
tfile.read((char*)&(m_bmiHeader.biHeight),sizeof(m_bmiHeader.biHeight));
tfile.read((char*)&(m_bmiHeader.biPlanes),sizeof(m_bmiHeader.biPlanes));

tfile.read((char*)&(m_bmiHeader.biBitCount),sizeof(m_bmiHeader.biBitCount));

tfile.read((char*)&(m_bmiHeader.biCompression),sizeof(m_bmiHeader.biCompress
ion));

tfile.read((char*)&(m_bmiHeader.biSizeImage),sizeof(m_bmiHeader.biSizeImage)
);

tfile.read((char*)&(m_bmiHeader.biXPelsPerMeter),sizeof(m_bmiHeader.biXPelsP
erMeter));

tfile.read((char*)&(m_bmiHeader.biYPelsPerMeter),sizeof(m_bmiHeader.biYPelsP
erMeter));
tfile.read((char*)&(m_bmiHeader.biClrUsed),sizeof(m_bmiHeader.biClrUsed));

tfile.read((char*)&(m_bmiHeader.biClrImportant),sizeof(m_bmiHeader.biClrImpo
rtant));
//RGB
RGBQUAD* rgb;
m_Palette.SetSize(256);
long i;
for (i=0;i <m_Palette.GetSize();i++)
{
rgb=0;
rgb=new RGBQUAD;
tfile.read((char*)&(rgb->rgbBlue),sizeof(rgb->rgbBlue));
tfile.read((char*)&(rgb->rgbGreen),sizeof(rgb->rgbGreen));
tfile.read((char*)&(rgb->rgbRed),sizeof(rgb->rgbRed));
tfile.read((char*)&(rgb->rgbReserved),sizeof(rgb->rgbReserved));
m_Palette.SetAtGrow(i,(CObject*)rgb);
}
//image data
long nSize=m_bmiHeader.biWidth*m_bmiHeader.biHeight;
m_pData=(BYTE*)malloc(nSize*sizeof(BYTE));
tfile.read((char*)m_pData,nSize);

}
tfile.close();
return 0;
}
// get pixel
RGBQUAD* CDib::GetPixel(LONG x, LONG y)
{
BYTE index=0;
RGBQUAD* rgb;
int nOffset=(m_bmiHeader.biHeight-y-1)*m_bmiHeader.biWidth+x;
index=*(m_pData+nOffset);
rgb=(RGBQUAD*)m_Palette.GetAt(index);
return rgb;

}

----- Original Message -----
From: "Joe" <[email protected]>
Newsgroups: comp.lang.c++
Sent: Wednesday, February 04, 2004 7:51 AM
Subject: Newbie read image help
 
J

Joe

Hi all.

I'm writing a program that is supposed to be able to read an image file from
the harddrive and put all image data into a big matrix (or if not possible,
into an array). I obviously don't know the size of the file in advance, only
the file name.
My question is, how do I do this in C++. All newbie examples I have looked
at are with simple ASCII textfiles ending with a null byte and a for loop
reading one byte or one line per loop. But what I want is to read an entire
binary file _at once_, and put all it's content into memory.
I guess this is no more than 2-3 lines of code, but I just can't find it
myself.
Also, is it possible to choose if I want to store the data in a matrix or an
array, that would be great if I could choose.

Also, is there any (free open source) libraries out there that someone can
recommened for reading an image and with functions for fetching the pixel
data at a certain coordinate, for example:
int pixelvalue=image.getPixel(x,y);
Nice features would be if the library could read jpeg images as well.

Any help and any ideas would be really appreciated.

Thanks in advance.

/Joe
 
P

Phlip

Joe said:
I'm writing a program that is supposed to be able to read an image file from
the harddrive and put all image data into a big matrix (or if not possible,
into an array). I obviously don't know the size of the file in advance, only
the file name.
My question is, how do I do this in C++. All newbie examples I have looked
at are with simple ASCII textfiles ending with a null byte and a for loop
reading one byte or one line per loop. But what I want is to read an entire
binary file _at once_, and put all it's content into memory.
I guess this is no more than 2-3 lines of code, but I just can't find it
myself.
Also, is it possible to choose if I want to store the data in a matrix or an
array, that would be great if I could choose.

Also, is there any (free open source) libraries out there that someone can
recommened for reading an image and with functions for fetching the pixel
data at a certain coordinate, for example:
int pixelvalue=image.getPixel(x,y);
Nice features would be if the library could read jpeg images as well.

If you were to use such a library, why use C++?

Your best bet is to look for ImageMagick and the related free software
libraries that support formats such as PNG. They will come with source,
links to other libraries, and hooks to languages like Ruby that then make
the high-level code very easy.
 
J

Joe

If you were to use such a library, why use C++?

Your best bet is to look for ImageMagick and the related free software
libraries that support formats such as PNG. They will come with source,
links to other libraries, and hooks to languages like Ruby that then make
the high-level code very easy.

Thanks for your answer Phlip.

Maybe I shall then start with just the low level programming first.
Lets say I just want to read an image file from the disc and store it in
lets say an int or byte-array.
What shall I write then? I don't know the file size, but I only have a file
name. There must be a built in function for this that I haven't found, but
which?
And from this I can start my programming and see in case I might need other
libraries later on.

/Joe
 
L

lilburne

Joe said:
What shall I write then? I don't know the file size, but I only have a file
name. There must be a built in function for this that I haven't found, but
which?

lookup seek().
 
P

Phlip

Joe said:
Thanks for your answer Phlip.

It's the best I can give.
Maybe I shall then start with just the low level programming first.
Lets say I just want to read an image file from the disc and store it in
lets say an int or byte-array.

Okay. First, the data will come with a header, possibly of a variable size.
Then it will have a data section, definitely of a variable size. The header
will contain 16- or 32-bit variables. But because some platforms pack bytes
into integers differently, you may or may not need to transpose in your
integers - by shifting and masking - before you can treat them as numbers.
Only then will you be able to determine how long the header and its
components are, and how long the data section is.

The header will contain a number of bits specifying the sub-type of the
image, and it may contain a color table - again of variable size. Various
bits in and around the color table will code for colors.

When you try to read the image data, it will not appear as a "rectangle of
integers" in memory. That's why a 2-million pixel photo does not occupy
2-million bytes (or ints) of memory. The data will be packed, using
anything as trivial as run-length encoding, or as advanced as some form of
huffman compression.

Next, the colors may or may not be packed into each byte or integer. Some
formats split the colors apart, like Technicolor, except by bit instead of
by RGB intensity. The file will contain, say, 8 rectangles of the same
size. Pack the rectangles on top of each other, read the bits in the same
location in each pixel, and then push these bits together to get the color
- or the color index - at that pixel.

I have programmed graphics for ~20 years, and I have never written a graphic
image reader from scratch. One or several always come bundled with your OS,
and with your compiler. Many more are available on the net. Reading
bit-packed data files is not trivial.

You will get absolutely nowhere trying to read bytes out with C++ library
primitives and looking at each byte.

Try this: Use a hex-dump program (MSDev can open a file in Binary mode
instead of Auto, for example), and look at all the hexes. Can you make
sense of them? That situation is where you will be, programming, soon if
you try to write an image file reader from scratch.
 
J

John Harrison

Joe said:
Hi all.

I'm writing a program that is supposed to be able to read an image file from
the harddrive and put all image data into a big matrix (or if not possible,
into an array). I obviously don't know the size of the file in advance, only
the file name.
My question is, how do I do this in C++.

You need to seek to the end of the file, you then need to get your file
position, this will be the size of the file, then you need to seek back to
the beginning of the file to start reading. How you do this depends upon
which I/O system you are using. You could use fseek and ftell from <stdio.h>
or you could use seekg and tellg from <iostream>

john
 

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

Similar Threads

I can see the music! 2
problem using vector::resize() 4
BitCount Help 1
WAV to BMP 9
What can cause a fopen call to lock up? 2
Loading BMP problem. 7
Problem Reading Bmp's .. 14
Expert help required for C++ newbie 2

Members online

Forum statistics

Threads
473,708
Messages
2,569,347
Members
44,654
Latest member
7Jhamal74

Latest Threads

Top