2. Boilerplate Janet stuff

To get this algorithm wrapped inside of Janet, you need some boilerplate. Quite dull actually. Why are you still reading this?

2.1. Loader + Plugin Entry

<<funclist>>=
#ifndef BUILD_DITHER_PLUGIN
#define NAME(s) "monolith/gfx-"s
#else
#define NAME(s) s
#endif

static const JanetReg cfuns[] = {
    {NAME("dither"),
     j_dither,
     "Dithers a rectangular region between two colors."},
    {NULL, NULL, NULL}
};
<<plugin_entry>>=
void monolith_gfx_dither_loader(JanetTable *env,
                                const char *name)
{
    janet_cfuns(env, name, cfuns);
}
#ifdef BUILD_DITHER_PLUGIN
JANET_MODULE_ENTRY(JanetTable *env)
{
    monolith_gfx_dither_loader(env, "dither");
}
#endif

2.2. Janet Function

<<janet_function>>=
static monolith_pixel rgbval(int *rgb)
{
    return monolith_pixel_make(rgb[0], rgb[1], rgb[2], 255);
}
static Janet j_dither(int32_t argc, Janet *argv)
{
    int x, y;
    int w, h;
    int fg[3];
    int bg[3];
    int i;
    monolith_d *m;
    monolith_framebuffer *fb;

    janet_fixarity(argc, 10);
    m = monolith_data_get();
    x = janet_getinteger(argv, 0);
    y = janet_getinteger(argv, 1);
    w = janet_getinteger(argv, 2);
    h = janet_getinteger(argv, 3);

    for (i = 0; i < 3; i++) {
        fg[i] = janet_getinteger(argv, 4 + i);
        bg[i] = janet_getinteger(argv, 7 + i);
    }

    fb = monolith_fb_get(m);

    atkinson_dither(fb, x, y, w, h, rgbval(fg), rgbval(bg));
    return janet_wrap_nil();
}



prev | home | next