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.
 
 
 
 
 
 

335 righe
10 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 various import functions.
  16. */
  17. #include "config.h"
  18. #if !defined __KERNEL__
  19. # include <stdlib.h>
  20. # include <string.h>
  21. # include <stdio.h>
  22. #endif
  23. #include "caca.h"
  24. #include "caca_internals.h"
  25. #include "codec.h"
  26. static inline uint32_t sscanu32(void const *s)
  27. {
  28. uint32_t x;
  29. memcpy(&x, s, 4);
  30. return hton32(x);
  31. }
  32. static inline uint16_t sscanu16(void const *s)
  33. {
  34. uint16_t x;
  35. memcpy(&x, s, 2);
  36. return hton16(x);
  37. }
  38. static ssize_t import_caca(caca_canvas_t *, void const *, size_t);
  39. /** \brief Import a memory buffer into a canvas
  40. *
  41. * Import a memory buffer into the given libcaca canvas's current
  42. * frame. The current frame is resized accordingly and its contents are
  43. * replaced with the imported data.
  44. *
  45. * Valid values for \c format are:
  46. * - \c "": attempt to autodetect the file format.
  47. * - \c "caca": import native libcaca files.
  48. * - \c "text": import ASCII text files.
  49. * - \c "ansi": import ANSI files.
  50. * - \c "utf8": import UTF-8 files with ANSI colour codes.
  51. *
  52. * The number of bytes read is returned. If the file format is valid, but
  53. * not enough data was available, 0 is returned.
  54. *
  55. * If an error occurs, -1 is returned and \b errno is set accordingly:
  56. * - \c ENOMEM Not enough memory to allocate canvas.
  57. * - \c EINVAL Invalid format requested.
  58. *
  59. * \param cv A libcaca canvas in which to import the file.
  60. * \param data A memory area containing the data to be loaded into the canvas.
  61. * \param len The size in bytes of the memory area.
  62. * \param format A string describing the input format.
  63. * \return The number of bytes read, or 0 if there was not enough data,
  64. * or -1 if an error occurred.
  65. */
  66. ssize_t caca_import_memory(caca_canvas_t *cv, void const *data,
  67. size_t len, char const *format)
  68. {
  69. if(!strcasecmp("caca", format))
  70. return import_caca(cv, data, len);
  71. if(!strcasecmp("utf8", format))
  72. return _import_ansi(cv, data, len, 1);
  73. if(!strcasecmp("text", format))
  74. return _import_text(cv, data, len);
  75. if(!strcasecmp("ansi", format))
  76. return _import_ansi(cv, data, len, 0);
  77. /* Autodetection */
  78. if(!strcasecmp("", format))
  79. {
  80. unsigned char const *str = data;
  81. unsigned int i;
  82. /* If 4 first bytes are 0xcaca + 'CV' */
  83. if(len >= 4 && str[0] == 0xca &&
  84. str[1] == 0xca && str[2] == 'C' && str[3] == 'V')
  85. return import_caca(cv, data, len);
  86. /* If we find ESC[ argv, we guess it's an ANSI file */
  87. for(i = 0; i + 1 < len; i++)
  88. if((str[i] == '\033') && (str[i + 1] == '['))
  89. return _import_ansi(cv, data, len, 0);
  90. /* Otherwise, import it as text */
  91. return _import_text(cv, data, len);
  92. }
  93. seterrno(EINVAL);
  94. return -1;
  95. }
  96. /** \brief Import a file into a canvas
  97. *
  98. * Import a file into the given libcaca canvas's current frame. The
  99. * current frame is resized accordingly and its contents are replaced
  100. * with the imported data.
  101. *
  102. * Valid values for \c format are:
  103. * - \c "": attempt to autodetect the file format.
  104. * - \c "caca": import native libcaca files.
  105. * - \c "text": import ASCII text files.
  106. * - \c "ansi": import ANSI files.
  107. * - \c "utf8": import UTF-8 files with ANSI colour codes.
  108. *
  109. * The number of bytes read is returned. If the file format is valid, but
  110. * not enough data was available, 0 is returned.
  111. *
  112. * If an error occurs, -1 is returned and \b errno is set accordingly:
  113. * - \c ENOSYS File access is not implemented on this system.
  114. * - \c ENOMEM Not enough memory to allocate canvas.
  115. * - \c EINVAL Invalid format requested.
  116. * caca_import_file() may also fail and set \b errno for any of the
  117. * errors specified for the routine fopen().
  118. *
  119. * \param cv A libcaca canvas in which to import the file.
  120. * \param filename The name of the file to load.
  121. * \param format A string describing the input format.
  122. * \return The number of bytes read, or 0 if there was not enough data,
  123. * or -1 if an error occurred.
  124. */
  125. ssize_t caca_import_file(caca_canvas_t *cv, char const *filename,
  126. char const *format)
  127. {
  128. #if defined __KERNEL__
  129. seterrno(ENOSYS);
  130. return -1;
  131. #else
  132. caca_file_t *f;
  133. char *data = NULL;
  134. ssize_t ret, size = 0;
  135. f = caca_file_open(filename, "rb");
  136. if(!f)
  137. return -1; /* fopen already set errno */
  138. while(!caca_file_eof(f))
  139. {
  140. data = realloc(data, size + 1024);
  141. if(!data)
  142. {
  143. caca_file_close(f);
  144. seterrno(ENOMEM);
  145. return -1;
  146. }
  147. ret = (ssize_t)caca_file_read(f, data + size, 1024);
  148. if(ret >= 0)
  149. size += ret;
  150. }
  151. caca_file_close(f);
  152. ret = caca_import_memory(cv, data, size, format);
  153. free(data);
  154. return ret;
  155. #endif
  156. }
  157. /** \brief Get available import formats
  158. *
  159. * Return a list of available import formats. The list is a NULL-terminated
  160. * array of strings, interleaving a string containing the internal value for
  161. * the import format, to be used with caca_import_canvas(), and a string
  162. * containing the natural language description for that import format.
  163. *
  164. * This function never fails.
  165. *
  166. * \return An array of strings.
  167. */
  168. char const * const * caca_get_import_list(void)
  169. {
  170. static char const * const list[] =
  171. {
  172. "", "autodetect",
  173. "caca", "native libcaca format",
  174. "text", "plain text",
  175. "ansi", "ANSI coloured text",
  176. "utf8", "UTF-8 files with ANSI colour codes",
  177. NULL, NULL
  178. };
  179. return list;
  180. }
  181. /*
  182. * XXX: the following functions are local.
  183. */
  184. static ssize_t import_caca(caca_canvas_t *cv, void const *data, size_t size)
  185. {
  186. uint8_t const *buf = (uint8_t const *)data;
  187. size_t control_size, data_size, expected_size;
  188. unsigned int frames, f, n, offset;
  189. uint16_t version, flags;
  190. int32_t xmin = 0, ymin = 0, xmax = 0, ymax = 0;
  191. if(size < 20)
  192. return 0;
  193. if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V')
  194. {
  195. debug("caca import error: expected \\xca\\xcaCV header");
  196. goto invalid_caca;
  197. }
  198. control_size = sscanu32(buf + 4);
  199. data_size = sscanu32(buf + 8);
  200. version = sscanu16(buf + 12);
  201. frames = sscanu32(buf + 14);
  202. flags = sscanu16(buf + 18);
  203. if(size < 4 + control_size + data_size)
  204. return 0;
  205. if(control_size < 16 + frames * 32)
  206. {
  207. debug("caca import error: control size %u < expected %u",
  208. (unsigned int)control_size, 16 + frames * 32);
  209. goto invalid_caca;
  210. }
  211. for(expected_size = 0, f = 0; f < frames; f++)
  212. {
  213. unsigned int width, height, duration;
  214. uint32_t attr;
  215. int x, y, handlex, handley;
  216. width = sscanu32(buf + 4 + 16 + f * 32);
  217. height = sscanu32(buf + 4 + 16 + f * 32 + 4);
  218. duration = sscanu32(buf + 4 + 16 + f * 32 + 8);
  219. attr = sscanu32(buf + 4 + 16 + f * 32 + 12);
  220. x = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 16);
  221. y = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 20);
  222. handlex = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 24);
  223. handley = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 28);
  224. expected_size += width * height * 8;
  225. if(-handlex < xmin)
  226. xmin = -handlex;
  227. if(-handley < ymin)
  228. ymin = -handley;
  229. if((((int32_t) width) - handlex) > xmax)
  230. xmax = ((int32_t) width) - handlex;
  231. if((((int32_t) height) - handley) > ymax)
  232. ymax = ((int32_t) height) - handley;
  233. }
  234. if(expected_size != data_size)
  235. {
  236. debug("caca import error: data size %u < expected %u",
  237. (unsigned int)data_size, (unsigned int)expected_size);
  238. goto invalid_caca;
  239. }
  240. caca_set_canvas_size(cv, 0, 0);
  241. caca_set_canvas_size(cv, xmax - xmin, ymax - ymin);
  242. for (f = caca_get_frame_count(cv); f--; )
  243. {
  244. caca_free_frame(cv, f);
  245. }
  246. for (offset = 0, f = 0; f < frames; f ++)
  247. {
  248. unsigned int width, height;
  249. width = sscanu32(buf + 4 + 16 + f * 32);
  250. height = sscanu32(buf + 4 + 16 + f * 32 + 4);
  251. caca_create_frame(cv, f);
  252. caca_set_frame(cv, f);
  253. cv->curattr = sscanu32(buf + 4 + 16 + f * 32 + 12);
  254. cv->frames[f].x = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 16);
  255. cv->frames[f].y = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 20);
  256. cv->frames[f].handlex = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 24);
  257. cv->frames[f].handley = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 28);
  258. /* FIXME: check for return value */
  259. for(n = width * height; n--; )
  260. {
  261. int x = (n % width) - cv->frames[f].handlex - xmin;
  262. int y = (n / width) - cv->frames[f].handley - ymin;
  263. caca_put_char(cv, x, y, sscanu32(buf + 4 + control_size
  264. + offset + 8 * n));
  265. caca_put_attr(cv, x, y, sscanu32(buf + 4 + control_size
  266. + offset + 8 * n + 4));
  267. }
  268. offset += width * height * 8;
  269. cv->frames[f].x -= cv->frames[f].handlex;
  270. cv->frames[f].y -= cv->frames[f].handley;
  271. cv->frames[f].handlex = -xmin;
  272. cv->frames[f].handley = -ymin;
  273. }
  274. caca_set_frame(cv, 0);
  275. return (ssize_t)(4 + control_size + data_size);
  276. invalid_caca:
  277. seterrno(EINVAL);
  278. return -1;
  279. }
  280. /*
  281. * XXX: The following functions are aliases.
  282. */
  283. ssize_t cucul_import_memory(cucul_canvas_t *, void const *, size_t,
  284. char const *) CACA_ALIAS(caca_import_memory);
  285. ssize_t cucul_import_file(cucul_canvas_t *, char const *,
  286. char const *) CACA_ALIAS(caca_import_file);
  287. char const * const * cucul_get_import_list(void)
  288. CACA_ALIAS(caca_get_import_list);