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

Last change on this file since 2304 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
RevLine 
[292]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 *
[1462]8 *  This program is free software. It comes without any warranty, to
[1452]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
[522]12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
[292]13 */
14
15#include "config.h"
16
[1048]17#if !defined(__KERNEL__)
18#   include <stdio.h>
19#endif
[849]20
[524]21#include "cucul.h"
[292]22#include "caca.h"
23
24#define XRATIO 100*100
25#define YRATIO 70*70
26#define FUZZY 5000000
27
[733]28unsigned int points[] =
[292]29{
[1257]30    CUCUL_BLACK, CUCUL_DARKGRAY, CUCUL_LIGHTGRAY,
31    CUCUL_WHITE, CUCUL_RED, CUCUL_LIGHTRED
[292]32};
33
[792]34char density[] = " ',+:;o&%w$W@#";
[292]35
[1048]36int main(int argc, char *argv[])
[292]37{
[811]38    cucul_canvas_t *cv;
39    caca_display_t *dp;
[292]40    int neara, dista, nearb, distb, dist;
41    int x, y;
42
[1823]43    cv = cucul_create_canvas(80, 24);
[1753]44    if(cv == NULL)
45    {
46        printf("Failed to create canvas\n");
47        return 1;
48    }
49
[819]50    dp = caca_create_display(cv);
[1753]51    if(dp == NULL)
52    {
53        printf("Failed to create display\n");
54        return 1;
55    }
[292]56
[1753]57
58
[292]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;
[815]70        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
[292]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;
[815]81        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
[292]82        {
83            nearb = neara; distb = dista; neara = 2; dista = dist;
84        }
[815]85        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
[292]86        {
87            nearb = 2; distb = dist;
88        }
89
90        /* check dist to white */
91        dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
[815]92        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
[292]93        {
94            nearb = neara; distb = dista; neara = 3; dista = dist;
95        }
[815]96        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
[292]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;
[815]105        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
[292]106        {
107            nearb = neara; distb = dista; neara = 4; dista = dist;
108        }
[815]109        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
[292]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;
[815]117        if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
[292]118        {
119            nearb = neara; distb = dista; neara = 5; dista = dist;
120        }
[815]121        else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
[292]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)];
[1267]132        cucul_set_color_ansi(cv, points[nearb], points[neara]);
[292]133
[1347]134        cucul_put_char(cv, x * cucul_get_canvas_width(cv) / 100,
[813]135                          (100 - y) * cucul_get_canvas_height(cv) / 100, ch);
[292]136    }
137
[819]138    caca_refresh_display(dp);
[292]139
[849]140    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
[292]141
[819]142    caca_free_display(dp);
[813]143    cucul_free_canvas(cv);
[524]144
[292]145    return 0;
146}
147
Note: See TracBrowser for help on using the repository browser.