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.
 
 
 
 
 
 

244 line
7.5 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. fprintf(stderr, "Usage: %s [OPTIONS]... <IMAGE>\n", argv[0]);
  39. fprintf(stderr, "Convert IMAGE to any text based available format.\n");
  40. fprintf(stderr, "Example : %s -w 80 -f ansi ./caca.png\n\n", argv[0]);
  41. fprintf(stderr, "Options:\n");
  42. fprintf(stderr, " -h, --help\t\t\tThis help\n");
  43. fprintf(stderr, " -v, --version\t\t\tVersion of the program\n");
  44. fprintf(stderr, " -W, --width=WIDTH\t\tWidth of resulting image\n");
  45. fprintf(stderr, " -H, --height=HEIGHT\t\tHeight of resulting image\n");
  46. fprintf(stderr, " -x, --font-width=WIDTH\t\tWidth of output font\n");
  47. fprintf(stderr, " -y, --font-height=HEIGHT\t\tHeight of output font\n");
  48. fprintf(stderr, " -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n");
  49. fprintf(stderr, " -c, --contrast=CONTRAST\tContrast of resulting image\n");
  50. fprintf(stderr, " -g, --gamma=GAMMA\t\tGamma of resulting image\n");
  51. fprintf(stderr, " -d, --dither=DITHER\t\tDithering algorithm to use :\n");
  52. fprintf(stderr, "\t\t\tnone : Nearest color\n");
  53. fprintf(stderr, "\t\t\tordered2 : Ordered 2x2\n");
  54. fprintf(stderr, "\t\t\tordered4 : Ordered 4x4\n");
  55. fprintf(stderr, "\t\t\tordered8 : Ordered 8x8\n");
  56. fprintf(stderr, "\t\t\trandom : Random\n");
  57. fprintf(stderr, "\t\t\tfstein : Floyd Steinberg (default)\n");
  58. fprintf(stderr, " -f, --format=FORMAT\t\tFormat of the resulting image :\n");
  59. fprintf(stderr, "\t\t\tansi : coloured ANSI (default)\n");
  60. fprintf(stderr, "\t\t\tcaca : internal libcaca format\n");
  61. fprintf(stderr, "\t\t\tutf8 : UTF8 with CR\n");
  62. fprintf(stderr, "\t\t\tutf8 : UTF8 with CRLF (MS Windows)\n");
  63. fprintf(stderr, "\t\t\thtml : HTML with CSS and DIV support\n");
  64. fprintf(stderr, "\t\t\thtml3 : Pure HTML3 with tables\n");
  65. fprintf(stderr, "\t\t\tirc : IRC with ctrl-k codes\n");
  66. fprintf(stderr, "\t\t\tps : Postscript\n");
  67. fprintf(stderr, "\t\t\tsvg : Scalable Vector Graphics\n");
  68. fprintf(stderr, "\t\t\ttga : Targa Image\n\n");
  69. #if !defined(USE_IMLIB2)
  70. fprintf(stderr, "NOTE: This program has NOT been built with Imlib2 support. Only BMP loading is supported.\n");
  71. #endif
  72. }
  73. static void version(void)
  74. {
  75. printf(
  76. "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n"
  77. "Internet: <sam@zoy.org> <jylam@lnxscene.org> Version: %s, date: %s\n"
  78. "\n"
  79. "img2txt, along with its documentation, may be freely copied and distributed.\n"
  80. "\n"
  81. "The latest version of img2txt is available from the web site,\n"
  82. " http://libcaca.zoy.org/ in the libcaca package.\n"
  83. "\n"
  84. , VERSION, __DATE__);
  85. }
  86. int main(int argc, char **argv)
  87. {
  88. /* libcucul context */
  89. cucul_canvas_t *cv;
  90. void *export;
  91. unsigned long int len;
  92. struct image *i;
  93. unsigned int cols = 0, lines = 0, font_width = 6, font_height = 10;
  94. char *format = NULL;
  95. char *dither = NULL;
  96. float gamma = -1, brightness = -1, contrast = -1;
  97. if(argc < 2)
  98. {
  99. fprintf(stderr, "%s: wrong argument count\n", argv[0]);
  100. usage(argc, argv);
  101. return 1;
  102. }
  103. for(;;)
  104. {
  105. int option_index = 0;
  106. static struct myoption long_options[] =
  107. {
  108. { "width", 1, NULL, 'W' },
  109. { "height", 1, NULL, 'H' },
  110. { "font-width", 1, NULL, 'x' },
  111. { "font-height", 1, NULL, 'y' },
  112. { "format", 1, NULL, 'f' },
  113. { "dither", 1, NULL, 'd' },
  114. { "gamma", 1, NULL, 'g' },
  115. { "brightness", 1, NULL, 'b' },
  116. { "contrast", 1, NULL, 'c' },
  117. { "help", 0, NULL, 'h' },
  118. { "version", 0, NULL, 'v' },
  119. };
  120. int c = mygetopt(argc, argv, "W:H:f:d:g:b:c:h:v:x:y:", long_options, &option_index);
  121. if(c == -1)
  122. break;
  123. switch(c)
  124. {
  125. case 'W': /* --width */
  126. cols = atoi(myoptarg);
  127. break;
  128. case 'H': /* --height */
  129. lines = atoi(myoptarg);
  130. break;
  131. case 'x': /* --width */
  132. font_width = atoi(myoptarg);
  133. break;
  134. case 'y': /* --height */
  135. font_height = atoi(myoptarg);
  136. break;
  137. case 'f': /* --format */
  138. format = myoptarg;
  139. break;
  140. case 'd': /* --dither */
  141. dither = myoptarg;
  142. break;
  143. case 'g': /* --gamma */
  144. gamma = atof(myoptarg);
  145. break;
  146. case 'b': /* --brightness */
  147. brightness = atof(myoptarg);
  148. break;
  149. case 'c': /* --contrast */
  150. contrast = atof(myoptarg);
  151. break;
  152. case 'h': /* --help */
  153. usage(argc, argv);
  154. return 0;
  155. break;
  156. case 'v': /* --version */
  157. version();
  158. return 0;
  159. break;
  160. default:
  161. return 1;
  162. break;
  163. }
  164. }
  165. cv = cucul_create_canvas(0, 0);
  166. if(!cv)
  167. {
  168. fprintf(stderr, "%s: unable to initialise libcucul\n", argv[0]);
  169. return 1;
  170. }
  171. i = load_image(argv[argc-1]);
  172. if(!i)
  173. {
  174. fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[argc-1]);
  175. cucul_free_canvas(cv);
  176. return 1;
  177. }
  178. /* Assume a 6×10 font */
  179. if(!cols && !lines)
  180. {
  181. cols = 60;
  182. lines = cols * i->h * font_width / i->w / font_height;
  183. }
  184. else if(cols && !lines)
  185. {
  186. lines = cols * i->h * font_width / i->w / font_height;
  187. }
  188. else if(!cols && lines)
  189. {
  190. cols = lines * i->w * font_height / i->h / font_width;
  191. }
  192. cucul_set_canvas_size(cv, cols, lines);
  193. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  194. cucul_clear_canvas(cv);
  195. if(cucul_set_dither_algorithm(i->dither, dither?dither:"fstein"))
  196. {
  197. fprintf(stderr, "%s: Can't dither image with algorithm '%s'\n", argv[0], dither);
  198. unload_image(i);
  199. cucul_free_canvas(cv);
  200. return -1;
  201. }
  202. if(brightness!=-1) cucul_set_dither_brightness (i->dither, brightness);
  203. if(contrast!=-1) cucul_set_dither_contrast (i->dither, contrast);
  204. if(gamma!=-1) cucul_set_dither_gamma (i->dither, gamma);
  205. cucul_dither_bitmap(cv, 0, 0, cols, lines, i->dither, i->pixels);
  206. unload_image(i);
  207. export = cucul_export_memory(cv, format?format:"ansi", &len);
  208. if(!export)
  209. {
  210. fprintf(stderr, "%s: Can't export to format '%s'\n", argv[0], format);
  211. }
  212. else
  213. {
  214. fwrite(export, len, 1, stdout);
  215. free(export);
  216. }
  217. cucul_free_canvas(cv);
  218. return 0;
  219. }