您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

dithering.c 3.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. char ch = '?';
  47. /* distance to black */
  48. dista = XRATIO * x * x;
  49. neara = 0;
  50. /* distance to 40% */
  51. dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y;
  52. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  53. {
  54. nearb = neara; distb = dista; neara = 1; dista = dist;
  55. }
  56. else
  57. {
  58. nearb = 1; distb = dist;
  59. }
  60. /* check dist to 70% */
  61. dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y;
  62. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  63. {
  64. nearb = neara; distb = dista; neara = 2; dista = dist;
  65. }
  66. else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
  67. {
  68. nearb = 2; distb = dist;
  69. }
  70. /* check dist to white */
  71. dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
  72. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  73. {
  74. nearb = neara; distb = dista; neara = 3; dista = dist;
  75. }
  76. else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
  77. {
  78. nearb = 3; distb = dist;
  79. }
  80. #if 1
  81. /* check dist to dark */
  82. dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100);
  83. dist = dist * 12 / 16;
  84. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  85. {
  86. nearb = neara; distb = dista; neara = 4; dista = dist;
  87. }
  88. else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
  89. {
  90. nearb = 4; distb = dist;
  91. }
  92. /* check dist to light */
  93. dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100);
  94. dist = dist * 8 / 16;
  95. if(caca_rand(-FUZZY, FUZZY) + dist < dista)
  96. {
  97. nearb = neara; distb = dista; neara = 5; dista = dist;
  98. }
  99. else if(caca_rand(-FUZZY, FUZZY) + dist < distb)
  100. {
  101. nearb = 5; distb = dist;
  102. }
  103. #endif
  104. /* dista can be > distb because of dithering fuzziness */
  105. if(dista > distb)
  106. ch = density[distb * 2 * 13 / (dista + distb)];
  107. else
  108. ch = density[dista * 2 * 13 / (dista + distb)];
  109. caca_set_color(points[nearb], points[neara]);
  110. caca_putchar(x * caca_get_width() / 100, (100 - y) * caca_get_height() / 100, ch);
  111. }
  112. caca_refresh();
  113. while(!caca_get_event(CACA_EVENT_KEY_PRESS));
  114. caca_end();
  115. return 0;
  116. }