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

Last change on this file since 3495 was 3495, checked in by Sam Hocevar, 11 years ago

Add caca_export_area_to_memory() to export only a selected part of the
current canvas. This is useful to export dirty rectangles. Accordingly,
add caca_import_area_from_memory() and caca_import_area_from_file().

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