Changeset 1228 for toilet/trunk


Ignore:
Timestamp:
Oct 25, 2006, 6:06:19 PM (16 years ago)
Author:
Sam Hocevar
Message:
  • Added filter_flip, filter_flop and filter_rotate.
  • Add a -F flag to specify filters to apply. Can be specified more than once, and filters can be chained using ":", eg. "-F flip:gay".
Location:
toilet/trunk/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • toilet/trunk/src/filter.c

    r1227 r1228  
    2121#   include <inttypes.h>
    2222#endif
     23#include <string.h>
     24#include <stdio.h>
     25#include <stdlib.h>
    2326#include <cucul.h>
    2427
     28#include "toilet.h"
    2529#include "filter.h"
    2630
    27 void filter_autocrop(cucul_canvas_t *cv)
     31static void filter_crop(cucul_canvas_t *);
     32static void filter_gay(cucul_canvas_t *);
     33static void filter_metal(cucul_canvas_t *);
     34static void filter_flip(cucul_canvas_t *);
     35static void filter_flop(cucul_canvas_t *);
     36static void filter_rotate(cucul_canvas_t *);
     37
     38struct
     39{
     40    char const *name;
     41    void (*function)(cucul_canvas_t *);
     42}
     43const lookup[] =
     44{
     45    { "crop", filter_crop },
     46    { "gay", filter_gay },
     47    { "metal", filter_metal },
     48    { "flip", filter_flip },
     49    { "flop", filter_flop },
     50    { "rotate", filter_rotate },
     51};
     52
     53int filter_add(context_t *cx, char const *filter)
     54{
     55    unsigned int n;
     56    int i;
     57
     58    for(;;)
     59    {
     60        while(*filter == ':')
     61            filter++;
     62
     63        if(*filter == '\0')
     64            break;
     65
     66        for(i = sizeof(lookup) / sizeof(lookup[0]); i--; )
     67            if(!strncmp(filter, lookup[i].name, strlen(lookup[i].name)))
     68                break;
     69
     70        n = strlen(lookup[i].name);
     71
     72        if(i == -1 || (filter[n] != ':' && filter[n] != '\0'))
     73        {
     74            fprintf(stderr, "unknown filter near `%s'\n", filter);
     75            return -1;
     76        }
     77
     78        if((cx->nfilters % 16) == 0)
     79            cx->filters = realloc(cx->filters, (cx->nfilters + 16)
     80                                                 * sizeof(lookup[0].function));
     81        cx->filters[cx->nfilters] = lookup[i].function;
     82        cx->nfilters++;
     83
     84        filter += n;
     85    }
     86
     87    return 0;
     88}
     89
     90int filter_do(context_t *cx)
     91{
     92    unsigned int i;
     93
     94    for(i = 0; i < cx->nfilters; i++)
     95        cx->filters[i](cx->cv);
     96
     97    return 0;
     98}
     99
     100static void filter_crop(cucul_canvas_t *cv)
    28101{
    29102    unsigned int x, y, w, h;
     
    59132}
    60133
    61 void filter_metal(cucul_canvas_t *cv)
     134static void filter_metal(cucul_canvas_t *cv)
    62135{
    63136    static unsigned char const palette[] =
     
    89162}
    90163
    91 void filter_gay(cucul_canvas_t *cv)
     164static void filter_gay(cucul_canvas_t *cv)
    92165{
    93166    static unsigned char const rainbow[] =
     
    118191}
    119192
     193static void filter_flip(cucul_canvas_t *cv)
     194{
     195    cucul_flip(cv);
     196}
     197
     198static void filter_flop(cucul_canvas_t *cv)
     199{
     200    cucul_flop(cv);
     201}
     202
     203static void filter_rotate(cucul_canvas_t *cv)
     204{
     205    cucul_rotate(cv);
     206}
     207
  • toilet/trunk/src/filter.h

    r1227 r1228  
    1616 */
    1717
    18 extern void filter_autocrop(cucul_canvas_t *);
    19 extern void filter_metal(cucul_canvas_t *);
    20 extern void filter_gay(cucul_canvas_t *);
     18extern int filter_add(context_t *, char const *);
     19extern int filter_do(context_t *);
    2120
  • toilet/trunk/src/io.c

    r1206 r1228  
    4646};
    4747
    48 TOIFILE *toiopen(const char *path, const char *mode)
     48TOIFILE *toiopen(char const *path, const char *mode)
    4949{
    5050    TOIFILE *toif = malloc(sizeof(*toif));
  • toilet/trunk/src/main.c

    r1227 r1228  
    5151    int i, j, ret;
    5252
    53     unsigned int flag_gay = 0;
    54     unsigned int flag_metal = 0;
    5553    int infocode = -1;
    5654
     
    6058
    6159    cx->term_width = 80;
     60
     61    cx->filters = NULL;
     62    cx->nfilters = 0;
    6263
    6364#if defined(HAVE_GETOPT_H)
     
    7475            { "width", 1, NULL, 'w' },
    7576            { "termwidth", 0, NULL, 't' },
     77            { "filter", 1, NULL, 'F' },
    7678            { "gay", 0, NULL, 'g' },
    7779            { "metal", 0, NULL, 'm' },
     
    8385        };
    8486
    85         int c = getopt_long(argc, argv, "d:f:I:w:ghimtv",
     87        int c = getopt_long(argc, argv, "f:d:w:tF:gmihI:v",
    8688                            long_options, &option_index);
    8789#   else
    8890#       define MOREINFO "Try `%s -h' for more information.\n"
    89         int c = getopt(argc, argv, "d:f:I:w:ghimtv");
     91        int c = getopt(argc, argv, "f:d:w:tF:gmihI:v");
    9092#   endif
    9193        if(c == -1)
     
    109111            cx->dir = optarg;
    110112            break;
     113        case 'F': /* --filter */
     114            if(filter_add(cx, optarg))
     115                return -1;
     116            break;
    111117        case 'g': /* --gay */
    112             flag_gay = 1;
     118            filter_add(cx, "gay");
    113119            break;
    114120        case 'm': /* --metal */
    115             flag_metal = 1;
     121            filter_add(cx, "metal");
    116122            break;
    117123        case 'w': /* --width */
     
    221227
    222228    /* Apply optional effects to our string */
    223     if(!strcasecmp(cx->font, "mono9"))
    224         filter_autocrop(cx->cv);
    225     if(flag_metal)
    226         filter_metal(cx->cv);
    227     if(flag_gay)
    228         filter_gay(cx->cv);
     229    filter_do(cx);
    229230
    230231    /* Output char */
     
    241242#if defined(HAVE_GETOPT_H)
    242243#   define USAGE \
    243     "Usage: toilet [ -ghimtv ] [ -d fontdirectory ]\n" \
     244    "Usage: toilet [ -ghimtvF ] [ -d fontdirectory ]\n" \
    244245    "              [ -f fontfile ] [ -w outputwidth ]\n" \
    245246    "              [ -I infocode ] [ message ]\n"
     
    275276    printf("  -w, --width <width>      set output width\n");
    276277    printf("  -t, --termwidth          adapt to terminal's width\n");
     278    printf("  -F, --filter             apply one or several filters to the text\n");
    277279    printf("  -g, --gay                add a rainbow effect to the text\n");
    278280    printf("  -m, --metal              add a metal effect to the text\n");
     
    286288    printf("  -w <width>       set output width\n");
    287289    printf("  -t               adapt to terminal's width\n");
     290    printf("  -F               apply one or several filters to the text\n");
    288291    printf("  -g               add a rainbow effect to the text\n");
    289292    printf("  -m               add a metal effect to the text\n");
  • toilet/trunk/src/toilet.h

    r1196 r1228  
    2727    unsigned int w, h, ew, eh, x, y;
    2828
    29     /* Methods */
     29    /* Render methods */
    3030    int (*feed)(struct toilet_context *, uint32_t);
    3131    int (*end)(struct toilet_context *);
     
    4444    cucul_canvas_t *image;
    4545    unsigned int *lookup;
     46
     47    /* Render filters */
     48    void (**filters)(cucul_canvas_t *);
     49    unsigned int nfilters;
    4650};
    4751
Note: See TracChangeset for help on using the changeset viewer.