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

Last change on this file since 2111 was 2101, checked in by Sam Hocevar, 13 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
RevLine 
[654]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 *
[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"
[858]16#include "common.h"
[654]17
[670]18#if !defined(__KERNEL__)
[1048]19#   if defined(HAVE_INTTYPES_H)
20#      include <inttypes.h>
21#   endif
[670]22#   include <stdio.h>
23#   include <math.h>
24#endif
25
[654]26#include "cucul.h"
27#include "caca.h"
28
[661]29uint32_t buffer[256 * 4];
[654]30
[1048]31int main(int argc, char *argv[])
[654]32{
[777]33    caca_event_t ev;
[811]34    cucul_canvas_t *cv, *cw, *mask;
35    caca_display_t *dp;
[777]36    cucul_dither_t *left, *right;
[2036]37    float gam;
[654]38    int x;
39
[813]40    cv = cucul_create_canvas(0, 0);
[1753]41    if(cv == NULL)
42    {
43        printf("Can't created canvas\n");
44        return -1;
45    }
[819]46    dp = caca_create_display(cv);
[1753]47    if(dp == NULL)
48    {
49        printf("Can't create display\n");
50        return -1;
51    }
[654]52
[813]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));
[670]55
[654]56    for(x = 0; x < 256; x++)
[661]57    {
[654]58        buffer[x] = (x << 16) | (x << 8) | (x<< 0);
[661]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    }
[654]63
[734]64    left = cucul_create_dither(32, 256, 4, 4 * 256,
[654]65                               0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
[734]66    right = cucul_create_dither(32, 256, 4, 4 * 256,
[654]67                                0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
[2036]68    gam = cucul_get_dither_gamma(right);
[964]69    caca_set_display_time(dp, 20000);
[654]70
[670]71    for(x = 0; ; x++)
[659]72    {
[811]73        int ret = caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0);
[659]74
[681]75        if(ret)
76        {
[2049]77            if(caca_get_event_key_ch(&ev) == CACA_KEY_LEFT)
[681]78                gam /= 1.03;
[2049]79            else if(caca_get_event_key_ch(&ev) == CACA_KEY_RIGHT)
[681]80                gam *= 1.03;
[2049]81            else if(caca_get_event_key_ch(&ev) == CACA_KEY_DOWN)
[681]82                gam = 1.0;
[2049]83            else if(caca_get_event_key_ch(&ev) == CACA_KEY_ESCAPE)
[681]84                break;
85        }
[659]86
[670]87        /* Resize the spare canvas, just in case the main one changed */
[813]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));
[670]90
[734]91        /* Draw the regular dither on the main canvas */
[816]92        cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv),
93                            cucul_get_canvas_height(cv), left, buffer);
[659]94
[734]95        /* Draw the gamma-modified dither on the spare canvas */
96        cucul_set_dither_gamma(right, gam);
[816]97        cucul_dither_bitmap(cw, 0, 0, cucul_get_canvas_width(cw),
98                            cucul_get_canvas_height(cw), right, buffer);
[659]99
[670]100        /* Draw something on the mask */
[1267]101        cucul_set_color_ansi(mask, CUCUL_LIGHTGRAY, CUCUL_BLACK);
[832]102        cucul_clear_canvas(mask);
[1267]103        cucul_set_color_ansi(mask, CUCUL_WHITE, CUCUL_WHITE);
[670]104        cucul_fill_ellipse(mask, (1.0 + sin(0.05 * (float)x))
[813]105                                   * 0.5 * cucul_get_canvas_width(mask),
[670]106                                 (1.0 + cos(0.05 * (float)x))
[813]107                                   * 0.5 * cucul_get_canvas_height(mask),
108                                 cucul_get_canvas_width(mask) / 2,
[1330]109                                 cucul_get_canvas_height(mask) / 2, '#');
[670]110
111        /* Blit the spare canvas onto the first one */
[811]112        cucul_blit(cv, 0, 0, cw, mask);
[670]113
[1267]114        cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
[811]115        cucul_printf(cv, 2, 1,
[670]116                     "gamma=%g - use arrows to change, Esc to quit", gam);
[659]117
[819]118        caca_refresh_display(dp);
[659]119    }
120
[734]121    cucul_free_dither(left);
122    cucul_free_dither(right);
[654]123
[819]124    caca_free_display(dp);
[2075]125    cucul_free_canvas(mask);
126    cucul_free_canvas(cw);
[813]127    cucul_free_canvas(cv);
[654]128
129    return 0;
130}
131
Note: See TracBrowser for help on using the repository browser.