Thursday, 3 December 2015

Low-level Graphics on Raspberry Pi (palette)

Again life has happened and the time has slipped. Thanks for the response on the Raspberry Pi forum. To iterate the message from there: I have disabled comments here because I cannot guarantee any kind of decent response time, sorry.

Just a quick, short one now on something I came across trawling through old stuff. Palette animation a.k.a color cycling (Wikipedia) is a technique that might produce interesting effects when applied to a suitably arranged image. In most cases it is likely to be a lot faster than redrawing the actual pixels. Continuing from the fbtest5.c introduced in Part V we can animate the palette like this:
        // draw...
        draw();
        sleep(1);

        int j;
        for(j = 0; j < 16; j++) {
            for(i = 0; i < 16; i++) {
                // rotate the original values based on j changing...
                r[i] = def_r[(i + j + 1) % 16] << 8;
                g[i] = def_g[(i + j + 1) % 16] << 8;
                b[i] = def_b[(i + j + 1) % 16] << 8;
            }
            // Note that we set up the 'pal' structure earlier
            // and it still points to the r, g, b arrays,
            // so we can just reuse 'pal' here
            if (ioctl(fbfd, FBIOPUTCMAP, &pal)) {
                printf("Error setting palette.\n");
            }
            sleep(1);
        }
...which should cycle the color bars in the lower half of the screen.

[Full source available in GitHub]

[Continued in next part Images]