/*
 *  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.
 */

/*
 * ordered.c: Bayer ordered dithering functions
 */

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

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

pipi_image_t *pipi_dither_ordered(pipi_image_t *img, pipi_image_t *kernel)
{
    pipi_image_t *dst;
    pipi_pixels_t *dstp, *kernelp;
    float *dstdata, *kerneldata;
    int x, y, w, h, kw, kh;

    w = img->w;
    h = img->h;
    kw = kernel->w;
    kh = kernel->h;

    dst = pipi_copy(img);
    dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
    dstdata = (float *)dstp->pixels;

    kernelp = pipi_getpixels(kernel, PIPI_PIXELS_Y_F);
    kerneldata = (float *)kernelp->pixels;

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

            p = dstdata[y * w + x];
            q = p > kerneldata[(y % kh) * kw + (x % kw)] ? 1. : 0.;
            dstdata[y * w + x] = q;
        }
    }

    return dst;
}