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

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

Fix headers.

File size: 3.3 KB
Line 
1/*
2 *  libpipi       Pathetic image processing interface 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#include <math.h>
24
25#include "pipi.h"
26#include "pipi_internals.h"
27
28pipi_image_t *pipi_dither_halftone(pipi_image_t *img, double r, double angle)
29{
30#define PRECISION 4.
31    pipi_image_t *ret, *kernel;
32    int k = (r * PRECISION / 2. / sqrt(2.) + .5);
33
34    kernel = pipi_render_halftone(k, k);
35    ret = pipi_dither_ordered_ext(img, kernel, 1. / PRECISION, angle + 45.);
36    pipi_free(kernel);
37
38    return ret;
39}
40
41pipi_image_t *pipi_dither_ordered(pipi_image_t *img, pipi_image_t *kernel)
42{
43    return pipi_dither_ordered_ext(img, kernel, 1.0, 0.0);
44}
45
46pipi_image_t *pipi_dither_ordered_ext(pipi_image_t *img, pipi_image_t *kernel,
47                                      double scale, double angle)
48{
49    double sint, cost;
50    pipi_image_t *dst;
51    pipi_pixels_t *dstp, *kernelp;
52    float *dstdata, *kerneldata;
53    int x, y, w, h, kx, ky, kw, kh;
54
55    w = img->w;
56    h = img->h;
57    kw = kernel->w;
58    kh = kernel->h;
59
60    cost = cos(angle * (M_PI / 180));
61    sint = sin(angle * (M_PI / 180));
62
63    dst = pipi_copy(img);
64    dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
65    dstdata = (float *)dstp->pixels;
66
67    kernelp = pipi_getpixels(kernel, PIPI_PIXELS_Y_F);
68    kerneldata = (float *)kernelp->pixels;
69
70    for(y = 0; y < h; y++)
71    {
72        for(x = 0; x < w; x++)
73        {
74            float p, q;
75
76            kx = (int)((cost * x - sint * y + 2 * w * h) / scale) % kw;
77            ky = (int)((cost * y + sint * x + 2 * w * h) / scale) % kh;
78
79            p = dstdata[y * w + x];
80            q = p > kerneldata[ky * kw + kx] ? 1. : 0.;
81            dstdata[y * w + x] = q;
82        }
83    }
84
85    return dst;
86}
87
88typedef struct
89{
90    int x, y;
91    double val;
92}
93dot_t;
94
95static int cmpdot(const void *p1, const void *p2)
96{
97    return ((dot_t const *)p1)->val > ((dot_t const *)p2)->val;
98}
99
100pipi_image_t *pipi_order(pipi_image_t *src)
101{
102    double epsilon;
103    pipi_image_t *dst;
104    pipi_pixels_t *dstp, *srcp;
105    float *dstdata, *srcdata;
106    dot_t *circle;
107    int x, y, w, h, n;
108
109    w = src->w;
110    h = src->h;
111    epsilon = 1. / (w * h + 1);
112
113    srcp = pipi_getpixels(src, PIPI_PIXELS_Y_F);
114    srcdata = (float *)srcp->pixels;
115
116    dst = pipi_new(w, h);
117    dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
118    dstdata = (float *)dstp->pixels;
119
120    circle = malloc(w * h * sizeof(dot_t));
121
122    for(y = 0; y < h; y++)
123        for(x = 0; x < w; x++)
124        {
125            circle[y * w + x].x = x;
126            circle[y * w + x].y = y;
127            circle[y * w + x].val = srcdata[y * w + x];
128        }
129    qsort(circle, w * h, sizeof(dot_t), cmpdot);
130
131    for(n = 0; n < w * h; n++)
132    {
133        x = circle[n].x;
134        y = circle[n].y;
135        dstdata[y * w + x] = (float)(n + 1) * epsilon;
136    }
137
138    free(circle);
139
140    return dst;
141}
142
Note: See TracBrowser for help on using the repository browser.