win32 - DIB's: how can i calculate the next position on pixels?

i have some functions for get pixel data on array:
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
69
70
71
72
73
74
75
typedef std::vector<BYTE> pixeldata;
pixeldata GetImagePixel(HDC hdcImage)
{
    BITMAP bmp = {0};
    BITMAPINFO Info = {0};
    memset( &bmp, 0, sizeof(BITMAP) );
    HBITMAP hBitmap =(HBITMAP) GetCurrentObject(hdcImage, OBJ_BITMAP);
    GetObject(hBitmap, sizeof(BITMAP), &bmp);

    BITMAPINFO info { };
    info.bmiHeader.biSize = sizeof(info.bmiHeader);
    info.bmiHeader.biWidth = bmp.bmWidth;
    // pay attention to the sign, you most likely want a
    // top-down pixel array as it's easier to use
    info.bmiHeader.biHeight = -bmp.bmHeight;
    info.bmiHeader.biPlanes = 1;
    info.bmiHeader.biBitCount = 32;
    info.bmiHeader.biCompression = BI_RGB;

    // the following calculations work for 16/24/32 bits bitmaps
    // but assume a byte pixel array
    size_t pixelSize = info.bmiHeader.biBitCount / 8;
    // the + 3 ) & ~3 part is there to ensure that each
    // scan line is 4 byte aligned
    size_t scanlineSize = (pixelSize * info.bmiHeader.biWidth + 3) & ~3;
    size_t bitmapSize = bmp.bmHeight * scanlineSize;

    pixeldata pixels(bitmapSize);
    GetDIBits(hdcImage, hBitmap, 0, bmp.bmHeight, &pixels[0], &info, DIB_RGB_COLORS);
    bmp.bmHeight = std::abs(bmp.bmHeight);
    return pixels;
}

void SetImageData(HDC hdcDestination, pixeldata pixels)
{
    BITMAP bmp = {0};
    BITMAPINFO Info = {0};
    memset( &bmp, 0, sizeof(BITMAP) );
    HBITMAP hBitmap =(HBITMAP) GetCurrentObject(hdcDestination, OBJ_BITMAP);
    GetObject(hBitmap, sizeof(BITMAP), &bmp);
    BITMAPINFO info { };
    info.bmiHeader.biSize = sizeof(info.bmiHeader);
    info.bmiHeader.biWidth = bmp.bmWidth;
    // pay attention to the sign, you most likely want a
    // top-down pixel array as it's easier to use
    info.bmiHeader.biHeight = -bmp.bmHeight;
    info.bmiHeader.biPlanes = 1;
    info.bmiHeader.biBitCount = 32;
    info.bmiHeader.biCompression = BI_RGB;

    SetDIBits(hdcDestination, hBitmap, 0, bmp.bmHeight, &pixels[0], &info, DIB_RGB_COLORS);
}
//how use it
/*pixeldata pixels=GetImagePixel(imgImage);

size_t pixelSize = bm.bmBitsPixel / 8;
size_t scanlineSize = (pixelSize * bm.bmWidth + 3) & ~3;

int x=0, y=0;
for(y=0; y<imgImage.height(); y++)
{
    for(x=0; x<imgImage.width(); x++)
    {
        size_t pixelOffset = y * scanlineSize + x * pixelSize;

        COLORREF clrActualColor=RGB(pixels[pixelOffset+2],pixels[pixelOffset+1],pixels[pixelOffset+ 0]);
        if(clrActualColor ==RGB(0,0,0))
        {
            pixels[pixelOffset+2]=GetRValue(GetSysColor(COLOR_MENU));
            pixels[pixelOffset+1]=GetGValue(GetSysColor(COLOR_MENU));
            pixels[pixelOffset+0]=GetBValue(GetSysColor(COLOR_MENU));
        }
    }
}
SetImageData(hMemDC,pixels);*/

