No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

241 líneas
6.9 KiB

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