Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

export_irc.c 2.9 KiB

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