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

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

Change _C pixel format suffixes into _U8 for more clarity.

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
21#include "pipi.h"
22#include "pipi_internals.h"
23
24pipi_image_t *pipi_dither_random(pipi_image_t *img)
25{
26    pipi_image_t *dst;
27    pipi_pixels_t *dstp;
28    float *dstdata;
29    unsigned int ctx = 1;
30    int x, y, w, h;
31
32    w = img->w;
33    h = img->h;
34
35    dst = pipi_copy(img);
[3342]36    dstp = pipi_get_pixels(dst, PIPI_PIXELS_Y_F32);
[2671]37    dstdata = (float *)dstp->pixels;
38
39    for(y = 0; y < h; y++)
40    {
41        for(x = 0; x < w; x++)
42        {
43            long hi, lo;
44            float p, q;
45
46            hi = ctx / 12773L;
47            lo = ctx % 12773L;
48            ctx = 16807L * lo - 2836L * hi;
49            if(ctx <= 0)
50                ctx += 0x7fffffffL;
51
52            p = dstdata[y * w + x];
53            q = p > (double)((ctx % 65536) / 65535.) ? 1. : 0.;
54            dstdata[y * w + x] = q;
55        }
56    }
57
58    return dst;
59}
60
Note: See TracBrowser for help on using the repository browser.