source: libcaca/trunk/test/dithering.c @ 292

Last change on this file since 292 was 292, checked in by Sam Hocevar, 17 years ago
  • Makefile.am: + Added a test directory for test programs.
  • test/dithering.c: + The value/saturation dithering example.
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1/*
2 *  dithering     libcaca dithering test program
3 *  Copyright (c) 2003 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: dithering.c 292 2003-12-31 12:26:22Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the GNU Lesser General Public
10 *  License as published by the Free Software Foundation; either
11 *  version 2 of the License, or (at your option) any later version.
12 *
13 *  This program is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 *  Lesser General Public License for more details.
17 *
18 *  You should have received a copy of the GNU Lesser General Public
19 *  License along with this program; if not, write to the Free Software
20 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 *  02111-1307  USA
22 */
23
24#include "config.h"
25
26#include "caca.h"
27
28#define XRATIO 100*100
29#define YRATIO 70*70
30#define FUZZY 5000000
31
32enum caca_color points[] =
33{
34    CACA_COLOR_BLACK,
35    CACA_COLOR_DARKGRAY,
36    CACA_COLOR_LIGHTGRAY,
37    CACA_COLOR_WHITE,
38    CACA_COLOR_RED,
39    CACA_COLOR_LIGHTRED
40};
41
42char density[] = " -,+:;o&%w$W@#";
43
44int main(void)
45{
46    int neara, dista, nearb, distb, dist;
47    int x, y;
48
49    caca_init();
50
51    for(x = 0; x < 100; x++)
52        for(y = 0; y < 100; y++)
53    {
54        int color = CACA_COLOR_WHITE;
55        char ch = '?';
56
57        /* distance to black */
58        dista = XRATIO * x * x;
59        neara = 0;
60
61        /* distance to 40% */
62        dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y;
63        if(caca_rand(-FUZZY, FUZZY) + dist < dista)
64        {
65            nearb = neara; distb = dista; neara = 1; dista = dist;
66        }
67        else
68        {
69            nearb = 1; distb = dist;
70        }
71
72        /* check dist to 70% */
73        dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y;
74        if(caca_rand(-FUZZY, FUZZY) + dist < dista)
75        {
76            nearb = neara; distb = dista; neara = 2; dista = dist;
77        }
78        else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
79        {
80            nearb = 2; distb = dist;
81        }
82
83        /* check dist to white */
84        dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
85        if(caca_rand(-FUZZY, FUZZY) + dist < dista)
86        {
87            nearb = neara; distb = dista; neara = 3; dista = dist;
88        }
89        else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
90        {
91            nearb = 3; distb = dist;
92        }
93
94#if 1
95        /* check dist to dark */
96        dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100);
97        dist = dist * 12 / 16;
98        if(caca_rand(-FUZZY, FUZZY) + dist < dista)
99        {
100            nearb = neara; distb = dista; neara = 4; dista = dist;
101        }
102        else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
103        {
104            nearb = 4; distb = dist;
105        }
106
107        /* check dist to light */
108        dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100);
109        dist = dist * 8 / 16;
110        if(caca_rand(-FUZZY, FUZZY) + dist < dista)
111        {
112            nearb = neara; distb = dista; neara = 5; dista = dist;
113        }
114        else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
115        {
116            nearb = 5; distb = dist;
117        }
118#endif
119
120        /* dista can be > distb because of dithering fuzziness */
121        if(dista > distb)
122            ch = density[distb * 2 * 13 / (dista + distb)];
123        else
124            ch = density[dista * 2 * 13 / (dista + distb)];
125        caca_set_color(points[nearb], points[neara]);
126
127        caca_putchar(x * caca_get_width() / 100, (100 - y) * caca_get_height() / 100, ch);
128    }
129
130    caca_refresh();
131
132    while((caca_get_event() & 0xff000000) != CACA_EVENT_KEY_PRESS);
133
134    caca_end();
135    return 0;
136}
137
Note: See TracBrowser for help on using the repository browser.