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 677 2006-03-23 13:12:56Z 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 | enum cucul_color 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 | cucul_t *qq; |
---|
38 | caca_t *kk; |
---|
39 | int neara, dista, nearb, distb, dist; |
---|
40 | int x, y; |
---|
41 | |
---|
42 | qq = cucul_create(0, 0); |
---|
43 | kk = caca_attach(qq); |
---|
44 | |
---|
45 | for(x = 0; x < 100; x++) |
---|
46 | for(y = 0; y < 100; y++) |
---|
47 | { |
---|
48 | char ch = '?'; |
---|
49 | |
---|
50 | /* distance to black */ |
---|
51 | dista = XRATIO * x * x; |
---|
52 | neara = 0; |
---|
53 | |
---|
54 | /* distance to 40% */ |
---|
55 | dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y; |
---|
56 | if(cucul_rand(-FUZZY, FUZZY) + dist < dista) |
---|
57 | { |
---|
58 | nearb = neara; distb = dista; neara = 1; dista = dist; |
---|
59 | } |
---|
60 | else |
---|
61 | { |
---|
62 | nearb = 1; distb = dist; |
---|
63 | } |
---|
64 | |
---|
65 | /* check dist to 70% */ |
---|
66 | dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y; |
---|
67 | if(cucul_rand(-FUZZY, FUZZY) + dist < dista) |
---|
68 | { |
---|
69 | nearb = neara; distb = dista; neara = 2; dista = dist; |
---|
70 | } |
---|
71 | else if(cucul_rand(-FUZZY, FUZZY) + dist < distb) |
---|
72 | { |
---|
73 | nearb = 2; distb = dist; |
---|
74 | } |
---|
75 | |
---|
76 | /* check dist to white */ |
---|
77 | dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y; |
---|
78 | if(cucul_rand(-FUZZY, FUZZY) + dist < dista) |
---|
79 | { |
---|
80 | nearb = neara; distb = dista; neara = 3; dista = dist; |
---|
81 | } |
---|
82 | else if(cucul_rand(-FUZZY, FUZZY) + dist < distb) |
---|
83 | { |
---|
84 | nearb = 3; distb = dist; |
---|
85 | } |
---|
86 | |
---|
87 | #if 1 |
---|
88 | /* check dist to dark */ |
---|
89 | dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100); |
---|
90 | dist = dist * 12 / 16; |
---|
91 | if(cucul_rand(-FUZZY, FUZZY) + dist < dista) |
---|
92 | { |
---|
93 | nearb = neara; distb = dista; neara = 4; dista = dist; |
---|
94 | } |
---|
95 | else if(cucul_rand(-FUZZY, FUZZY) + dist < distb) |
---|
96 | { |
---|
97 | nearb = 4; distb = dist; |
---|
98 | } |
---|
99 | |
---|
100 | /* check dist to light */ |
---|
101 | dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100); |
---|
102 | dist = dist * 8 / 16; |
---|
103 | if(cucul_rand(-FUZZY, FUZZY) + dist < dista) |
---|
104 | { |
---|
105 | nearb = neara; distb = dista; neara = 5; dista = dist; |
---|
106 | } |
---|
107 | else if(cucul_rand(-FUZZY, FUZZY) + dist < distb) |
---|
108 | { |
---|
109 | nearb = 5; distb = dist; |
---|
110 | } |
---|
111 | #endif |
---|
112 | |
---|
113 | /* dista can be > distb because of dithering fuzziness */ |
---|
114 | if(dista > distb) |
---|
115 | ch = density[distb * 2 * 13 / (dista + distb)]; |
---|
116 | else |
---|
117 | ch = density[dista * 2 * 13 / (dista + distb)]; |
---|
118 | cucul_set_color(qq, points[nearb], points[neara]); |
---|
119 | |
---|
120 | cucul_putchar(qq, x * cucul_get_width(qq) / 100, |
---|
121 | (100 - y) * cucul_get_height(qq) / 100, ch); |
---|
122 | } |
---|
123 | |
---|
124 | caca_display(kk); |
---|
125 | |
---|
126 | while(!caca_get_event(kk, CACA_EVENT_KEY_PRESS)); |
---|
127 | |
---|
128 | caca_detach(kk); |
---|
129 | cucul_free(qq); |
---|
130 | |
---|
131 | return 0; |
---|
132 | } |
---|
133 | |
---|