Solved

GetBitmapBits

Posted on 2001-06-03
9
493 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

773 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