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.
 
 
 
 
 
 

442 lines
14 KiB

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