Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

149 wiersze
3.8 KiB

  1. /*
  2. * dithering libcaca dithering test program
  3. * Copyright (c) 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. #include "config.h"
  15. #include "common.h"
  16. #if !defined(__KERNEL__)
  17. # include <stdio.h>
  18. #endif
  19. #include "cucul.h"
  20. #include "caca.h"
  21. #define XRATIO 100*100
  22. #define YRATIO 70*70
  23. #define FUZZY 5000000
  24. unsigned int points[] =
  25. {
  26. CUCUL_BLACK, CUCUL_DARKGRAY, CUCUL_LIGHTGRAY,
  27. CUCUL_WHITE, CUCUL_RED, CUCUL_LIGHTRED
  28. };
  29. char density[] = " ',+:;o&%w$W@#";
  30. int main(int argc, char *argv[])
  31. {
  32. cucul_canvas_t *cv;
  33. caca_display_t *dp;
  34. int neara, dista, nearb, distb, dist;
  35. int x, y;
  36. cv = cucul_create_canvas(32, 16);
  37. if(cv == NULL)
  38. {
  39. printf("Failed to create canvas\n");
  40. return 1;
  41. }
  42. dp = caca_create_display(cv);
  43. if(dp == NULL)
  44. {
  45. printf("Failed to create display\n");
  46. return 1;
  47. }
  48. for(x = 0; x < 100; x++)
  49. for(y = 0; y < 100; y++)
  50. {
  51. char ch = '?';
  52. /* distance to black */
  53. dista = XRATIO * x * x;
  54. neara = 0;
  55. /* distance to 40% */
  56. dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y;
  57. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  58. {
  59. nearb = neara; distb = dista; neara = 1; dista = dist;
  60. }
  61. else
  62. {
  63. nearb = 1; distb = dist;
  64. }
  65. /* check dist to 70% */
  66. dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y;
  67. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  68. {
  69. nearb = neara; distb = dista; neara = 2; dista = dist;
  70. }
  71. else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
  72. {
  73. nearb = 2; distb = dist;
  74. }
  75. /* check dist to white */
  76. dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
  77. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  78. {
  79. nearb = neara; distb = dista; neara = 3; dista = dist;
  80. }
  81. else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
  82. {
  83. nearb = 3; distb = dist;
  84. }
  85. #if 1
  86. /* check dist to dark */
  87. dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100);
  88. dist = dist * 12 / 16;
  89. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  90. {
  91. nearb = neara; distb = dista; neara = 4; dista = dist;
  92. }
  93. else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
  94. {
  95. nearb = 4; distb = dist;
  96. }
  97. /* check dist to light */
  98. dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100);
  99. dist = dist * 8 / 16;
  100. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  101. {
  102. nearb = neara; distb = dista; neara = 5; dista = dist;
  103. }
  104. else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
  105. {
  106. nearb = 5; distb = dist;
  107. }
  108. #endif
  109. /* dista can be > distb because of dithering fuzziness */
  110. if(dista > distb)
  111. ch = density[distb * 2 * 13 / (dista + distb)];
  112. else
  113. ch = density[dista * 2 * 13 / (dista + distb)];
  114. cucul_set_color_ansi(cv, points[nearb], points[neara]);
  115. cucul_put_char(cv, x * cucul_get_canvas_width(cv) / 100,
  116. (100 - y) * cucul_get_canvas_height(cv) / 100, ch);
  117. }
  118. caca_refresh_display(dp);
  119. caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
  120. caca_free_display(dp);
  121. cucul_free_canvas(cv);
  122. return 0;
  123. }