Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

453 linhas
14 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2014 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://www.wtfpl.net/ for more details.
  11. */
  12. /*
  13. * This file contains various import functions.
  14. */
  15. #include "config.h"
  16. #if !defined __KERNEL__
  17. # include <stdlib.h>
  18. # include <string.h>
  19. # include <stdio.h>
  20. #endif
  21. #include "caca.h"
  22. #include "caca_internals.h"
  23. #include "codec.h"
  24. static inline uint32_t sscanu32(void const *s)
  25. {
  26. uint32_t x;
  27. memcpy(&x, s, 4);
  28. return hton32(x);
  29. }
  30. static inline uint16_t sscanu16(void const *s)
  31. {
  32. uint16_t x;
  33. memcpy(&x, s, 2);
  34. return hton16(x);
  35. }
  36. static ssize_t import_caca(caca_canvas_t *, void const *, size_t);
  37. /** \brief Import a memory buffer into a canvas
  38. *
  39. * Import a memory buffer into the given libcaca canvas's current
  40. * frame. The current frame is resized accordingly and its contents are
  41. * replaced with the imported data.
  42. *
  43. * Valid values for \c format are:
  44. * - \c "": attempt to autodetect the file format.
  45. * - \c "caca": import native libcaca files.
  46. * - \c "text": import ASCII text files.
  47. * - \c "ansi": import ANSI files.
  48. * - \c "utf8": import UTF-8 files with ANSI colour codes.
  49. * - \c "bin": import BIN files.
  50. *
  51. * The number of bytes read is returned. If the file format is valid, but
  52. * not enough data was available, 0 is returned.
  53. *
  54. * If an error occurs, -1 is returned and \b errno is set accordingly:
  55. * - \c ENOMEM Not enough memory to allocate canvas.
  56. * - \c EINVAL Invalid format requested.
  57. *
  58. * \param cv A libcaca canvas in which to import the file.
  59. * \param data A memory area containing the data to be loaded into the canvas.
  60. * \param len The size in bytes of the memory area.
  61. * \param format A string describing the input format.
  62. * \return The number of bytes read, or 0 if there was not enough data,
  63. * or -1 if an error occurred.
  64. */
  65. ssize_t caca_import_canvas_from_memory(caca_canvas_t *cv, void const *data,
  66. size_t len, char const *format)
  67. {
  68. if(!strcasecmp("caca", format))
  69. return import_caca(cv, data, len);
  70. if(!strcasecmp("utf8", format))
  71. return _import_ansi(cv, data, len, 1);
  72. if(!strcasecmp("text", format))
  73. return _import_text(cv, data, len);
  74. if(!strcasecmp("ansi", format))
  75. return _import_ansi(cv, data, len, 0);
  76. if(!strcasecmp("bin", format))
  77. return _import_bin(cv, data, len);
  78. /* Autodetection */
  79. if(!strcasecmp("", format))
  80. {
  81. unsigned char const *str = data;
  82. unsigned int i, j, k;
  83. /* If 4 first bytes are 0xcaca + 'CV' */
  84. if(len >= 4 && str[0] == 0xca &&
  85. str[1] == 0xca && str[2] == 'C' && str[3] == 'V')
  86. return import_caca(cv, data, len);
  87. /* If we find ESC[ argv, we guess it's an ANSI file */
  88. for(i = 0; i + 1 < len; i++)
  89. if((str[i] == '\033') && (str[i + 1] == '['))
  90. return _import_ansi(cv, data, len, 0);
  91. /* If we find a lot of spaces at even locations,
  92. * we guess it's a BIN file. */
  93. for (i = j = k = 0; i < len; i += 2)
  94. {
  95. j += (str[i] == ' ');
  96. k += (str[i + 1] == ' ');
  97. }
  98. if (j > 10 && j > len / 40 && k < 10)
  99. return _import_bin(cv, data, len);
  100. /* Otherwise, import it as text */
  101. return _import_text(cv, data, len);
  102. }
  103. seterrno(EINVAL);
  104. return -1;
  105. }
  106. /** \brief Import a file into a canvas
  107. *
  108. * Import a file into the given libcaca canvas's current frame. The
  109. * current frame is resized accordingly and its contents are replaced
  110. * with the imported data.
  111. *
  112. * Valid values for \c format are:
  113. * - \c "": attempt to autodetect the file format.
  114. * - \c "caca": import native libcaca files.
  115. * - \c "text": import ASCII text files.
  116. * - \c "ansi": import ANSI files.
  117. * - \c "utf8": import UTF-8 files with ANSI colour codes.
  118. * - \c "bin": import BIN files.
  119. *
  120. * The number of bytes read is returned. If the file format is valid, but
  121. * not enough data was available, 0 is returned.
  122. *
  123. * If an error occurs, -1 is returned and \b errno is set accordingly:
  124. * - \c ENOSYS File access is not implemented on this system.
  125. * - \c ENOMEM Not enough memory to allocate canvas.
  126. * - \c EINVAL Invalid format requested.
  127. * caca_import_file() may also fail and set \b errno for any of the
  128. * errors specified for the routine fopen().
  129. *
  130. * \param cv A libcaca canvas in which to import the file.
  131. * \param filename The name of the file to load.
  132. * \param format A string describing the input format.
  133. * \return The number of bytes read, or 0 if there was not enough data,
  134. * or -1 if an error occurred.
  135. */
  136. ssize_t caca_import_canvas_from_file(caca_canvas_t *cv, char const *filename,
  137. char const *format)
  138. {
  139. #if defined __KERNEL__
  140. seterrno(ENOSYS);
  141. return -1;
  142. #else
  143. caca_file_t *f;
  144. char *data = NULL;
  145. ssize_t ret, size = 0;
  146. f = caca_file_open(filename, "rb");
  147. if(!f)
  148. return -1; /* fopen already set errno */
  149. while(!caca_file_eof(f))
  150. {
  151. data = realloc(data, size + 1024);
  152. if(!data)
  153. {
  154. caca_file_close(f);
  155. seterrno(ENOMEM);
  156. return -1;
  157. }
  158. ret = (ssize_t)caca_file_read(f, data + size, 1024);
  159. if(ret >= 0)
  160. size += ret;
  161. }
  162. caca_file_close(f);
  163. ret = caca_import_canvas_from_memory(cv, data, size, format);
  164. free(data);
  165. return ret;
  166. #endif
  167. }
  168. /** \brief Import a memory buffer into a canvas area
  169. *
  170. * Import a memory buffer into the given libcaca canvas's current
  171. * frame, at the specified position. For more information, see
  172. * caca_import_canvas_from_memory().
  173. *
  174. * If an error occurs, -1 is returned and \b errno is set accordingly:
  175. * - \c EINVAL Unsupported format requested or invalid coordinates.
  176. * - \c ENOMEM Not enough memory to allocate canvas.
  177. *
  178. * \param cv A libcaca canvas in which to import the file.
  179. * \param x The leftmost coordinate of the area to import to.
  180. * \param y The topmost coordinate of the area to import to.
  181. * \param data A memory area containing the data to be loaded into the canvas.
  182. * \param len The size in bytes of the memory area.
  183. * \param format A string describing the input format.
  184. * \return The number of bytes read, or 0 if there was not enough data,
  185. * or -1 if an error occurred.
  186. */
  187. ssize_t caca_import_area_from_memory(caca_canvas_t *cv, int x, int y,
  188. void const *data, size_t len,
  189. char const *format)
  190. {
  191. caca_canvas_t *tmp;
  192. ssize_t ret;
  193. tmp = caca_create_canvas(0, 0);
  194. ret = caca_import_canvas_from_memory(tmp, data, len, format);
  195. if(ret > 0)
  196. caca_blit(cv, x, y, tmp, NULL);
  197. caca_free_canvas(tmp);
  198. return ret;
  199. }
  200. /** \brief Import a file into a canvas area
  201. *
  202. * Import a file into the given libcaca canvas's current frame, at the
  203. * specified position. For more information, see
  204. * caca_import_canvas_from_file().
  205. *
  206. * If an error occurs, -1 is returned and \b errno is set accordingly:
  207. * - \c ENOSYS File access is not implemented on this system.
  208. * - \c ENOMEM Not enough memory to allocate canvas.
  209. * - \c EINVAL Unsupported format requested or invalid coordinates.
  210. * caca_import_file() may also fail and set \b errno for any of the
  211. * errors specified for the routine fopen().
  212. *
  213. * \param cv A libcaca canvas in which to import the file.
  214. * \param x The leftmost coordinate of the area to import to.
  215. * \param y The topmost coordinate of the area to import to.
  216. * \param filename The name of the file to load.
  217. * \param format A string describing the input format.
  218. * \return The number of bytes read, or 0 if there was not enough data,
  219. * or -1 if an error occurred.
  220. */
  221. ssize_t caca_import_area_from_file(caca_canvas_t *cv, int x, int y,
  222. char const *filename, char const *format)
  223. {
  224. caca_canvas_t *tmp;
  225. ssize_t ret;
  226. tmp = caca_create_canvas(0, 0);
  227. ret = caca_import_canvas_from_file(tmp, filename, format);
  228. if(ret > 0)
  229. caca_blit(cv, x, y, tmp, NULL);
  230. caca_free_canvas(tmp);
  231. return ret;
  232. }
  233. /** \brief Get available import formats
  234. *
  235. * Return a list of available import formats. The list is a NULL-terminated
  236. * array of strings, interleaving a string containing the internal value for
  237. * the import format, to be used with caca_import_canvas(), and a string
  238. * containing the natural language description for that import format.
  239. *
  240. * This function never fails.
  241. *
  242. * \return An array of strings.
  243. */
  244. char const * const * caca_get_import_list(void)
  245. {
  246. static char const * const list[] =
  247. {
  248. "", "autodetect",
  249. "caca", "native libcaca format",
  250. "text", "plain text",
  251. "ansi", "ANSI coloured text",
  252. "utf8", "UTF-8 files with ANSI colour codes",
  253. "bin", "BIN binary ANSI art",
  254. NULL, NULL
  255. };
  256. return list;
  257. }
  258. /*
  259. * XXX: the following functions are local.
  260. */
  261. static ssize_t import_caca(caca_canvas_t *cv, void const *data, size_t size)
  262. {
  263. uint8_t const *buf = (uint8_t const *)data;
  264. size_t control_size, data_size, expected_size;
  265. unsigned int frames, f, n, offset;
  266. uint16_t version, flags;
  267. int32_t xmin = 0, ymin = 0, xmax = 0, ymax = 0;
  268. if(size < 20)
  269. return 0;
  270. if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V')
  271. {
  272. debug("caca import error: expected \\xca\\xcaCV header");
  273. goto invalid_caca;
  274. }
  275. control_size = sscanu32(buf + 4);
  276. data_size = sscanu32(buf + 8);
  277. version = sscanu16(buf + 12);
  278. frames = sscanu32(buf + 14);
  279. flags = sscanu16(buf + 18);
  280. if(size < 4 + control_size + data_size)
  281. return 0;
  282. if(control_size < 16 + frames * 32)
  283. {
  284. debug("caca import error: control size %u < expected %u",
  285. (unsigned int)control_size, 16 + frames * 32);
  286. goto invalid_caca;
  287. }
  288. for(expected_size = 0, f = 0; f < frames; f++)
  289. {
  290. unsigned int width, height, duration;
  291. uint32_t attr;
  292. int x, y, handlex, handley;
  293. width = sscanu32(buf + 4 + 16 + f * 32);
  294. height = sscanu32(buf + 4 + 16 + f * 32 + 4);
  295. duration = sscanu32(buf + 4 + 16 + f * 32 + 8);
  296. attr = sscanu32(buf + 4 + 16 + f * 32 + 12);
  297. x = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 16);
  298. y = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 20);
  299. handlex = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 24);
  300. handley = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 28);
  301. expected_size += width * height * 8;
  302. if(-handlex < xmin)
  303. xmin = -handlex;
  304. if(-handley < ymin)
  305. ymin = -handley;
  306. if((((int32_t) width) - handlex) > xmax)
  307. xmax = ((int32_t) width) - handlex;
  308. if((((int32_t) height) - handley) > ymax)
  309. ymax = ((int32_t) height) - handley;
  310. }
  311. if(expected_size != data_size)
  312. {
  313. debug("caca import error: data size %u < expected %u",
  314. (unsigned int)data_size, (unsigned int)expected_size);
  315. goto invalid_caca;
  316. }
  317. caca_set_canvas_size(cv, 0, 0);
  318. caca_set_canvas_size(cv, xmax - xmin, ymax - ymin);
  319. for (f = caca_get_frame_count(cv); f--; )
  320. {
  321. caca_free_frame(cv, f);
  322. }
  323. for (offset = 0, f = 0; f < frames; f ++)
  324. {
  325. unsigned int width, height;
  326. width = sscanu32(buf + 4 + 16 + f * 32);
  327. height = sscanu32(buf + 4 + 16 + f * 32 + 4);
  328. caca_create_frame(cv, f);
  329. caca_set_frame(cv, f);
  330. cv->curattr = sscanu32(buf + 4 + 16 + f * 32 + 12);
  331. cv->frames[f].x = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 16);
  332. cv->frames[f].y = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 20);
  333. cv->frames[f].handlex = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 24);
  334. cv->frames[f].handley = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 28);
  335. /* FIXME: check for return value */
  336. for(n = width * height; n--; )
  337. {
  338. int x = (n % width) - cv->frames[f].handlex - xmin;
  339. int y = (n / width) - cv->frames[f].handley - ymin;
  340. caca_put_char(cv, x, y, sscanu32(buf + 4 + control_size
  341. + offset + 8 * n));
  342. caca_put_attr(cv, x, y, sscanu32(buf + 4 + control_size
  343. + offset + 8 * n + 4));
  344. }
  345. offset += width * height * 8;
  346. cv->frames[f].x -= cv->frames[f].handlex;
  347. cv->frames[f].y -= cv->frames[f].handley;
  348. cv->frames[f].handlex = -xmin;
  349. cv->frames[f].handley = -ymin;
  350. }
  351. caca_set_frame(cv, 0);
  352. return (ssize_t)(4 + control_size + data_size);
  353. invalid_caca:
  354. seterrno(EINVAL);
  355. return -1;
  356. }
  357. ssize_t _import_bin(caca_canvas_t *cv, void const *data, size_t len)
  358. {
  359. uint8_t const *buf = (uint8_t const *)data;
  360. size_t i;
  361. int x = 0, y = 0;
  362. caca_set_canvas_size(cv, 0, 0);
  363. caca_set_canvas_size(cv, 160, len / 160);
  364. for (i = 0; i < len; i += 2)
  365. {
  366. caca_set_color_ansi(cv, buf[i + 1] & 0xf, buf[i + 1] >> 4);
  367. caca_put_char(cv, x, y, caca_cp437_to_utf32(buf[i]));
  368. ++x;
  369. if (x >= 160)
  370. {
  371. ++y;
  372. x = 0;
  373. }
  374. }
  375. return len & ~(size_t)1;
  376. }
  377. /*
  378. * XXX: The following functions are aliases.
  379. */
  380. ssize_t cucul_import_memory(cucul_canvas_t *, void const *, size_t,
  381. char const *) CACA_ALIAS(caca_import_canvas_from_memory);
  382. ssize_t cucul_import_file(cucul_canvas_t *, char const *,
  383. char const *) CACA_ALIAS(caca_import_canvas_from_file);
  384. ssize_t caca_import_memory(caca_canvas_t *, void const *, size_t, char const *)
  385. CACA_ALIAS(caca_import_canvas_from_memory);
  386. ssize_t caca_import_file(caca_canvas_t *, char const *, char const *)
  387. CACA_ALIAS(caca_import_canvas_from_file);
  388. char const * const * cucul_get_import_list(void)
  389. CACA_ALIAS(caca_get_import_list);