You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

137 lines
3.5 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; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include "common.h"
  15. #include <stdio.h>
  16. #include "cucul.h"
  17. #include "caca.h"
  18. #define XRATIO 100*100
  19. #define YRATIO 70*70
  20. #define FUZZY 5000000
  21. unsigned int points[] =
  22. {
  23. CUCUL_COLOR_BLACK,
  24. CUCUL_COLOR_DARKGRAY,
  25. CUCUL_COLOR_LIGHTGRAY,
  26. CUCUL_COLOR_WHITE,
  27. CUCUL_COLOR_RED,
  28. CUCUL_COLOR_LIGHTRED
  29. };
  30. char density[] = " ',+:;o&%w$W@#";
  31. int main(void)
  32. {
  33. cucul_canvas_t *cv;
  34. caca_display_t *dp;
  35. int neara, dista, nearb, distb, dist;
  36. int x, y;
  37. cv = cucul_create_canvas(0, 0);
  38. dp = caca_create_display(cv);
  39. for(x = 0; x < 100; x++)
  40. for(y = 0; y < 100; y++)
  41. {
  42. char ch = '?';
  43. /* distance to black */
  44. dista = XRATIO * x * x;
  45. neara = 0;
  46. /* distance to 40% */
  47. dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y;
  48. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  49. {
  50. nearb = neara; distb = dista; neara = 1; dista = dist;
  51. }
  52. else
  53. {
  54. nearb = 1; distb = dist;
  55. }
  56. /* check dist to 70% */
  57. dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y;
  58. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  59. {
  60. nearb = neara; distb = dista; neara = 2; dista = dist;
  61. }
  62. else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
  63. {
  64. nearb = 2; distb = dist;
  65. }
  66. /* check dist to white */
  67. dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
  68. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  69. {
  70. nearb = neara; distb = dista; neara = 3; dista = dist;
  71. }
  72. else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
  73. {
  74. nearb = 3; distb = dist;
  75. }
  76. #if 1
  77. /* check dist to dark */
  78. dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100);
  79. dist = dist * 12 / 16;
  80. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  81. {
  82. nearb = neara; distb = dista; neara = 4; dista = dist;
  83. }
  84. else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
  85. {
  86. nearb = 4; distb = dist;
  87. }
  88. /* check dist to light */
  89. dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100);
  90. dist = dist * 8 / 16;
  91. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  92. {
  93. nearb = neara; distb = dista; neara = 5; dista = dist;
  94. }
  95. else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
  96. {
  97. nearb = 5; distb = dist;
  98. }
  99. #endif
  100. /* dista can be > distb because of dithering fuzziness */
  101. if(dista > distb)
  102. ch = density[distb * 2 * 13 / (dista + distb)];
  103. else
  104. ch = density[dista * 2 * 13 / (dista + distb)];
  105. cucul_set_color(cv, points[nearb], points[neara]);
  106. cucul_putchar(cv, x * cucul_get_canvas_width(cv) / 100,
  107. (100 - y) * cucul_get_canvas_height(cv) / 100, ch);
  108. }
  109. caca_refresh_display(dp);
  110. caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
  111. caca_free_display(dp);
  112. cucul_free_canvas(cv);
  113. return 0;
  114. }