source: libcaca/trunk/examples/gamma.c @ 2101

Last change on this file since 2101 was 2101, checked in by Sam Hocevar, 12 years ago
  • Rename "test" to "examples". The former will become a real testsuite.
  • Autodetect zzuf in the configure step. It might become handy.
  • Property svn:keywords set to Id
File size: 4.2 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 2101 2007-11-30 23:48:39Z 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#include "common.h"
17
18#if !defined(__KERNEL__)
19#   if defined(HAVE_INTTYPES_H)
20#      include <inttypes.h>
21#   endif
22#   include <stdio.h>
23#   include <math.h>
24#endif
25
26#include "cucul.h"
27#include "caca.h"
28
29uint32_t buffer[256 * 4];
30
31int main(int argc, char *argv[])
32{
33    caca_event_t ev;
34    cucul_canvas_t *cv, *cw, *mask;
35    caca_display_t *dp;
36    cucul_dither_t *left, *right;
37    float gam;
38    int x;
39
40    cv = cucul_create_canvas(0, 0);
41    if(cv == NULL)
42    {
43        printf("Can't created canvas\n");
44        return -1;
45    }
46    dp = caca_create_display(cv);
47    if(dp == NULL)
48    {
49        printf("Can't create display\n");
50        return -1;
51    }
52
53    cw = cucul_create_canvas(cucul_get_canvas_width(cv), cucul_get_canvas_height(cv));
54    mask = cucul_create_canvas(cucul_get_canvas_width(cv), cucul_get_canvas_height(cv));
55
56    for(x = 0; x < 256; x++)
57    {
58        buffer[x] = (x << 16) | (x << 8) | (x<< 0);
59        buffer[x + 256] = (0xff << 16) |    (x << 8) | (0x00 << 0);
60        buffer[x + 512] = (0x00 << 16) | (0xff << 8) |    (x << 0);
61        buffer[x + 768] =    (x << 16) | (0x00 << 8) | (0xff << 0);
62    }
63
64    left = cucul_create_dither(32, 256, 4, 4 * 256,
65                               0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
66    right = cucul_create_dither(32, 256, 4, 4 * 256,
67                                0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
68    gam = cucul_get_dither_gamma(right);
69    caca_set_display_time(dp, 20000);
70
71    for(x = 0; ; x++)
72    {
73        int ret = caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0);
74
75        if(ret)
76        {
77            if(caca_get_event_key_ch(&ev) == CACA_KEY_LEFT)
78                gam /= 1.03;
79            else if(caca_get_event_key_ch(&ev) == CACA_KEY_RIGHT)
80                gam *= 1.03;
81            else if(caca_get_event_key_ch(&ev) == CACA_KEY_DOWN)
82                gam = 1.0;
83            else if(caca_get_event_key_ch(&ev) == CACA_KEY_ESCAPE)
84                break;
85        }
86
87        /* Resize the spare canvas, just in case the main one changed */
88        cucul_set_canvas_size(cw, cucul_get_canvas_width(cv), cucul_get_canvas_height(cv));
89        cucul_set_canvas_size(mask, cucul_get_canvas_width(cv), cucul_get_canvas_height(cv));
90
91        /* Draw the regular dither on the main canvas */
92        cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv),
93                            cucul_get_canvas_height(cv), left, buffer);
94
95        /* Draw the gamma-modified dither on the spare canvas */
96        cucul_set_dither_gamma(right, gam);
97        cucul_dither_bitmap(cw, 0, 0, cucul_get_canvas_width(cw),
98                            cucul_get_canvas_height(cw), right, buffer);
99
100        /* Draw something on the mask */
101        cucul_set_color_ansi(mask, CUCUL_LIGHTGRAY, CUCUL_BLACK);
102        cucul_clear_canvas(mask);
103        cucul_set_color_ansi(mask, CUCUL_WHITE, CUCUL_WHITE);
104        cucul_fill_ellipse(mask, (1.0 + sin(0.05 * (float)x))
105                                   * 0.5 * cucul_get_canvas_width(mask),
106                                 (1.0 + cos(0.05 * (float)x))
107                                   * 0.5 * cucul_get_canvas_height(mask),
108                                 cucul_get_canvas_width(mask) / 2,
109                                 cucul_get_canvas_height(mask) / 2, '#');
110
111        /* Blit the spare canvas onto the first one */
112        cucul_blit(cv, 0, 0, cw, mask);
113
114        cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
115        cucul_printf(cv, 2, 1,
116                     "gamma=%g - use arrows to change, Esc to quit", gam);
117
118        caca_refresh_display(dp);
119    }
120
121    cucul_free_dither(left);
122    cucul_free_dither(right);
123
124    caca_free_display(dp);
125    cucul_free_canvas(mask);
126    cucul_free_canvas(cw);
127    cucul_free_canvas(cv);
128
129    return 0;
130}
131
Note: See TracBrowser for help on using the repository browser.