Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

dithering.c 3.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * dithering libcaca dithering test program
  3. * Copyright (c) 2003-2010 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This program is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What The Fuck You Want
  9. * To Public License, Version 2, as published by Sam Hocevar. See
  10. * http://sam.zoy.org/wtfpl/COPYING for more details.
  11. */
  12. #include "config.h"
  13. #if !defined(__KERNEL__)
  14. # include <stdio.h>
  15. #endif
  16. #include "caca.h"
  17. #define XRATIO 100*100
  18. #define YRATIO 70*70
  19. #define FUZZY 5000000
  20. unsigned int points[] =
  21. {
  22. CACA_BLACK, CACA_DARKGRAY, CACA_LIGHTGRAY,
  23. CACA_WHITE, CACA_RED, CACA_LIGHTRED
  24. };
  25. char density[] = " ',+:;o&%w$W@#";
  26. int main(int argc, char *argv[])
  27. {
  28. caca_canvas_t *cv;
  29. caca_display_t *dp;
  30. int neara, dista, nearb, distb, dist;
  31. int x, y;
  32. cv = caca_create_canvas(80, 24);
  33. if(cv == NULL)
  34. {
  35. printf("Failed to create canvas\n");
  36. return 1;
  37. }
  38. dp = caca_create_display(cv);
  39. if(dp == NULL)
  40. {
  41. printf("Failed to create display\n");
  42. return 1;
  43. }
  44. for(x = 0; x < 100; x++)
  45. for(y = 0; y < 100; y++)
  46. {
  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+1) + 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+1) + dist < dista)
  64. {
  65. nearb = neara; distb = dista; neara = 2; dista = dist;
  66. }
  67. else if(caca_rand(-FUZZY, FUZZY+1) + 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+1) + dist < dista)
  74. {
  75. nearb = neara; distb = dista; neara = 3; dista = dist;
  76. }
  77. else if(caca_rand(-FUZZY, FUZZY+1) + 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+1) + dist < dista)
  86. {
  87. nearb = neara; distb = dista; neara = 4; dista = dist;
  88. }
  89. else if(caca_rand(-FUZZY, FUZZY+1) + 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+1) + dist < dista)
  97. {
  98. nearb = neara; distb = dista; neara = 5; dista = dist;
  99. }
  100. else if(caca_rand(-FUZZY, FUZZY+1) + 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_ansi(cv, points[nearb], points[neara]);
  111. caca_put_char(cv, x * caca_get_canvas_width(cv) / 100,
  112. (100 - y) * caca_get_canvas_height(cv) / 100, ch);
  113. }
  114. caca_refresh_display(dp);
  115. caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
  116. caca_free_display(dp);
  117. caca_free_canvas(cv);
  118. return 0;
  119. }