Changeset 3302 for libcaca/trunk/caca-php/examples/img2txt.php
- Timestamp:
- Nov 7, 2008, 12:25:26 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/caca-php/examples/img2txt.php
-
Property
svn:executable
set to
*
r3297 r3302 1 #!/usr/bin/php5 2 <?php 1 3 /* 2 4 * img2txt image to text converter … … 14 16 */ 15 17 16 #include "config.h" 17 18 #if !defined(__KERNEL__) 19 # include <stdio.h> 20 # include <string.h> 21 # include <stdlib.h> 22 #endif 23 24 #if !defined HAVE_GETOPT_LONG 25 # include "mygetopt.h" 26 #elif defined HAVE_GETOPT_H 27 # include <getopt.h> 28 #endif 29 #if defined HAVE_GETOPT_LONG 30 # define mygetopt getopt_long 31 # define myoptind optind 32 # define myoptarg optarg 33 # define myoption option 34 #endif 35 36 #include "caca.h" 37 38 #include "common-image.h" 39 40 #define IMG2TXTVERSION "0.1" 41 42 static void usage(int argc, char **argv) 18 if (php_sapi_name() != "cli") { 19 die("You have to run this program with php-cli!\n"); 20 } 21 22 function usage($argc, $argv) 43 23 { 44 char const * const * list; 45 46 fprintf(stderr, "Usage: %s [OPTIONS]... <IMAGE>\n", argv[0]); 47 fprintf(stderr, "Convert IMAGE to any text based available format.\n"); 48 fprintf(stderr, "Example : %s -w 80 -f ansi ./caca.png\n\n", argv[0]); 49 fprintf(stderr, "Options:\n"); 50 fprintf(stderr, " -h, --help\t\t\tThis help\n"); 51 fprintf(stderr, " -v, --version\t\t\tVersion of the program\n"); 52 fprintf(stderr, " -W, --width=WIDTH\t\tWidth of resulting image\n"); 53 fprintf(stderr, " -H, --height=HEIGHT\t\tHeight of resulting image\n"); 54 fprintf(stderr, " -x, --font-width=WIDTH\t\tWidth of output font\n"); 55 fprintf(stderr, " -y, --font-height=HEIGHT\t\tHeight of output font\n"); 56 fprintf(stderr, " -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n"); 57 fprintf(stderr, " -c, --contrast=CONTRAST\tContrast of resulting image\n"); 58 fprintf(stderr, " -g, --gamma=GAMMA\t\tGamma of resulting image\n"); 59 fprintf(stderr, " -d, --dither=DITHER\t\tDithering algorithm to use :\n"); 60 list = caca_get_dither_algorithm_list(NULL); 61 while(*list) 62 { 63 fprintf(stderr, "\t\t\t%s: %s\n", list[0], list[1]); 64 list += 2; 65 } 66 67 fprintf(stderr, " -f, --format=FORMAT\t\tFormat of the resulting image :\n"); 68 list = caca_get_export_list(); 69 while(*list) 70 { 71 fprintf(stderr, "\t\t\t%s: %s\n", list[0], list[1]); 72 list += 2; 73 } 74 75 #if !defined(USE_IMLIB2) 76 fprintf(stderr, "NOTE: This program has NOT been built with Imlib2 support. Only BMP loading is supported.\n"); 77 #endif 78 } 79 80 static void version(void) 24 fprintf(STDERR, "Usage: %s [OPTIONS]... <IMAGE>\n", $argv[0]); 25 fprintf(STDERR, "Convert IMAGE to any text based available format.\n"); 26 fprintf(STDERR, "Example : %s -w 80 -f ansi ./caca.png\n\n", $argv[0]); 27 fprintf(STDERR, "Options:\n"); 28 fprintf(STDERR, " -h, --help\t\t\tThis help\n"); 29 fprintf(STDERR, " -v, --version\t\t\tVersion of the program\n"); 30 fprintf(STDERR, " -W, --width=WIDTH\t\tWidth of resulting image\n"); 31 fprintf(STDERR, " -H, --height=HEIGHT\t\tHeight of resulting image\n"); 32 fprintf(STDERR, " -x, --font-width=WIDTH\t\tWidth of output font\n"); 33 fprintf(STDERR, " -y, --font-height=HEIGHT\t\tHeight of output font\n"); 34 fprintf(STDERR, " -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n"); 35 fprintf(STDERR, " -c, --contrast=CONTRAST\tContrast of resulting image\n"); 36 fprintf(STDERR, " -g, --gamma=GAMMA\t\tGamma of resulting image\n"); 37 fprintf(STDERR, " -d, --dither=DITHER\t\tDithering algorithm to use :\n"); 38 $list = caca_get_dither_algorithm_list(caca_create_dither(imagecreate(1, 1))); 39 foreach($list as $type => $name) 40 { 41 fprintf(STDERR, "\t\t\t%s: %s\n", $type, $name); 42 } 43 44 fprintf(STDERR, " -f, --format=FORMAT\t\tFormat of the resulting image :\n"); 45 $list = caca_get_export_list(); 46 foreach($list as $type => $name) 47 { 48 fprintf(STDERR, "\t\t\t%s: %s\n", $type, $name); 49 } 50 } 51 52 function version() 81 53 { 82 83 "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n" 84 "Internet: <sam@zoy.org> <jylam@lnxscene.org> Version: %s, date: %s\n" 85 "\n" 86 "img2txt, along with its documentation, may be freely copied and distributed.\n" 87 "\n" 88 "The latest version of img2txt is available from the web site,\n" 89 " http://caca.zoy.org/wiki/libcaca in the libcaca package.\n" 90 91 caca_get_version(), __DATE__);92 } 93 int main(int argc, char **argv)54 printf( 55 "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n" . 56 "Internet: <sam@zoy.org> <jylam@lnxscene.org> Version: %s\n" . 57 "\n" . 58 "img2txt, along with its documentation, may be freely copied and distributed.\n" . 59 "\n" . 60 "The latest version of img2txt is available from the web site,\n" . 61 " http://caca.zoy.org/wiki/libcaca in the libcaca package.\n" . 62 "\n", 63 caca_get_version()); 64 } 65 function main() 94 66 { 95 /* libcaca context */ 96 caca_canvas_t *cv; 97 void *export; 98 size_t len; 99 struct image *i; 100 unsigned int cols = 0, lines = 0, font_width = 6, font_height = 10; 101 char *format = NULL; 102 char *dither = NULL; 103 float gamma = -1, brightness = -1, contrast = -1; 104 105 if(argc < 2) 106 { 107 fprintf(stderr, "%s: wrong argument count\n", argv[0]); 108 usage(argc, argv); 109 return 1; 110 } 111 112 for(;;) 113 { 114 int option_index = 0; 115 static struct myoption long_options[] = 116 { 117 { "width", 1, NULL, 'W' }, 118 { "height", 1, NULL, 'H' }, 119 { "font-width", 1, NULL, 'x' }, 120 { "font-height", 1, NULL, 'y' }, 121 { "format", 1, NULL, 'f' }, 122 { "dither", 1, NULL, 'd' }, 123 { "gamma", 1, NULL, 'g' }, 124 { "brightness", 1, NULL, 'b' }, 125 { "contrast", 1, NULL, 'c' }, 126 { "help", 0, NULL, 'h' }, 127 { "version", 0, NULL, 'v' }, 128 }; 129 int c = mygetopt(argc, argv, "W:H:f:d:g:b:c:hvx:y:", long_options, &option_index); 130 if(c == -1) 131 break; 132 133 switch(c) 134 { 135 case 'W': /* --width */ 136 cols = atoi(myoptarg); 137 break; 138 case 'H': /* --height */ 139 lines = atoi(myoptarg); 140 break; 141 case 'x': /* --width */ 142 font_width = atoi(myoptarg); 143 break; 144 case 'y': /* --height */ 145 font_height = atoi(myoptarg); 146 break; 147 case 'f': /* --format */ 148 format = myoptarg; 149 break; 150 case 'd': /* --dither */ 151 dither = myoptarg; 152 break; 153 case 'g': /* --gamma */ 154 gamma = atof(myoptarg); 155 break; 156 case 'b': /* --brightness */ 157 brightness = atof(myoptarg); 158 break; 159 case 'c': /* --contrast */ 160 contrast = atof(myoptarg); 161 break; 162 case 'h': /* --help */ 163 usage(argc, argv); 164 return 0; 165 break; 166 case 'v': /* --version */ 167 version(); 168 return 0; 169 break; 170 default: 171 return 1; 172 break; 173 } 174 } 175 176 177 cv = caca_create_canvas(0, 0); 178 if(!cv) 179 { 180 fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]); 181 return 1; 182 } 183 184 i = load_image(argv[argc-1]); 185 if(!i) 186 { 187 fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[argc-1]); 188 caca_free_canvas(cv); 189 return 1; 190 } 191 192 /* Assume a 6×10 font */ 193 if(!cols && !lines) 194 { 195 cols = 60; 196 lines = cols * i->h * font_width / i->w / font_height; 197 } 198 else if(cols && !lines) 199 { 200 lines = cols * i->h * font_width / i->w / font_height; 201 } 202 else if(!cols && lines) 203 { 204 cols = lines * i->w * font_height / i->h / font_width; 205 } 206 207 208 caca_set_canvas_size(cv, cols, lines); 209 caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT); 210 caca_clear_canvas(cv); 211 if(caca_set_dither_algorithm(i->dither, dither?dither:"fstein")) 212 { 213 fprintf(stderr, "%s: Can't dither image with algorithm '%s'\n", argv[0], dither); 214 unload_image(i); 215 caca_free_canvas(cv); 216 return -1; 217 } 218 219 if(brightness!=-1) caca_set_dither_brightness (i->dither, brightness); 220 if(contrast!=-1) caca_set_dither_contrast (i->dither, contrast); 221 if(gamma!=-1) caca_set_dither_gamma (i->dither, gamma); 222 223 caca_dither_bitmap(cv, 0, 0, cols, lines, i->dither, i->pixels); 224 225 unload_image(i); 226 227 export = caca_export_memory(cv, format?format:"ansi", &len); 228 if(!export) 229 { 230 fprintf(stderr, "%s: Can't export to format '%s'\n", argv[0], format); 231 } 232 else 233 { 234 fwrite(export, len, 1, stdout); 235 free(export); 236 } 237 238 caca_free_canvas(cv); 239 240 return 0; 241 } 242 67 global $argc, $argv; 68 $cols = 0; 69 $lines = 0; 70 $font_width = 6; 71 $font_height = 10; 72 $format = NULL; 73 $dither = NULL; 74 $gamma = $brightness = $contrast = -1.0; 75 76 if($argc < 2) 77 { 78 fprintf(STDERR, "%s: wrong argument count\n", $argv[0]); 79 usage($argc, $argv); 80 return 1; 81 } 82 83 $long_options = array( 84 "width:" => 'W', 85 "height:" => 'H', 86 "font-width:" => 'x', 87 "font-height:" => 'y', 88 "format:" => 'f', 89 "dither:" => 'd', 90 "gamma:" => 'g', 91 "brightness:" => 'b', 92 "contrast:" => 'c', 93 "help" => 'h', 94 "version" => 'v' 95 ); 96 $options = getopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options)); 97 if (! $options) 98 { 99 /* PHP before 5.3 or so does not have long option support in 100 * most cases */ 101 $options = getopt("W:H:f:d:g:b:c:hvx:y:"); 102 } 103 104 foreach($options as $opt => $arg) 105 { 106 if (array_key_exists($opt + (isset($arg) ? ':' : ''), $long_options)) 107 { 108 $opt = $long_options[$opt + (isset($arg) ? ':' : '')]; 109 } 110 switch($opt) 111 { 112 case 'W': /* --width */ 113 $cols = intval($arg); 114 break; 115 case 'H': /* --height */ 116 $lines = intval($arg); 117 break; 118 case 'x': /* --width */ 119 $font_width = intval($arg); 120 break; 121 case 'y': /* --height */ 122 $font_height = intval($arg); 123 break; 124 case 'f': /* --format */ 125 $format = $arg; 126 break; 127 case 'd': /* --dither */ 128 $dither = $arg; 129 break; 130 case 'g': /* --gamma */ 131 $gamma = floatval($arg); 132 break; 133 case 'b': /* --brightness */ 134 $brightness = floatval($arg); 135 break; 136 case 'c': /* --contrast */ 137 $contrast = floatval($arg); 138 break; 139 case 'h': /* --help */ 140 usage($argc, $argv); 141 return 0; 142 case 'v': /* --version */ 143 version(); 144 return 0; 145 default: 146 return 1; 147 } 148 } 149 150 151 $cv = caca_create_canvas(0, 0); 152 if(!$cv) 153 { 154 fprintf(STDERR, "%s: unable to initialise libcaca\n", $argv[0]); 155 return 1; 156 } 157 158 $i_str = file_get_contents($argv[$argc-1]); 159 $i = $i_str ? imagecreatefromstring($i_str) : NULL; 160 if(!$i) 161 { 162 fprintf(STDERR, "%s: unable to load %s\n", $argv[0], $argv[$argc-1]); 163 return 1; 164 } 165 166 /* Assume a 6×10 font */ 167 if(!$cols && !$lines) 168 { 169 $cols = 60; 170 $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height; 171 } 172 else if($cols && !$lines) 173 { 174 $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height; 175 } 176 else if(!$cols && $lines) 177 { 178 $cols = $lines * imagesx($i) * $font_height / imagesy($i) / $font_width; 179 } 180 181 182 caca_set_canvas_size($cv, $cols, $lines); 183 caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT); 184 caca_clear_canvas($cv); 185 $i_dither = caca_create_dither($i); 186 if(! caca_set_dither_algorithm($i_dither, $dither?$dither:"fstein")) 187 { 188 fprintf(STDERR, "%s: Can't dither image with algorithm '%s'\n", $argv[0], $dither?$dither:"fstein"); 189 return -1; 190 } 191 192 if($brightness!=-1) caca_set_dither_brightness ($i_dither, $brightness); 193 if($contrast!=-1) caca_set_dither_contrast ($i_dither, $contrast); 194 if($gamma!=-1) caca_set_dither_gamma ($i_dither, $gamma); 195 196 caca_dither_bitmap($cv, 0, 0, $cols, $lines, $i_dither, $i); 197 198 $export = caca_export_string($cv, $format?$format:"ansi"); 199 if(!$export) 200 { 201 fprintf(STDERR, "%s: Can't export to format '%s'\n", $argv[0], $format); 202 } 203 else 204 { 205 echo $export; 206 } 207 208 return 0; 209 } 210 exit(main()); 211 ?> -
Property
svn:executable
set to
Note: See TracChangeset
for help on using the changeset viewer.