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

Last change on this file since 1452 was 1452, checked in by Sam Hocevar, 13 years ago
  • Add a no warranty clause to the code.
  • Property svn:keywords set to Id
File size: 3.6 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 1452 2006-12-11 15:48:46Z sam $
7 *
8 *  This program is free software. It commes 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#include "common.h"
17
18#if !defined(__KERNEL__)
19#   include <stdio.h>
20#endif
21
22#include "cucul.h"
23#include "caca.h"
24
25#define XRATIO 100*100
26#define YRATIO 70*70
27#define FUZZY 5000000
28
29unsigned int points[] =
30{
31    CUCUL_BLACK, CUCUL_DARKGRAY, CUCUL_LIGHTGRAY,
32    CUCUL_WHITE, CUCUL_RED, CUCUL_LIGHTRED
33};
34
35char density[] = " ',+:;o&%w$W@#";
36
37int main(int argc, char *argv[])
38{
39    cucul_canvas_t *cv;
40    caca_display_t *dp;
41    int neara, dista, nearb, distb, dist;
42    int x, y;
43
44    cv = cucul_create_canvas(0, 0);
45    dp = caca_create_display(cv);
46
47    for(x = 0; x < 100; x++)
48        for(y = 0; y < 100; y++)
49    {
50        char ch = '?';
51
52        /* distance to black */
53        dista = XRATIO * x * x;
54        neara = 0;
55
56        /* distance to 40% */
57        dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y;
58        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
59        {
60            nearb = neara; distb = dista; neara = 1; dista = dist;
61        }
62        else
63        {
64            nearb = 1; distb = dist;
65        }
66
67        /* check dist to 70% */
68        dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y;
69        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
70        {
71            nearb = neara; distb = dista; neara = 2; dista = dist;
72        }
73        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
74        {
75            nearb = 2; distb = dist;
76        }
77
78        /* check dist to white */
79        dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
80        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
81        {
82            nearb = neara; distb = dista; neara = 3; dista = dist;
83        }
84        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
85        {
86            nearb = 3; distb = dist;
87        }
88
89#if 1
90        /* check dist to dark */
91        dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100);
92        dist = dist * 12 / 16;
93        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
94        {
95            nearb = neara; distb = dista; neara = 4; dista = dist;
96        }
97        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
98        {
99            nearb = 4; distb = dist;
100        }
101
102        /* check dist to light */
103        dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100);
104        dist = dist * 8 / 16;
105        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
106        {
107            nearb = neara; distb = dista; neara = 5; dista = dist;
108        }
109        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
110        {
111            nearb = 5; distb = dist;
112        }
113#endif
114
115        /* dista can be > distb because of dithering fuzziness */
116        if(dista > distb)
117            ch = density[distb * 2 * 13 / (dista + distb)];
118        else
119            ch = density[dista * 2 * 13 / (dista + distb)];
120        cucul_set_color_ansi(cv, points[nearb], points[neara]);
121
122        cucul_put_char(cv, x * cucul_get_canvas_width(cv) / 100,
123                          (100 - y) * cucul_get_canvas_height(cv) / 100, ch);
124    }
125
126    caca_refresh_display(dp);
127
128    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
129
130    caca_free_display(dp);
131    cucul_free_canvas(cv);
132
133    return 0;
134}
135
Note: See TracBrowser for help on using the repository browser.