Solved

GetBitmapBits

Posted on 2001-06-03
9
489 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
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 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

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

930 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

10 Experts available now in Live!

Get 1:1 Help Now