Solved

GetBitmapBits

Posted on 2001-06-03
9
482 Views
Last Modified: 2009-12-16
all i need to to is to take a bitmap from memory, and copy the bits to an array. then i need to set ALL the bits to a RGB color given the R , G, B
0
Comment
Question by:GoldStone32767
9 Comments
 
LVL 3

Expert Comment

by:JackThornton
ID: 6151138
If you're going to set all the bits to the same color, why do you have to copy a bitmap from memory? Why not fill your array with that color to begin with? (Perhaps your question needs to be phrased better)

0
 

Author Comment

by:GoldStone32767
ID: 6151243
i said that simply to get the code i needed to work with. And i dont want to use GetPixel/SetPixel. That api is slow
if this is still confusing i will restate it for you.

I need an image in memory copied to an array so that i can manipulate the colors. And at the end, move the array of bits back into the image location in memory.
0
 
LVL 2

Expert Comment

by:smitty1276
ID: 6151425
 This will read a bitmap straight from the file.  Change all occurences of GLubyte to unsigned char.

  BITMAPFILEHEADER fh;
  BITMAPINFOHEADER ih;
  GLubyte          *bits;

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

  //read the actual bits
  bits = new GLubyte[(const)bitsize];
  in.read( (unsigned char*)bits, bitsize );
  in.close();

The pixel values will be stored in BGR format, three bytes per pixel (assuming a 24-bit bitmap).
 
 
0
 
LVL 5

Expert Comment

by:robpitt
ID: 6151858
Assuming your running MS Windows (something you didn't state) what is your "bitmap" currently?
Is it a DIBSection, a DDB or just a block of raw memory?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Accepted Solution

by:
JackThornton earned 50 total points
ID: 6153856
Here is some code that should do the trick. I scratched it out mostly from memory without testing (not intending to be snotty here, but for 50 points I'm not going to do all your work for you).

DIB's are usually kept in memory in bottom-up order (i.e. the first row in memory is the bottommost on the display). You can change that by negating the height you feed to it (i.e. setting the height to 100 gives you a bottom-up bitmap 100 pixels tall; setting height to -100 gives you a top-down bitmap 100 pixels tall). Rows are padded to the nearest DWORD in byte width; therefore, if your 24bpp bitmap is 33 pixels wide, then the number of bytes on each row is 100, not 99. 24bpp bytes are in blue, green, red order.

The algorithm is as follows: allocate a DIB section (this gives you directly accessable bitmap data) - create a compatible DC and select the DIB section into it - blit bitmap from the screen into your DIB section - manipulate your bitmap to your heart's content - blit your bitmap back onto the screen - clean up.

// assume we have hDC as handle to current device context, as in the middle of OnPaint
// X, Y, width and height are location and dimensions of bitmap to be copied

BITMAPINFOHEADER hdr;
HBITMAP hOurBitmap;  // handle to custom bitmap (returned by CreateDIBSection)
void *pBitmapData; // pointer to bitmap array (allocated by CreateDIBSection, freed by DeleteObject)

    // fill in bitmap specifications
    hdr.biSize = sizeof(BITMAPINFOHEADER);
    hdr.biWidth = width;
    hdr.biHeight = height;     // use -height if you want top-down order
    hdr.biPlanes = 1;
    hdr.biBitCount = 24;
    hdr.biCompression = BI_RGB;
    hdr.biSizeImage = 0;
    hdr.biXPelsPerMeter = hdr.biYPelsPerMeter = 72 * 12 * 3;  // who really cares!
    hdr.biClrUsed = 0;
    hdr.biClrImportant = 0;

    // create a DIB section
    hOurBitmap = CreateDIBSection(hDC, &hdr, DIB_RGB_COLORS, &pBitmapData, 0, 0);

    // create compatable DC
    HDC memDC = CreateCompatibleDC(hDC);

    // select our new bitmap into it
    SelectObject(hDC, hOurBitmap);

    // blit from screen to our bitmap
    BitBlt(memDC, 0, 0, width, height, hDC, X, Y, SRCCOPY);

    // manipulate the bitmap in memory
    ...

    // blit back to the screen
    BitBlt(hDC, X, Y, width, height, memDC, 0, 0, SRCCOPY);

    // get rid of the temporary DC
    DeleteDC(memDC);

    // free up the DIB section
    DeleteObject(hOurBitmap);

0
 
LVL 1

Expert Comment

by:danielj040300
ID: 6155586
I would like to asume you only have a handle to the bitmap available.

eg. HBITMAP hBitmap
You aquired this from some object.

To get the byte array of the bitmap of this HBITMAP variable you use the following code.

BITMAP bitmap;
GetObject( hBitmap, sizeof( HBITMAP ), &bitmap );

bitmap.bmBits will contain the bits to your bitmap you want to manipulate.

Daniel.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6811440
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days.
Unless there is objection or further activity,  I will suggest to accept "JackThornton" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Please do not accept this comment as an answer!
======
Werner
0
 
LVL 11

Expert Comment

by:griessh
ID: 6811462
GoldStone32767

You have several old questions open where experts tried to help you and that need to be worked on.


I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days.
Unless there is objection or further activity,  I will suggest to accept "JackThornton" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Please do not accept this comment as an answer!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6828435
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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

13 Experts available now in Live!

Get 1:1 Help Now