CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2006
    Posts
    205

    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.

  2. #2
    Join Date
    Oct 2006
    Posts
    616

    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.

  3. #3
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    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);

  4. #4
    Join Date
    May 2005
    Posts
    4,954

    Re: i want rgb details from .bmp

    Quote 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
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  5. #5
    Join Date
    Mar 2007
    Posts
    157

    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
  •  





Click Here to Expand Forum to Full Width

Featured