View Raw SPL

/* fade image from left/top/right/bottom */
imfade(w = refwindow(w0), percent = 1, direction = 0)
{
        local acnt, r, g, b, a, f, h, nf, len, img;

        acnt = argc;

        if (isscalar(w))
        {
                direction = w;
                w = refwindow(w0);
                acnt = 0;
        }

        /* rgb and alpha components */
        (r, g, b, a) = getrgb(w);

        (nr, nc) = size(r);

        if (imhasalpha(w))
        {
                /* factor out existing transparency */
                r /= a;
                g /= a;
                b /= a;
        }

        /* transparency ramp */
        nf  = (direction > 1) ? nc : nr;
        len = castint(nf * percent);
        h   = linspace(0, 1, len);
        f   = ones(nf, 1);
        f[1..len] = h;

        /* update alpha matrix based on direction */
        if (direction > 1)
        {
                /* top or bottom */
                if (len > nc)
                {
                        len = nc;
                        f = extract(f, 1, len);
                }

                if (direction > 2)
                {
                        f = rev(f);
                }

                f = rep(f', nr);
        }
        else
        {
                /* right or left */
                if (len > nr)
                {
                        len = nr;
                        f = extract(f, 1, len);
                }

                if (direction > 0)
                {
                        f = rev(f);
                }

                f = repcol(f, nc);
        }

        /* new transparency mask */
        a *= f;

        /* premultiply transparency */
        img = rgbimage(r*a, g*a, b*a, a);

        /* attributes */
        img.xoffset = w.xoffset;
        img.yoffset = w.yoffset;

        if (acnt > 0)
        {
                return(img);
        }
        else
        {
                w0 = img;
        }
}