• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 525
  • Last Modified:

GetBitmapBits

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
GoldStone32767
Asked:
GoldStone32767
1 Solution
 
JackThorntonCommented:
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
 
GoldStone32767Author Commented:
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
 
smitty1276Commented:
 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
robpittCommented:
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
 
JackThorntonCommented:
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
 
danielj040300Commented:
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
 
griesshCommented:
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
 
griesshCommented:
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
 
NetminderCommented:
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now