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

Last change on this file since 1753 was 1753, checked in by Jean-Yves Lamoureux, 16 years ago
  • Added error checks on canvas / display creations
  • 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 1753 2007-02-22 15:31:39Z jylam $
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#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(32, 16);
45    if(cv == NULL)
46    {
47        printf("Failed to create canvas\n");
48        return 1;
49    }
50
51    dp = caca_create_display(cv);
52    if(dp == NULL)
53    {
54        printf("Failed to create display\n");
55        return 1;
56    }
57
58
59
60    for(x = 0; x < 100; x++)
61        for(y = 0; y < 100; y++)
62    {
63        char ch = '?';
64
65        /* distance to black */
66        dista = XRATIO * x * x;
67        neara = 0;
68
69        /* distance to 40% */
70        dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y;
71        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
72        {
73            nearb = neara; distb = dista; neara = 1; dista = dist;
74        }
75        else
76        {
77            nearb = 1; distb = dist;
78        }
79
80        /* check dist to 70% */
81        dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y;
82        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
83        {
84            nearb = neara; distb = dista; neara = 2; dista = dist;
85        }
86        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
87        {
88            nearb = 2; distb = dist;
89        }
90
91        /* check dist to white */
92        dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
93        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
94        {
95            nearb = neara; distb = dista; neara = 3; dista = dist;
96        }
97        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
98        {
99            nearb = 3; distb = dist;
100        }
101
102#if 1
103        /* check dist to dark */
104        dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100);
105        dist = dist * 12 / 16;
106        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
107        {
108            nearb = neara; distb = dista; neara = 4; dista = dist;
109        }
110        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
111        {
112            nearb = 4; distb = dist;
113        }
114
115        /* check dist to light */
116        dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100);
117        dist = dist * 8 / 16;
118        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
119        {
120            nearb = neara; distb = dista; neara = 5; dista = dist;
121        }
122        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
123        {
124            nearb = 5; distb = dist;
125        }
126#endif
127
128        /* dista can be > distb because of dithering fuzziness */
129        if(dista > distb)
130            ch = density[distb * 2 * 13 / (dista + distb)];
131        else
132            ch = density[dista * 2 * 13 / (dista + distb)];
133        cucul_set_color_ansi(cv, points[nearb], points[neara]);
134
135        cucul_put_char(cv, x * cucul_get_canvas_width(cv) / 100,
136                          (100 - y) * cucul_get_canvas_height(cv) / 100, ch);
137    }
138
139    caca_refresh_display(dp);
140
141    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
142
143    caca_free_display(dp);
144    cucul_free_canvas(cv);
145
146    return 0;
147}
148
Note: See TracBrowser for help on using the repository browser.