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.
 
 
 
 
 
 

130 rivejä
3.5 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains various import functions.
  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 "cucul.h"
  23. #include "cucul_internals.h"
  24. static cucul_canvas_t *import_caca(void const *, unsigned int);
  25. /** \brief Import a buffer into a canvas
  26. *
  27. * This function imports a memory area into an internal libcucul canvas.
  28. *
  29. * Valid values for \c format are:
  30. *
  31. * \li \c "": attempt to autodetect the file format.
  32. *
  33. * \li \c "caca": import native libcaca files.
  34. *
  35. * \param data The memory area to be loaded into a canvas.
  36. * \param size The length of the memory area.
  37. * \param format A string describing the input format.
  38. * \return A libcucul canvas, or NULL in case of error.
  39. */
  40. cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size,
  41. char const *format)
  42. {
  43. if(!strcasecmp("caca", format))
  44. return import_caca(data, size);
  45. /* FIXME: Try to autodetect */
  46. if(!strcasecmp("", format))
  47. return import_caca(data, size);
  48. return NULL;
  49. }
  50. /** \brief Get available import formats
  51. *
  52. * Return a list of available import formats. The list is a NULL-terminated
  53. * array of strings, interleaving a string containing the internal value for
  54. * the import format, to be used with cucul_import_canvas(), and a string
  55. * containing the natural language description for that import format.
  56. *
  57. * \return An array of strings.
  58. */
  59. char const * const * cucul_get_import_list(void)
  60. {
  61. static char const * const list[] =
  62. {
  63. "", "autodetect",
  64. "caca", "native libcaca format",
  65. NULL, NULL
  66. };
  67. return list;
  68. }
  69. /*
  70. * XXX: the following functions are local.
  71. */
  72. static cucul_canvas_t *import_caca(void const *data, unsigned int size)
  73. {
  74. cucul_canvas_t *cv;
  75. uint8_t const *buf = (uint8_t const *)data;
  76. unsigned int width, height, n;
  77. if(size < 16)
  78. return NULL;
  79. if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
  80. return NULL;
  81. if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V')
  82. return NULL;
  83. width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
  84. | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
  85. height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
  86. | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
  87. if(!width || !height)
  88. return NULL;
  89. if(size != 16 + width * height * 8)
  90. return NULL;
  91. cv = cucul_create_canvas(width, height);
  92. if(!cv)
  93. return NULL;
  94. for(n = height * width; n--; )
  95. {
  96. cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24)
  97. | ((uint32_t)buf[16 + 1 + 8 * n] << 16)
  98. | ((uint32_t)buf[16 + 2 + 8 * n] << 8)
  99. | (uint32_t)buf[16 + 3 + 8 * n];
  100. cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24)
  101. | ((uint32_t)buf[16 + 5 + 8 * n] << 16)
  102. | ((uint32_t)buf[16 + 6 + 8 * n] << 8)
  103. | (uint32_t)buf[16 + 7 + 8 * n];
  104. }
  105. return cv;
  106. }