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

Last change on this file since 829 was 829, checked in by Sam Hocevar, 15 years ago
  • Fixed a compilation warning.
  • Property svn:keywords set to Id
File size: 3.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 829 2006-04-21 19:26:18Z 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
20#if !defined(__KERNEL__)
21#   include <stdio.h>
22#   include <stdlib.h>
23#   include <string.h>
24#endif
25
26#include "cucul.h"
27#include "cucul_internals.h"
28
29static cucul_canvas_t *import_caca(void const *, unsigned int);
30
31/** \brief Import a buffer into a canvas
32 *
33 *  This function imports a memory area into an internal libcucul canvas.
34 *
35 *  Valid values for \c format are:
36 *
37 *  \li \c "": attempt to autodetect the file format.
38 *
39 *  \li \c "caca": import native libcaca files.
40 *
41 *  \param data The memory area to be loaded into a canvas.
42 *  \param size The length of the memory area.
43 *  \param format A string describing the input format.
44 *  \return A libcucul canvas, or NULL in case of error.
45 */
46cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size,
47                                     char const *format)
48{
49    if(!strcasecmp("caca", format))
50        return import_caca(data, size);
51
52    /* FIXME: Try to autodetect */
53    if(!strcasecmp("", format))
54        return import_caca(data, size);
55
56    return NULL;
57}
58
59/** \brief Get available import formats
60 *
61 *  Return a list of available import formats. The list is a NULL-terminated
62 *  array of strings, interleaving a string containing the internal value for
63 *  the import format, to be used with cucul_import_canvas(), and a string
64 *  containing the natural language description for that import format.
65 *
66 *  \return An array of strings.
67 */
68char const * const * cucul_get_import_list(void)
69{
70    static char const * const list[] =
71    {
72        "", "autodetect",
73        "caca", "native libcaca format",
74        NULL, NULL
75    };
76
77    return list;
78}
79
80/*
81 * XXX: the following functions are local.
82 */
83
84static cucul_canvas_t *import_caca(void const *data, unsigned int size)
85{
86    cucul_canvas_t *cv;
87    uint8_t const *buf = (uint8_t const *)data;
88    unsigned int width, height, n;
89
90    if(size < 16)
91        return NULL;
92
93    if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
94        return NULL;
95
96    if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V')
97        return NULL;
98
99    width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
100           | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
101    height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
102            | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
103
104    if(!width || !height)
105        return NULL;
106
107    if(size != 16 + width * height * 8)
108        return NULL;
109
110    cv = cucul_create_canvas(width, height);
111
112    if(!cv)
113        return NULL;
114
115    for(n = height * width; n--; )
116    {
117        cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24)
118                     | ((uint32_t)buf[16 + 1 + 8 * n] << 16)
119                     | ((uint32_t)buf[16 + 2 + 8 * n] << 8)
120                     | (uint32_t)buf[16 + 3 + 8 * n];
121        cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24)
122                    | ((uint32_t)buf[16 + 5 + 8 * n] << 16)
123                    | ((uint32_t)buf[16 + 6 + 8 * n] << 8)
124                    | (uint32_t)buf[16 + 7 + 8 * n];
125    }
126
127    return cv;
128}
129
Note: See TracBrowser for help on using the repository browser.