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

Last change on this file since 2299 was 2299, checked in by Sam Hocevar, 12 years ago
  • Remove #include <stdint.h> etc. from "common.h". Instead, make sure that <cucul.h> will provide the C99 types, even if libcaca has been installed.
  • Rename what's left of "common.h" to "stubs.h".
  • Remove all references to erroneous <inttypes.h> from source files.
  • Property svn:keywords set to Id
File size: 3.8 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 2299 2008-04-19 12:42:50Z sam $
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 "cucul.h"
22#include "caca.h"
23
24#define XRATIO 100*100
25#define YRATIO 70*70
26#define FUZZY 5000000
27
28unsigned int points[] =
29{
30    CUCUL_BLACK, CUCUL_DARKGRAY, CUCUL_LIGHTGRAY,
31    CUCUL_WHITE, CUCUL_RED, CUCUL_LIGHTRED
32};
33
34char density[] = " ',+:;o&%w$W@#";
35
36int main(int argc, char *argv[])
37{
38    cucul_canvas_t *cv;
39    caca_display_t *dp;
40    int neara, dista, nearb, distb, dist;
41    int x, y;
42
43    cv = cucul_create_canvas(80, 24);
44    if(cv == NULL)
45    {
46        printf("Failed to create canvas\n");
47        return 1;
48    }
49
50    dp = caca_create_display(cv);
51    if(dp == NULL)
52    {
53        printf("Failed to create display\n");
54        return 1;
55    }
56
57
58
59    for(x = 0; x < 100; x++)
60        for(y = 0; y < 100; y++)
61    {
62        char ch = '?';
63
64        /* distance to black */
65        dista = XRATIO * x * x;
66        neara = 0;
67
68        /* distance to 40% */
69        dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y;
70        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
71        {
72            nearb = neara; distb = dista; neara = 1; dista = dist;
73        }
74        else
75        {
76            nearb = 1; distb = dist;
77        }
78
79        /* check dist to 70% */
80        dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y;
81        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
82        {
83            nearb = neara; distb = dista; neara = 2; dista = dist;
84        }
85        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
86        {
87            nearb = 2; distb = dist;
88        }
89
90        /* check dist to white */
91        dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
92        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
93        {
94            nearb = neara; distb = dista; neara = 3; dista = dist;
95        }
96        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
97        {
98            nearb = 3; distb = dist;
99        }
100
101#if 1
102        /* check dist to dark */
103        dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100);
104        dist = dist * 12 / 16;
105        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
106        {
107            nearb = neara; distb = dista; neara = 4; dista = dist;
108        }
109        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
110        {
111            nearb = 4; distb = dist;
112        }
113
114        /* check dist to light */
115        dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100);
116        dist = dist * 8 / 16;
117        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
118        {
119            nearb = neara; distb = dista; neara = 5; dista = dist;
120        }
121        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
122        {
123            nearb = 5; distb = dist;
124        }
125#endif
126
127        /* dista can be > distb because of dithering fuzziness */
128        if(dista > distb)
129            ch = density[distb * 2 * 13 / (dista + distb)];
130        else
131            ch = density[dista * 2 * 13 / (dista + distb)];
132        cucul_set_color_ansi(cv, points[nearb], points[neara]);
133
134        cucul_put_char(cv, x * cucul_get_canvas_width(cv) / 100,
135                          (100 - y) * cucul_get_canvas_height(cv) / 100, ch);
136    }
137
138    caca_refresh_display(dp);
139
140    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
141
142    caca_free_display(dp);
143    cucul_free_canvas(cv);
144
145    return 0;
146}
147
Note: See TracBrowser for help on using the repository browser.