Tuesday, April 17, 2007

Image Transition - Fading

Today my friend asked how to do fading image transition effect. I suggested him an algorithm and thought of sharing it here.

The Algorithm:

#define MAX_ALPHA 255
for (int alpha = 0; alpha < (MAX_ALPHA + 1); alpha++)
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
P1Ratio = 1 - (alpha / MAX_ALPHA);
P2Ratio = (alpha / MAX_ALPHA);
PF[x][y][R] = (P1[x][y][R] * P1Ratio) + (P2[x][y][R] * P2Ratio)
PF[x][y][G] = (P1[x][y][G] * P1Ratio) + (P2[x][y][G] * P2Ratio)
PF[x][y][B] = (P1[x][y][B] * P1Ratio) + (P2[x][y][B] * P2Ratio)
}
}

DISPLAY PICTURE
}


The transition will be very smooth and also very slow since 'alpha++'. The speed can be achieve by 'alpha += 2' or 'alpha += 10'. The algorithm is limited to same resolution of images. I didn't test this code but hope it will work.

No comments: