Debian Codegraph @

examples/gamma.c

raw
1 /*
2 * gamma libcaca gamma test program
3 * Copyright (c) 2006-2012 Sam Hocevar <[email protected]>
4 * All Rights Reserved
5 *
6 * This program is free software. It comes without any warranty, to
7 * the extent permitted by applicable law. You can redistribute it
8 * and/or modify it under the terms of the Do What the Fuck You Want
9 * to Public License, Version 2, as published by Sam Hocevar. See
10 * http://www.wtfpl.net/ for more details.
11 */
12
13 #include "config.h"
14
15 #if !defined(__KERNEL__)
16 # include <stdio.h>
17 # include <math.h>
18 #endif
19
20 #include "caca.h"
21
22 uint32_t buffer[256 * 4];
23
24 int main(int argc, char *argv[])
25 {
26 caca_event_t ev;
27 caca_canvas_t *cv, *cw, *mask;
28 caca_display_t *dp;
29 caca_dither_t *left, *right;
30 float gam;
31 int x;
32
33 cv = caca_create_canvas(0, 0);
34 if(cv == NULL)
35 {
36 printf("Can't created canvas\n");
37 return -1;
38 }
39 dp = caca_create_display(cv);
40 if(dp == NULL)
41 {
42 printf("Can't create display\n");
43 return -1;
44 }
45
46 cw = caca_create_canvas(caca_get_canvas_width(cv), caca_get_canvas_height(cv));
47 mask = caca_create_canvas(caca_get_canvas_width(cv), caca_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 = caca_create_dither(32, 256, 4, 4 * 256,
58 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
59 right = caca_create_dither(32, 256, 4, 4 * 256,
60 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
61 gam = caca_get_dither_gamma(right);
62 caca_set_display_time(dp, 20000);
63
64 for(x = 0; ; x++)
65 {
66 int ret = caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0);
67
68 if(ret)
69 {
70 if(caca_get_event_key_ch(&ev) == CACA_KEY_LEFT)
71 gam /= 1.03;
72 else if(caca_get_event_key_ch(&ev) == CACA_KEY_RIGHT)
73 gam *= 1.03;
74 else if(caca_get_event_key_ch(&ev) == CACA_KEY_DOWN)
75 gam = 1.0;
76 else if(caca_get_event_key_ch(&ev) == CACA_KEY_ESCAPE)
77 break;
78 }
79
80 /* Resize the spare canvas, just in case the main one changed */
81 caca_set_canvas_size(cw, caca_get_canvas_width(cv), caca_get_canvas_height(cv));
82 caca_set_canvas_size(mask, caca_get_canvas_width(cv), caca_get_canvas_height(cv));
83
84 /* Draw the regular dither on the main canvas */
85 caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv),
86 caca_get_canvas_height(cv), left, buffer);
87
88 /* Draw the gamma-modified dither on the spare canvas */
89 caca_set_dither_gamma(right, gam);
90 caca_dither_bitmap(cw, 0, 0, caca_get_canvas_width(cw),
91 caca_get_canvas_height(cw), right, buffer);
92
93 /* Draw something on the mask */
94 caca_set_color_ansi(mask, CACA_LIGHTGRAY, CACA_BLACK);
95 caca_clear_canvas(mask);
96 caca_set_color_ansi(mask, CACA_WHITE, CACA_WHITE);
97 caca_fill_ellipse(mask, (1.0 + sin(0.05 * (float)x))
98 * 0.5 * caca_get_canvas_width(mask),
99 (1.0 + cos(0.05 * (float)x))
100 * 0.5 * caca_get_canvas_height(mask),
101 caca_get_canvas_width(mask) / 2,
102 caca_get_canvas_height(mask) / 2, '#');
103
104 /* Blit the spare canvas onto the first one */
105 caca_blit(cv, 0, 0, cw, mask);
106
107 caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
108 caca_printf(cv, 2, 1,
109 "gamma=%g - use arrows to change, Esc to quit", gam);
110
111 caca_refresh_display(dp);
112 }
113
114 caca_free_dither(left);
115 caca_free_dither(right);
116
117 caca_free_display(dp);
118 caca_free_canvas(mask);
119 caca_free_canvas(cw);
120 caca_free_canvas(cv);
121
122 return 0;
123 }
124