source: libpipi/trunk/pipi/dither/random.c @ 2844

Last change on this file since 2844 was 2844, checked in by Sam Hocevar, 11 years ago

Fix headers.

File size: 1.4 KB
RevLine 
[2671]1/*
[2844]2 *  libpipi       Pathetic image processing interface library
[2671]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 * random.c: random 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_random(pipi_image_t *img)
26{
27    pipi_image_t *dst;
28    pipi_pixels_t *dstp;
29    float *dstdata;
30    unsigned int ctx = 1;
31    int x, y, w, h;
32
33    w = img->w;
34    h = img->h;
35
36    dst = pipi_copy(img);
37    dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
38    dstdata = (float *)dstp->pixels;
39
40    for(y = 0; y < h; y++)
41    {
42        for(x = 0; x < w; x++)
43        {
44            long hi, lo;
45            float p, q;
46
47            hi = ctx / 12773L;
48            lo = ctx % 12773L;
49            ctx = 16807L * lo - 2836L * hi;
50            if(ctx <= 0)
51                ctx += 0x7fffffffL;
52
53            p = dstdata[y * w + x];
54            q = p > (double)((ctx % 65536) / 65535.) ? 1. : 0.;
55            dstdata[y * w + x] = q;
56        }
57    }
58
59    return dst;
60}
61
Note: See TracBrowser for help on using the repository browser.