source: libcaca/trunk/caca/legacy.c @ 4333

Last change on this file since 4333 was 4333, checked in by Sam Hocevar, 10 years ago

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  This library is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13/*
14 *  This file contains legacy functions that we keep around until all
15 *  applications are ported.
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 "caca.h"
27#include "caca_internals.h"
28
29struct cucul_buffer
30{
31    size_t size;
32    char *data;
33    int user_data;
34};
35
36/*
37 * Functions from canvas.c
38 */
39
40int cucul_putchar(cucul_canvas_t *cv, int x, int y, unsigned long int ch)
41{
42    return caca_put_char(cv, x, y, ch);
43}
44
45unsigned long int cucul_getchar(cucul_canvas_t *cv, int x, int y)
46{
47    return caca_get_char(cv, x, y);
48}
49
50int cucul_putstr(cucul_canvas_t *cv, int x, int y, char const *s)
51{
52    return caca_put_str(cv, x, y, s);
53}
54
55/*
56 * Functions from color.c
57 */
58
59int cucul_set_color(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
60{
61    return caca_set_color_ansi(cv, fg, bg);
62}
63
64int cucul_set_truecolor(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
65{
66    return caca_set_color_argb(cv, fg, bg);
67}
68
69/*
70 * Functions from dither.c
71 */
72int cucul_set_dither_invert(cucul_dither_t *d, int value)
73{
74    float gamma = caca_get_dither_gamma(d);
75
76    if(gamma * (value ? -1 : 1) < 0)
77        caca_set_dither_gamma(d, -gamma);
78
79    return 0;
80}
81
82int cucul_set_dither_mode(cucul_dither_t *d, char const *s)
83{
84    return caca_set_dither_algorithm(d, s);
85}
86
87char const * const * cucul_get_dither_mode_list(cucul_dither_t const *d)
88{
89    return caca_get_dither_algorithm_list(d);
90}
91
92/*
93 * Functions from import.c
94 */
95
96cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buf, char const *format)
97{
98    caca_canvas_t *cv = caca_create_canvas(0, 0);
99    int ret = caca_import_canvas_from_memory(cv,
100                                             (unsigned char const *)buf->data,
101                                             buf->size, format);
102    if(ret < 0)
103    {
104        caca_free_canvas(cv);
105        return NULL;
106    }
107
108    return cv;
109}
110
111/*
112 * Functions from export.c
113 */
114
115cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format)
116{
117    cucul_buffer_t *ex;
118
119    ex = malloc(sizeof(cucul_buffer_t));
120    if(!ex)
121    {
122        seterrno(ENOMEM);
123        return NULL;
124    }
125
126    ex->data = caca_export_canvas_to_memory(cv, format, &ex->size);
127    if(!ex->data)
128    {
129        free(ex);
130        return NULL;
131    }
132
133    ex->user_data = 0;
134
135    return ex;
136}
137
138/*
139 * Functions from frame.c
140 */
141
142unsigned int cucul_get_canvas_frame_count(cucul_canvas_t *cv)
143{
144    return caca_get_frame_count(cv);
145}
146
147int cucul_set_canvas_frame(cucul_canvas_t *cv, unsigned int id)
148{
149    return caca_set_frame(cv, id);
150}
151
152int cucul_create_canvas_frame(cucul_canvas_t *cv, unsigned int id)
153{
154    return caca_create_frame(cv, id);
155}
156
157int cucul_free_canvas_frame(cucul_canvas_t *cv, unsigned int id)
158{
159    return caca_free_frame(cv, id);
160}
161
162/*
163 * Functions from buffer.c
164 */
165
166cucul_buffer_t *cucul_load_memory(void *data, unsigned long int size)
167{
168    cucul_buffer_t *buf;
169
170    buf = malloc(sizeof(cucul_buffer_t));
171    if(!buf)
172        return NULL;
173
174    buf->data = data;
175    buf->size = size;
176    buf->user_data = 1;
177
178    return buf;
179}
180
181cucul_buffer_t *cucul_load_file(char const *file)
182{
183    cucul_buffer_t *buf;
184    caca_file_t *f;
185    int ret;
186
187    f = caca_file_open(file, "rb");
188    if(!f)
189        return NULL;
190
191    buf = malloc(sizeof(cucul_buffer_t));
192    if(!buf)
193    {
194        caca_file_close(f);
195        return NULL;
196    }
197
198    buf->data = NULL;
199    buf->size = 0;
200
201    while(!caca_file_eof(f))
202    {
203        buf->data = realloc(buf->data, buf->size + 1024);
204        if(!buf->data)
205        {
206            int saved_errno = geterrno();
207            free(buf);
208            caca_file_close(f);
209            seterrno(saved_errno);
210            return NULL;
211        }
212
213        ret = caca_file_read(f, buf->data + buf->size, 1024);
214        if(ret >= 0)
215            buf->size += ret;
216    }
217    caca_file_close(f);
218
219    return buf;
220}
221
222unsigned long int cucul_get_buffer_size(cucul_buffer_t *buf)
223{
224    return buf->size;
225}
226
227void * cucul_get_buffer_data(cucul_buffer_t *buf)
228{
229    return buf->data;
230}
231
232int cucul_free_buffer(cucul_buffer_t *buf)
233{
234    if(!buf->user_data)
235        free(buf->data);
236
237    free(buf);
238
239    return 0;
240}
241
242/*
243 * Functions from transform.c
244 */
245
246int cucul_rotate(cucul_canvas_t *cv)
247{
248    return caca_rotate_180(cv);
249}
250
Note: See TracBrowser for help on using the repository browser.