Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

253 righe
4.7 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  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. * This file contains legacy functions that we keep around until all
  16. * applications are ported.
  17. */
  18. #include "config.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdio.h>
  21. # include <stdlib.h>
  22. # include <string.h>
  23. #endif
  24. #include "caca.h"
  25. #include "caca_internals.h"
  26. struct cucul_buffer
  27. {
  28. size_t size;
  29. char *data;
  30. int user_data;
  31. };
  32. /*
  33. * Functions from canvas.c
  34. */
  35. int cucul_putchar(cucul_canvas_t *cv, int x, int y, unsigned long int ch)
  36. {
  37. return caca_put_char(cv, x, y, ch);
  38. }
  39. unsigned long int cucul_getchar(cucul_canvas_t *cv, int x, int y)
  40. {
  41. return caca_get_char(cv, x, y);
  42. }
  43. int cucul_putstr(cucul_canvas_t *cv, int x, int y, char const *s)
  44. {
  45. return caca_put_str(cv, x, y, s);
  46. }
  47. /*
  48. * Functions from color.c
  49. */
  50. int cucul_set_color(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
  51. {
  52. return caca_set_color_ansi(cv, fg, bg);
  53. }
  54. int cucul_set_truecolor(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
  55. {
  56. return caca_set_color_argb(cv, fg, bg);
  57. }
  58. /*
  59. * Functions from dither.c
  60. */
  61. int cucul_set_dither_invert(cucul_dither_t *d, int value)
  62. {
  63. float gamma = caca_get_dither_gamma(d);
  64. if(gamma * (value ? -1 : 1) < 0)
  65. caca_set_dither_gamma(d, -gamma);
  66. return 0;
  67. }
  68. int cucul_set_dither_mode(cucul_dither_t *d, char const *s)
  69. {
  70. return caca_set_dither_algorithm(d, s);
  71. }
  72. char const * const * cucul_get_dither_mode_list(cucul_dither_t const *d)
  73. {
  74. return caca_get_dither_algorithm_list(d);
  75. }
  76. /*
  77. * Functions from import.c
  78. */
  79. cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buf, char const *format)
  80. {
  81. caca_canvas_t *cv = caca_create_canvas(0, 0);
  82. int ret = caca_import_canvas_from_memory(cv,
  83. (unsigned char const *)buf->data,
  84. buf->size, format);
  85. if(ret < 0)
  86. {
  87. caca_free_canvas(cv);
  88. return NULL;
  89. }
  90. return cv;
  91. }
  92. /*
  93. * Functions from export.c
  94. */
  95. cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format)
  96. {
  97. cucul_buffer_t *ex;
  98. ex = malloc(sizeof(cucul_buffer_t));
  99. if(!ex)
  100. {
  101. seterrno(ENOMEM);
  102. return NULL;
  103. }
  104. ex->data = caca_export_canvas_to_memory(cv, format, &ex->size);
  105. if(!ex->data)
  106. {
  107. free(ex);
  108. return NULL;
  109. }
  110. ex->user_data = 0;
  111. return ex;
  112. }
  113. /*
  114. * Functions from frame.c
  115. */
  116. unsigned int cucul_get_canvas_frame_count(cucul_canvas_t *cv)
  117. {
  118. return caca_get_frame_count(cv);
  119. }
  120. int cucul_set_canvas_frame(cucul_canvas_t *cv, unsigned int id)
  121. {
  122. return caca_set_frame(cv, id);
  123. }
  124. int cucul_create_canvas_frame(cucul_canvas_t *cv, unsigned int id)
  125. {
  126. return caca_create_frame(cv, id);
  127. }
  128. int cucul_free_canvas_frame(cucul_canvas_t *cv, unsigned int id)
  129. {
  130. return caca_free_frame(cv, id);
  131. }
  132. /*
  133. * Functions from buffer.c
  134. */
  135. cucul_buffer_t *cucul_load_memory(void *data, unsigned long int size)
  136. {
  137. cucul_buffer_t *buf;
  138. buf = malloc(sizeof(cucul_buffer_t));
  139. if(!buf)
  140. return NULL;
  141. buf->data = data;
  142. buf->size = size;
  143. buf->user_data = 1;
  144. return buf;
  145. }
  146. cucul_buffer_t *cucul_load_file(char const *file)
  147. {
  148. cucul_buffer_t *buf;
  149. caca_file_t *f;
  150. int ret;
  151. f = caca_file_open(file, "rb");
  152. if(!f)
  153. return NULL;
  154. buf = malloc(sizeof(cucul_buffer_t));
  155. if(!buf)
  156. {
  157. caca_file_close(f);
  158. return NULL;
  159. }
  160. buf->data = NULL;
  161. buf->size = 0;
  162. while(!caca_file_eof(f))
  163. {
  164. buf->data = realloc(buf->data, buf->size + 1024);
  165. if(!buf->data)
  166. {
  167. int saved_errno = geterrno();
  168. free(buf);
  169. caca_file_close(f);
  170. seterrno(saved_errno);
  171. return NULL;
  172. }
  173. ret = caca_file_read(f, buf->data + buf->size, 1024);
  174. if(ret >= 0)
  175. buf->size += ret;
  176. }
  177. caca_file_close(f);
  178. return buf;
  179. }
  180. unsigned long int cucul_get_buffer_size(cucul_buffer_t *buf)
  181. {
  182. return buf->size;
  183. }
  184. void * cucul_get_buffer_data(cucul_buffer_t *buf)
  185. {
  186. return buf->data;
  187. }
  188. int cucul_free_buffer(cucul_buffer_t *buf)
  189. {
  190. if(!buf->user_data)
  191. free(buf->data);
  192. free(buf);
  193. return 0;
  194. }
  195. /*
  196. * Functions from transform.c
  197. */
  198. int cucul_rotate(cucul_canvas_t *cv)
  199. {
  200. return caca_rotate_180(cv);
  201. }