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

Revision 2670, 1.9 KB checked in by sam, 5 years ago (diff)
  • Dithering algorithms no longer modify the original image.
Line 
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_dither_floydsteinberg(pipi_image_t *img, pipi_scan_t scan)
26{
27    pipi_image_t *dst;
28    pipi_pixels_t *dstp;
29    float *dstdata;
30    int x, y, w, h;
31
32    w = img->w;
33    h = img->h;
34
35    dst = pipi_copy(img);
36    dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
37    dstdata = (float *)dstp->pixels;
38
39    for(y = 0; y < h; y++)
40    {
41        int reverse = (y & 1) && (scan == PIPI_SCAN_SERPENTINE);
42
43        for(x = 0; x < w; x++)
44        {
45            float p, q, e;
46            int x2 = reverse ? w - 1 - x : x;
47            int s = reverse ? -1 : 1;
48
49            p = dstdata[y * w + x2];
50            q = p < 0.5 ? 0. : 1.;
51            dstdata[y * w + x2] = q;
52
53            /* FIXME: according to our 2008 paper, [7 4 5 0] is a better
54             * error diffusion kernel for serpentine scan. */
55            e = p - q;
56            if(x < w - 1)
57                dstdata[y * w + x2 + s] += e * .4375;
58            if(y < h - 1)
59            {
60                if(x > 0)
61                    dstdata[(y + 1) * w + x2 - s] += e * .1875;
62                dstdata[(y + 1) * w + x2] += e * .3125;
63                if(x < w - 1)
64                    dstdata[(y + 1) * w + x2 + s] += e * .0625;
65            }
66        }
67    }
68
69    return dst;
70}
71
Note: See TracBrowser for help on using the repository browser.