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)


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.

Wednesday, April 11, 2007

Mingw/Dev-C++ binary file operations

I encountered this problem when I was porting a Linux utility program to Windows. The job of the utility is to read a XML file and generate a equivalent binary file. I have written it in Linux few weeks before and it works without any issue. When I ported and run the program everything went fine except the reading part of the utility. The utility suppose to read the generated binary file for verification but it fails. The problem is 'read' function returns '0'(indication of EOF) though it has data to read. I have gone through the code many times and found nothing wrong. Then I goggled and found that any file opening in Windows will be in text mode if unspecified.

This can be solved if I use 'fopen' call("rb" mode) but I use 'open' system call. So I again googled and found that I can include the binmode.o(in Mingw lib directory) object along with my executable and which forces the binary mode file open as default. This can also be done by

#include ...
#undef _fmode
int _fmode = _O_BINARY;

Ref: http://www.mingw.org/MinGWiki/index.php/binary