Debian Codegraph @

src/img2txt.c

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