how speed up DIB's and Regions?

i have these code for DIB's:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
BYTE* Get24BitPixels(HBITMAP pBitmap, WORD *pwWidth, WORD *pwHeight)
    {
        // a bitmap object just to get bitmap width and height
        BITMAP bmpBmp;

        // pointer to original bitmap info
        LPBITMAPINFO pbmiInfo;

        // bitmap info will hold the new 24bit bitmap info
        BITMAPINFO bmiInfo;

        // width and height of the bitmap
        WORD wBmpWidth, wBmpHeight;

        // ---------------------------------------------------------
        // get some info from the bitmap
        // ---------------------------------------------------------
        GetObject(pBitmap, sizeof(bmpBmp),&bmpBmp);
        pbmiInfo   = (LPBITMAPINFO)&bmpBmp;

        // get width and height
        wBmpWidth  = (WORD)pbmiInfo->bmiHeader.biWidth;
        wBmpWidth -= (wBmpWidth%4);                       // width is 4 byte boundary aligned.
        wBmpHeight = (WORD)pbmiInfo->bmiHeader.biHeight;

        // copy to caller width and height parms
        *pwWidth  = wBmpWidth;
        *pwHeight = wBmpHeight;
        // ---------------------------------------------------------

        // allocate width * height * 24bits pixels
        //BYTE *pPixels = new BYTE[wBmpWidth*wBmpHeight*3];
        BYTE *pPixels = new (std::nothrow) BYTE[wBmpWidth*wBmpHeight*3];
        if (pPixels==0)
            return NULL;

        // get user desktop device context to get pixels from
        HDC hDC = GetWindowDC(NULL);

        // fill desired structure
        bmiInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        bmiInfo.bmiHeader.biWidth = wBmpWidth;
        bmiInfo.bmiHeader.biHeight = -wBmpHeight;
        bmiInfo.bmiHeader.biPlanes = 1;
        bmiInfo.bmiHeader.biBitCount = 24;
        bmiInfo.bmiHeader.biCompression = BI_RGB;
        bmiInfo.bmiHeader.biSizeImage = wBmpWidth*wBmpHeight*3;
        bmiInfo.bmiHeader.biXPelsPerMeter = 0;
        bmiInfo.bmiHeader.biYPelsPerMeter = 0;
        bmiInfo.bmiHeader.biClrUsed = 0;
        bmiInfo.bmiHeader.biClrImportant = 0;

        // get pixels from the original bitmap converted to 24bits
        int iRes = GetDIBits(hDC,pBitmap,0,wBmpHeight,(LPVOID)pPixels,&bmiInfo,DIB_RGB_COLORS);

        // release the device context
        ReleaseDC(NULL,hDC);

        // if failed, cancel the operation.
        if (!iRes)
        {
            delete[] pPixels;
            return NULL;
        };

        // return the pixel array
        return pPixels;
    }

and Regions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
HRGN RegionbyBitmap(HBITMAP pBitmap,COLORREF clrTransparent=-1 )
    {
        BYTE jTranspR = GetRValue(clrTransparent), jTranspG=GetGValue(clrTransparent), jTranspB=GetBValue(clrTransparent);
        // bitmap width and height
        WORD wBmpWidth,wBmpHeight;

        // the final region and a temporary region
        HRGN hRgn, hTmpRgn;

        // 24bit pixels from the bitmap
        BYTE *pPixels = Get24BitPixels(pBitmap, &wBmpWidth, &wBmpHeight);
        if (!pPixels) return NULL;

        // create our working region
        hRgn = CreateRectRgn(0,0,wBmpWidth,wBmpHeight);
        if (!hRgn)
        {
            delete[] pPixels;
            return NULL;
        }

        // ---------------------------------------------------------
        // scan the bitmap
        // ---------------------------------------------------------


        DWORD p=0;
        for (WORD y=0; y<wBmpHeight; y++)
        {
            for (WORD x=0; x<wBmpWidth; x++)
            {
              BYTE jRed   = pPixels[p+2];
              BYTE jGreen = pPixels[p+1];
              BYTE jBlue  = pPixels[p+0];

              if ((jRed == jTranspR && jGreen == jTranspG && jBlue == jTranspB))
              {
                // remove transparent color from region
                hTmpRgn = CreateRectRgn(x,y,x+1,y+1);
                CombineRgn(hRgn, hRgn, hTmpRgn, RGN_XOR);
                DeleteObject(hTmpRgn);


              }

              // next pixel
              p+=3;
            }
        }

        // release pixels
        delete[] pPixels;

        return hRgn;
    }

on DIB's: i don't know if theres another way for be more faster;
on Regions: using region on pixel way it can be very slow. can anyone advice me?
(using these code on a timer, it can freezes the application :( )
Last edited on
The best would be using your own bitmap using CreateDIBSection(...):

https://msdn.microsoft.com/en-us/library/windows/desktop/dd183494%28v=vs.85%29.aspx

This allows you to access the pixel directly. You might create it within the WM_SIZE message.

Within the WM_PAINT message:

First create your own dc using CreateCompatibleDC(...):

https://msdn.microsoft.com/en-us/library/windows/desktop/dd183489%28v=vs.85%29.aspx

Select the the previously created bitmap. After you're done with your drawing you need BitBlt(...):

https://msdn.microsoft.com/en-us/library/windows/desktop/dd183370%28v=vs.85%29.aspx

So it becomes visible.

Why do you think you need that region stuff?
Yes, it is slow especially when use with each and every pixel.

Topic archived. No new replies allowed.