Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

110 linhas
3.0 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. * 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_irc.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. for(y = 0; y < qq->height; y++)
  52. {
  53. uint32_t *lineattr = qq->attr + y * qq->width;
  54. uint32_t *linechar = qq->chars + y * qq->width;
  55. uint8_t prevfg = -1;
  56. uint8_t prevbg = -1;
  57. for(x = 0; x < qq->width; x++)
  58. {
  59. uint8_t fg = palette[_cucul_argb32_to_ansi4fg(lineattr[x])];
  60. uint8_t bg = palette[_cucul_argb32_to_ansi4bg(lineattr[x])];
  61. uint32_t c = linechar[x];
  62. if(bg == prevbg)
  63. {
  64. if(fg == prevfg)
  65. ; /* Same fg/bg, do nothing */
  66. else if(c == (uint32_t)' ')
  67. fg = prevfg; /* Hackety hack */
  68. else
  69. {
  70. cur += sprintf(cur, "\x03%d", fg);
  71. if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
  72. cur += sprintf(cur, "\x02\x02");
  73. }
  74. }
  75. else
  76. {
  77. if(fg == prevfg)
  78. cur += sprintf(cur, "\x03,%d", bg);
  79. else
  80. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  81. if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
  82. cur += sprintf(cur, "\x02\x02");
  83. }
  84. *cur++ = c & 0x7f;
  85. prevfg = fg;
  86. prevbg = bg;
  87. }
  88. *cur++ = '\r';
  89. *cur++ = '\n';
  90. }
  91. /* Crop to really used size */
  92. ex->size = (uintptr_t)(cur - ex->buffer);
  93. ex->buffer = realloc(ex->buffer, ex->size);
  94. }