Version 0 of fast alpha blending (transparency)

Updated 2003-12-06 14:25:27

Here is a quick and dirty alpha blending code, for achieving a fade in/out transition between two images. It is coded is C, with critcl for speed, but the algorithm is very simple, a mixing of the pixels from two images returning in a third.

 package require Img
 package require critcl
 package provide alphablend 1.0 

 critcl::config outdir .
 critcl::cheaders -I/usr/local/include
 critcl::ccode {
    #include <tcl.h>
    #include <tk.h>
 }

 ::critcl::ccommand blend_img { dummy ip objc objv } {
    Tk_PhotoHandle ph[3];
    Tk_PhotoImageBlock ib[3];
    Tcl_Obj *obj;
    int i,j,k,blend,pxsiz,pitch;
    unsigned char *p[3];

    if (objc != 5) {
        Tcl_WrongNumArgs(ip,1,objv,"img1 img2 img3 blend");
        return TCL_ERROR;
    }
    ph[0] = Tk_FindPhoto(ip,Tcl_GetString(objv[1]));
    ph[1] = Tk_FindPhoto(ip,Tcl_GetString(objv[2]));
    ph[2] = Tk_FindPhoto(ip,Tcl_GetString(objv[3]));
    Tcl_GetIntFromObj(ip,objv[4],&blend);
    Tk_PhotoGetImage(ph[0],ib);
    Tk_PhotoGetImage(ph[1],ib+1);