How to calculate to get new pixel color by mixing the alpha value with r, g and b value
-
DWORD destRedColor = 0;
DWORD destBlueColor = 0;
DWORD destGreenColor = 0;
DWORD destAlphaColor = 0;
DWORD SrcRedColor = 0;
DWORD SrcBlueColor = 0;
DWORD SrcGreenColor = 0;
DWORD SrcAlphaColor = 0;
int src_const_alpha = 0;// After all the inits we can do the job : Replace Color
for ( int i = ( ( bm.bmWidth * bm.bmHeight ) - 1 ); i >= 0; --i )
{
COLORREF clrRef = ptPixels[i]; //Bitmap pixel color taken with CreateDIBSection's void* lpBits;
clrRef = COLORREF2RGB( clrRef );//taking alpha,red,green and blue values. DWORD redDest = GetRValue( clrRef ); DWORD greenDest = GetGValue( clrRef); DWORD blueDest = GetBValue( clrRef); BYTE \*ptrAlphaDest = reinterpret\_cast<BYTE\*>( &clrRef ); DWORD alphaDest; alphaDest = ptrAlphaDest\[0\]; //taking alpha,red,green and blue values. DWORD redSrc = GetRValue( \_cNewColor ); //Shadow Color ( of user's choice ) DWORD greensrc = GetGValue( \_cNewColor ); DWORD blueSrc = GetBValue( \_cNewColor ); BYTE \*ptrAlphaSrc = reinterpret\_cast<BYTE\*>( &\_cNewColor ); DWORD alphaSrc; alphaSrc = ptrAlphaSrc\[0\]; src\_const\_alpha = int( m\_intensity \* 2.55 ); SrcRedColor = redSrc \* src\_const\_alpha / 255.0; SrcGreenColor = greensrc \* src\_const\_alpha / 255.0; SrcBlueColor = blueSrc \* src\_const\_alpha / 255.0; SrcAlphaColor = alphaSrc \* src\_const\_alpha / 255.0; destRedColor = SrcRedColor + ( 1 - SrcAlphaColor ) \* redDest; destGreenColor = SrcGreenColor + ( 1 - SrcAlphaColor ) \* greenDest; destBlueColor = SrcBlueColor + ( 1 - SrcAlphaColor ) \* blueDest; destAlphaColor = SrcAlphaColor + ( 1 - SrcAlphaColor ) \* alphaDest; ptPixels\[i\] = (destAlphaColor << 24 ) | (destRedColor << 16 ) | (destGreenColor << 8 ) | (destBlueColor);
}
-
DWORD destRedColor = 0;
DWORD destBlueColor = 0;
DWORD destGreenColor = 0;
DWORD destAlphaColor = 0;
DWORD SrcRedColor = 0;
DWORD SrcBlueColor = 0;
DWORD SrcGreenColor = 0;
DWORD SrcAlphaColor = 0;
int src_const_alpha = 0;// After all the inits we can do the job : Replace Color
for ( int i = ( ( bm.bmWidth * bm.bmHeight ) - 1 ); i >= 0; --i )
{
COLORREF clrRef = ptPixels[i]; //Bitmap pixel color taken with CreateDIBSection's void* lpBits;
clrRef = COLORREF2RGB( clrRef );//taking alpha,red,green and blue values. DWORD redDest = GetRValue( clrRef ); DWORD greenDest = GetGValue( clrRef); DWORD blueDest = GetBValue( clrRef); BYTE \*ptrAlphaDest = reinterpret\_cast<BYTE\*>( &clrRef ); DWORD alphaDest; alphaDest = ptrAlphaDest\[0\]; //taking alpha,red,green and blue values. DWORD redSrc = GetRValue( \_cNewColor ); //Shadow Color ( of user's choice ) DWORD greensrc = GetGValue( \_cNewColor ); DWORD blueSrc = GetBValue( \_cNewColor ); BYTE \*ptrAlphaSrc = reinterpret\_cast<BYTE\*>( &\_cNewColor ); DWORD alphaSrc; alphaSrc = ptrAlphaSrc\[0\]; src\_const\_alpha = int( m\_intensity \* 2.55 ); SrcRedColor = redSrc \* src\_const\_alpha / 255.0; SrcGreenColor = greensrc \* src\_const\_alpha / 255.0; SrcBlueColor = blueSrc \* src\_const\_alpha / 255.0; SrcAlphaColor = alphaSrc \* src\_const\_alpha / 255.0; destRedColor = SrcRedColor + ( 1 - SrcAlphaColor ) \* redDest; destGreenColor = SrcGreenColor + ( 1 - SrcAlphaColor ) \* greenDest; destBlueColor = SrcBlueColor + ( 1 - SrcAlphaColor ) \* blueDest; destAlphaColor = SrcAlphaColor + ( 1 - SrcAlphaColor ) \* alphaDest; ptPixels\[i\] = (destAlphaColor << 24 ) | (destRedColor << 16 ) | (destGreenColor << 8 ) | (destBlueColor);
}
-
DWORD destRedColor = 0;
DWORD destBlueColor = 0;
DWORD destGreenColor = 0;
DWORD destAlphaColor = 0;
DWORD SrcRedColor = 0;
DWORD SrcBlueColor = 0;
DWORD SrcGreenColor = 0;
DWORD SrcAlphaColor = 0;
int src_const_alpha = 0;// After all the inits we can do the job : Replace Color
for ( int i = ( ( bm.bmWidth * bm.bmHeight ) - 1 ); i >= 0; --i )
{
COLORREF clrRef = ptPixels[i]; //Bitmap pixel color taken with CreateDIBSection's void* lpBits;
clrRef = COLORREF2RGB( clrRef );//taking alpha,red,green and blue values. DWORD redDest = GetRValue( clrRef ); DWORD greenDest = GetGValue( clrRef); DWORD blueDest = GetBValue( clrRef); BYTE \*ptrAlphaDest = reinterpret\_cast<BYTE\*>( &clrRef ); DWORD alphaDest; alphaDest = ptrAlphaDest\[0\]; //taking alpha,red,green and blue values. DWORD redSrc = GetRValue( \_cNewColor ); //Shadow Color ( of user's choice ) DWORD greensrc = GetGValue( \_cNewColor ); DWORD blueSrc = GetBValue( \_cNewColor ); BYTE \*ptrAlphaSrc = reinterpret\_cast<BYTE\*>( &\_cNewColor ); DWORD alphaSrc; alphaSrc = ptrAlphaSrc\[0\]; src\_const\_alpha = int( m\_intensity \* 2.55 ); SrcRedColor = redSrc \* src\_const\_alpha / 255.0; SrcGreenColor = greensrc \* src\_const\_alpha / 255.0; SrcBlueColor = blueSrc \* src\_const\_alpha / 255.0; SrcAlphaColor = alphaSrc \* src\_const\_alpha / 255.0; destRedColor = SrcRedColor + ( 1 - SrcAlphaColor ) \* redDest; destGreenColor = SrcGreenColor + ( 1 - SrcAlphaColor ) \* greenDest; destBlueColor = SrcBlueColor + ( 1 - SrcAlphaColor ) \* blueDest; destAlphaColor = SrcAlphaColor + ( 1 - SrcAlphaColor ) \* alphaDest; ptPixels\[i\] = (destAlphaColor << 24 ) | (destRedColor << 16 ) | (destGreenColor << 8 ) | (destBlueColor);
}
Perhaps elaborate on what you are trying to do so that we can help you better. Also note that Bitmap pictures pad each scanline to a multiple of 4 bytes. If your bitmaps are 32bit, then you can ignore this. That is if your picture is 5 pixels wide, each pixel taking 3 bytes (24 bits) then each scanline would be
5 * 3 = 15bytes
wide. Since that is not a multiple of 4 some bytes (1 in this case) are added to align the next scanline. Note: The MSDN page for BITMAP[^] says they are WORD (2 byte) aligned, but from previous experience I have found that they are DWORD (4 byte) aligned. This means that you must iterate throughbm.bmHeight
, and then for each iteration of that, iterate throughbm.bmWidth
.BYTE *pBitmapData = (BYTE *)XXX; //XXX is the value from CreateDIBSection
int nBytesPerPixel = bm.bmBitsPixel / 8;
for (int y = 0; y < bm.bmHeight; ++y) {
for (int x = 0; x < bm.bmWidth; ++x) {
COLORREF clrRef = *((COLORREF *)pBitmapData);
//blend images somehow
pBitmapData += nBytesPerPixel; //Simply skip to the next pixel
}
pBitmapData += (nBytesPerPixel * bm.bmHeight + 3) & 3; //Round up to the next DWORD.
} -
Perhaps elaborate on what you are trying to do so that we can help you better. Also note that Bitmap pictures pad each scanline to a multiple of 4 bytes. If your bitmaps are 32bit, then you can ignore this. That is if your picture is 5 pixels wide, each pixel taking 3 bytes (24 bits) then each scanline would be
5 * 3 = 15bytes
wide. Since that is not a multiple of 4 some bytes (1 in this case) are added to align the next scanline. Note: The MSDN page for BITMAP[^] says they are WORD (2 byte) aligned, but from previous experience I have found that they are DWORD (4 byte) aligned. This means that you must iterate throughbm.bmHeight
, and then for each iteration of that, iterate throughbm.bmWidth
.BYTE *pBitmapData = (BYTE *)XXX; //XXX is the value from CreateDIBSection
int nBytesPerPixel = bm.bmBitsPixel / 8;
for (int y = 0; y < bm.bmHeight; ++y) {
for (int x = 0; x < bm.bmWidth; ++x) {
COLORREF clrRef = *((COLORREF *)pBitmapData);
//blend images somehow
pBitmapData += nBytesPerPixel; //Simply skip to the next pixel
}
pBitmapData += (nBytesPerPixel * bm.bmHeight + 3) & 3; //Round up to the next DWORD.
}Actually there is one Picture In Picture( PIP ) module in the application. There is a shadow functionality is available with masking for the front layer. My mask( like heart shape ) is a bitmap file ( of 32 bpp ) which is put in the place of front layer( picture ). It's shadow looks like the same. We can give shadow color of my choice as well( it is a provision ). Shadow will have selected color. So as it should be shown a transparent look I have used AlphaBlend() function to show transparent and semi-transparent pixels present in mask bitmap. For the BLENDFUNCTION structure( m_blendfunc ) I given at Initialization m_blendfunc.BlendOp = AC_SRC_OVER; m_blendfunc.BlendFlags = 0; m_blendfunc.SourceConstantAlpha = 0; m_blendfunc.AlphaFormat = AC_SRC_ALPHA; We can change the intensity of shadow as well( it's a provision ). It's rage from 0 to 100. So that In OnPaint() I have changed the SCA value as per intenisty value. m_blendfunc.SourceConstantAlpha = int( m_intensity * 2.55 ); As per the MSDN I have taken the red, green, blue and alpha value from Source( mask bitmap ) and mix up with destination colors ( of selected shadow color ) by this type of calculation Src.Red = Src.Red * SourceConstantAlpha / 255.0; Src.Green = Src.Green * SourceConstantAlpha / 255.0; Src.Blue = Src.Blue * SourceConstantAlpha / 255.0; Src.Alpha = Src.Alpha * SourceConstantAlpha / 255.0; Dst.Red = Src.Red + (1 - Src.Alpha) * Dst.Red Dst.Green = Src.Green + (1 - Src.Alpha) * Dst.Green Dst.Blue = Src.Blue + (1 - Src.Alpha) * Dst.Blue Dst.Alpha = Src.Alpha + (1 - Src.Alpha) * Dst.Alpha The shadow is not getting proper it's look like some type of animation when I change the intesity. Are I am making any mistake for taking source and destination colors or there is some mistake in calculation of new colors. I have tried ur suggetsion of iterating through bitmap height & width but the result is same.