您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "caca": import native libcaca files.
  32. *
  33. * \param data The memory area to be loaded into a canvas.
  34. * \param size The length of the memory area.
  35. * \param format A string describing the input format.
  36. * \return A libcucul canvas, or NULL in case of error.
  37. */
  38. cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size,
  39. char const *format)
  40. {
  41. if(!strcasecmp("caca", format))
  42. return import_caca(data, size);
  43. /* FIXME: Try to autodetect */
  44. if(!strcasecmp("", format))
  45. return import_caca(data, size);
  46. return NULL;
  47. }
  48. /** \brief Get available import formats
  49. *
  50. * Return a list of available import formats. The list is a NULL-terminated
  51. * array of strings, interleaving a string containing the internal value for
  52. * the import format, to be used with cucul_import_canvas(), and a string
  53. * containing the natural language description for that import format.
  54. *
  55. * \return An array of strings.
  56. */
  57. char const * const * cucul_get_import_list(void)
  58. {
  59. static char const * const list[] =
  60. {
  61. "", "autodetect",
  62. "caca", "native libcaca format",
  63. NULL, NULL
  64. };
  65. return list;
  66. }
  67. /*
  68. * XXX: the following functions are local.
  69. */
  70. static cucul_canvas_t *import_caca(void const *data, unsigned int size)
  71. {
  72. cucul_canvas_t *cv;
  73. uint8_t *buf = (uint8_t *)data;
  74. unsigned int width, height, n;
  75. if(size < 16)
  76. return NULL;
  77. if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
  78. return NULL;
  79. if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V')
  80. return NULL;
  81. width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
  82. | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
  83. height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
  84. | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
  85. if(!width || !height)
  86. return NULL;
  87. if(size != 16 + width * height * 8)
  88. return NULL;
  89. cv = cucul_create_canvas(width, height);
  90. if(!cv)
  91. return NULL;
  92. for(n = height * width; n--; )
  93. {
  94. cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24)
  95. | ((uint32_t)buf[16 + 1 + 8 * n] << 16)
  96. | ((uint32_t)buf[16 + 2 + 8 * n] << 8)
  97. | (uint32_t)buf[16 + 3 + 8 * n];
  98. cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24)
  99. | ((uint32_t)buf[16 + 5 + 8 * n] << 16)
  100. | ((uint32_t)buf[16 + 6 + 8 * n] << 8)
  101. | (uint32_t)buf[16 + 7 + 8 * n];
  102. }
  103. return cv;
  104. }