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 811 2006-04-18 15:11:25Z sam $ |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | #include "config.h" |
---|
15 | |
---|
16 | #include "cucul.h" |
---|
17 | #include "caca.h" |
---|
18 | |
---|
19 | #define XRATIO 100*100 |
---|
20 | #define YRATIO 70*70 |
---|
21 | #define FUZZY 5000000 |
---|
22 | |
---|
23 | unsigned int points[] = |
---|
24 | { |
---|
25 | CUCUL_COLOR_BLACK, |
---|
26 | CUCUL_COLOR_DARKGRAY, |
---|
27 | CUCUL_COLOR_LIGHTGRAY, |
---|
28 | CUCUL_COLOR_WHITE, |
---|
29 | CUCUL_COLOR_RED, |
---|
30 | CUCUL_COLOR_LIGHTRED |
---|
31 | }; |
---|
32 | |
---|
33 | char density[] = " ',+:;o&%w$W@#"; |
---|
34 | |
---|
35 | int main(void) |
---|
36 | { |
---|
37 | caca_event_t ev; |
---|
38 | cucul_canvas_t *cv; |
---|
39 | caca_display_t *dp; |
---|
40 | int neara, dista, nearb, distb, dist; |
---|
41 | int x, y; |
---|
42 | |
---|
43 | cv = cucul_create(0, 0); |
---|
44 | dp = caca_attach(cv); |
---|
45 | |
---|
46 | for(x = 0; x < 100; x++) |
---|
47 | for(y = 0; y < 100; y++) |
---|
48 | { |
---|
49 | char ch = '?'; |
---|
50 | |
---|
51 | /* distance to black */ |
---|
52 | dista = XRATIO * x * x; |
---|
53 | neara = 0; |
---|
54 | |
---|
55 | /* distance to 40% */ |
---|
56 | dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y; |
---|
57 | if(cucul_rand(-FUZZY, FUZZY) + dist < dista) |
---|
58 | { |
---|
59 | nearb = neara; distb = dista; neara = 1; dista = dist; |
---|
60 | } |
---|
61 | else |
---|
62 | { |
---|
63 | nearb = 1; distb = dist; |
---|
64 | } |
---|
65 | |
---|
66 | /* check dist to 70% */ |
---|
67 | dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y; |
---|
68 | if(cucul_rand(-FUZZY, FUZZY) + dist < dista) |
---|
69 | { |
---|
70 | nearb = neara; distb = dista; neara = 2; dista = dist; |
---|
71 | } |
---|
72 | else if(cucul_rand(-FUZZY, FUZZY) + dist < distb) |
---|
73 | { |
---|
74 | nearb = 2; distb = dist; |
---|
75 | } |
---|
76 | |
---|
77 | /* check dist to white */ |
---|
78 | dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y; |
---|
79 | if(cucul_rand(-FUZZY, FUZZY) + dist < dista) |
---|
80 | { |
---|
81 | nearb = neara; distb = dista; neara = 3; dista = dist; |
---|
82 | } |
---|
83 | else if(cucul_rand(-FUZZY, FUZZY) + dist < distb) |
---|
84 | { |
---|
85 | nearb = 3; distb = dist; |
---|
86 | } |
---|
87 | |
---|
88 | #if 1 |
---|
89 | /* check dist to dark */ |
---|
90 | dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100); |
---|
91 | dist = dist * 12 / 16; |
---|
92 | if(cucul_rand(-FUZZY, FUZZY) + dist < dista) |
---|
93 | { |
---|
94 | nearb = neara; distb = dista; neara = 4; dista = dist; |
---|
95 | } |
---|
96 | else if(cucul_rand(-FUZZY, FUZZY) + dist < distb) |
---|
97 | { |
---|
98 | nearb = 4; distb = dist; |
---|
99 | } |
---|
100 | |
---|
101 | /* check dist to light */ |
---|
102 | dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100); |
---|
103 | dist = dist * 8 / 16; |
---|
104 | if(cucul_rand(-FUZZY, FUZZY) + dist < dista) |
---|
105 | { |
---|
106 | nearb = neara; distb = dista; neara = 5; dista = dist; |
---|
107 | } |
---|
108 | else if(cucul_rand(-FUZZY, FUZZY) + dist < distb) |
---|
109 | { |
---|
110 | nearb = 5; distb = dist; |
---|
111 | } |
---|
112 | #endif |
---|
113 | |
---|
114 | /* dista can be > distb because of dithering fuzziness */ |
---|
115 | if(dista > distb) |
---|
116 | ch = density[distb * 2 * 13 / (dista + distb)]; |
---|
117 | else |
---|
118 | ch = density[dista * 2 * 13 / (dista + distb)]; |
---|
119 | cucul_set_color(cv, points[nearb], points[neara]); |
---|
120 | |
---|
121 | cucul_putchar(cv, x * cucul_get_width(cv) / 100, |
---|
122 | (100 - y) * cucul_get_height(cv) / 100, ch); |
---|
123 | } |
---|
124 | |
---|
125 | caca_display(dp); |
---|
126 | |
---|
127 | caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1); |
---|
128 | |
---|
129 | caca_detach(dp); |
---|
130 | cucul_free(cv); |
---|
131 | |
---|
132 | return 0; |
---|
133 | } |
---|
134 | |
---|