#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <pipi.h>

int main(int argc, char *argv[])
{
    static double const mypal[] =
    {
#if 1
        0.800, 0.001, 0.001, /* red */
        0.001, 0.700, 0.001, /* green */
        0.005, 0.001, 0.600, /* blue */
        0.900, 0.900, 0.900, /* white */
        0.900, 0.900, 0.001, /* yellow */
        0.850, 0.450, 0.001, /* orange */
#else
        1,1,1,
        0,.05,.60,
        1,0,0,
        .15,.76,0,
        .96,1,0,
        1,.61,0,
#endif
    };
#define NCOLORS ((int)(sizeof(mypal)/sizeof(*mypal)/3))

    int x, y, w, h;

    pipi_image_t *src = pipi_load(argv[1]);
    pipi_image_t *dst = pipi_reduce(src, NCOLORS, mypal);

    pipi_pixels_t *p = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
    float *data = (float *)p->pixels;
    w = p->w;
    h = p->h;

    for(y = 0; y < h; y++)
        for(x = 0; x < w; x++)
        {
            double pr, pg, pb, qr, qg, qb, er, eg, eb;

            pr = data[4 * (y * w + x) + 2];
            pg = data[4 * (y * w + x) + 1];
            pb = data[4 * (y * w + x)];

            double dist = 10.0;
            int n, best = -1;

            for(n = 0; n < NCOLORS; n++)
            {
                double dr, dg, db, d;

                dr = pr - mypal[n * 3];
                dg = pg - mypal[n * 3 + 1];
                db = pb - mypal[n * 3 + 2];

                d = dr * dr + dg * dg + db * db;
                //d = 0.299 * dr * dr + 0.587 * dg * dg + 0.114 * db * db;

                if(d < dist)
                {
                    best = n;
                    dist = d;
                }
            }

            qr = mypal[best * 3];
            qg = mypal[best * 3 + 1];
            qb = mypal[best * 3 + 2];

            er = (pr - qr) / 16;
            eg = (pg - qg) / 16;
            eb = (pb - qb) / 16;

            if(x < w - 1)
            {
                data[4 * (y * w + x + 1) + 2] += 7 * er;
                data[4 * (y * w + x + 1) + 1] += 7 * eg;
                data[4 * (y * w + x + 1)] += 7 * eb;
            }

            if(y < h - 1)
            {
                if(x < w - 1)
                {
                    data[4 * (y * w + x + w + 1) + 2] += 1 * er;
                    data[4 * (y * w + x + w + 1) + 1] += 1 * eg;
                    data[4 * (y * w + x + w + 1)] += 1 * eb;
                }

                data[4 * (y * w + x + w) + 2] += 5 * er;
                data[4 * (y * w + x + w) + 1] += 5 * eg;
                data[4 * (y * w + x + w)] += 5 * eb;

                if(x > 0)
                {
                    data[4 * (y * w + x + w - 1) + 2] += 3 * er;
                    data[4 * (y * w + x + w - 1) + 1] += 3 * eg;
                    data[4 * (y * w + x + w - 1)] += 3 * eb;
                }
            }

            data[4 * (y * w + x) + 2] = qr;
            data[4 * (y * w + x) + 1] = qg;
            data[4 * (y * w + x)] = qb;
        }

    pipi_save(dst, argv[2]);

    return 0;
}