-
April 15th, 2007, 04:07 AM
#1
i want rgb details from .bmp
Hello,
I am reading the values of r,g,b from a .bmp file.
Code:
ifstream fin;
fin.open("deer256.bmp",ios::binary);
int height,width;
char *imageByte;
int index=0;
if(fin!=NULL)
{
fin.seekg(18);
fin.read((char*)&width,4);
fin.read((char*)&height,4);
cout<<height<<endl;
cout<<width<<endl;
imageByte = new char;
imageByte = new unsigned char[height*width];
while(fin.good())
{
fin.read(imageByte,1);
cout<<(int)imageByte;
++index;
}
}
else
{
cout<<"image file not open\n";
}
The height and width are read perfectly but the values in imageByte are junk values. Is something wrong?
rgds
swetha
Last edited by swetha.bsharma; April 15th, 2007 at 04:10 AM.
-
April 15th, 2007, 05:15 AM
#2
Re: i want rgb details from .bmp
I think you are not reading the file correctly.
See this link on BMP format. Take a look at the source code provided in the page.
-
April 15th, 2007, 05:37 AM
#3
Re: i want rgb details from .bmp
Also, you're writing all the chars to same location.(imageByte[0]) Change this:
Code:
fin.read(imageByte+index,1);
-
April 15th, 2007, 02:51 PM
#4
Re: i want rgb details from .bmp
Originally Posted by swetha.bsharma
Hello,
I am reading the values of r,g,b from a .bmp file.
Code:
ifstream fin;
fin.open("deer256.bmp",ios::binary);
int height,width;
char *imageByte;
int index=0;
if(fin!=NULL)
{
fin.seekg(18);
fin.read((char*)&width,4);
fin.read((char*)&height,4);
cout<<height<<endl;
cout<<width<<endl;
imageByte = new char;
imageByte = new unsigned char[height*width];
while(fin.good())
{
fin.read(imageByte,1);
cout<<(int)imageByte;
++index;
}
}
else
{
cout<<"image file not open\n";
}
The height and width are read perfectly but the values in imageByte are junk values. Is something wrong?
rgds
swetha
Why you are not using ::LoadImage() and ::GetBitmapBits() or ::GetDiBits() ?
Cheers
-
April 17th, 2007, 07:30 AM
#5
Re: i want rgb details from .bmp
Use CImage class and create a object of it use like this
CImage imgoriginal;
imgOriginal.Load(dlg.GetFileName());
COLORREF pixel;
int maxY = imgOriginal.GetHeight(), maxX = imgOriginal.GetWidth();
byte r,g,b,avg;
for (int x=0; x<maxX; x++) {
for (int y=0; y<maxY; y++) {
pixel = imgOriginal.GetPixel(x,y);
r = GetRValue(pixel);
g = GetGValue(pixel);
b = GetBValue(pixel);
now you can get RGB values from it
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|