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

Last change on this file since 1109 was 1109, checked in by Sam Hocevar, 15 years ago
  • Do not try to autocrop a canvas that has only spaces.
  • 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 1109 2006-09-26 21:39:07Z 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    if(xmax < xmin || ymax < ymin)
55        return;
56
57    cucul_set_canvas_boundaries(cv, xmin, ymin,
58                                xmax - xmin + 1, ymax - ymin + 1);
59}
60
61void filter_metal(cucul_canvas_t *cv)
62{
63    static struct
64    {
65        char ch[6];
66        unsigned char fg, bg;
67    }
68    const palette[] =
69    {
70        { " ", CUCUL_COLOR_LIGHTBLUE, CUCUL_COLOR_LIGHTBLUE },
71        { "░", CUCUL_COLOR_BLUE, CUCUL_COLOR_LIGHTBLUE },
72        { "▒", CUCUL_COLOR_BLUE, CUCUL_COLOR_LIGHTBLUE },
73        { "░", CUCUL_COLOR_LIGHTBLUE, CUCUL_COLOR_BLUE },
74        { " ", CUCUL_COLOR_BLUE, CUCUL_COLOR_BLUE },
75        { " ", CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_LIGHTGRAY },
76        { "░", CUCUL_COLOR_DARKGRAY, CUCUL_COLOR_LIGHTGRAY },
77        { "▒", CUCUL_COLOR_DARKGRAY, CUCUL_COLOR_LIGHTGRAY },
78        { "░", CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_DARKGRAY },
79        { " ", CUCUL_COLOR_DARKGRAY, CUCUL_COLOR_DARKGRAY },
80    };
81
82    unsigned int x, y, w, h;
83
84    w = cucul_get_canvas_width(cv);
85    h = cucul_get_canvas_height(cv);
86
87    for(y = 0; y < h; y++)
88        for(x = 0; x < w; x++)
89    {
90        int i;
91
92        if(cucul_getchar(cv, x, y) == (unsigned char)' ')
93            continue;
94
95        i = y * 10 / h;
96        cucul_set_color(cv, palette[i].fg, palette[i].bg);
97        cucul_putstr(cv, x, y, palette[i].ch);
98    }
99}
100
101void filter_gay(cucul_canvas_t *cv)
102{
103    static unsigned char const rainbow[] =
104    {
105        CUCUL_COLOR_LIGHTMAGENTA,
106        CUCUL_COLOR_LIGHTRED,
107        CUCUL_COLOR_YELLOW,
108        CUCUL_COLOR_LIGHTGREEN,
109        CUCUL_COLOR_LIGHTCYAN,
110        CUCUL_COLOR_LIGHTBLUE,
111    };
112    unsigned int x, y, w, h;
113
114    w = cucul_get_canvas_width(cv);
115    h = cucul_get_canvas_height(cv);
116
117    for(y = 0; y < h; y++)
118        for(x = 0; x < w; x++)
119    {
120        unsigned long int ch = cucul_getchar(cv, x, y);
121        if(ch != (unsigned char)' ')
122        {
123            cucul_set_color(cv, rainbow[(x / 2 + y) % 6],
124                                CUCUL_COLOR_TRANSPARENT);
125            cucul_putchar(cv, x, y, ch);
126        }
127    }
128}
129
Note: See TracBrowser for help on using the repository browser.