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

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