Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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