Explorar el Código

no more horrible hacks needed, since fixing the alpha mismatch in the C binding

tags/v0.99.beta17
Ben Wiley Sittler bsittler hace 16 años
padre
commit
d71ddf0f51
Se han modificado 2 ficheros con 189 adiciones y 222 borrados
  1. +1
    -3
      caca-php/examples/export.php
  2. +188
    -219
      caca-php/examples/img2txt.php

+ 1
- 3
caca-php/examples/export.php Ver fichero

@@ -26,8 +26,6 @@ define('WIDTH', 80);
define('HEIGHT', 32); define('HEIGHT', 32);


$pixels = imagecreatetruecolor(256, 256); $pixels = imagecreatetruecolor(256, 256);
imagealphablending($pixels, false);
imagesavealpha($pixels, true);


$exports = caca_get_export_list(); $exports = caca_get_export_list();


@@ -80,7 +78,7 @@ else
$r = $x; $r = $x;
$g = (255 - $y + $x) / 2; $g = (255 - $y + $x) / 2;
$b = $y * (255 - $x) / 256; $b = $y * (255 - $x) / 256;
imagesetpixel($pixels, $x, $y, imagecolorallocatealpha($pixels, $r, $g, $b, 127)); imagesetpixel($pixels, $x, $y, imagecolorallocate($pixels, $r, $g, $b));
} }
} }




+ 188
- 219
caca-php/examples/img2txt.php Ver fichero

@@ -1,3 +1,5 @@
#!/usr/bin/php5
<?php
/* /*
* img2txt image to text converter * img2txt image to text converter
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org> * Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
@@ -13,230 +15,197 @@
* http://sam.zoy.org/wtfpl/COPYING for more details. * http://sam.zoy.org/wtfpl/COPYING for more details.
*/ */


#include "config.h" if (php_sapi_name() != "cli") {

die("You have to run this program with php-cli!\n");
#if !defined(__KERNEL__) }
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
#endif

#if !defined HAVE_GETOPT_LONG
# include "mygetopt.h"
#elif defined HAVE_GETOPT_H
# include <getopt.h>
#endif
#if defined HAVE_GETOPT_LONG
# define mygetopt getopt_long
# define myoptind optind
# define myoptarg optarg
# define myoption option
#endif

#include "caca.h"

#include "common-image.h"

#define IMG2TXTVERSION "0.1"


static void usage(int argc, char **argv) function usage($argc, $argv)
{ {
char const * const * list; fprintf(STDERR, "Usage: %s [OPTIONS]... <IMAGE>\n", $argv[0]);

fprintf(STDERR, "Convert IMAGE to any text based available format.\n");
fprintf(stderr, "Usage: %s [OPTIONS]... <IMAGE>\n", argv[0]); fprintf(STDERR, "Example : %s -w 80 -f ansi ./caca.png\n\n", $argv[0]);
fprintf(stderr, "Convert IMAGE to any text based available format.\n"); fprintf(STDERR, "Options:\n");
fprintf(stderr, "Example : %s -w 80 -f ansi ./caca.png\n\n", argv[0]); fprintf(STDERR, " -h, --help\t\t\tThis help\n");
fprintf(stderr, "Options:\n"); fprintf(STDERR, " -v, --version\t\t\tVersion of the program\n");
fprintf(stderr, " -h, --help\t\t\tThis help\n"); fprintf(STDERR, " -W, --width=WIDTH\t\tWidth of resulting image\n");
fprintf(stderr, " -v, --version\t\t\tVersion of the program\n"); fprintf(STDERR, " -H, --height=HEIGHT\t\tHeight of resulting image\n");
fprintf(stderr, " -W, --width=WIDTH\t\tWidth of resulting image\n"); fprintf(STDERR, " -x, --font-width=WIDTH\t\tWidth of output font\n");
fprintf(stderr, " -H, --height=HEIGHT\t\tHeight of resulting image\n"); fprintf(STDERR, " -y, --font-height=HEIGHT\t\tHeight of output font\n");
fprintf(stderr, " -x, --font-width=WIDTH\t\tWidth of output font\n"); fprintf(STDERR, " -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n");
fprintf(stderr, " -y, --font-height=HEIGHT\t\tHeight of output font\n"); fprintf(STDERR, " -c, --contrast=CONTRAST\tContrast of resulting image\n");
fprintf(stderr, " -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n"); fprintf(STDERR, " -g, --gamma=GAMMA\t\tGamma of resulting image\n");
fprintf(stderr, " -c, --contrast=CONTRAST\tContrast of resulting image\n"); fprintf(STDERR, " -d, --dither=DITHER\t\tDithering algorithm to use :\n");
fprintf(stderr, " -g, --gamma=GAMMA\t\tGamma of resulting image\n"); $list = caca_get_dither_algorithm_list(caca_create_dither(imagecreate(1, 1)));
fprintf(stderr, " -d, --dither=DITHER\t\tDithering algorithm to use :\n"); foreach($list as $type => $name)
list = caca_get_dither_algorithm_list(NULL); {
while(*list) fprintf(STDERR, "\t\t\t%s: %s\n", $type, $name);
{ }
fprintf(stderr, "\t\t\t%s: %s\n", list[0], list[1]); fprintf(STDERR, " -f, --format=FORMAT\t\tFormat of the resulting image :\n");
list += 2; $list = caca_get_export_list();
} foreach($list as $type => $name)

{
fprintf(stderr, " -f, --format=FORMAT\t\tFormat of the resulting image :\n"); fprintf(STDERR, "\t\t\t%s: %s\n", $type, $name);
list = caca_get_export_list(); }
while(*list)
{
fprintf(stderr, "\t\t\t%s: %s\n", list[0], list[1]);
list += 2;
}

#if !defined(USE_IMLIB2)
fprintf(stderr, "NOTE: This program has NOT been built with Imlib2 support. Only BMP loading is supported.\n");
#endif
} }


