Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

243 строки
6.9 KiB

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