Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

252 rindas
4.6 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  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_memory(cv, (unsigned char const *)buf->data,
  83. buf->size, format);
  84. if(ret < 0)
  85. {
  86. caca_free_canvas(cv);
  87. return NULL;
  88. }
  89. return cv;
  90. }
  91. /*
  92. * Functions from export.c
  93. */
  94. cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format)
  95. {
  96. cucul_buffer_t *ex;
  97. ex = malloc(sizeof(cucul_buffer_t));
  98. if(!ex)
  99. {
  100. seterrno(ENOMEM);
  101. return NULL;
  102. }
  103. ex->data = caca_export_memory(cv, format, &ex->size);
  104. if(!ex->data)
  105. {
  106. free(ex);
  107. return NULL;
  108. }
  109. ex->user_data = 0;
  110. return ex;
  111. }
  112. /*
  113. * Functions from frame.c
  114. */
  115. unsigned int cucul_get_canvas_frame_count(cucul_canvas_t *cv)
  116. {
  117. return caca_get_frame_count(cv);
  118. }
  119. int cucul_set_canvas_frame(cucul_canvas_t *cv, unsigned int id)
  120. {
  121. return caca_set_frame(cv, id);
  122. }
  123. int cucul_create_canvas_frame(cucul_canvas_t *cv, unsigned int id)
  124. {
  125. return caca_create_frame(cv, id);
  126. }
  127. int cucul_free_canvas_frame(cucul_canvas_t *cv, unsigned int id)
  128. {
  129. return caca_free_frame(cv, id);
  130. }
  131. /*
  132. * Functions from buffer.c
  133. */
  134. cucul_buffer_t *cucul_load_memory(void *data, unsigned long int size)
  135. {
  136. cucul_buffer_t *buf;
  137. buf = malloc(sizeof(cucul_buffer_t));
  138. if(!buf)
  139. return NULL;
  140. buf->data = data;
  141. buf->size = size;
  142. buf->user_data = 1;
  143. return buf;
  144. }
  145. cucul_buffer_t *cucul_load_file(char const *file)
  146. {
  147. cucul_buffer_t *buf;
  148. caca_file_t *f;
  149. int ret;
  150. f = caca_file_open(file, "rb");
  151. if(!f)
  152. return NULL;
  153. buf = malloc(sizeof(cucul_buffer_t));
  154. if(!buf)
  155. {
  156. caca_file_close(f);
  157. return NULL;
  158. }
  159. buf->data = NULL;
  160. buf->size = 0;
  161. while(!caca_file_eof(f))
  162. {
  163. buf->data = realloc(buf->data, buf->size + 1024);
  164. if(!buf->data)
  165. {
  166. int saved_errno = geterrno();
  167. free(buf);
  168. caca_file_close(f);
  169. seterrno(saved_errno);
  170. return NULL;
  171. }
  172. ret = caca_file_read(f, buf->data + buf->size, 1024);
  173. if(ret >= 0)
  174. buf->size += ret;
  175. }
  176. caca_file_close(f);
  177. return buf;
  178. }
  179. unsigned long int cucul_get_buffer_size(cucul_buffer_t *buf)
  180. {
  181. return buf->size;
  182. }
  183. void * cucul_get_buffer_data(cucul_buffer_t *buf)
  184. {
  185. return buf->data;
  186. }
  187. int cucul_free_buffer(cucul_buffer_t *buf)
  188. {
  189. if(!buf->user_data)
  190. free(buf->data);
  191. free(buf);
  192. return 0;
  193. }
  194. /*
  195. * Functions from transform.c
  196. */
  197. int cucul_rotate(cucul_canvas_t *cv)
  198. {
  199. return caca_rotate_180(cv);
  200. }