Solved

extracting RGB values from BMP

Posted on 2003-12-11
6
721 Views
Last Modified: 2013-11-20
Hello people
newbie here well i have displayed a BMP image using CBitmap HBITMAP and BITMAP I am using MFC ofcourse and would like to manipulate the actual pixel values that is the RGB values but i am finding it extremely difficult to get the RGB values please help me out how to get the RGB values for a BMP image
0
Comment
Question by:cyberizen
  • 2
6 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 50 total points
ID: 9926483
Put your bitmap into a DC

CDC* pDC=GetDC();
    CDC MemDC;
    MemDC.CreateCompatibleDC(pDC);
    CBitmap* pOldBitmap = MemDC.SelectObject(&m_bitmap);

MemDC.GetPixel(x,y); returns the RGB of the pixel at x,y


RELEASE THE DC WHEN FINISHED
ReleaseDC(pDC);
0
 

Expert Comment

by:MAYURNATH
ID: 9972131
My opinion about the bytes to extract is :


BITMAP FILE Contains :

BITMAPFILEHEADER
BITMAPINFOHEADER
COLORTABLE
BYTES OF THE BITMAP DATA


4 aspects are serailly placed in bitmap file OK.

BITMAPINFOHEADER tells about the bitmap type - 16 colors ( 4 bit ) (0.5 bytes ) , 256 colors ( 8 bit ) (1 byte ) , 24 bit ( 3 bytes ).

Colortable explains the table of colors that exists in bitmap data. - RGB values ( Here RGB is RGBQUAD - 4 bytes )

according to the color type using the color table we can get RGB value .

for 16 colors ( 4 bit ) (1/2 byte) - first half byte in bytes of bitmap  data represent the index of the color in color table.
with this index u have to get the color from the color table.
ex. let us take first byte is 0xBA - 10111010 (binary ) then 2 colors in 4 bit type , u get 0xB and 0xA ie. 11 and 10.
then u go for 11 and 10 indices of color table and get the RGB color.

if it is 256 colors in above example 0xBA - 186 index. in 256 colors color table contains 256 colors.
so go for 186 index and get the corresponding RGB value.


in the case of  24 bit - 3 bytes which will directly give RGB value.
ie no color table concept in 24 bit type bitmap.
directly 3 bytes on pixel so R,G,B - 3 bytes u get.

i think this is clear about the bmp file format and bitmap format.

My opinion is write the bit handling code - because it will be faster and easy for modification once written.
The above solution is good but to setpixel it will take lot of time if u try for more than 80X80 pixel bmp .

thanx
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 11850729
My comment would be a way to achieve the desired effect
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Change owner from userA to server Local Administrators 7 62
only14 challenge 19 75
firstChar challenge 13 115
Problem to open Excel file 15 130
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question