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.
 
 
 
 
 
 

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