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.php 6.9 KiB

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