You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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