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

Last change on this file since 2821 was 2821, checked in by Sam Hocevar, 11 years ago

Starting refactoring to get rid of libcucul. The initial reason for the
split is rendered moot by the plugin system: when enabled, binaries do
not link directly with libX11 or libGL. I hope this is a step towards
more consisteny and clarity.

  • Property svn:keywords set to Id
File size: 3.7 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 2821 2008-09-27 13:12:46Z 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
[292]21#include "caca.h"
22
23#define XRATIO 100*100
24#define YRATIO 70*70
25#define FUZZY 5000000
26
[733]27unsigned int points[] =
[292]28{
[2821]29    CACA_BLACK, CACA_DARKGRAY, CACA_LIGHTGRAY,
30    CACA_WHITE, CACA_RED, CACA_LIGHTRED
[292]31};
32
[792]33char density[] = " ',+:;o&%w$W@#";
[292]34
[1048]35int main(int argc, char *argv[])
[292]36{
[2821]37    caca_canvas_t *cv;
[811]38    caca_display_t *dp;
[292]39    int neara, dista, nearb, distb, dist;
40    int x, y;
41
[2821]42    cv = caca_create_canvas(80, 24);
[1753]43    if(cv == NULL)
44    {
45        printf("Failed to create canvas\n");
46        return 1;
47    }
48
[819]49    dp = caca_create_display(cv);
[1753]50    if(dp == NULL)
51    {
52        printf("Failed to create display\n");
53        return 1;
54    }
[292]55
[1753]56
57
[292]58    for(x = 0; x < 100; x++)
59        for(y = 0; y < 100; y++)
60    {
61        char ch = '?';
62
63        /* distance to black */
64        dista = XRATIO * x * x;
65        neara = 0;
66
67        /* distance to 40% */
68        dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y;
[2821]69        if(caca_rand(-FUZZY, FUZZY+1) + dist < dista)
[292]70        {
71            nearb = neara; distb = dista; neara = 1; dista = dist;
72        }
73        else
74        {
75            nearb = 1; distb = dist;
76        }
77
78        /* check dist to 70% */
79        dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y;
[2821]80        if(caca_rand(-FUZZY, FUZZY+1) + dist < dista)
[292]81        {
82            nearb = neara; distb = dista; neara = 2; dista = dist;
83        }
[2821]84        else if(caca_rand(-FUZZY, FUZZY+1) + dist < distb)
[292]85        {
86            nearb = 2; distb = dist;
87        }
88
89        /* check dist to white */
90        dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
[2821]91        if(caca_rand(-FUZZY, FUZZY+1) + dist < dista)
[292]92        {
93            nearb = neara; distb = dista; neara = 3; dista = dist;
94        }
[2821]95        else if(caca_rand(-FUZZY, FUZZY+1) + dist < distb)
[292]96        {
97            nearb = 3; distb = dist;
98        }
99
100#if 1
101        /* check dist to dark */
102        dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100);
103        dist = dist * 12 / 16;
[2821]104        if(caca_rand(-FUZZY, FUZZY+1) + dist < dista)
[292]105        {
106            nearb = neara; distb = dista; neara = 4; dista = dist;
107        }
[2821]108        else if(caca_rand(-FUZZY, FUZZY+1) + dist < distb)
[292]109        {
110            nearb = 4; distb = dist;
111        }
112
113        /* check dist to light */
114        dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100);
115        dist = dist * 8 / 16;
[2821]116        if(caca_rand(-FUZZY, FUZZY+1) + dist < dista)
[292]117        {
118            nearb = neara; distb = dista; neara = 5; dista = dist;
119        }
[2821]120        else if(caca_rand(-FUZZY, FUZZY+1) + dist < distb)
[292]121        {
122            nearb = 5; distb = dist;
123        }
124#endif
125
126        /* dista can be > distb because of dithering fuzziness */
127        if(dista > distb)
128            ch = density[distb * 2 * 13 / (dista + distb)];
129        else
130            ch = density[dista * 2 * 13 / (dista + distb)];
[2821]131        caca_set_color_ansi(cv, points[nearb], points[neara]);
[292]132
[2821]133        caca_put_char(cv, x * caca_get_canvas_width(cv) / 100,
134                          (100 - y) * caca_get_canvas_height(cv) / 100, ch);
[292]135    }
136
[819]137    caca_refresh_display(dp);
[292]138
[849]139    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
[292]140
[819]141    caca_free_display(dp);
[2821]142    caca_free_canvas(cv);
[524]143
[292]144    return 0;
145}
146
Note: See TracBrowser for help on using the repository browser.