Jump to content
  • Advertisement
Sign in to follow this  
cambalinho

win32 - DIB's: creating a shadow effect

This topic is 909 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 have these functions for get a image pixel array:

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);*/

on my image class, i have 1 function for shadow(just add some black pixels for do a shadow effect):

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);
    }

i don't know if my problem is how i calculate the pixelOffset2 :(

but i get these results :(

(see the image that don't give me a shadow effect :( )

what i'm doing wrong?

[attachment=31244:shadow problem.png]

 

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!