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

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