Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 805007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:00:42+00:00 2026-05-15T00:00:42+00:00

I am trying to update a source image with the contents of multiple destination

  • 0

I am trying to update a source image with the contents of multiple destination images. From what I can tell using premultiplied alpha is the way to go with this, but I think I am doing something wrong (function below). the image I am starting with is initialized with all ARGB values set to 0. When I run the function once the resulting image looks great, but when I start compositing on any others all the pixels that have alpha information get really messed up. Does anyone know if I am doing something glaringly wrong or if there is something extra I need to do to modify the color values?

void CompositeImage(unsigned char *src, unsigned char *dest, int srcW, int srcH){
int w = srcW;
int h = srcH;

int px0;
int px1;
int px2;
int px3;

int inverseAlpha;

int r;
int g;
int b;
int a;

int y;
int x;

for (y = 0; y < h; y++) {
    for (x= 0; x< w*4; x+=4) {
        // pixel number
        px0 = (y*w*4) + x;
        px1 = (y*w*4) + (x+1);
        px2 = (y*w*4) + (x+2);
        px3 = (y*w*4) + (x+3);

        inverseAlpha = 1 - src[px3];

        // create new values
        r = src[px0]  + inverseAlpha * dest[px0];
        g = src[px1]  + inverseAlpha * dest[px1];
        b = src[px2]  + inverseAlpha * dest[px2];
        a = src[px3]  + inverseAlpha * dest[px3];

        // update destination image
        dest[px0] = r;
        dest[px1] = g;
        dest[px2] = b;
        dest[px3] = a;
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:00:43+00:00Added an answer on May 15, 2026 at 12:00 am

    I’m not clear on what data you are working with. Do your source images already have the alpha values pre-multiplied as they are stored? If not, then pre-multiplied alpha does not apply here and you would need to do normal alpha blending.

    Anyway, the big problem in your code is that you’re not keeping track of the value ranges that you’re dealing with.

    inverseAlpha = 1 - src[px3];
    

    This needs to be changed to:

    inverseAlpha = 255 - src[px3];
    

    You have all integral value types here, so the normal incoming 0..255 value range will result in an inverseAlpha range of -254..1, which will give you some truly wacky results.

    After changing the 1 to 255, you also need to divide your results for each channel by 255 to scale them back down to the appropriate range. The alternative is to do the intermediate calculations using floats instead of integers and divide the initial channel values by 255.0 (instead of these other changes) to get values in the 0..1 range.

    If your source data really does already have pre-multiplied alpha, then your result lines should look like this.

    r = src[px0]  + inverseAlpha * dest[px0] / 255;
    

    If your source data does not have pre-multiplied alpha, then it should be:

    r = src[px0] * src[px3] / 255  + inverseAlpha * dest[px0] / 255;
    

    There’s nothing special about blending the alpha channel. Use the same calculation as for r, g, and b.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to update the source of an image using jQuery. The problem is
I'm trying to set a WPF Image's source. XAML works: <Image Name=ImageThing Source=images/Thing.png/> Visual
When trying to update a subversion working copy from Netbeans, I get the following
Im trying to update the contents of an element after running some php code.
I am trying to update a database with values from a csv file, the
I'm trying to install Image Magick on my Ubuntu machine using XAMPP, but so
I'm trying to post a photo to Facebook page from user using Facebook SDK
I am trying to load an image in the background and then update the
I am trying to get the source for images on some pages but there
I'm trying to update the source of an iFrame when a button is clicked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.