Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

114 строки
3.0 KiB

  1. /*
  2. * libcucul Unicode canvas library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file export.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \author Jean-Yves Lamoureux <jylam@lnxscene.org>
  15. * \brief Export function
  16. *
  17. * This file contains export functions for IRC
  18. */
  19. #include "config.h"
  20. #if !defined(__KERNEL__)
  21. # include <stdlib.h>
  22. # include <stdio.h>
  23. # include <string.h>
  24. #endif
  25. #include "cucul.h"
  26. #include "cucul_internals.h"
  27. /** \brief Generate IRC representation of current image.
  28. *
  29. * This function generates and returns an IRC representation of
  30. * the current image.
  31. */
  32. void _cucul_get_irc(cucul_t *qq, struct cucul_export *ex)
  33. {
  34. static int const palette[] =
  35. {
  36. 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
  37. 14, 12, 9, 11, 4, 13, 8, 0, /* Light */
  38. };
  39. char *cur;
  40. unsigned int x, y;
  41. /* 11 bytes assumed for max length per pixel. Worst case scenario:
  42. * ^Cxx,yy 6 bytes
  43. * ^B^B 2 bytes
  44. * c 1 byte
  45. * \r\n 2 bytes
  46. * In real life, the average bytes per pixel value will be around 5.
  47. */
  48. ex->size = 2 + (qq->width * qq->height * 11);
  49. ex->buffer = malloc(ex->size);
  50. cur = ex->buffer;
  51. *cur++ = '\x0f';
  52. for(y = 0; y < qq->height; y++)
  53. {
  54. uint8_t *lineattr = qq->attr + y * qq->width;
  55. uint32_t *linechar = qq->chars + y * qq->width;
  56. uint8_t prevfg = -1;
  57. uint8_t prevbg = -1;
  58. for(x = 0; x < qq->width; x++)
  59. {
  60. uint8_t fg = palette[lineattr[x] & 0x0f];
  61. uint8_t bg = palette[lineattr[x] >> 4];
  62. uint32_t c = linechar[x];
  63. if(bg == prevbg)
  64. {
  65. if(fg == prevfg)
  66. ; /* Same fg/bg, do nothing */
  67. else if(c == (uint32_t)' ')
  68. fg = prevfg; /* Hackety hack */
  69. else
  70. {
  71. cur += sprintf(cur, "\x03%d", fg);
  72. if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
  73. cur += sprintf(cur, "\x02\x02");
  74. }
  75. }
  76. else
  77. {
  78. if(fg == prevfg)
  79. cur += sprintf(cur, "\x03,%d", bg);
  80. else
  81. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  82. if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
  83. cur += sprintf(cur, "\x02\x02");
  84. }
  85. *cur++ = c & 0x7f;
  86. prevfg = fg;
  87. prevbg = bg;
  88. }
  89. *cur++ = '\r';
  90. *cur++ = '\n';
  91. }
  92. *cur++ = '\x0f';
  93. /* Crop to really used size */
  94. ex->size = strlen(ex->buffer) + 1;
  95. ex->buffer = realloc(ex->buffer, ex->size);
  96. }