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

Last change on this file since 2811 was 2811, checked in by Sam Hocevar, 12 years ago
  • ordered.c: allow to rotate the dither pattern, using nearest-neighbour rotation interpolation.
File size: 2.9 KB
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#include <math.h>
24
25#include "pipi.h"
26#include "pipi_internals.h"
27
28pipi_image_t *pipi_dither_ordered(pipi_image_t *img, pipi_image_t *kernel)
29{
30    return pipi_dither_ordered_ext(img, kernel, 0.0);
31}
32
33pipi_image_t *pipi_dither_ordered_ext(pipi_image_t *img, pipi_image_t *kernel,
34                                      double angle)
35{
36    double sint, cost;
37    pipi_image_t *dst;
38    pipi_pixels_t *dstp, *kernelp;
39    float *dstdata, *kerneldata;
40    int x, y, w, h, kx, ky, kw, kh;
41
42    w = img->w;
43    h = img->h;
44    kw = kernel->w;
45    kh = kernel->h;
46
47    cost = cos(angle * (M_PI / 180));
48    sint = sin(angle * (M_PI / 180));
49
50    dst = pipi_copy(img);
51    dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
52    dstdata = (float *)dstp->pixels;
53
54    kernelp = pipi_getpixels(kernel, PIPI_PIXELS_Y_F);
55    kerneldata = (float *)kernelp->pixels;
56
57    for(y = 0; y < h; y++)
58    {
59        for(x = 0; x < w; x++)
60        {
61            float p, q;
62
63            kx = (int)(cost * x - sint * y + 2 * w * h) % kw;
64            ky = (int)(cost * y + sint * x + 2 * w * h) % kh;
65
66            p = dstdata[y * w + x];
67            q = p > kerneldata[ky * kw + kx] ? 1. : 0.;
68            dstdata[y * w + x] = q;
69        }
70    }
71
72    return dst;
73}
74
75typedef struct
76{
77    int x, y;
78    double val;
79}
80dot_t;
81
82static int cmpdot(const void *p1, const void *p2)
83{
84    return ((dot_t const *)p1)->val > ((dot_t const *)p2)->val;
85}
86
87pipi_image_t *pipi_order(pipi_image_t *src)
88{
89    double epsilon;
90    pipi_image_t *dst;
91    pipi_pixels_t *dstp, *srcp;
92    float *dstdata, *srcdata;
93    dot_t *circle;
94    int x, y, w, h, n;
95
96    w = src->w;
97    h = src->h;
98    epsilon = 1. / (w * h + 1);
99
100    srcp = pipi_getpixels(src, PIPI_PIXELS_Y_F);
101    srcdata = (float *)srcp->pixels;
102
103    dst = pipi_new(w, h);
104    dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
105    dstdata = (float *)dstp->pixels;
106
107    circle = malloc(w * h * sizeof(dot_t));
108
109    for(y = 0; y < h; y++)
110        for(x = 0; x < w; x++)
111        {
112            circle[y * w + x].x = x;
113            circle[y * w + x].y = y;
114            circle[y * w + x].val = srcdata[y * w + x];
115        }
116    qsort(circle, w * h, sizeof(dot_t), cmpdot);
117
118    for(n = 0; n < w * h; n++)
119    {
120        x = circle[n].x;
121        y = circle[n].y;
122        dstdata[y * w + x] = (float)(n + 1) * epsilon;
123    }
124
125    free(circle);
126
127    return dst;
128}
129
Note: See TracBrowser for help on using the repository browser.