[654] | 1 | /* |
---|
[2821] | 2 | * gamma libcaca gamma test program |
---|
[654] | 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
| 4 | * All Rights Reserved |
---|
| 5 | * |
---|
| 6 | * $Id: gamma.c 2821 2008-09-27 13:12:46Z sam $ |
---|
| 7 | * |
---|
[1462] | 8 | * This program is free software. It comes without any warranty, to |
---|
[1452] | 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 |
---|
[654] | 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | #include "config.h" |
---|
| 16 | |
---|
[670] | 17 | #if !defined(__KERNEL__) |
---|
| 18 | # include <stdio.h> |
---|
| 19 | # include <math.h> |
---|
| 20 | #endif |
---|
| 21 | |
---|
[654] | 22 | #include "caca.h" |
---|
| 23 | |
---|
[661] | 24 | uint32_t buffer[256 * 4]; |
---|
[654] | 25 | |
---|
[1048] | 26 | int main(int argc, char *argv[]) |
---|
[654] | 27 | { |
---|
[777] | 28 | caca_event_t ev; |
---|
[2821] | 29 | caca_canvas_t *cv, *cw, *mask; |
---|
[811] | 30 | caca_display_t *dp; |
---|
[2821] | 31 | caca_dither_t *left, *right; |
---|
[2036] | 32 | float gam; |
---|
[654] | 33 | int x; |
---|
| 34 | |
---|
[2821] | 35 | cv = caca_create_canvas(0, 0); |
---|
[1753] | 36 | if(cv == NULL) |
---|
| 37 | { |
---|
| 38 | printf("Can't created canvas\n"); |
---|
| 39 | return -1; |
---|
| 40 | } |
---|
[819] | 41 | dp = caca_create_display(cv); |
---|
[1753] | 42 | if(dp == NULL) |
---|
| 43 | { |
---|
| 44 | printf("Can't create display\n"); |
---|
| 45 | return -1; |
---|
| 46 | } |
---|
[654] | 47 | |
---|
[2821] | 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)); |
---|
[670] | 50 | |
---|
[654] | 51 | for(x = 0; x < 256; x++) |
---|
[661] | 52 | { |
---|
[654] | 53 | buffer[x] = (x << 16) | (x << 8) | (x<< 0); |
---|
[661] | 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 | } |
---|
[654] | 58 | |
---|
[2821] | 59 | left = caca_create_dither(32, 256, 4, 4 * 256, |
---|
[654] | 60 | 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0); |
---|
[2821] | 61 | right = caca_create_dither(32, 256, 4, 4 * 256, |
---|
[654] | 62 | 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0); |
---|
[2821] | 63 | gam = caca_get_dither_gamma(right); |
---|
[964] | 64 | caca_set_display_time(dp, 20000); |
---|
[654] | 65 | |
---|
[670] | 66 | for(x = 0; ; x++) |
---|
[659] | 67 | { |
---|
[811] | 68 | int ret = caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0); |
---|
[659] | 69 | |
---|
[681] | 70 | if(ret) |
---|
| 71 | { |
---|
[2049] | 72 | if(caca_get_event_key_ch(&ev) == CACA_KEY_LEFT) |
---|
[681] | 73 | gam /= 1.03; |
---|
[2049] | 74 | else if(caca_get_event_key_ch(&ev) == CACA_KEY_RIGHT) |
---|
[681] | 75 | gam *= 1.03; |
---|
[2049] | 76 | else if(caca_get_event_key_ch(&ev) == CACA_KEY_DOWN) |
---|
[681] | 77 | gam = 1.0; |
---|
[2049] | 78 | else if(caca_get_event_key_ch(&ev) == CACA_KEY_ESCAPE) |
---|
[681] | 79 | break; |
---|
| 80 | } |
---|
[659] | 81 | |
---|
[670] | 82 | /* Resize the spare canvas, just in case the main one changed */ |
---|
[2821] | 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)); |
---|
[670] | 85 | |
---|
[734] | 86 | /* Draw the regular dither on the main canvas */ |
---|
[2821] | 87 | caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv), |
---|
| 88 | caca_get_canvas_height(cv), left, buffer); |
---|
[659] | 89 | |
---|
[734] | 90 | /* Draw the gamma-modified dither on the spare canvas */ |
---|
[2821] | 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); |
---|
[659] | 94 | |
---|
[670] | 95 | /* Draw something on the mask */ |
---|
[2821] | 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), |
---|
[670] | 101 | (1.0 + cos(0.05 * (float)x)) |
---|
[2821] | 102 | * 0.5 * caca_get_canvas_height(mask), |
---|
| 103 | caca_get_canvas_width(mask) / 2, |
---|
| 104 | caca_get_canvas_height(mask) / 2, '#'); |
---|
[670] | 105 | |
---|
| 106 | /* Blit the spare canvas onto the first one */ |
---|
[2821] | 107 | caca_blit(cv, 0, 0, cw, mask); |
---|
[670] | 108 | |
---|
[2821] | 109 | caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE); |
---|
| 110 | caca_printf(cv, 2, 1, |
---|
[670] | 111 | "gamma=%g - use arrows to change, Esc to quit", gam); |
---|
[659] | 112 | |
---|
[819] | 113 | caca_refresh_display(dp); |
---|
[659] | 114 | } |
---|
| 115 | |
---|
[2821] | 116 | caca_free_dither(left); |
---|
| 117 | caca_free_dither(right); |
---|
[654] | 118 | |
---|
[819] | 119 | caca_free_display(dp); |
---|
[2821] | 120 | caca_free_canvas(mask); |
---|
| 121 | caca_free_canvas(cw); |
---|
| 122 | caca_free_canvas(cv); |
---|
[654] | 123 | |
---|
| 124 | return 0; |
---|
| 125 | } |
---|
| 126 | |
---|