Jump to content


Bitmap RGB Array - Newbie


  • You cannot reply to this topic
6 replies to this topic

#1 hug-hes

    New Member

  • Members
  • Pip
  • 4 posts

Posted 05 December 2006 - 02:46 PM

I need to write a C++ program to give me the RGB values for a bitmap image so that I can calculate different areas of colours in the image.

Any help would be appreciated!!:yes:

#2 Goz

    Senior Member

  • Members
  • PipPipPipPip
  • 574 posts

Posted 05 December 2006 - 03:54 PM

Assuming your bitmap stores 32-bits per component u would do it as follows.


unsigned int uRGB = ((unsigned int*)pBitmap)[x + (y * width)];

unsigned char uR  = (uRGB & 0x00ff0000) >> 16;

unsigned char uG  = (uRGB & 0x0000ff00) >> 8;

unsigned char uB  = (uRGB & 0x000000ff) >> 0;


Of course this assumes that its 32-bit and that R, G and B are in that order. If not you have to modify that basic algorithm a little bit.

Hope that helps!

#3 hug-hes

    New Member

  • Members
  • Pip
  • 4 posts

Posted 05 December 2006 - 03:56 PM

can u explain what this means?

#4 Goz

    Senior Member

  • Members
  • PipPipPipPip
  • 574 posts

Posted 05 December 2006 - 04:01 PM

Ok .. "width" is the bitmap's width. "x" is the x coordinate position and "y" is the y coordinate position.

The code above looks up a 32-bit value from the bitmap at (x,y) and then breaks the value up into individual RGB components.

#5 hug-hes

    New Member

  • Members
  • Pip
  • 4 posts

Posted 05 December 2006 - 04:10 PM

thanku for your help!

I am still having trouble loading my bitmap file

#6 Goz

    Senior Member

  • Members
  • PipPipPipPip
  • 574 posts

Posted 05 December 2006 - 04:14 PM

Hmmm ... if you are trying to load a .bmp directly you may find www.wotsit.org useful. It describes numerous file formats ... including the bmp format :)

#7 hug-hes

    New Member

  • Members
  • Pip
  • 4 posts

Posted 05 December 2006 - 04:16 PM

I will check it out thanks





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users