source: libcaca/trunk/cucul/import.c @ 859

Last change on this file since 859 was 859, checked in by Sam Hocevar, 15 years ago
  • Removed duplicate uint*_t defines from *_internal.h and included common.h in all .c files that needed it.
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: import.c 859 2006-04-24 20:35:59Z sam $
7 *
8 *  This library 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 various import functions.
16 */
17
18#include "config.h"
19#include "common.h"
20
21#if !defined(__KERNEL__)
22#   include <stdio.h>
23#   include <stdlib.h>
24#   include <string.h>
25#endif
26
27#include "cucul.h"
28#include "cucul_internals.h"
29
30static cucul_canvas_t *import_caca(void const *, unsigned int);
31static cucul_canvas_t *import_text(void const *, unsigned int);
32
33/** \brief Import a buffer into a canvas
34 *
35 *  This function imports a memory area into an internal libcucul canvas.
36 *
37 *  Valid values for \c format are:
38 *
39 *  \li \c "": attempt to autodetect the file format.
40 *
41 *  \li \c "caca": import native libcaca files.
42 *
43 *  \param data The memory area to be loaded into a canvas.
44 *  \param size The length of the memory area.
45 *  \param format A string describing the input format.
46 *  \return A libcucul canvas, or NULL in case of error.
47 */
48cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size,
49                                     char const *format)
50{
51    if(!strcasecmp("caca", format))
52        return import_caca(data, size);
53    if(!strcasecmp("text", format))
54        return import_text(data, size);
55
56    /* FIXME: Try to autodetect */
57    if(!strcasecmp("", format))
58        return import_caca(data, size);
59
60    return NULL;
61}
62
63/** \brief Get available import formats
64 *
65 *  Return a list of available import formats. The list is a NULL-terminated
66 *  array of strings, interleaving a string containing the internal value for
67 *  the import format, to be used with cucul_import_canvas(), and a string
68 *  containing the natural language description for that import format.
69 *
70 *  \return An array of strings.
71 */
72char const * const * cucul_get_import_list(void)
73{
74    static char const * const list[] =
75    {
76        "", "autodetect",
77        "text", "plain text",
78        "caca", "native libcaca format",
79        NULL, NULL
80    };
81
82    return list;
83}
84
85/*
86 * XXX: the following functions are local.
87 */
88
89static cucul_canvas_t *import_caca(void const *data, unsigned int size)
90{
91    cucul_canvas_t *cv;
92    uint8_t const *buf = (uint8_t const *)data;
93    unsigned int width, height, n;
94
95    if(size < 16)
96        return NULL;
97
98    if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
99        return NULL;
100
101    if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V')
102        return NULL;
103
104    width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
105           | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
106    height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
107            | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
108
109    if(!width || !height)
110        return NULL;
111
112    if(size != 16 + width * height * 8)
113        return NULL;
114
115    cv = cucul_create_canvas(width, height);
116
117    if(!cv)
118        return NULL;
119
120    for(n = height * width; n--; )
121    {
122        cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24)
123                     | ((uint32_t)buf[16 + 1 + 8 * n] << 16)
124                     | ((uint32_t)buf[16 + 2 + 8 * n] << 8)
125                     | (uint32_t)buf[16 + 3 + 8 * n];
126        cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24)
127                    | ((uint32_t)buf[16 + 5 + 8 * n] << 16)
128                    | ((uint32_t)buf[16 + 6 + 8 * n] << 8)
129                    | (uint32_t)buf[16 + 7 + 8 * n];
130    }
131
132    return cv;
133}
134
135static cucul_canvas_t *import_text(void const *data, unsigned int size)
136{
137    cucul_canvas_t *cv;
138    char const *text = (char const *)data;
139    unsigned int width = 1, height = 1, x = 0, y = 0, i;
140
141    cv = cucul_create_canvas(width, height);
142    cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT);
143
144    for(i = 0; i < size; i++)
145    {
146        unsigned char ch = *text++;
147
148        if(ch == '\r')
149            continue;
150
151        if(ch == '\n')
152        {
153            x = 0;
154            y++;
155            continue;
156        }
157
158        while(x >= width)
159        {
160            width++;
161            cucul_set_canvas_size(cv, width, height);
162        }
163
164        while(y >= height)
165        {
166            height++;
167            cucul_set_canvas_size(cv, width, height);
168        }
169
170        cucul_putchar(cv, x, y, ch);
171        x++;
172    }
173
174    return cv;
175}
176
Note: See TracBrowser for help on using the repository browser.