source: libcaca/trunk/examples/dithering.c @ 4148

Last change on this file since 4148 was 4148, checked in by sam, 5 years ago

Update my e-mail address everywhere.

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