Solved

get bits of a bitmap

Posted on 2001-06-21
15
336 Views
Last Modified: 2013-12-03
All i need to do is determine the RGB colors of each bit in a BITMAP stored in memory. I allready asked this question, but no one could completely answer the question. I only got parts of answers that really never added up. This uses up all 500 points i used to have... So hopefully someone knows this now. Doesnt seem too hard of a problem does it?
0
Comment
Question by:GoldStone32767
  • 5
  • 4
  • 3
  • +3
15 Comments
 
LVL 2

Expert Comment

by:smitty1276
ID: 6216874
BITMAPFILEHEADER fh;
BITMAPINFOHEADER ih;
unsigned char    *bits;

//load the bitmap
ifstream in(fileName, ios::in | ios::binary);
if(in.fail())
{
  bits = NULL;
  return;
}

in.read( (unsigned char*)&fh, sizeof(BITMAPFILEHEADER) );

//check for valid bitmap
if( fh.bfType != 'MB' )
{
  bits = NULL;
  return;
}

in.read( (unsigned char*)&ih, sizeof(BITMAPINFOHEADER) );

int bitsize;
if( (bitsize = ih.biSizeImage) == 0 )
  bitsize = (ih.biWidth * ih.biBitCount + 7) /
            8 * abs(ih.biHeight);

bits = new unsigned char[(const)bitsize];
in.read( (unsigned char*)bits, bitsize );
}


If you use this code, bits will contain an array of unsigned characters (0-255) containing the BGR values of each bit. 3 bytes per pixel... B then G then R.


   
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6216939
Smitty1276:
It's a long shot, but ...

>>a BITMAP stored in memory

would seem to indicate that the bitmap is stored in memory, rather than in a file.

Goldstone32767:
Assuming that you have an HBITMAP to it, you can simply call:

int nByteCnt= 10000; // or whatever the size...

BYTE *pBitBuf= malloc( nByteCnt );
LONG GetBitmapBits(hbmp, nByteCnt, pBitBuf );

Alas, this does not guarantee that you will get a nice, clean 1-byte per pixel.  If the bitmap is a True color, each pixel will use three bytes.  If it is is HighColor, it will use two bytes.  If it is 256-color, it will use one byte (an index into a color palette, not an RGB value),  If it is a 16-color bitmap, it will use 4-bits per pixel and if it is monochrome, it will use 1 bit per pixel.

Perhaps the easiest way to work on this is to come in from the end result and work backward.  What do you want to do with the pixels once you have them?

-- Dan
0
 
LVL 22

Expert Comment

by:ambience
ID: 6216992
GetBitmapBits is obsolete now and is only there for backward compatibility , use GetDIBits now , this also lets you sprcify the desired format of the bitmap and that definitely includes the no of bits per pixel that you want.
0
 
LVL 22

Expert Comment

by:ambience
ID: 6217006
The BITMAPINFO structure that you pass to GetDIBits must have in its bitmapinfoheader field the following fields set. Note width and height are to be the same as that of hBitmap

biWidth = (width of the bitmap)
biHeight = (height of the bitmap)
biBitCount = [desired color resolution (1, 4, 8, or 24)]
0
 

Author Comment

by:GoldStone32767
ID: 6218289
Actually i have a CBitmap where i store my picture. Does this affect the code in any way?
0
 
LVL 22

Expert Comment

by:ambience
ID: 6218308
no CBitmap has an overloaded HBITMAP operator so its is usable whereever you would require and object of type HBITMAP .. if this is what you need to know
0
 

Author Comment

by:GoldStone32767
ID: 6218341
are you saying not to use the CBitmap to store my image?

and for the other question, i need the pixels so i can apply image effects to them. i will need to change all the bytes individually and then set them back to the bitmap. SetPixel does that but is way to slow, so i am looking for speed.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:ambience
ID: 6218382
No i did not say not to use CBitmap , i mean that you can use it easily where an ordinary HBITMAP is required.

There is another API SetDIBits which does exactly the opposite of GetDIBits , get bits using GetDIBits in the required format , manipulate them and then set them using SetDIBits...

hope this helps
0
 

Author Comment

by:GoldStone32767
ID: 6218868
is there any source i can see how to use the GetDIBits API? i tried once before... but it always returns a 0. which means it didnt read anything

here is the src i currently have... maybe someone can find the problem:

CBitmap bmp;
BITMAP bmpStr;
BITMAPINFO bmi;

me.imgBMP.GetBitmap( &bmpStr );

bmi.bmiHeader.biSize = 0x28;
bmi.bmiHeader.biBitCount = 0;
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biSizeImage = bmpStr.bmWidthBytes*bmpStr.bmHeight;

BYTE *pData = new BYTE[bmi.bmiHeader.biSizeImage];

int numBits = GetDIBits(mDC,(HBITMAP)me.imgBMP,0,bmpStr.bmHeight,pData,&bmi,DIB_RGB_COLORS );
     
where me is my own object. imgBMP is the CBitmap in object 'me'    
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6219440
>>but it always returns a 0. which means it didnt read anything

Call GetLastError() to learn why the GDI fn failed.  That is a general rule of thumb that will save you hours of frustation reading unhelpful comments like this one from self-proclamed experts like me.

-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6221130
THis link

http://home.earthlink.net/~danrollins/ee/FastBmpRotation.htm

Includes code that does all of the necessary stuff.  It gets down-and-dirty with the raw bits.

-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6307038
hi GoldStone32767,

Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 175 total points
ID: 6320351
hi GoldStone32767,

Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 
LVL 1

Expert Comment

by:Computer101
ID: 6703235
Comment from DanRollins accepted as answer.

Thank you
Computer101
Community Support Moderator
0
 

Expert Comment

by:Zakbass
ID: 8598171
Hi, Im zak

I want to know where and how can I store bitmap data in Palm table field !!
i use on palm bitmap field type bitmapType !

thanks.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

895 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now