static void version(void) function version()
{ {
printf( printf(
"img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n" "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n" .
"Internet: <sam@zoy.org> <jylam@lnxscene.org> Version: %s, date: %s\n" "Internet: <sam@zoy.org> <jylam@lnxscene.org> Version: %s\n" .
"\n" "\n" .
"img2txt, along with its documentation, may be freely copied and distributed.\n" "img2txt, along with its documentation, may be freely copied and distributed.\n" .
"\n" "\n" .
"The latest version of img2txt is available from the web site,\n" "The latest version of img2txt is available from the web site,\n" .
" http://caca.zoy.org/wiki/libcaca in the libcaca package.\n" " http://caca.zoy.org/wiki/libcaca in the libcaca package.\n" .
"\n", "\n",
caca_get_version(), __DATE__); caca_get_version());
} }
int main(int argc, char **argv) function main()
{ {
/* libcaca context */ global $argc, $argv;
caca_canvas_t *cv; $cols = 0;
void *export; $lines = 0;
size_t len; $font_width = 6;
struct image *i; $font_height = 10;
unsigned int cols = 0, lines = 0, font_width = 6, font_height = 10; $format = NULL;
char *format = NULL; $dither = NULL;
char *dither = NULL; $gamma = $brightness = $contrast = -1.0;
float gamma = -1, brightness = -1, contrast = -1; if($argc < 2)

{
if(argc < 2) fprintf(STDERR, "%s: wrong argument count\n", $argv[0]);
{ usage($argc, $argv);
fprintf(stderr, "%s: wrong argument count\n", argv[0]); return 1;
usage(argc, argv); }
return 1; $long_options = array(
} "width:" => 'W',

"height:" => 'H',
for(;;) "font-width:" => 'x',
{ "font-height:" => 'y',
int option_index = 0; "format:" => 'f',
static struct myoption long_options[] = "dither:" => 'd',
{ "gamma:" => 'g',
{ "width", 1, NULL, 'W' }, "brightness:" => 'b',
{ "height", 1, NULL, 'H' }, "contrast:" => 'c',
{ "font-width", 1, NULL, 'x' }, "help" => 'h',
{ "font-height", 1, NULL, 'y' }, "version" => 'v'
{ "format", 1, NULL, 'f' }, );
{ "dither", 1, NULL, 'd' }, $options = getopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options));
{ "gamma", 1, NULL, 'g' }, if (! $options)
{ "brightness", 1, NULL, 'b' }, {
{ "contrast", 1, NULL, 'c' }, /* PHP before 5.3 or so does not have long option support in
{ "help", 0, NULL, 'h' }, * most cases */
{ "version", 0, NULL, 'v' }, $options = getopt("W:H:f:d:g:b:c:hvx:y:");
}; }
int c = mygetopt(argc, argv, "W:H:f:d:g:b:c:hvx:y:", long_options, &option_index); foreach($options as $opt => $arg)
if(c == -1) {
break; if (array_key_exists($opt + (isset($arg) ? ':' : ''), $long_options))

{
switch(c) $opt = $long_options[$opt + (isset($arg) ? ':' : '')];
{ }
case 'W': /* --width */ switch($opt)
cols = atoi(myoptarg); {
break; case 'W': /* --width */
case 'H': /* --height */ $cols = intval($arg);
lines = atoi(myoptarg); break;
break; case 'H': /* --height */
case 'x': /* --width */ $lines = intval($arg);
font_width = atoi(myoptarg); break;
break; case 'x': /* --width */
case 'y': /* --height */ $font_width = intval($arg);
font_height = atoi(myoptarg); break;
break; case 'y': /* --height */
case 'f': /* --format */ $font_height = intval($arg);
format = myoptarg; break;
break; case 'f': /* --format */
case 'd': /* --dither */ $format = $arg;
dither = myoptarg; break;
break; case 'd': /* --dither */
case 'g': /* --gamma */ $dither = $arg;
gamma = atof(myoptarg); break;
break; case 'g': /* --gamma */
case 'b': /* --brightness */ $gamma = floatval($arg);
brightness = atof(myoptarg); break;
break; case 'b': /* --brightness */
case 'c': /* --contrast */ $brightness = floatval($arg);
contrast = atof(myoptarg); break;
break; case 'c': /* --contrast */
case 'h': /* --help */ $contrast = floatval($arg);
usage(argc, argv); break;
return 0; case 'h': /* --help */
break; usage($argc, $argv);
case 'v': /* --version */ return 0;
version(); case 'v': /* --version */
return 0; version();
break; return 0;
default: default:
return 1; return 1;
break; }
} }
} $cv = caca_create_canvas(0, 0);

