25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

158 lines
4.9 KiB

  1. /*
  2. * export libcaca export test program
  3. * Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This program is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What The Fuck You Want
  9. * To Public License, Version 2, as published by Sam Hocevar. See
  10. * http://sam.zoy.org/wtfpl/COPYING for more details.
  11. */
  12. #include "config.h"
  13. #if !defined(__KERNEL__)
  14. # include <stdio.h>
  15. # include <stdlib.h>
  16. # include <string.h>
  17. #endif
  18. #include "caca.h"
  19. #define WIDTH 80
  20. #define HEIGHT 32
  21. uint32_t pixels[256*256];
  22. int main(int argc, char *argv[])
  23. {
  24. caca_canvas_t *cv;
  25. caca_dither_t *dither;
  26. void *buffer;
  27. char *file, *format;
  28. char const * const * exports, * const * p;
  29. size_t len;
  30. int x, y;
  31. exports = caca_get_export_list();
  32. if(argc < 2 || argc > 3)
  33. {
  34. fprintf(stderr, "%s: wrong argument count\n", argv[0]);
  35. fprintf(stderr, "usage: %s [file] <format>\n", argv[0]);
  36. fprintf(stderr, "where <format> is one of:\n");
  37. for(p = exports; *p; p += 2)
  38. fprintf(stderr, " \"%s\" (%s)\n", *p, *(p + 1));
  39. exit(-1);
  40. }
  41. if(argc == 2)
  42. {
  43. file = NULL;
  44. format = argv[1];
  45. }
  46. else
  47. {
  48. file = argv[1];
  49. format = argv[2];
  50. }
  51. for(p = exports; *p; p += 2)
  52. if(!strcasecmp(format, *p))
  53. break;
  54. if(!*p)
  55. {
  56. fprintf(stderr, "%s: unknown format `%s'\n", argv[0], format);
  57. fprintf(stderr, "please use one of:\n");
  58. for(p = exports; *p; p += 2)
  59. fprintf(stderr, " \"%s\" (%s)\n", *p, *(p + 1));
  60. exit(-1);
  61. }
  62. if(file)
  63. {
  64. cv = caca_create_canvas(0, 0);
  65. if(caca_import_canvas_from_file(cv, file, "") < 0)
  66. {
  67. fprintf(stderr, "%s: `%s' has unknown format\n", argv[0], file);
  68. exit(-1);
  69. }
  70. }
  71. else
  72. {
  73. cv = caca_create_canvas(WIDTH, HEIGHT);
  74. for(y = 0; y < 256; y++)
  75. {
  76. for(x = 0; x < 256; x++)
  77. {
  78. uint32_t r = x;
  79. uint32_t g = (255 - y + x) / 2;
  80. uint32_t b = y * (255 - x) / 256;
  81. pixels[y * 256 + x] = (r << 16) | (g << 8) | (b << 0);
  82. }
  83. }
  84. dither = caca_create_dither(32, 256, 256, 4 * 256,
  85. 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
  86. if(!strcmp(format, "ansi") || !strcmp(format, "utf8"))
  87. caca_set_dither_charset(dither, "shades");
  88. caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv),
  89. caca_get_canvas_height(cv), dither, pixels);
  90. caca_free_dither(dither);
  91. caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);
  92. caca_draw_thin_box(cv, 0, 0, WIDTH - 1, HEIGHT - 1);
  93. caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE);
  94. caca_fill_ellipse(cv, WIDTH / 2, HEIGHT / 2,
  95. WIDTH / 4, HEIGHT / 4, ' ');
  96. caca_set_color_ansi(cv, CACA_LIGHTGRAY, CACA_BLACK);
  97. caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 6,
  98. " lightgray on black ");
  99. caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT);
  100. caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 5,
  101. " default on transparent ");
  102. caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE);
  103. caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 4,
  104. " black on white ");
  105. caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE);
  106. caca_put_str(cv, WIDTH / 2 - 8, HEIGHT / 2 - 3, "[<><><><> <>--<>]");
  107. caca_put_str(cv, WIDTH / 2 - 8, HEIGHT / 2 - 2, "[ドラゴン ボーレ]");
  108. caca_put_str(cv, WIDTH / 2 - 7, HEIGHT / 2 + 2, "äβç ░▒▓█▓▒░ ΔЗҒ");
  109. caca_put_str(cv, WIDTH / 2 - 5, HEIGHT / 2 + 4, "(\") \\o/ <&>");
  110. caca_set_attr(cv, CACA_BOLD);
  111. caca_put_str(cv, WIDTH / 2 - 16, HEIGHT / 2 + 3, "Bold");
  112. caca_set_attr(cv, CACA_BLINK);
  113. caca_put_str(cv, WIDTH / 2 - 9, HEIGHT / 2 + 3, "Blink");
  114. caca_set_attr(cv, CACA_ITALICS);
  115. caca_put_str(cv, WIDTH / 2 - 1, HEIGHT / 2 + 3, "Italics");
  116. caca_set_attr(cv, CACA_UNDERLINE);
  117. caca_put_str(cv, WIDTH / 2 + 8, HEIGHT / 2 + 3, "Underline");
  118. caca_set_attr(cv, 0);
  119. caca_set_color_ansi(cv, CACA_WHITE, CACA_LIGHTBLUE);
  120. caca_put_str(cv, WIDTH / 2 - 7, HEIGHT / 2, " LIBCACA ");
  121. for(x = 0; x < 16; x++)
  122. {
  123. caca_set_color_argb(cv, 0xff00 | x, 0xf00f | (x << 4));
  124. caca_put_char(cv, WIDTH / 2 - 7 + x, HEIGHT / 2 + 6, '#');
  125. }
  126. }
  127. buffer = caca_export_canvas_to_memory(cv, format, &len);
  128. fwrite(buffer, len, 1, stdout);
  129. free(buffer);
  130. caca_free_canvas(cv);
  131. return 0;
  132. }