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.
 
 
 
 
 
 

213 lines
6.3 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. static void usage(int argc, char **argv)
  36. {
  37. fprintf(stderr, "Usage: %s [OPTIONS]... <IMAGE>\n", argv[0]);
  38. fprintf(stderr, "Convert IMAGE to any text based available format.\n");
  39. fprintf(stderr, "Example : %s -w 80 -f ansi ./caca.png\n\n", argv[0]);
  40. fprintf(stderr, "Options:\n");
  41. fprintf(stderr, " -h, --help\t\t\tThis help\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, " -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n");
  45. fprintf(stderr, " -c, --contrast=CONTRAST\tContrast of resulting image\n");
  46. fprintf(stderr, " -g, --gamma=GAMMA\t\tGamma of resulting image\n");
  47. fprintf(stderr, " -d, --dither=DITHER\t\tDithering algorithm to use :\n");
  48. fprintf(stderr, "\t\t\tnone : Nearest color\n");
  49. fprintf(stderr, "\t\t\tordered2 : Ordered 2x2\n");
  50. fprintf(stderr, "\t\t\tordered4 : Ordered 4x4\n");
  51. fprintf(stderr, "\t\t\tordered8 : Ordered 8x8\n");
  52. fprintf(stderr, "\t\t\trandom : Random\n");
  53. fprintf(stderr, "\t\t\tfstein : Floyd Steinberg (default)\n");
  54. fprintf(stderr, " -f, --format=FORMAT\t\tFormat of the resulting image :\n");
  55. fprintf(stderr, "\t\t\tansi : coloured ANSI (default)\n");
  56. fprintf(stderr, "\t\t\tcaca : internal libcaca format\n");
  57. fprintf(stderr, "\t\t\tutf8 : UTF8 with CR\n");
  58. fprintf(stderr, "\t\t\tutf8 : UTF8 with CRLF (MS Windows)\n");
  59. fprintf(stderr, "\t\t\thtml : HTML with CSS and DIV support\n");
  60. fprintf(stderr, "\t\t\thtml3 : Pure HTML3 with tables\n");
  61. fprintf(stderr, "\t\t\tirc : IRC with ctrl-k codes\n");
  62. fprintf(stderr, "\t\t\tps : Postscript\n");
  63. fprintf(stderr, "\t\t\tsvg : Scalable Vector Graphics\n");
  64. fprintf(stderr, "\t\t\ttga : Targa Image\n\n");
  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. int main(int argc, char **argv)
  70. {
  71. /* libcucul context */
  72. cucul_canvas_t *cv;
  73. void *export;
  74. unsigned long int len;
  75. struct image *i;
  76. unsigned int cols = 0, lines = 0;
  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 myoption long_options[] =
  90. {
  91. { "width", 1, NULL, 'W' },
  92. { "height", 1, NULL, 'H' },
  93. { "format", 1, NULL, 'f' },
  94. { "dither", 1, NULL, 'd' },
  95. { "gamma", 1, NULL, 'g' },
  96. { "brightness", 1, NULL, 'b' },
  97. { "contrast", 1, NULL, 'c' },
  98. { "help", 0, NULL, 'h' },
  99. };
  100. int c = mygetopt(argc, argv, "W:H:f:d:g:b:c:h", long_options, &option_index);
  101. if(c == -1)
  102. break;
  103. switch(c)
  104. {
  105. case 'W': /* --width */
  106. cols = atoi(myoptarg);
  107. break;
  108. case 'H': /* --height */
  109. lines = atoi(myoptarg);
  110. break;
  111. case 'f': /* --format */
  112. format = myoptarg;
  113. break;
  114. case 'd': /* --dither */
  115. dither = myoptarg;
  116. break;
  117. case 'h': /* --help */
  118. usage(argc, argv);
  119. return 0;
  120. break;
  121. case 'g': /* --gamma */
  122. gamma = atof(myoptarg);
  123. break;
  124. case 'b': /* --brightness */
  125. brightness = atof(myoptarg);
  126. break;
  127. case 'c': /* --contrast */
  128. contrast = atof(myoptarg);
  129. break;
  130. default:
  131. return 1;
  132. break;
  133. }
  134. }
  135. cv = cucul_create_canvas(0, 0);
  136. if(!cv)
  137. {
  138. fprintf(stderr, "%s: unable to initialise libcucul\n", argv[0]);
  139. return 1;
  140. }
  141. i = load_image(argv[argc-1]);
  142. if(!i)
  143. {
  144. fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[argc-1]);
  145. cucul_free_canvas(cv);
  146. return 1;
  147. }
  148. /* Assume a 6×10 font */
  149. if(!cols && !lines)
  150. {
  151. cols = 60;
  152. lines = cols * i->h * 6 / i->w / 10;
  153. }
  154. else if(cols && !lines)
  155. {
  156. lines = cols * i->h * 6 / i->w / 10;
  157. }
  158. else if(!cols && lines)
  159. {
  160. cols = lines * i->w * 10 / i->h / 6;
  161. }
  162. cucul_set_canvas_size(cv, cols, lines);
  163. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  164. cucul_clear_canvas(cv);
  165. if(cucul_set_dither_algorithm(i->dither, dither?dither:"fstein"))
  166. {
  167. fprintf(stderr, "%s: Can't dither image with algorithm '%s'\n", argv[0], dither);
  168. unload_image(i);
  169. cucul_free_canvas(cv);
  170. return -1;
  171. }
  172. if(brightness!=-1) cucul_set_dither_brightness (i->dither, brightness);
  173. if(contrast!=-1) cucul_set_dither_contrast (i->dither, contrast);
  174. if(gamma!=-1) cucul_set_dither_gamma (i->dither, gamma);
  175. cucul_dither_bitmap(cv, 0, 0, cols, lines, i->dither, i->pixels);
  176. unload_image(i);
  177. export = cucul_export_memory(cv, format?format:"ansi", &len);
  178. if(!export)
  179. {
  180. fprintf(stderr, "%s: Can't export to format '%s'\n", argv[0], format);
  181. }
  182. else
  183. {
  184. fwrite(export, len, 1, stdout);
  185. free(export);
  186. }
  187. cucul_free_canvas(cv);
  188. return 0;
  189. }