CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    C++: how calculate the stretch and alpha blend on pixels?

    i have 2 pixels pointers(Destination and Source).
    1 - how can i calculate or get Alpha value on pixel?
    2 - how can i combine the 2 pixels for AlphaBlend?
    3 - how can i calculate for stretch the image?
    i tried several code, but now success
    Code:
    bool Draw(HDC dcDest, int x, int y, int cx, int cy, HDC dcSrc, int sx, int sy, int scx, int scy, int alpha, COLORREF rgbMask)        {
                BITMAPINFOHEADER BMI;
                // Fill in the header info.
                BMI.biSize = sizeof(BITMAPINFOHEADER);
                BMI.biWidth = cx;
                BMI.biHeight = -(LONG)cy;
                BMI.biPlanes = 1;
                BMI.biBitCount = 32;
                BMI.biCompression = BI_RGB;   // No compression
                BMI.biSizeImage = 0;
                BMI.biXPelsPerMeter = 0;
                BMI.biYPelsPerMeter = 0;
                BMI.biClrUsed = 0;           // Always use the whole palette.
                BMI.biClrImportant = 0;
    
    
                BYTE * pSrcBits;
                HBITMAP hbmSrc;
                // Create DIB section in shared memory
                hbmSrc = CreateDIBSection(dcSrc, (BITMAPINFO *)&BMI,
                    DIB_RGB_COLORS, (void **)&pSrcBits, 0, 0l);
    
    
                BYTE * pDestBits;
                HBITMAP hbmDest;
                // Create DIB section in shared memory
                hbmDest = CreateDIBSection(dcDest, (BITMAPINFO *)&BMI,
                    DIB_RGB_COLORS, (void **)&pDestBits, 0, 0l);
    
    
    
    
                // Copy our source and destination bitmaps onto our DIBSections,
                // so we can get access to their bits using the BYTE *'s we passed into CreateDIBSection
    
    
                HDC dc = CreateCompatibleDC(NULL);
    
    
                HBITMAP dcOld = (HBITMAP)SelectObject(dc, hbmSrc);
    
    
                if (!StretchBlt(dc, 0, 0, cx, cy, dcSrc, sx, sy, scx, scy, SRCCOPY))
                    return false;
    
    
                SelectObject(dc, hbmDest);
                if (!StretchBlt(dc, 0, 0, cx, cy, dcDest, x, x, cx, cy, SRCCOPY))
                    return false;
    
    
                SelectObject(dc, dcOld);
                DeleteDC(dc);
    
    
                int red = GetRValue(rgbMask);
                int green = GetGValue(rgbMask);
                int blue = GetBValue(rgbMask);
    
    
                for (int j = 0; j < cy; ++j)
                {
                    LPBYTE pbDestRGB = (LPBYTE)&((DWORD*)pDestBits)[j * cx];
                    LPBYTE pbSrcRGB = (LPBYTE)&((DWORD*)pSrcBits)[j * cx];
    
    
                    for (int i = 0; i < cx; ++i)
                    {
                        if (pbSrcRGB[0] != blue ||
                            pbSrcRGB[1] != green ||
                            pbSrcRGB[2] != red)
                        {
                            pbSrcRGB[0] = (pbDestRGB[0] * (255 - alpha) + pbSrcRGB[0] * alpha) >> 8;
                            pbSrcRGB[1] = (pbDestRGB[1] * (255 - alpha) + pbSrcRGB[1] * alpha) >> 8;
                            pbSrcRGB[2] = (pbDestRGB[2] * (255 - alpha) + pbSrcRGB[2] * alpha) >> 8;
                        }
                        else
                        {
                            pbSrcRGB[0] = pbDestRGB[0];
                            pbSrcRGB[1] = pbDestRGB[1];
                            pbSrcRGB[2] = pbDestRGB[2];
                        }
    
    
                        pbSrcRGB += 4;
                        pbDestRGB += 4;
                    }
                }
    
    
                dc = CreateCompatibleDC(NULL);
    
    
                dcOld = (HBITMAP)SelectObject(dc, hbmSrc);
                if (!BitBlt(dcDest, x, y, cx, cy, dc, 0, 0, SRCCOPY))
    
    
                return false;
                DeleteDC(dc);
    
    
                DeleteObject(hbmSrc);
                DeleteObject(hbmDest);
    
    
                return true;
            }
    or no pixels, or flickers... but no correct alphablend
    Code:
    pbSrcRGB[0] = (pbDestRGB[0] * (255 - alpha) + pbSrcRGB[0] * alpha) >> 8; //Red             
     pbSrcRGB[1] = (pbDestRGB[1] * (255 - alpha) + pbSrcRGB[1] * alpha) >> 8;//Green
                            pbSrcRGB[2] = (pbDestRGB[2] * (255 - alpha) + pbSrcRGB[2] * alpha) >> 8; //Blue
    Last edited by Cambalinho; April 10th, 2022 at 10:41 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured