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

Last change on this file since 522 was 522, checked in by Sam Hocevar, 14 years ago
  • Changed the licensing to WTFPL, as per all copyright holders' permission.
  • Property svn:keywords set to Id
File size: 3.2 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 522 2006-03-04 00:35:45Z 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 "caca.h"
17
18#define XRATIO 100*100
19#define YRATIO 70*70
20#define FUZZY 5000000
21
22enum caca_color points[] =
23{
24    CACA_COLOR_BLACK,
25    CACA_COLOR_DARKGRAY,
26    CACA_COLOR_LIGHTGRAY,
27    CACA_COLOR_WHITE,
28    CACA_COLOR_RED,
29    CACA_COLOR_LIGHTRED
30};
31
32char density[] = " -,+:;o&%w$W@#";
33
34int main(void)
35{
36    int neara, dista, nearb, distb, dist;
37    int x, y;
38
39    caca_init();
40
41    for(x = 0; x < 100; x++)
42        for(y = 0; y < 100; y++)
43    {
44        char ch = '?';
45
46        /* distance to black */
47        dista = XRATIO * x * x;
48        neara = 0;
49
50        /* distance to 40% */
51        dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y;
52        if(caca_rand(-FUZZY, FUZZY) + dist < dista)
53        {
54            nearb = neara; distb = dista; neara = 1; dista = dist;
55        }
56        else
57        {
58            nearb = 1; distb = dist;
59        }
60
61        /* check dist to 70% */
62        dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y;
63        if(caca_rand(-FUZZY, FUZZY) + dist < dista)
64        {
65            nearb = neara; distb = dista; neara = 2; dista = dist;
66        }
67        else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
68        {
69            nearb = 2; distb = dist;
70        }
71
72        /* check dist to white */
73        dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
74        if(caca_rand(-FUZZY, FUZZY) + dist < dista)
75        {
76            nearb = neara; distb = dista; neara = 3; dista = dist;
77        }
78        else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
79        {
80            nearb = 3; distb = dist;
81        }
82
83#if 1
84        /* check dist to dark */
85        dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100);
86        dist = dist * 12 / 16;
87        if(caca_rand(-FUZZY, FUZZY) + dist < dista)
88        {
89            nearb = neara; distb = dista; neara = 4; dista = dist;
90        }
91        else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
92        {
93            nearb = 4; distb = dist;
94        }
95
96        /* check dist to light */
97        dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100);
98        dist = dist * 8 / 16;
99        if(caca_rand(-FUZZY, FUZZY) + dist < dista)
100        {
101            nearb = neara; distb = dista; neara = 5; dista = dist;
102        }
103        else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
104        {
105            nearb = 5; distb = dist;
106        }
107#endif
108
109        /* dista can be > distb because of dithering fuzziness */
110        if(dista > distb)
111            ch = density[distb * 2 * 13 / (dista + distb)];
112        else
113            ch = density[dista * 2 * 13 / (dista + distb)];
114        caca_set_color(points[nearb], points[neara]);
115
116        caca_putchar(x * caca_get_width() / 100, (100 - y) * caca_get_height() / 100, ch);
117    }
118
119    caca_refresh();
120
121    while(!caca_get_event(CACA_EVENT_KEY_PRESS));
122
123    caca_end();
124    return 0;
125}
126
Note: See TracBrowser for help on using the repository browser.