source: libpipi/trunk/pipi/dither/floydsteinberg.c @ 2637

Last change on this file since 2637 was 2637, checked in by Sam Hocevar, 14 years ago
  • Add Floyd-Steinberg grayscale dithering.
File size: 1.7 KB
RevLine 
[2637]1/*
2 *  libpipi       Proper image processing implementation library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 * floydsteinberg.c: Floyd-Steinberg dithering functions
17 */
18
19#include "config.h"
20#include "common.h"
21
22#include "pipi.h"
23#include "pipi_internals.h"
24
25pipi_image_t *pipi_floydsteinberg(pipi_image_t *src)
26{
27    pipi_image_t *dst;
28    pipi_pixels_t *srcp, *dstp;
29    float *srcdata, *dstdata;
30    int x, y, w, h;
31
32    w = src->w;
33    h = src->h;
34
35    srcp = pipi_getpixels(src, PIPI_PIXELS_Y_F);
36    srcdata = (float *)srcp->pixels;
37
38    dst = pipi_new(w, h);
39    dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
40    dstdata = (float *)dstp->pixels;
41
42    for(y = 0; y < h; y++)
43    {
44        for(x = 0; x < w; x++)
45        {
46            float p, q, e;
47
48            p = srcdata[y * w + x];
49            q = p < 0.5 ? 0. : 1.;
50            dstdata[y * w + x] = q;
51
52            e = p - q;
53            if(x < w - 1)
54                srcdata[y * w + x + 1] += e * .4375;
55            if(y < h - 1)
56            {
57                if(x > 0)
58                    srcdata[(y + 1) * w + x - 1] += e * .1875;
59                srcdata[(y + 1) * w + x] += e * .3125;
60                if(x < w - 1)
61                    srcdata[(y + 1) * w + x + 1] += e * .0625;
62            }
63        }
64    }
65
66    return dst;
67}
68
Note: See TracBrowser for help on using the repository browser.