source: libpipi/trunk/pipi/dither/ordered.c @ 2790

Revision 2790, 2.5 KB checked in by sam, 5 years ago (diff)
  • Add pipi_order, to transform any image into an ordered dithering matrix.
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 * ordered.c: Bayer ordered dithering functions
17 */
18
19#include "config.h"
20#include "common.h"
21
22#include <stdlib.h>
23
24#include "pipi.h"
25#include "pipi_internals.h"
26
27pipi_image_t *pipi_dither_ordered(pipi_image_t *img, pipi_image_t *kernel)
28{
29    pipi_image_t *dst;
30    pipi_pixels_t *dstp, *kernelp;
31    float *dstdata, *kerneldata;
32    int x, y, w, h, kw, kh;
33
34    w = img->w;
35    h = img->h;
36    kw = kernel->w;
37    kh = kernel->h;
38
39    dst = pipi_copy(img);
40    dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
41    dstdata = (float *)dstp->pixels;
42
43    kernelp = pipi_getpixels(kernel, PIPI_PIXELS_Y_F);
44    kerneldata = (float *)kernelp->pixels;
45
46    for(y = 0; y < h; y++)
47    {
48        for(x = 0; x < w; x++)
49        {
50            float p, q;
51
52            p = dstdata[y * w + x];
53            q = p > kerneldata[(y % kh) * kw + (x % kw)] ? 1. : 0.;
54            dstdata[y * w + x] = q;
55        }
56    }
57
58    return dst;
59}
60
61typedef struct
62{
63    int x, y;
64    double val;
65}
66dot_t;
67
68static int cmpdot(const void *p1, const void *p2)
69{
70    return ((dot_t const *)p1)->val > ((dot_t const *)p2)->val;
71}
72
73pipi_image_t *pipi_order(pipi_image_t *src)
74{
75    double epsilon;
76    pipi_image_t *dst;
77    pipi_pixels_t *dstp, *srcp;
78    float *dstdata, *srcdata;
79    dot_t *circle;
80    int x, y, w, h, n;
81
82    w = src->w;
83    h = src->h;
84    epsilon = 1. / (w * h + 1);
85
86    srcp = pipi_getpixels(src, PIPI_PIXELS_Y_F);
87    srcdata = (float *)srcp->pixels;
88
89    dst = pipi_new(w, h);
90    dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
91    dstdata = (float *)dstp->pixels;
92
93    circle = malloc(w * h * sizeof(dot_t));
94
95    for(y = 0; y < h; y++)
96        for(x = 0; x < w; x++)
97        {
98            circle[y * w + x].x = x;
99            circle[y * w + x].y = y;
100            circle[y * w + x].val = srcdata[y * w + x];
101        }
102    qsort(circle, w * h, sizeof(dot_t), cmpdot);
103
104    for(n = 0; n < w * h; n++)
105    {
106        x = circle[n].x;
107        y = circle[n].y;
108        dstdata[y * w + x] = (float)(n + 1) * epsilon;
109    }
110
111    free(circle);
112
113    return dst;
114}
115
Note: See TracBrowser for help on using the repository browser.