source: libcaca/trunk/examples/gamma.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: 4.1 KB
Line 
1/*
2 *  gamma         libcucul gamma test program
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: gamma.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#   include <math.h>
20#endif
21
22#include "cucul.h"
23#include "caca.h"
24
25uint32_t buffer[256 * 4];
26
27int main(int argc, char *argv[])
28{
29    caca_event_t ev;
30    cucul_canvas_t *cv, *cw, *mask;
31    caca_display_t *dp;
32    cucul_dither_t *left, *right;
33    float gam;
34    int x;
35
36    cv = cucul_create_canvas(0, 0);
37    if(cv == NULL)
38    {
39        printf("Can't created canvas\n");
40        return -1;
41    }
42    dp = caca_create_display(cv);
43    if(dp == NULL)
44    {
45        printf("Can't create display\n");
46        return -1;
47    }
48
49    cw = cucul_create_canvas(cucul_get_canvas_width(cv), cucul_get_canvas_height(cv));
50    mask = cucul_create_canvas(cucul_get_canvas_width(cv), cucul_get_canvas_height(cv));
51
52    for(x = 0; x < 256; x++)
53    {
54        buffer[x] = (x << 16) | (x << 8) | (x<< 0);
55        buffer[x + 256] = (0xff << 16) |    (x << 8) | (0x00 << 0);
56        buffer[x + 512] = (0x00 << 16) | (0xff << 8) |    (x << 0);
57        buffer[x + 768] =    (x << 16) | (0x00 << 8) | (0xff << 0);
58    }
59
60    left = cucul_create_dither(32, 256, 4, 4 * 256,
61                               0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
62    right = cucul_create_dither(32, 256, 4, 4 * 256,
63                                0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
64    gam = cucul_get_dither_gamma(right);
65    caca_set_display_time(dp, 20000);
66
67    for(x = 0; ; x++)
68    {
69        int ret = caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0);
70
71        if(ret)
72        {
73            if(caca_get_event_key_ch(&ev) == CACA_KEY_LEFT)
74                gam /= 1.03;
75            else if(caca_get_event_key_ch(&ev) == CACA_KEY_RIGHT)
76                gam *= 1.03;
77            else if(caca_get_event_key_ch(&ev) == CACA_KEY_DOWN)
78                gam = 1.0;
79            else if(caca_get_event_key_ch(&ev) == CACA_KEY_ESCAPE)
80                break;
81        }
82
83        /* Resize the spare canvas, just in case the main one changed */
84        cucul_set_canvas_size(cw, cucul_get_canvas_width(cv), cucul_get_canvas_height(cv));
85        cucul_set_canvas_size(mask, cucul_get_canvas_width(cv), cucul_get_canvas_height(cv));
86
87        /* Draw the regular dither on the main canvas */
88        cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv),
89                            cucul_get_canvas_height(cv), left, buffer);
90
91        /* Draw the gamma-modified dither on the spare canvas */
92        cucul_set_dither_gamma(right, gam);
93        cucul_dither_bitmap(cw, 0, 0, cucul_get_canvas_width(cw),
94                            cucul_get_canvas_height(cw), right, buffer);
95
96        /* Draw something on the mask */
97        cucul_set_color_ansi(mask, CUCUL_LIGHTGRAY, CUCUL_BLACK);
98        cucul_clear_canvas(mask);
99        cucul_set_color_ansi(mask, CUCUL_WHITE, CUCUL_WHITE);
100        cucul_fill_ellipse(mask, (1.0 + sin(0.05 * (float)x))
101                                   * 0.5 * cucul_get_canvas_width(mask),
102                                 (1.0 + cos(0.05 * (float)x))
103                                   * 0.5 * cucul_get_canvas_height(mask),
104                                 cucul_get_canvas_width(mask) / 2,
105                                 cucul_get_canvas_height(mask) / 2, '#');
106
107        /* Blit the spare canvas onto the first one */
108        cucul_blit(cv, 0, 0, cw, mask);
109
110        cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
111        cucul_printf(cv, 2, 1,
112                     "gamma=%g - use arrows to change, Esc to quit", gam);
113
114        caca_refresh_display(dp);
115    }
116
117    cucul_free_dither(left);
118    cucul_free_dither(right);
119
120    caca_free_display(dp);
121    cucul_free_canvas(mask);
122    cucul_free_canvas(cw);
123    cucul_free_canvas(cv);
124
125    return 0;
126}
127
Note: See TracBrowser for help on using the repository browser.