This topic is now archived and is closed to further replies.

Problem while creating a bitmask

This topic is 6016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I try to make a bitmask of a bitmap, but when I try to get the value of a pixel (TRUE or FALSE), an error occurs. A message box says that the memory can't be read. Here's the code: #include #include using namespace std; /////////////////////////////////////////////////////////////////// class CBoolArray { int width; int height; bool* bArray; public: CBoolArray(int x, int y); ~CBoolArray() {delete [] bArray;} Set(int x, int y, bool value); bool& operator() (int x, int y) {return bArray[x+y*width];} }; CBoolArray::CBoolArray(int x, int y) { width = x; height = y; bArray = new bool[width*height]; } CBoolArray::Set(int x, int y, bool value) { bArray[x+y*width] = value; return 0; } /////////////////////////////////////////////////////////////////// int CreateBitmask(const char szBitmap[], CBoolArray bMask, int width, int height, int rtrans, int gtrans, int btrans) { HDC hdcImage; HBITMAP hbm; int pixelvalue = 0; hbm = (HBITMAP) LoadImage(GetModuleHandle(NULL), szBitmap, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION); if (hbm == NULL) hbm = (HBITMAP) LoadImage(NULL, szBitmap, IMAGE_BITMAP, width, height, LR_LOADFROMFILE | LR_CREATEDIBSECTION); hdcImage = CreateCompatibleDC(NULL); SelectObject(hdcImage, hbm); for (short y = 0; y < height; y++) { for (short x = 0; x < width; x++) { short r, g, b; pixelvalue = GetPixel(hdcImage, x, y); r = (pixelvalue & 0xFF); g = (pixelvalue & 0xFF00)>>8; b = (pixelvalue & 0xFF0000)>>16; if (r != rtrans || g != gtrans || b != btrans) bMask.Set(x, y, true); else bMask.Set(x, y, false); } } return 0; } /////////////////////////////////////////////////////////////////// int main() { CBoolArray BackgroundMask(1024, 768); CreateBitmask("background.bmp", BackgroundMask, 1024, 768, 255, 0, 255); if (BackgroundMask(50, 50) == true) cout << "TRUE" << endl; else cout << "FALSE" << endl; while (1) { } return 0; } I think it's my way of reading the pixel value with BackgroundMask(50, 50) which causes the error. But how can I solve that problem? Thanks. Edited by - Alload on June 22, 2001 4:54:46 AM

Share this post

Link to post
Share on other sites