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.

dithering.c 3.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #if !defined(__KERNEL__)
  16. # include <stdio.h>
  17. #endif
  18. #include "cucul.h"
  19. #include "caca.h"
  20. #define XRATIO 100*100
  21. #define YRATIO 70*70
  22. #define FUZZY 5000000
  23. unsigned int points[] =
  24. {
  25. CUCUL_BLACK, CUCUL_DARKGRAY, CUCUL_LIGHTGRAY,
  26. CUCUL_WHITE, CUCUL_RED, CUCUL_LIGHTRED
  27. };
  28. char density[] = " ',+:;o&%w$W@#";
  29. int main(int argc, char *argv[])
  30. {
  31. cucul_canvas_t *cv;
  32. caca_display_t *dp;
  33. int neara, dista, nearb, distb, dist;
  34. int x, y;
  35. cv = cucul_create_canvas(80, 24);
  36. if(cv == NULL)
  37. {
  38. printf("Failed to create canvas\n");
  39. return 1;
  40. }
  41. dp = caca_create_display(cv);
  42. if(dp == NULL)
  43. {
  44. printf("Failed to create display\n");
  45. return 1;
  46. }
  47. for(x = 0; x < 100; x++)
  48. for(y = 0; y < 100; y++)
  49. {
  50. char ch = '?';
  51. /* distance to black */
  52. dista = XRATIO * x * x;
  53. neara = 0;
  54. /* distance to 40% */
  55. dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y;
  56. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  57. {
  58. nearb = neara; distb = dista; neara = 1; dista = dist;
  59. }
  60. else
  61. {
  62. nearb = 1; distb = dist;
  63. }
  64. /* check dist to 70% */
  65. dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y;
  66. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  67. {
  68. nearb = neara; distb = dista; neara = 2; dista = dist;
  69. }
  70. else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
  71. {
  72. nearb = 2; distb = dist;
  73. }
  74. /* check dist to white */
  75. dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y;
  76. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  77. {
  78. nearb = neara; distb = dista; neara = 3; dista = dist;
  79. }
  80. else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
  81. {
  82. nearb = 3; distb = dist;
  83. }
  84. #if 1
  85. /* check dist to dark */
  86. dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100);
  87. dist = dist * 12 / 16;
  88. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  89. {
  90. nearb = neara; distb = dista; neara = 4; dista = dist;
  91. }
  92. else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
  93. {
  94. nearb = 4; distb = dist;
  95. }
  96. /* check dist to light */
  97. dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100);
  98. dist = dist * 8 / 16;
  99. if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista)
  100. {
  101. nearb = neara; distb = dista; neara = 5; dista = dist;
  102. }
  103. else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb)
  104. {
  105. nearb = 5; distb = dist;
  106. }
  107. #endif
  108. /* dista can be > distb because of dithering fuzziness */
  109. if(dista > distb)
  110. ch = density[distb * 2 * 13 / (dista + distb)];
  111. else
  112. ch = density[dista * 2 * 13 / (dista + distb)];
  113. cucul_set_color_ansi(cv, points[nearb], points[neara]);
  114. cucul_put_char(cv, x * cucul_get_canvas_width(cv) / 100,
  115. (100 - y) * cucul_get_canvas_height(cv) / 100, ch);
  116. }
  117. caca_refresh_display(dp);
  118. caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
  119. caca_free_display(dp);
  120. cucul_free_canvas(cv);
  121. return 0;
  122. }