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.
 
 
 
 
 
 

127 lines
3.2 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 "caca.h"
  15. #define XRATIO 100*100
  16. #define YRATIO 70*70
  17. #define FUZZY 5000000
  18. enum caca_color points[] =
  19. {
  20. CACA_COLOR_BLACK,
  21. CACA_COLOR_DARKGRAY,
  22. CACA_COLOR_LIGHTGRAY,
  23. CACA_COLOR_WHITE,
  24. CACA_COLOR_RED,
  25. CACA_COLOR_LIGHTRED
  26. };
  27. char density[] = " -,+:;o&%w$W@#";
  28. int main(void)
  29. {
  30. int neara, dista, nearb, distb, dist;
  31. int x, y;
  32. caca_init();
  33. for(x = 0; x < 100; x++)
  34. for(y = 0; y < 100; y++)
  35. {
  36. char ch = '?';
  37. /* distance to black */
  38. dista = XRATIO * x * x;
  39. neara = 0;
  40. /* distance to 40% */
  41. dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y;
  42. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  43. {
  44. nearb = neara; distb = dista; neara = 1; dista = dist;
  45. }
  46. else
  47. {
  48. nearb = 1; distb = dist;
  49. }
  50. /* check dist to 70% */
  51. dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y;
  52. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  53. {
  54. nearb = neara; distb = dista; neara = 2; dista = dist;
  55. }
  56. else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
  57. {
  58. nearb = 2; distb = dist;
  59. }
  60. /* check dist to white */
  61. dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
  62. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  63. {
  64. nearb = neara; distb = dista; neara = 3; dista = dist;
  65. }
  66. else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
  67. {
  68. nearb = 3; distb = dist;
  69. }
  70. #if 1
  71. /* check dist to dark */
  72. dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100);
  73. dist = dist * 12 / 16;
  74. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  75. {
  76. nearb = neara; distb = dista; neara = 4; dista = dist;
  77. }
  78. else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
  79. {
  80. nearb = 4; distb = dist;
  81. }
  82. /* check dist to light */
  83. dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100);
  84. dist = dist * 8 / 16;
  85. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  86. {
  87. nearb = neara; distb = dista; neara = 5; dista = dist;
  88. }
  89. else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
  90. {
  91. nearb = 5; distb = dist;
  92. }
  93. #endif
  94. /* dista can be > distb because of dithering fuzziness */
  95. if(dista > distb)
  96. ch = density[distb * 2 * 13 / (dista + distb)];
  97. else
  98. ch = density[dista * 2 * 13 / (dista + distb)];
  99. caca_set_color(points[nearb], points[neara]);
  100. caca_putchar(x * caca_get_width() / 100, (100 - y) * caca_get_height() / 100, ch);
  101. }
  102. caca_refresh();
  103. while(!caca_get_event(CACA_EVENT_KEY_PRESS));
  104. caca_end();
  105. return 0;
  106. }