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 297 2003-12-31 13:55:34Z 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 | |
---|
32 | enum 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 | |
---|
42 | char density[] = " -,+:;o&%w$W@#"; |
---|
43 | |
---|
44 | int 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 | char ch = '?'; |
---|
55 | |
---|
56 | /* distance to black */ |
---|
57 | dista = XRATIO * x * x; |
---|
58 | neara = 0; |
---|
59 | |
---|
60 | /* distance to 40% */ |
---|
61 | dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y; |
---|
62 | if(caca_rand(-FUZZY, FUZZY) + dist < dista) |
---|
63 | { |
---|
64 | nearb = neara; distb = dista; neara = 1; dista = dist; |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | nearb = 1; distb = dist; |
---|
69 | } |
---|
70 | |
---|
71 | /* check dist to 70% */ |
---|
72 | dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y; |
---|
73 | if(caca_rand(-FUZZY, FUZZY) + dist < dista) |
---|
74 | { |
---|
75 | nearb = neara; distb = dista; neara = 2; dista = dist; |
---|
76 | } |
---|
77 | else if(caca_rand(-FUZZY, FUZZY) + dist < distb) |
---|
78 | { |
---|
79 | nearb = 2; distb = dist; |
---|
80 | } |
---|
81 | |
---|
82 | /* check dist to white */ |
---|
83 | dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y; |
---|
84 | if(caca_rand(-FUZZY, FUZZY) + dist < dista) |
---|
85 | { |
---|
86 | nearb = neara; distb = dista; neara = 3; dista = dist; |
---|
87 | } |
---|
88 | else if(caca_rand(-FUZZY, FUZZY) + dist < distb) |
---|
89 | { |
---|
90 | nearb = 3; distb = dist; |
---|
91 | } |
---|
92 | |
---|
93 | #if 1 |
---|
94 | /* check dist to dark */ |
---|
95 | dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100); |
---|
96 | dist = dist * 12 / 16; |
---|
97 | if(caca_rand(-FUZZY, FUZZY) + dist < dista) |
---|
98 | { |
---|
99 | nearb = neara; distb = dista; neara = 4; dista = dist; |
---|
100 | } |
---|
101 | else if(caca_rand(-FUZZY, FUZZY) + dist < distb) |
---|
102 | { |
---|
103 | nearb = 4; distb = dist; |
---|
104 | } |
---|
105 | |
---|
106 | /* check dist to light */ |
---|
107 | dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100); |
---|
108 | dist = dist * 8 / 16; |
---|
109 | if(caca_rand(-FUZZY, FUZZY) + dist < dista) |
---|
110 | { |
---|
111 | nearb = neara; distb = dista; neara = 5; dista = dist; |
---|
112 | } |
---|
113 | else if(caca_rand(-FUZZY, FUZZY) + dist < distb) |
---|
114 | { |
---|
115 | nearb = 5; distb = dist; |
---|
116 | } |
---|
117 | #endif |
---|
118 | |
---|
119 | /* dista can be > distb because of dithering fuzziness */ |
---|
120 | if(dista > distb) |
---|
121 | ch = density[distb * 2 * 13 / (dista + distb)]; |
---|
122 | else |
---|
123 | ch = density[dista * 2 * 13 / (dista + distb)]; |
---|
124 | caca_set_color(points[nearb], points[neara]); |
---|
125 | |
---|
126 | caca_putchar(x * caca_get_width() / 100, (100 - y) * caca_get_height() / 100, ch); |
---|
127 | } |
---|
128 | |
---|
129 | caca_refresh(); |
---|
130 | |
---|
131 | while((caca_get_event() & 0xff000000) != CACA_EVENT_KEY_PRESS); |
---|
132 | |
---|
133 | caca_end(); |
---|
134 | return 0; |
---|
135 | } |
---|
136 | |
---|