Ignore:
Timestamp:
Nov 5, 2006, 11:20:51 PM (13 years ago)
Author:
Sam Hocevar
Message:
  • Updated the caca export format so that it supports multiple frames.
  • Updated the caca importer to reflect that; only one frame is read at the moment.
  • Added an "utf8cr" export format for UTF-8 + CRLF exports.
  • Updated cacaserver to reflect file format changes.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/cucul/export.c

    r1266 r1283  
    3232#include "cucul_internals.h"
    3333
     34static inline int sprintu32(char *s, uint32_t x)
     35{
     36    s[0] = (uint8_t)(x >> 24);
     37    s[1] = (uint8_t)(x >> 16) & 0xff;
     38    s[2] = (uint8_t)(x >>  8) & 0xff;
     39    s[3] = (uint8_t)(x      ) & 0xff;
     40    return 4;
     41}
     42
     43static inline int sprintu16(char *s, uint16_t x)
     44{
     45    s[0] = (uint8_t)(x >>  8) & 0xff;
     46    s[1] = (uint8_t)(x      ) & 0xff;
     47    return 2;
     48}
     49
    3450static int export_caca(cucul_canvas_t *, cucul_buffer_t *);
    3551static int export_ansi(cucul_canvas_t *, cucul_buffer_t *);
    36 static int export_utf8(cucul_canvas_t *, cucul_buffer_t *);
     52static int export_utf8(cucul_canvas_t *, cucul_buffer_t *, int);
    3753static int export_html(cucul_canvas_t *, cucul_buffer_t *);
    3854static int export_html3(cucul_canvas_t *, cucul_buffer_t *);
     
    91107        ret = export_ansi(cv, ex);
    92108    else if(!strcasecmp("utf8", format))
    93         ret = export_utf8(cv, ex);
     109        ret = export_utf8(cv, ex, 0);
     110    else if(!strcasecmp("utf8cr", format))
     111        ret = export_utf8(cv, ex, 1);
    94112    else if(!strcasecmp("html", format))
    95113        ret = export_html(cv, ex);
     
    135153        "ansi", "ANSI",
    136154        "utf8", "UTF-8 with ANSI escape codes",
     155        "utf8cr", "UTF-8 with ANSI escape codes and MS-DOS \\r",
    137156        "html", "HTML",
    138157        "html3", "backwards-compatible HTML",
     
    157176    uint32_t *chars = cv->chars;
    158177    char *cur;
    159     uint32_t w, h;
    160178    unsigned int n;
    161179
    162     /* 16 bytes for the canvas, 8 bytes for each character cell. */
    163     ex->size = 16 + 8 * cv->width * cv->height;
     180    /* 44 bytes for the header:
     181     *  - 4 bytes for "\xCA\xCA" + "CV"
     182     *  - 16 bytes for the canvas header
     183     *  - 24 bytes for the frame info
     184     * 8 bytes for each character cell */
     185    ex->size = 44 + 8 * cv->width * cv->height;
    164186    ex->data = malloc(ex->size);
    165187
    166188    cur = ex->data;
    167189
    168     w = cv->width;
    169     h = cv->height;
    170 
    171     cur += sprintf(cur, "CACACANV%c%c%c%c%c%c%c%c",
    172                    (unsigned char)(w >> 24), (unsigned char)((w >> 16) & 0xff),
    173                    (unsigned char)((w >> 8) & 0xff), (unsigned char)(w & 0xff),
    174                    (unsigned char)(h >> 24), (unsigned char)((h >> 16) & 0xff),
    175                    (unsigned char)((h >> 8) & 0xff), (unsigned char)(h & 0xff));
    176 
     190    /* magic */
     191    cur += sprintf(cur, "%s", "\xCA\xCA" "CV");
     192
     193    /* canvas_header */
     194    cur += sprintu32(cur, 16 + 24);
     195    cur += sprintu32(cur, cv->width * cv->height * 8);
     196    cur += sprintu16(cur, 0x0001);
     197    cur += sprintu32(cur, 1);
     198    cur += sprintu16(cur, 0x0000);
     199
     200    /* frame_info */
     201    cur += sprintu32(cur, cv->width);
     202    cur += sprintu32(cur, cv->height);
     203    cur += sprintu32(cur, 0);
     204    cur += sprintu32(cur, cv->curattr);
     205    cur += sprintu32(cur, 0);
     206    cur += sprintu32(cur, 0);
     207
     208    /* canvas_data */
    177209    for(n = cv->height * cv->width; n--; )
    178210    {
    179         uint32_t ch = *chars++;
    180         uint32_t a = *attrs++;
    181 
    182         *cur++ = ch >> 24;
    183         *cur++ = (ch >> 16) & 0xff;
    184         *cur++ = (ch >> 8) & 0xff;
    185         *cur++ = ch & 0xff;
    186 
    187         *cur++ = a >> 24;
    188         *cur++ = (a >> 16) & 0xff;
    189         *cur++ = (a >> 8) & 0xff;
    190         *cur++ = a & 0xff;
     211        cur += sprintu32(cur, *chars++);
     212        cur += sprintu32(cur, *attrs++);
    191213    }
    192214
     
    194216}
    195217
     218/*
     219 * The libcaca canvas format, version 1
     220 * ------------------------------------
     221 *
     222 * All types are big endian.
     223 *
     224 * struct
     225 * {
     226 * magic:
     227 *    uint8_t caca_header[2];    // "\xCA\xCA"
     228 *    uint8_t caca_file_type[2]; // "CV"
     229 *
     230 * canvas_header:
     231 *    uint32_t control_size;     // Control size (canvas_data - canvas_header)
     232 *    uint32_t data_size;        // Data size (EOF - canvas_data)
     233 *
     234 *    uint16_t version;          // Canvas format version
     235 *                               //  bit 0: set to 1 if canvas is compatible
     236 *                               //         with version 1 of the format
     237 *                               //  bits 1-15: unused yet, must be 0
     238 *
     239 *    uint32_t frames;           // Frame count
     240 *
     241 *    uint16_t flags;            // Feature flags
     242 *                               //  bits 0-15: unused yet, must be 0
     243 *
     244 * frame_info:
     245 *    struct
     246 *    {
     247 *       uint32_t width;         // Frame width
     248 *       uint32_t height;        // Frame height
     249 *       uint32_t duration;      // Frame duration in milliseconds, 0 to
     250 *                               // not specify a duration
     251 *       uint32_t attr;          // Graphics context attribute
     252 *       int32_t handle_x;       // Handle X coordinate
     253 *       int32_t handle_y;       // Handle Y coordinate
     254 *    }
     255 *    frame_list[frames];
     256 *
     257 * control_extension_1:
     258 * control_extension_2:
     259 *    ...
     260 * control_extension_N:
     261 *    ...                        // reserved for future use
     262 *
     263 * canvas_data:
     264 *    uint8_t data[data_size];   // canvas data
     265 *
     266 * data_extension_1:
     267 * data_extension_2:
     268 *    ...
     269 * data_extension_N:
     270 *    ...                        // reserved for future use
     271 * };
     272 */
     273
    196274/* Generate UTF-8 representation of current canvas. */
    197 static int export_utf8(cucul_canvas_t *cv, cucul_buffer_t *ex)
     275static int export_utf8(cucul_canvas_t *cv, cucul_buffer_t *ex, int cr)
    198276{
    199277    static uint8_t const palette[] =
     
    263341            cur += sprintf(cur, "\033[0m");
    264342
    265         cur += sprintf(cur, "\n");
     343        cur += sprintf(cur, cr ? "\r\n" : "\n");
    266344    }
    267345
Note: See TracChangeset for help on using the changeset viewer.