1 | /* |
---|
2 | * libcaca Colour ASCII-Art library |
---|
3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: legacy.c 2821 2008-09-27 13:12:46Z 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 | |
---|
31 | struct cucul_buffer |
---|
32 | { |
---|
33 | size_t size; |
---|
34 | char *data; |
---|
35 | int user_data; |
---|
36 | }; |
---|
37 | |
---|
38 | /* |
---|
39 | * Functions from canvas.c |
---|
40 | */ |
---|
41 | |
---|
42 | int 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 | |
---|
47 | unsigned long int cucul_getchar(cucul_canvas_t *cv, int x, int y) |
---|
48 | { |
---|
49 | return caca_get_char(cv, x, y); |
---|
50 | } |
---|
51 | |
---|
52 | int 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 | |
---|
61 | int 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 | |
---|
66 | int 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 | */ |
---|
74 | int 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 | |
---|
84 | int cucul_set_dither_mode(cucul_dither_t *d, char const *s) |
---|
85 | { |
---|
86 | return caca_set_dither_algorithm(d, s); |
---|
87 | } |
---|
88 | |
---|
89 | char 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 | |
---|
98 | cucul_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_memory(cv, (unsigned char const *)buf->data, |
---|
102 | buf->size, format); |
---|
103 | if(ret < 0) |
---|
104 | { |
---|
105 | caca_free_canvas(cv); |
---|
106 | return NULL; |
---|
107 | } |
---|
108 | |
---|
109 | return cv; |
---|
110 | } |
---|
111 | |
---|
112 | /* |
---|
113 | * Functions from export.c |
---|
114 | */ |
---|
115 | |
---|
116 | cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format) |
---|
117 | { |
---|
118 | cucul_buffer_t *ex; |
---|
119 | |
---|
120 | ex = malloc(sizeof(cucul_buffer_t)); |
---|
121 | if(!ex) |
---|
122 | { |
---|
123 | seterrno(ENOMEM); |
---|
124 | return NULL; |
---|
125 | } |
---|
126 | |
---|
127 | ex->data = caca_export_memory(cv, format, &ex->size); |
---|
128 | if(!ex->data) |
---|
129 | { |
---|
130 | free(ex); |
---|
131 | return NULL; |
---|
132 | } |
---|
133 | |
---|
134 | ex->user_data = 0; |
---|
135 | |
---|
136 | return ex; |
---|
137 | } |
---|
138 | |
---|
139 | /* |
---|
140 | * Functions from frame.c |
---|
141 | */ |
---|
142 | |
---|
143 | unsigned int cucul_get_canvas_frame_count(cucul_canvas_t *cv) |
---|
144 | { |
---|
145 | return caca_get_frame_count(cv); |
---|
146 | } |
---|
147 | |
---|
148 | int cucul_set_canvas_frame(cucul_canvas_t *cv, unsigned int id) |
---|
149 | { |
---|
150 | return caca_set_frame(cv, id); |
---|
151 | } |
---|
152 | |
---|
153 | int cucul_create_canvas_frame(cucul_canvas_t *cv, unsigned int id) |
---|
154 | { |
---|
155 | return caca_create_frame(cv, id); |
---|
156 | } |
---|
157 | |
---|
158 | int cucul_free_canvas_frame(cucul_canvas_t *cv, unsigned int id) |
---|
159 | { |
---|
160 | return caca_free_frame(cv, id); |
---|
161 | } |
---|
162 | |
---|
163 | /* |
---|
164 | * Functions from buffer.c |
---|
165 | */ |
---|
166 | |
---|
167 | cucul_buffer_t *cucul_load_memory(void *data, unsigned long int size) |
---|
168 | { |
---|
169 | cucul_buffer_t *buf; |
---|
170 | |
---|
171 | buf = malloc(sizeof(cucul_buffer_t)); |
---|
172 | if(!buf) |
---|
173 | return NULL; |
---|
174 | |
---|
175 | buf->data = data; |
---|
176 | buf->size = size; |
---|
177 | buf->user_data = 1; |
---|
178 | |
---|
179 | return buf; |
---|
180 | } |
---|
181 | |
---|
182 | cucul_buffer_t *cucul_load_file(char const *file) |
---|
183 | { |
---|
184 | cucul_buffer_t *buf; |
---|
185 | caca_file_t *f; |
---|
186 | int ret; |
---|
187 | |
---|
188 | f = caca_file_open(file, "rb"); |
---|
189 | if(!f) |
---|
190 | return NULL; |
---|
191 | |
---|
192 | buf = malloc(sizeof(cucul_buffer_t)); |
---|
193 | if(!buf) |
---|
194 | { |
---|
195 | caca_file_close(f); |
---|
196 | return NULL; |
---|
197 | } |
---|
198 | |
---|
199 | buf->data = NULL; |
---|
200 | buf->size = 0; |
---|
201 | |
---|
202 | while(!caca_file_eof(f)) |
---|
203 | { |
---|
204 | buf->data = realloc(buf->data, buf->size + 1024); |
---|
205 | if(!buf->data) |
---|
206 | { |
---|
207 | int saved_errno = geterrno(); |
---|
208 | free(buf); |
---|
209 | caca_file_close(f); |
---|
210 | seterrno(saved_errno); |
---|
211 | return NULL; |
---|
212 | } |
---|
213 | |
---|
214 | ret = caca_file_read(f, buf->data + buf->size, 1024); |
---|
215 | if(ret >= 0) |
---|
216 | buf->size += ret; |
---|
217 | } |
---|
218 | caca_file_close(f); |
---|
219 | |
---|
220 | return buf; |
---|
221 | } |
---|
222 | |
---|
223 | unsigned long int cucul_get_buffer_size(cucul_buffer_t *buf) |
---|
224 | { |
---|
225 | return buf->size; |
---|
226 | } |
---|
227 | |
---|
228 | void * cucul_get_buffer_data(cucul_buffer_t *buf) |
---|
229 | { |
---|
230 | return buf->data; |
---|
231 | } |
---|
232 | |
---|
233 | int cucul_free_buffer(cucul_buffer_t *buf) |
---|
234 | { |
---|
235 | if(!buf->user_data) |
---|
236 | free(buf->data); |
---|
237 | |
---|
238 | free(buf); |
---|
239 | |
---|
240 | return 0; |
---|
241 | } |
---|
242 | |
---|
243 | /* |
---|
244 | * Functions from transform.c |
---|
245 | */ |
---|
246 | |
---|
247 | int cucul_rotate(cucul_canvas_t *cv) |
---|
248 | { |
---|
249 | return caca_rotate_180(cv); |
---|
250 | } |
---|
251 | |
---|