Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to calculate to get new pixel color by mixing the alpha value with r, g and b value

How to calculate to get new pixel color by mixing the alpha value with r, g and b value

Scheduled Pinned Locked Moved C / C++ / MFC
graphicstutorialcareer
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amrit Agr
    wrote on last edited by
    #1

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

    }

    L A 2 Replies Last reply
    0
    • A Amrit Agr

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

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I expect there is a question in here somewhere but I can't quite see it.

      I must get a clever new signature for 2011.

      1 Reply Last reply
      0
      • A Amrit Agr

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

        }

        A Offline
        A Offline
        Andrew Brock
        wrote on last edited by
        #3

        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 through bm.bmHeight, and then for each iteration of that, iterate through bm.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.
        }

        A 1 Reply Last reply
        0
        • A Andrew Brock

          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 through bm.bmHeight, and then for each iteration of that, iterate through bm.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.
          }

          A Offline
          A Offline
          Amrit Agr
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups