Changeset 4865 for libcaca


Ignore:
Timestamp:
Mar 30, 2013, 7:39:42 PM (6 years ago)
Author:
Sam Hocevar
Message:

codec: add import code for .BIN binary ANSI files.

Location:
libcaca/trunk/caca/codec
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/codec/codec.h

    r4859 r4865  
    1414ssize_t _import_text(caca_canvas_t *, void const *, size_t);
    1515ssize_t _import_ansi(caca_canvas_t *, void const *, size_t, int);
     16ssize_t _import_bin(caca_canvas_t *, void const *, size_t);
    1617
    1718void *_export_ansi(caca_canvas_t const *, size_t *);
  • libcaca/trunk/caca/codec/import.c

    r4859 r4865  
    5555 *  - \c "ansi": import ANSI files.
    5656 *  - \c "utf8": import UTF-8 files with ANSI colour codes.
     57 *  - \c "bin": import BIN files.
    5758 *
    5859 *  The number of bytes read is returned. If the file format is valid, but
     
    8182    if(!strcasecmp("ansi", format))
    8283        return _import_ansi(cv, data, len, 0);
     84    if(!strcasecmp("bin", format))
     85        return _import_bin(cv, data, len);
    8386
    8487    /* Autodetection */
     
    8689    {
    8790        unsigned char const *str = data;
    88         unsigned int i;
     91        unsigned int i, j, k;
    8992
    9093        /* If 4 first bytes are 0xcaca + 'CV' */
     
    97100            if((str[i] == '\033') && (str[i + 1] == '['))
    98101                return _import_ansi(cv, data, len, 0);
     102
     103        /* If we find a lot of spaces at even locations,
     104         * we guess it's a BIN file. */
     105        for (i = j = k = 0; i < len; i += 2)
     106        {
     107            j += (str[i] == ' ');
     108            k += (str[i + 1] == ' ');
     109        }
     110
     111        if (j > 10 && j > len / 40 && k < 10)
     112            return _import_bin(cv, data, len);
    99113
    100114        /* Otherwise, import it as text */
     
    118132 *  - \c "ansi": import ANSI files.
    119133 *  - \c "utf8": import UTF-8 files with ANSI colour codes.
     134 *  - \c "bin": import BIN files.
    120135 *
    121136 *  The number of bytes read is returned. If the file format is valid, but
     
    268283        "ansi", "ANSI coloured text",
    269284        "utf8", "UTF-8 files with ANSI colour codes",
     285        "bin", "BIN binary ANSI art",
    270286        NULL, NULL
    271287    };
     
    395411}
    396412
     413ssize_t _import_bin(caca_canvas_t *cv, void const *data, size_t len)
     414{
     415    uint8_t const *buf = (uint8_t const *)data;
     416    size_t i;
     417    int x = 0, y = 0;
     418
     419    caca_set_canvas_size(cv, 0, 0);
     420    caca_set_canvas_size(cv, 160, len / 160);
     421
     422    for (i = 0; i < len; i += 2)
     423    {
     424        caca_set_color_ansi(cv, buf[i + 1] & 0xf, buf[i + 1] >> 4);
     425        caca_put_char(cv, x, y, caca_cp437_to_utf32(buf[i]));
     426
     427        ++x;
     428        if (x >= 160)
     429        {
     430            ++y;
     431            x = 0;
     432        }
     433    }
     434
     435    return len & ~(size_t)1;
     436}
     437
    397438/*
    398439 * XXX: The following functions are aliases.
Note: See TracChangeset for help on using the changeset viewer.