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

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