source: toilet/trunk/src/filters.c @ 1100

Last change on this file since 1100 was 1100, checked in by Sam Hocevar, 15 years ago
  • Split main.c into main.c/render.c/filters.c.
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1/*
2 *  TOIlet        The Other Implementation’s letters
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: filters.c 1100 2006-09-23 19:54:20Z 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#endif
19#include <cucul.h>
20
21#include "filters.h"
22
23void filter_autocrop(cucul_canvas_t *cv)
24{
25    unsigned int x, y, w, h;
26    unsigned int xmin, xmax, ymin, ymax;
27
28    xmin = w = cucul_get_canvas_width(cv);
29    xmax = 0;
30    ymin = h = cucul_get_canvas_height(cv);
31    ymax = 0;
32
33    for(y = 0; y < h; y++)
34        for(x = 0; x < w; x++)
35    {
36        unsigned long int ch = cucul_getchar(cv, x, y);
37        if(ch != (unsigned char)' ')
38        {
39            if(x < xmin)
40                xmin = x;
41            if(x > xmax)
42                xmax = x;
43            if(y < ymin)
44                ymin = y;
45            if(y > ymax)
46                ymax = y;
47        }
48    }
49
50    cucul_set_canvas_boundaries(cv, xmin, ymin,
51                                xmax - xmin + 1, ymax - ymin + 1);
52}
53
54void filter_metal(cucul_canvas_t *cv)
55{
56    static struct
57    {
58        char ch[6];
59        unsigned char fg, bg;
60    }
61    const palette[] =
62    {
63        { " ", CUCUL_COLOR_LIGHTBLUE, CUCUL_COLOR_LIGHTBLUE },
64        { "░", CUCUL_COLOR_BLUE, CUCUL_COLOR_LIGHTBLUE },
65        { "▒", CUCUL_COLOR_BLUE, CUCUL_COLOR_LIGHTBLUE },
66        { "░", CUCUL_COLOR_LIGHTBLUE, CUCUL_COLOR_BLUE },
67        { " ", CUCUL_COLOR_BLUE, CUCUL_COLOR_BLUE },
68        { " ", CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_LIGHTGRAY },
69        { "░", CUCUL_COLOR_DARKGRAY, CUCUL_COLOR_LIGHTGRAY },
70        { "▒", CUCUL_COLOR_DARKGRAY, CUCUL_COLOR_LIGHTGRAY },
71        { "░", CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_DARKGRAY },
72        { " ", CUCUL_COLOR_DARKGRAY, CUCUL_COLOR_DARKGRAY },
73    };
74
75    unsigned int x, y, w, h;
76
77    w = cucul_get_canvas_width(cv);
78    h = cucul_get_canvas_height(cv);
79
80    for(y = 0; y < h; y++)
81        for(x = 0; x < w; x++)
82    {
83        int i;
84
85        if(cucul_getchar(cv, x, y) == (unsigned char)' ')
86            continue;
87
88        i = y * 10 / h;
89        cucul_set_color(cv, palette[i].fg, palette[i].bg);
90        cucul_putstr(cv, x, y, palette[i].ch);
91    }
92}
93
94void filter_gay(cucul_canvas_t *cv)
95{
96    static unsigned char const rainbow[] =
97    {
98        CUCUL_COLOR_LIGHTMAGENTA,
99        CUCUL_COLOR_LIGHTRED,
100        CUCUL_COLOR_YELLOW,
101        CUCUL_COLOR_LIGHTGREEN,
102        CUCUL_COLOR_LIGHTCYAN,
103        CUCUL_COLOR_LIGHTBLUE,
104    };
105    unsigned int x, y, w, h;
106
107    w = cucul_get_canvas_width(cv);
108    h = cucul_get_canvas_height(cv);
109
110    for(y = 0; y < h; y++)
111        for(x = 0; x < w; x++)
112    {
113        unsigned long int ch = cucul_getchar(cv, x, y);
114        if(ch != (unsigned char)' ')
115        {
116            cucul_set_color(cv, rainbow[(x / 2 + y) % 6],
117                                CUCUL_COLOR_TRANSPARENT);
118            cucul_putchar(cv, x, y, ch);
119        }
120    }
121}
122
Note: See TracBrowser for help on using the repository browser.