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.
 
 
 
 
 
 

138 lines
3.7 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 GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "config.h"
  24. #include "caca.h"
  25. #define XRATIO 100*100
  26. #define YRATIO 70*70
  27. #define FUZZY 5000000
  28. enum caca_color points[] =
  29. {
  30. CACA_COLOR_BLACK,
  31. CACA_COLOR_DARKGRAY,
  32. CACA_COLOR_LIGHTGRAY,
  33. CACA_COLOR_WHITE,
  34. CACA_COLOR_RED,
  35. CACA_COLOR_LIGHTRED
  36. };
  37. char density[] = " -,+:;o&%w$W@#";
  38. int main(void)
  39. {
  40. int neara, dista, nearb, distb, dist;
  41. int x, y;
  42. caca_init();
  43. for(x = 0; x < 100; x++)
  44. for(y = 0; y < 100; y++)
  45. {
  46. int color = CACA_COLOR_WHITE;
  47. char ch = '?';
  48. /* distance to black */
  49. dista = XRATIO * x * x;
  50. neara = 0;
  51. /* distance to 40% */
  52. dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y;
  53. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  54. {
  55. nearb = neara; distb = dista; neara = 1; dista = dist;
  56. }
  57. else
  58. {
  59. nearb = 1; distb = dist;
  60. }
  61. /* check dist to 70% */
  62. dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y;
  63. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  64. {
  65. nearb = neara; distb = dista; neara = 2; dista = dist;
  66. }
  67. else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
  68. {
  69. nearb = 2; distb = dist;
  70. }
  71. /* check dist to white */
  72. dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
  73. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  74. {
  75. nearb = neara; distb = dista; neara = 3; dista = dist;
  76. }
  77. else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
  78. {
  79. nearb = 3; distb = dist;
  80. }
  81. #if 1
  82. /* check dist to dark */
  83. dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100);
  84. dist = dist * 12 / 16;
  85. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  86. {
  87. nearb = neara; distb = dista; neara = 4; dista = dist;
  88. }
  89. else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
  90. {
  91. nearb = 4; distb = dist;
  92. }
  93. /* check dist to light */
  94. dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100);
  95. dist = dist * 8 / 16;
  96. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  97. {
  98. nearb = neara; distb = dista; neara = 5; dista = dist;
  99. }
  100. else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
  101. {
  102. nearb = 5; distb = dist;
  103. }
  104. #endif
  105. /* dista can be > distb because of dithering fuzziness */
  106. if(dista > distb)
  107. ch = density[distb * 2 * 13 / (dista + distb)];
  108. else
  109. ch = density[dista * 2 * 13 / (dista + distb)];
  110. caca_set_color(points[nearb], points[neara]);
  111. caca_putchar(x * caca_get_width() / 100, (100 - y) * caca_get_height() / 100, ch);
  112. }
  113. caca_refresh();
  114. while((caca_get_event() & 0xff000000) != CACA_EVENT_KEY_PRESS);
  115. caca_end();
  116. return 0;
  117. }