source: libcaca/trunk/test/gamma.c @ 813

Last change on this file since 813 was 813, checked in by Sam Hocevar, 15 years ago
  • Renamed main canvas-related functions so that they have "canvas" in their name, eg. cucul_create() -> cucul_create_canvas() etc.
  • Moved buffer-related functions to cucul/buffer.c.
  • Property svn:keywords set to Id
File size: 3.8 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 813 2006-04-18 15:54:33Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14#include "config.h"
15
16#if defined(HAVE_INTTYPES_H)
17#   include <inttypes.h>
18#else
19typedef unsigned char uint8_t;
20typedef unsigned short uint16_t;
21typedef unsigned int uint32_t;
22#endif
23
24#if !defined(__KERNEL__)
25#   include <stdio.h>
26#   include <math.h>
27#endif
28
29#include "cucul.h"
30#include "caca.h"
31
32uint32_t buffer[256 * 4];
33
34int main(void)
35{
36    caca_event_t ev;
37    cucul_canvas_t *cv, *cw, *mask;
38    caca_display_t *dp;
39    cucul_dither_t *left, *right;
40    float gam = 1.0;
41    int x;
42
43    cv = cucul_create_canvas(0, 0);
44    dp = caca_attach(cv);
45
46    cw = cucul_create_canvas(cucul_get_canvas_width(cv), cucul_get_canvas_height(cv));
47    mask = cucul_create_canvas(cucul_get_canvas_width(cv), cucul_get_canvas_height(cv));
48
49    for(x = 0; x < 256; x++)
50    {
51        buffer[x] = (x << 16) | (x << 8) | (x<< 0);
52        buffer[x + 256] = (0xff << 16) |    (x << 8) | (0x00 << 0);
53        buffer[x + 512] = (0x00 << 16) | (0xff << 8) |    (x << 0);
54        buffer[x + 768] =    (x << 16) | (0x00 << 8) | (0xff << 0);
55    }
56
57    left = cucul_create_dither(32, 256, 4, 4 * 256,
58                               0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
59    right = cucul_create_dither(32, 256, 4, 4 * 256,
60                                0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
61    caca_set_delay(dp, 20000);
62
63    for(x = 0; ; x++)
64    {
65        int ret = caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0);
66
67        if(ret)
68        {
69            if(ev.data.key.ch == CACA_KEY_LEFT)
70                gam /= 1.03;
71            else if(ev.data.key.ch == CACA_KEY_RIGHT)
72                gam *= 1.03;
73            else if(ev.data.key.ch == CACA_KEY_DOWN)
74                gam = 1.0;
75            else if(ev.data.key.ch == CACA_KEY_ESCAPE)
76                break;
77        }
78
79        /* Resize the spare canvas, just in case the main one changed */
80        cucul_set_canvas_size(cw, cucul_get_canvas_width(cv), cucul_get_canvas_height(cv));
81        cucul_set_canvas_size(mask, cucul_get_canvas_width(cv), cucul_get_canvas_height(cv));
82
83        /* Draw the regular dither on the main canvas */
84        cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv) - 1,
85                            cucul_get_canvas_height(cv) - 1, left, buffer);
86
87        /* Draw the gamma-modified dither on the spare canvas */
88        cucul_set_dither_gamma(right, gam);
89        cucul_dither_bitmap(cw, 0, 0, cucul_get_canvas_width(cw) - 1,
90                            cucul_get_canvas_height(cw) - 1, right, buffer);
91
92        /* Draw something on the mask */
93        cucul_clear(mask);
94        cucul_set_color(mask, CUCUL_COLOR_WHITE, CUCUL_COLOR_WHITE);
95        cucul_fill_ellipse(mask, (1.0 + sin(0.05 * (float)x))
96                                   * 0.5 * cucul_get_canvas_width(mask),
97                                 (1.0 + cos(0.05 * (float)x))
98                                   * 0.5 * cucul_get_canvas_height(mask),
99                                 cucul_get_canvas_width(mask) / 2,
100                                 cucul_get_canvas_height(mask) / 2, "#");
101
102        /* Blit the spare canvas onto the first one */
103        cucul_blit(cv, 0, 0, cw, mask);
104
105        cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
106        cucul_printf(cv, 2, 1,
107                     "gamma=%g - use arrows to change, Esc to quit", gam);
108
109        caca_display(dp);
110    }
111
112    cucul_free_dither(left);
113    cucul_free_dither(right);
114
115    caca_detach(dp);
116    cucul_free_canvas(cv);
117
118    return 0;
119}
120
Note: See TracBrowser for help on using the repository browser.