Debian Codegraph @

examples/dithering.c

raw
1 /*
2 * dithering libcaca dithering test program
3 * Copyright (c) 2003-2012 Sam Hocevar <[email protected]>
4 * All Rights Reserved
5 *
6 * This program is free software. It comes without any warranty, to
7 * the extent permitted by applicable law. You can redistribute it
8 * and/or modify it under the terms of the Do What the Fuck You Want
9 * to Public License, Version 2, as published by Sam Hocevar. See
10 * http://www.wtfpl.net/ for more details.
11 */
12
13 #include "config.h"
14
15 #if !defined(__KERNEL__)
16 # include <stdio.h>
17 #endif
18
19 #include "caca.h"
20
21 #define XRATIO 100*100
22 #define YRATIO 70*70
23 #define FUZZY 5000000
24
25 unsigned int points[] =
26 {
27 CACA_BLACK, CACA_DARKGRAY, CACA_LIGHTGRAY,
28 CACA_WHITE, CACA_RED, CACA_LIGHTRED
29 };
30
31 char density[] = " ',+:;o&%w$W@#";
32
33 int main(int argc, char *argv[])
34 {
35 caca_canvas_t *cv;
36 caca_display_t *dp;
37 int neara, dista, nearb, distb, dist;
38 int x, y;
39
40 cv = caca_create_canvas(80, 24);
41 if(cv == NULL)
42 {
43 printf("Failed to create canvas\n");
44 return 1;
45 }
46
47 dp = caca_create_display(cv);
48 if(dp == NULL)
49 {
50 printf("Failed to create display\n");
51 return 1;
52 }
53
54
55
56 for(x = 0; x < 100; x++)
57 for(y = 0; y < 100; y++)
58 {
59 char ch = '?';
60
61 /* distance to black */
62 dista = XRATIO * x * x;
63 neara = 0;
64
65 /* distance to 40% */
66 dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y;
67 if(caca_rand(-FUZZY, FUZZY+1) + dist < dista)
68 {
69 nearb = neara; distb = dista; neara = 1; dista = dist;
70 }
71 else
72 {
73 nearb = 1; distb = dist;
74 }
75
76 /* check dist to 70% */
77 dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y;
78 if(caca_rand(-FUZZY, FUZZY+1) + dist < dista)
79 {
80 nearb = neara; distb = dista; neara = 2; dista = dist;
81 }
82 else if(caca_rand(-FUZZY, FUZZY+1) + dist < distb)
83 {
84 nearb = 2; distb = dist;
85 }
86
87 /* check dist to white */
88 dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
89 if(caca_rand(-FUZZY, FUZZY+1) + dist < dista)
90 {
91 nearb = neara; distb = dista; neara = 3; dista = dist;
92 }
93 else if(caca_rand(-FUZZY, FUZZY+1) + dist < distb)
94 {
95 nearb = 3; distb = dist;
96 }
97
98 #if 1
99 /* check dist to dark */
100 dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100);
101 dist = dist * 12 / 16;
102 if(caca_rand(-FUZZY, FUZZY+1) + dist < dista)
103 {
104 nearb = neara; distb = dista; neara = 4; dista = dist;
105 }
106 else if(caca_rand(-FUZZY, FUZZY+1) + dist < distb)
107 {
108 nearb = 4; distb = dist;
109 }
110
111 /* check dist to light */
112 dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100);
113 dist = dist * 8 / 16;
114 if(caca_rand(-FUZZY, FUZZY+1) + dist < dista)
115 {
116 nearb = neara; distb = dista; neara = 5; dista = dist;
117 }
118 else if(caca_rand(-FUZZY, FUZZY+1) + dist < distb)
119 {
120 nearb = 5; distb = dist;
121 }
122 #endif
123
124 /* dista can be > distb because of dithering fuzziness */
125 if(dista > distb)
126 ch = density[distb * 2 * 13 / (dista + distb)];
127 else
128 ch = density[dista * 2 * 13 / (dista + distb)];
129 caca_set_color_ansi(cv, points[nearb], points[neara]);
130
131 caca_put_char(cv, x * caca_get_canvas_width(cv) / 100,
132 (100 - y) * caca_get_canvas_height(cv) / 100, ch);
133 }
134
135 caca_refresh_display(dp);
136
137 caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
138
139 caca_free_display(dp);
140 caca_free_canvas(cv);
141
142 return 0;
143 }
144