if(!$cv)

{
cv = caca_create_canvas(0, 0); fprintf(STDERR, "%s: unable to initialise libcaca\n", $argv[0]);
if(!cv) return 1;
{ }
fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]); $i_str = file_get_contents($argv[$argc-1]);
return 1; $i = $i_str ? imagecreatefromstring($i_str) : NULL;
} if(!$i)

{
i = load_image(argv[argc-1]); fprintf(STDERR, "%s: unable to load %s\n", $argv[0], $argv[$argc-1]);
if(!i) return 1;
{ }
fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[argc-1]); /* Assume a 6×10 font */
caca_free_canvas(cv); if(!$cols && !$lines)
return 1; {
} $cols = 60;

$lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height;
/* Assume a 6×10 font */ }
if(!cols && !lines) else if($cols && !$lines)
{ {
cols = 60; $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height;
lines = cols * i->h * font_width / i->w / font_height; }
} else if(!$cols && $lines)
else if(cols && !lines) {
{ $cols = $lines * imagesx($i) * $font_height / imagesy($i) / $font_width;
lines = cols * i->h * font_width / i->w / font_height; }
} caca_set_canvas_size($cv, $cols, $lines);
else if(!cols && lines) caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT);
{ caca_clear_canvas($cv);
cols = lines * i->w * font_height / i->h / font_width; $i_dither = caca_create_dither($i);
} if(! caca_set_dither_algorithm($i_dither, $dither?$dither:"fstein"))

{

fprintf(STDERR, "%s: Can't dither image with algorithm '%s'\n", $argv[0], $dither?$dither:"fstein");
caca_set_canvas_size(cv, cols, lines); return -1;
caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT); }
caca_clear_canvas(cv); if($brightness!=-1) caca_set_dither_brightness ($i_dither, $brightness);
if(caca_set_dither_algorithm(i->dither, dither?dither:"fstein")) if($contrast!=-1) caca_set_dither_contrast ($i_dither, $contrast);
{ if($gamma!=-1) caca_set_dither_gamma ($i_dither, $gamma);
fprintf(stderr, "%s: Can't dither image with algorithm '%s'\n", argv[0], dither); caca_dither_bitmap($cv, 0, 0, $cols, $lines, $i_dither, $i);
unload_image(i); $export = caca_export_string($cv, $format?$format:"ansi");
caca_free_canvas(cv); if(!$export)
return -1; {
} fprintf(STDERR, "%s: Can't export to format '%s'\n", $argv[0], $format);

}
if(brightness!=-1) caca_set_dither_brightness (i->dither, brightness); else
if(contrast!=-1) caca_set_dither_contrast (i->dither, contrast); {
if(gamma!=-1) caca_set_dither_gamma (i->dither, gamma); echo $export;

}
caca_dither_bitmap(cv, 0, 0, cols, lines, i->dither, i->pixels); return 0;

unload_image(i);

export = caca_export_memory(cv, format?format:"ansi", &len);
if(!export)
{
fprintf(stderr, "%s: Can't export to format '%s'\n", argv[0], format);
}
else
{
fwrite(export, len, 1, stdout);
free(export);
}

caca_free_canvas(cv);

return 0;
} }

exit(main());
?>

||||||
x
 
000:0
Cargando…
Cancelar
Guardar