Thread: how use DIB's?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    how use DIB's?

    i'm trying build 2 functions for get and set pixles:
    Code:
    COLORREF GetDIBPixel(HDC hdcImage, long posX, long posY)
    {
        BITMAP structBitmapHeader;
        memset( &structBitmapHeader, 0, sizeof(BITMAP) );
    
        HBITMAP hBitmap =(HBITMAP) GetCurrentObject(hdcImage, OBJ_BITMAP);
        GetObject(hBitmap, sizeof(BITMAP), &structBitmapHeader);
    
        WORD wBmpWidth=structBitmapHeader.bmWidth;
        WORD wBmpHeight=structBitmapHeader.bmHeight;
    
        BYTE ImageData[(structBitmapHeader.bmBitsPixel % 8) - 1] [structBitmapHeader.bmWidth - 1] [structBitmapHeader.bmHeight - 1];
        GetBitmapBits(hBitmap, structBitmapHeader.bmWidthBytes * structBitmapHeader.bmHeight, &ImageData);
    
        COLORREF clrReturnColor=RGB(ImageData[2] [posX] [posY],ImageData[1] [posX] [posY],ImageData[0] [posX] [posY]);
        return clrReturnColor;
    }
    
    void SetDIBPixel(HDC hdcImage, long posX, long posY, COLORREF clrNewColor)
    {
        BITMAP structBitmapHeader;
        memset( &structBitmapHeader, 0, sizeof(BITMAP) );
    
        HBITMAP hBitmap =(HBITMAP) GetCurrentObject(hdcImage, OBJ_BITMAP);
        GetObject(hBitmap, sizeof(BITMAP), &structBitmapHeader);
    
        WORD wBmpWidth=structBitmapHeader.bmWidth;
        WORD wBmpHeight=structBitmapHeader.bmHeight;
    
        BYTE ImageData[(structBitmapHeader.bmBitsPixel % 8) - 1] [structBitmapHeader.bmWidth - 1] [structBitmapHeader.bmHeight - 1];
        GetBitmapBits(hBitmap, structBitmapHeader.bmWidthBytes * structBitmapHeader.bmHeight, &ImageData);
    
        ImageData[2] [posX] [posY]=GetRValue(clrNewColor);
        ImageData[1] [posX] [posY]=GetGValue(clrNewColor);
        ImageData[0] [posX] [posY]=GetBValue(clrNewColor);
    
        SetBitmapBits(hBitmap, structBitmapHeader.bmWidthBytes * structBitmapHeader.bmHeight, &ImageData);
    }
    but i get a memory leak.
    can anyone advice me?

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    What does memset() do?

    Aside from a memory leak problem, are you calling GetDIBPixel() and SetDIBPixel() a very large number of times?

    It looks like there is a lot of things being done in those two functions that maybe only need to be done once outside of those two functions?

    -
    Last edited by megafiddle; 08-22-2015 at 09:34 PM.

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Since you have the HDC, what's wrong with the normal GetPixel and SetPixel functions? Yes, they're slow, but calling into the OS three times instead of one isn't likely to be any faster. They're also not likely to overflow the stack given a big enough DIB

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by megafiddle View Post
    What does memset() do?
    Disregard that. I thought memset was one of your own functions.

    -

Popular pages Recent additions subscribe to a feed