from these code, i'm doing a shadow effect:
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
void Shadow(int PosX=0, int PosY=0)
    {
        pixeldata pixels=GetImagePixel(HBitmap);

        size_t pixelSize = HBitmap.bitperpixel() / 8;
        size_t scanlineSize = (pixelSize * HBitmap.Width() + 3) & ~3;

        int x=0, y=0;
        COLORREF BackColor;
        for(y=0; y<HBitmap.Height(); y++)
        {
            for(x=0; x<HBitmap.Width(); x++)
            {
                //getting pixel and drawed pixel positions:
                size_t pixelOffset = y * scanlineSize + x * pixelSize;//actual pixel position
                size_t pixelOffset2 = (y+PosY) * scanlineSize + (x+PosX) * pixelSize; //the position where the shadow pixel is drawed

                //gettin backcolor:
                if(x==0 && y==0)
                    BackColor=RGB(pixels[pixelOffset+2],pixels[pixelOffset+1],pixels[pixelOffset+ 0]);

                //getting actual color:
                COLORREF clrActualColor=RGB(pixels[pixelOffset+2],pixels[pixelOffset+1],pixels[pixelOffset+ 0]);
                if(clrActualColor !=BackColor)//if the actual color is diferent of backcolor then:
                {
                    //if the drawed pixel position is backcolor pixel, then draw it:
                    if(RGB(pixels[pixelOffset2+2],pixels[pixelOffset2+1],pixels[pixelOffset2+0])==BackColor && RGB(pixels[pixelOffset2+2],pixels[pixelOffset2+1],pixels[pixelOffset2+0])!=CLR_INVALID)
                    {
                        pixels[pixelOffset2+2]=GetRValue(0);
                        pixels[pixelOffset2+1]=GetGValue(0);
                        pixels[pixelOffset2+0]=GetBValue(0);
                    }

                }
            }
        }
        SetImageData(HBitmap,pixels);
    }

PosX and PosY are for change shadow position.
but or it's the pixelOffset2 calculation or something, because i don't get a shadow effect :(
how these function works:
1 - i have the image(i must add the shadow effect above the image, in these case);
2 - if the actual pixel is a backcolor, then ignoret it. if it's another color...;
3 - the actual position pixel, plus PosX and PosY pixel position are backcolor, then add a black pixel, else ignore it.

i'm trying do the shadow on these way. but i don't get a shadow, i get a diferent strange effect. can anyone explain to me what i'm doing wrong?
i belive the pixelOffset2 calculation maybe wrong :(
i did a several mistakes with code:
1 - i didn't tested the x+PosX and y+PosY limits;
2 - i was not testing, correctly, the clrActualColor.
heres the function corrected:
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
void Shadow(int PosX=0, int PosY=0, COLORREF ShadowColor=RGB(0,0,0))
    {
        pixeldata pixels=GetImagePixel(HBitmap);

        size_t pixelSize = HBitmap.bitperpixel() / 8;
        size_t scanlineSize = (pixelSize * HBitmap.Width() + 3) & ~3;

        int x=0, y=0;
        COLORREF BackColor;
        for(y=0; y<HBitmap.Height(); y++)
        {
            for(x=0; x<HBitmap.Width(); x++)
            {
                if((y+PosY)>=HBitmap.Height() || (x+PosX)>=HBitmap.Width())
                    continue;
                //getting pixel and drawed pixel positions:
                size_t pixelOffset = y * scanlineSize + x * pixelSize;//actual pixel position
                size_t pixelOffset2 =(y+PosY) * scanlineSize + (x+PosX) * pixelSize; //the position where the shadow pixel is drawed

                //gettin backcolor:
                if(x==0 && y==0)
                {
                   BackColor=RGB(pixels[pixelOffset+2],pixels[pixelOffset+1],pixels[pixelOffset+ 0]);
                }


                //getting actual color:
                COLORREF clrActualColor=RGB(pixels[pixelOffset+2],pixels[pixelOffset+1],pixels[pixelOffset+ 0]);
                COLORREF clrActualColor2=RGB(pixels[pixelOffset2+2],pixels[pixelOffset2+1],pixels[pixelOffset2+ 0]);
                if((clrActualColor!=BackColor && clrActualColor!=ShadowColor)&& clrActualColor2==BackColor)//if the actual color is diferent of backcolor then:
                {
                    pixels[pixelOffset2+2]=GetRValue(ShadowColor);
                    pixels[pixelOffset2+1]=GetGValue(ShadowColor);
                    pixels[pixelOffset2+0]=GetBValue(ShadowColor);
                }
            }
        }
        SetImageData(HBitmap,pixels);
    }

i hope these topic make the readers thinking where i did the mistakes and why ;)
heres the image result:

https://static.daniweb.com/attachments/4/e6c3a6bf502bb43923a6e757d35a7ecd.png

thanks to all
Topic archived. No new replies allowed.