Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

122 rader
3.2 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: export.c 361 2006-03-09 13:24:06Z jylam $
  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. char* cucul_get_irc(cucul_t *qq, int *size)
  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. if(qq->irc_buffer)
  49. free(qq->irc_buffer);
  50. qq->irc_buffer = malloc((2 + (qq->width * qq->height * 11)) * sizeof(char));
  51. if(qq->irc_buffer == NULL)
  52. return NULL;
  53. cur = qq->irc_buffer;
  54. *cur++ = '\x0f';
  55. for(y = 0; y < qq->height; y++)
  56. {
  57. uint8_t *lineattr = qq->attr + y * qq->width;
  58. uint32_t *linechar = qq->chars + y * qq->width;
  59. uint8_t prevfg = -1;
  60. uint8_t prevbg = -1;
  61. for(x = 0; x < qq->width; x++)
  62. {
  63. uint8_t fg = palette[lineattr[x] & 0x0f];
  64. uint8_t bg = palette[lineattr[x] >> 4];
  65. uint32_t c = linechar[x];
  66. if(bg == prevbg)
  67. {
  68. if(fg == prevfg)
  69. ; /* Same fg/bg, do nothing */
  70. else if(c == (uint32_t)' ')
  71. fg = prevfg; /* Hackety hack */
  72. else
  73. {
  74. cur += sprintf(cur, "\x03%d", fg);
  75. if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
  76. cur += sprintf(cur, "\x02\x02");
  77. }
  78. }
  79. else
  80. {
  81. if(fg == prevfg)
  82. cur += sprintf(cur, "\x03,%d", bg);
  83. else
  84. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  85. if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
  86. cur += sprintf(cur, "\x02\x02");
  87. }
  88. *cur++ = c & 0x7f;
  89. prevfg = fg;
  90. prevbg = bg;
  91. }
  92. *cur++ = '\r';
  93. *cur++ = '\n';
  94. }
  95. *cur++ = '\x0f';
  96. /* Crop to really used size */
  97. *size = (strlen(qq->irc_buffer) + 1) * sizeof(char);
  98. qq->irc_buffer = realloc(qq->irc_buffer, *size);
  99. return qq->irc_buffer;
  100. }