Search code examples
cimagefadebmp

How does this program work C?


void fade(ImagenBMP *imagen1, ImagenBMP *imagen2, int f) {
float v = (float)f/255;
for (int i = 0; i < imagen1->tamanyo; i++) {
    imagen1->datos[i] = (imagen1->datos[i] - imagen2->datos[i])*v + imagen2->datos[i];
}}; //end of fade

It's supose to fade two images into a single one.


Solution

  • If you rewrite the equation, you will end up with:

    imagen1->datos[i] = v*imagen1->datos[i] + (1-v)*imagen2->datos[i];
    

    This is how the blend works. You are specifying how much of the first image (out of 255 parts) should be included in the function parameter f, 255-f parts will be included from the second image.