/*
 *  libpipi       Proper image processing implementation library
 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
 *                All Rights Reserved
 *
 *  $Id$
 *
 *  This library is free software. It comes without any warranty, to
 *  the extent permitted by applicable law. You can redistribute it
 *  and/or modify it under the terms of the Do What The Fuck You Want
 *  To Public License, Version 2, as published by Sam Hocevar. See
 *  http://sam.zoy.org/wtfpl/COPYING for more details.
 */

/*
 * test.c: my repository of test functions
 */

#include "config.h"
#include "common.h"

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

#include "pipi.h"
#include "pipi_internals.h"

void pipi_test(pipi_image_t *img)
{
    pipi_pixels_t *pixels;
    float *data;
    int x, y;

    pixels = pipi_getpixels(img, PIPI_PIXELS_RGBA_F);
    data = (float *)pixels->pixels;

    for(y = 0; y < img->h; y++)
    {
        for(x = 0; x < img->w; x++)
        {
            double r = 0, g = 0, b = 0;

            r = data[(y * img->w + x) * 4];
            g = data[(y * img->w + x) * 4 + 1];
            b = data[(y * img->w + x) * 4 + 2];

            if(r + g + b == 0)
                r = g = b = 1. / 3;
            else if(r + g + b < 1.)
            {
                double d = (1. - r - g - b) / 3;
                r += d; g += d; b += d;
            }
            else if(2. - r + g - b < 1.)
            {
                double d = (-1. + r - g + b) / 3;
                r -= d; g += d; b -= d;
            }
            else if(2. + r - g - b < 1.)
            {
                double d = (-1. - r + g + b) / 3;
                r += d; g -= d; b -= d;
            }

            data[(y * img->w + x) * 4] = r;
            data[(y * img->w + x) * 4 + 1] = g;
            data[(y * img->w + x) * 4 + 2] = b;
        }
    }
}