1 | /* |
---|
2 | * gamma libcaca gamma test program |
---|
3 | * Copyright (c) 2006 Sam Hocevar <sam@hocevar.net> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: gamma.c 4148 2009-12-19 14:38:38Z 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 "caca.h" |
---|
23 | |
---|
24 | uint32_t buffer[256 * 4]; |
---|
25 | |
---|
26 | int main(int argc, char *argv[]) |
---|
27 | { |
---|
28 | caca_event_t ev; |
---|
29 | caca_canvas_t *cv, *cw, *mask; |
---|
30 | caca_display_t *dp; |
---|
31 | caca_dither_t *left, *right; |
---|
32 | float gam; |
---|
33 | int x; |
---|
34 | |
---|
35 | cv = caca_create_canvas(0, 0); |
---|
36 | if(cv == NULL) |
---|
37 | { |
---|
38 | printf("Can't created canvas\n"); |
---|
39 | return -1; |
---|
40 | } |
---|
41 | dp = caca_create_display(cv); |
---|
42 | if(dp == NULL) |
---|
43 | { |
---|
44 | printf("Can't create display\n"); |
---|
45 | return -1; |
---|
46 | } |
---|
47 | |
---|
48 | cw = caca_create_canvas(caca_get_canvas_width(cv), caca_get_canvas_height(cv)); |
---|
49 | mask = caca_create_canvas(caca_get_canvas_width(cv), caca_get_canvas_height(cv)); |
---|
50 | |
---|
51 | for(x = 0; x < 256; x++) |
---|
52 | { |
---|
53 | buffer[x] = (x << 16) | (x << 8) | (x<< 0); |
---|
54 | buffer[x + 256] = (0xff << 16) | (x << 8) | (0x00 << 0); |
---|
55 | buffer[x + 512] = (0x00 << 16) | (0xff << 8) | (x << 0); |
---|
56 | buffer[x + 768] = (x << 16) | (0x00 << 8) | (0xff << 0); |
---|
57 | } |
---|
58 | |
---|
59 | left = caca_create_dither(32, 256, 4, 4 * 256, |
---|
60 | 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0); |
---|
61 | right = caca_create_dither(32, 256, 4, 4 * 256, |
---|
62 | 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0); |
---|
63 | gam = caca_get_dither_gamma(right); |
---|
64 | caca_set_display_time(dp, 20000); |
---|
65 | |
---|
66 | for(x = 0; ; x++) |
---|
67 | { |
---|
68 | int ret = caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0); |
---|
69 | |
---|
70 | if(ret) |
---|
71 | { |
---|
72 | if(caca_get_event_key_ch(&ev) == CACA_KEY_LEFT) |
---|
73 | gam /= 1.03; |
---|
74 | else if(caca_get_event_key_ch(&ev) == CACA_KEY_RIGHT) |
---|
75 | gam *= 1.03; |
---|
76 | else if(caca_get_event_key_ch(&ev) == CACA_KEY_DOWN) |
---|
77 | gam = 1.0; |
---|
78 | else if(caca_get_event_key_ch(&ev) == CACA_KEY_ESCAPE) |
---|
79 | break; |
---|
80 | } |
---|
81 | |
---|
82 | /* Resize the spare canvas, just in case the main one changed */ |
---|
83 | caca_set_canvas_size(cw, caca_get_canvas_width(cv), caca_get_canvas_height(cv)); |
---|
84 | caca_set_canvas_size(mask, caca_get_canvas_width(cv), caca_get_canvas_height(cv)); |
---|
85 | |
---|
86 | /* Draw the regular dither on the main canvas */ |
---|
87 | caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv), |
---|
88 | caca_get_canvas_height(cv), left, buffer); |
---|
89 | |
---|
90 | /* Draw the gamma-modified dither on the spare canvas */ |
---|
91 | caca_set_dither_gamma(right, gam); |
---|
92 | caca_dither_bitmap(cw, 0, 0, caca_get_canvas_width(cw), |
---|
93 | caca_get_canvas_height(cw), right, buffer); |
---|
94 | |
---|
95 | /* Draw something on the mask */ |
---|
96 | caca_set_color_ansi(mask, CACA_LIGHTGRAY, CACA_BLACK); |
---|
97 | caca_clear_canvas(mask); |
---|
98 | caca_set_color_ansi(mask, CACA_WHITE, CACA_WHITE); |
---|
99 | caca_fill_ellipse(mask, (1.0 + sin(0.05 * (float)x)) |
---|
100 | * 0.5 * caca_get_canvas_width(mask), |
---|
101 | (1.0 + cos(0.05 * (float)x)) |
---|
102 | * 0.5 * caca_get_canvas_height(mask), |
---|
103 | caca_get_canvas_width(mask) / 2, |
---|
104 | caca_get_canvas_height(mask) / 2, '#'); |
---|
105 | |
---|
106 | /* Blit the spare canvas onto the first one */ |
---|
107 | caca_blit(cv, 0, 0, cw, mask); |
---|
108 | |
---|
109 | caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE); |
---|
110 | caca_printf(cv, 2, 1, |
---|
111 | "gamma=%g - use arrows to change, Esc to quit", gam); |
---|
112 | |
---|
113 | caca_refresh_display(dp); |
---|
114 | } |
---|
115 | |
---|
116 | caca_free_dither(left); |
---|
117 | caca_free_dither(right); |
---|
118 | |
---|
119 | caca_free_display(dp); |
---|
120 | caca_free_canvas(mask); |
---|
121 | caca_free_canvas(cw); |
---|
122 | caca_free_canvas(cv); |
---|
123 | |
---|
124 | return 0; |
---|
125 | } |
---|
126 | |
---|