/*
 *  libpipi       Pathetic image processing interface 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.
 */

/*
 * mean.c: Mean computation function
 */

#include "config.h"

#include <stdlib.h>

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

pipi_image_t *pipi_mean(pipi_image_t *img1, pipi_image_t *img2)
{
    pipi_image_t *dst;
    pipi_pixels_t *img1p, *img2p, *dstp;
    float *img1data, *img2data, *dstdata;
    int x, y, w, h;

    if(img1->w != img2->w || img1->h != img2->h)
        return NULL;

    w = img1->w;
    h = img1->h;

    dst = pipi_new(w, h);
    dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
    dstdata = (float *)dstp->pixels;

    img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
    img1data = (float *)img1p->pixels;
    img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
    img2data = (float *)img2p->pixels;

    for(y = 0; y < h; y++)
    {
        for(x = 0; x < w; x++)
        {
            float p, q;

            p = img1data[4 * (y * w + x)];
            q = img2data[4 * (y * w + x)];
            dstdata[4 * (y * w + x)] = (p + q) * 0.5;

            p = img1data[4 * (y * w + x) + 1];
            q = img2data[4 * (y * w + x) + 1];
            dstdata[4 * (y * w + x) + 1] = (p + q) * 0.5;

            p = img1data[4 * (y * w + x) + 2];
            q = img2data[4 * (y * w + x) + 2];
            dstdata[4 * (y * w + x) + 2] = (p + q) * 0.5;

            p = img1data[4 * (y * w + x) + 3];
            q = img2data[4 * (y * w + x) + 3];
            dstdata[4 * (y * w + x) + 3] = (p + q) * 0.5;
        }
    }

    return dst;
}