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.

img2txt.c 6.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * img2txt image to text converter
  3. * Copyright © 2006—2018 Sam Hocevar <sam@hocevar.net>
  4. * 2007 Jean-Yves Lamoureux <jylam@lnxscene.org>
  5. * All Rights Reserved
  6. *
  7. * This program is free software. It comes without any warranty, to
  8. * the extent permitted by applicable law. You can redistribute it
  9. * and/or modify it under the terms of the Do What the Fuck You Want
  10. * to Public License, Version 2, as published by Sam Hocevar. See
  11. * http://www.wtfpl.net/ for more details.
  12. */
  13. #include "config.h"
  14. #if !defined(__KERNEL__)
  15. # include <stdio.h>
  16. # include <string.h>
  17. # include <stdlib.h>
  18. #endif
  19. #include "caca.h"
  20. #include "common-image.h"
  21. #define IMG2TXTVERSION "0.1"
  22. static void usage(int argc, char **argv)
  23. {
  24. char const * const * list;
  25. fprintf(stderr, "Usage: %s [OPTIONS]... <IMAGE>\n", argv[0]);
  26. fprintf(stderr, "Convert IMAGE to any text based available format.\n");
  27. fprintf(stderr, "Example : %s -W 80 -f ansi ./caca.png\n\n", argv[0]);
  28. fprintf(stderr, "Options:\n");
  29. fprintf(stderr, " -h, --help\t\t\tThis help\n");
  30. fprintf(stderr, " -v, --version\t\t\tVersion of the program\n");
  31. fprintf(stderr, " -W, --width=WIDTH\t\tWidth of resulting image\n");
  32. fprintf(stderr, " -H, --height=HEIGHT\t\tHeight of resulting image\n");
  33. fprintf(stderr, " -x, --font-width=WIDTH\t\tWidth of output font\n");
  34. fprintf(stderr, " -y, --font-height=HEIGHT\t\tHeight of output font\n");
  35. fprintf(stderr, " -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n");
  36. fprintf(stderr, " -c, --contrast=CONTRAST\tContrast of resulting image\n");
  37. fprintf(stderr, " -g, --gamma=GAMMA\t\tGamma of resulting image\n");
  38. fprintf(stderr, " -d, --dither=DITHER\t\tDithering algorithm to use :\n");
  39. list = caca_get_dither_algorithm_list(NULL);
  40. while(*list)
  41. {
  42. fprintf(stderr, "\t\t\t%s: %s\n", list[0], list[1]);
  43. list += 2;
  44. }
  45. fprintf(stderr, " -f, --format=FORMAT\t\tFormat of the resulting image :\n");
  46. list = caca_get_export_list();
  47. while(*list)
  48. {
  49. fprintf(stderr, "\t\t\t%s: %s\n", list[0], list[1]);
  50. list += 2;
  51. }
  52. #if !defined(USE_IMLIB2)
  53. fprintf(stderr, "NOTE: This program has NOT been built with Imlib2 support. Only BMP loading is supported.\n");
  54. #endif
  55. }
  56. static void version(void)
  57. {
  58. printf(
  59. "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n"
  60. "Internet: <sam@hocevar.net> <jylam@lnxscene.org> Version: %s, date: %s\n"
  61. "\n"
  62. "img2txt, along with its documentation, may be freely copied and distributed.\n"
  63. "\n"
  64. "The latest version of img2txt is available from the web site,\n"
  65. " http://caca.zoy.org/wiki/libcaca in the libcaca package.\n"
  66. "\n",
  67. caca_get_version(), __DATE__);
  68. }
  69. int main(int argc, char **argv)
  70. {
  71. /* libcaca context */
  72. caca_canvas_t *cv;
  73. void *export;
  74. size_t len;
  75. struct image *i;
  76. unsigned int cols = 0, lines = 0, font_width = 6, font_height = 10;
  77. char *format = NULL;
  78. char *dither = NULL;
  79. float gamma = -1, brightness = -1, contrast = -1;
  80. if(argc < 2)
  81. {
  82. fprintf(stderr, "%s: wrong argument count\n", argv[0]);
  83. usage(argc, argv);
  84. return 1;
  85. }
  86. for(;;)
  87. {
  88. int option_index = 0;
  89. static struct caca_option long_options[] =
  90. {
  91. { "width", 1, NULL, 'W' },
  92. { "height", 1, NULL, 'H' },
  93. { "font-width", 1, NULL, 'x' },
  94. { "font-height", 1, NULL, 'y' },
  95. { "format", 1, NULL, 'f' },
  96. { "dither", 1, NULL, 'd' },
  97. { "gamma", 1, NULL, 'g' },
  98. { "brightness", 1, NULL, 'b' },
  99. { "contrast", 1, NULL, 'c' },
  100. { "help", 0, NULL, 'h' },
  101. { "version", 0, NULL, 'v' },
  102. };
  103. int c = caca_getopt(argc, argv, "W:H:f:d:g:b:c:hvx:y:",
  104. long_options, &option_index);
  105. if(c == -1)
  106. break;
  107. switch(c)
  108. {
  109. case 'W': /* --width */
  110. cols = atoi(caca_optarg);
  111. break;
  112. case 'H': /* --height */
  113. lines = atoi(caca_optarg);
  114. break;
  115. case 'x': /* --width */
  116. font_width = atoi(caca_optarg);
  117. break;
  118. case 'y': /* --height */
  119. font_height = atoi(caca_optarg);
  120. break;
  121. case 'f': /* --format */
  122. format = caca_optarg;
  123. break;
  124. case 'd': /* --dither */
  125. dither = caca_optarg;
  126. break;
  127. case 'g': /* --gamma */
  128. gamma = atof(caca_optarg);
  129. break;
  130. case 'b': /* --brightness */
  131. brightness = atof(caca_optarg);
  132. break;
  133. case 'c': /* --contrast */
  134. contrast = atof(caca_optarg);
  135. break;
  136. case 'h': /* --help */
  137. usage(argc, argv);
  138. return 0;
  139. break;
  140. case 'v': /* --version */
  141. version();
  142. return 0;
  143. break;
  144. default:
  145. return 1;
  146. break;
  147. }
  148. }
  149. cv = caca_create_canvas(0, 0);
  150. if(!cv)
  151. {
  152. fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
  153. return 1;
  154. }
  155. i = load_image(argv[argc-1]);
  156. if(!i)
  157. {
  158. fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[argc-1]);
  159. caca_free_canvas(cv);
  160. return 1;
  161. }
  162. /* Assume a 6×10 font */
  163. if(!cols && !lines)
  164. {
  165. cols = 60;
  166. lines = cols * i->h * font_width / i->w / font_height;
  167. }
  168. else if(cols && !lines)
  169. {
  170. lines = cols * i->h * font_width / i->w / font_height;
  171. }
  172. else if(!cols && lines)
  173. {
  174. cols = lines * i->w * font_height / i->h / font_width;
  175. }
  176. caca_set_canvas_size(cv, cols, lines);
  177. caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT);
  178. caca_clear_canvas(cv);
  179. if(caca_set_dither_algorithm(i->dither, dither?dither:"fstein"))
  180. {
  181. fprintf(stderr, "%s: Can't dither image with algorithm '%s'\n", argv[0], dither);
  182. unload_image(i);
  183. caca_free_canvas(cv);
  184. return -1;
  185. }
  186. if(brightness!=-1) caca_set_dither_brightness (i->dither, brightness);
  187. if(contrast!=-1) caca_set_dither_contrast (i->dither, contrast);
  188. if(gamma!=-1) caca_set_dither_gamma (i->dither, gamma);
  189. caca_dither_bitmap(cv, 0, 0, cols, lines, i->dither, i->pixels);
  190. unload_image(i);
  191. export = caca_export_canvas_to_memory(cv, format?format:"ansi", &len);
  192. if(!export)
  193. {
  194. fprintf(stderr, "%s: Can't export to format '%s'\n", argv[0], format);
  195. }
  196. else
  197. {
  198. fwrite(export, len, 1, stdout);
  199. free(export);
  200. }
  201. caca_free_canvas(cv);
  202. return 0;
  203. }