From 8fd5e11ff4b2864c79b33a595de881b946d94600 Mon Sep 17 00:00:00 2001 From: Marcus Boerger Date: Sun, 15 Jun 2003 20:00:08 +0000 Subject: [PATCH] Add ImageXBM --- ext/gd/gd.c | 11 ++++++ ext/gd/gd_ctx.c | 60 ++++++++++++++++++++++++++----- ext/gd/libgd/gd.h | 1 + ext/gd/libgd/xbm.c | 90 ++++++++++++++++++++++++++++++++++++++++++++-- ext/gd/php_gd.h | 1 + 5 files changed, 153 insertions(+), 10 deletions(-) diff --git a/ext/gd/gd.c b/ext/gd/gd.c index 3d12901f5a..e2ea2c483a 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -306,6 +306,7 @@ function_entry gd_functions[] = { #if HAVE_GD_BUNDLED PHP_FE(imagelayereffect, NULL) PHP_FE(imagecolormatch, NULL) + PHP_FE(imagexbm, NULL) #endif /* gd filters */ #ifdef HAVE_GD_BUNDLED @@ -1782,6 +1783,16 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char } /* }}} */ +/* {{{ proto int imagexbm(int im, string filename [, int foreground]) + Output XBM image to browser or file */ +#if HAVE_GD_BUNDLED +PHP_FUNCTION(imagexbm) +{ + _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XBM, "XBM", gdImageXbmCtx); +} +#endif +/* }}} */ + #ifdef HAVE_GD_GIF_CREATE /* {{{ proto bool imagegif(resource im [, string filename]) Output GIF image to browser or file */ diff --git a/ext/gd/gd_ctx.c b/ext/gd/gd_ctx.c index 89853c1e60..ae318051c9 100644 --- a/ext/gd/gd_ctx.c +++ b/ext/gd/gd_ctx.c @@ -1,5 +1,24 @@ -#include "php_gd.h" +/* + +----------------------------------------------------------------------+ + | PHP Version 4 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2003 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.0 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_0.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev | + +----------------------------------------------------------------------+ + */ + +/* $Id$ */ +#include "php_gd.h" #define CTX_PUTC(c,ctx) ctx->putC(ctx, c) @@ -21,7 +40,8 @@ static void _php_image_output_ctxfree(struct gdIOCtx *ctx) efree(ctx); } } - + +/* {{{ _php_image_output_ctx */ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)()) { zval **imgind, **file, **quality; @@ -32,8 +52,14 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, int q = -1, i; gdIOCtx *ctx; - /* The quality parameter for Wbmp stands for the threshold when called from image2wbmp() */ + /* The third (quality) parameter for Wbmp stands for the threshold when called from image2wbmp(). + * The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called + * from imagey(). + */ + if (argc < 2 && image_type == PHP_GDIMG_TYPE_XBM) { + WRONG_PARAM_COUNT; + } if (argc < 1 || argc > 3 || zend_get_parameters_ex(argc, &imgind, &file, &quality) == FAILURE) { WRONG_PARAM_COUNT; @@ -46,7 +72,7 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, fn = Z_STRVAL_PP(file); if (argc == 3) { convert_to_long_ex(quality); - q = Z_LVAL_PP(quality); + q = Z_LVAL_PP(quality);/* or colorindex for foreground of BW images (defaults to black) */ } } @@ -88,11 +114,19 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, case PHP_GDIMG_TYPE_JPG: (*func_p)(im, ctx, q); break; + case PHP_GDIMG_TYPE_XBM: case PHP_GDIMG_TYPE_WBM: - for(i=0; i < gdImageColorsTotal(im); i++) { - if(gdImageRed(im, i) == 0) break; - } - (*func_p)(im, i, ctx); + if (argc < 3) { + for(i=0; i < gdImageColorsTotal(im); i++) { + if(!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break; + } + q = i; + } + if (image_type == PHP_GDIMG_TYPE_XBM) { + (*func_p)(im, fn, q, ctx); + } else { + (*func_p)(im, q, ctx); + } break; default: (*func_p)(im, ctx); @@ -112,3 +146,13 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, RETURN_TRUE; } +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/ext/gd/libgd/gd.h b/ext/gd/libgd/gd.h index 2f982916ed..4742fd5ab9 100644 --- a/ext/gd/libgd/gd.h +++ b/ext/gd/libgd/gd.h @@ -256,6 +256,7 @@ gdImagePtr gdImageCreateFromGd2Part(FILE *in, int srcx, int srcy, int w, int h); gdImagePtr gdImageCreateFromGd2PartCtx(gdIOCtxPtr in, int srcx, int srcy, int w, int h); gdImagePtr gdImageCreateFromXbm(FILE *fd); +void gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOCtx * out); gdImagePtr gdImageCreateFromXpm (char *filename); diff --git a/ext/gd/libgd/xbm.c b/ext/gd/libgd/xbm.c index f3a22e6617..100fe9b950 100644 --- a/ext/gd/libgd/xbm.c +++ b/ext/gd/libgd/xbm.c @@ -29,8 +29,8 @@ #define MAX_XBM_LINE_SIZE 255 -gdImagePtr -gdImageCreateFromXbm (FILE * fd) +/* {{{ gdImagePtr gdImageCreateFromXbm */ +gdImagePtr gdImageCreateFromXbm(FILE * fd) { char fline[MAX_XBM_LINE_SIZE]; char iname[MAX_XBM_LINE_SIZE]; @@ -151,3 +151,89 @@ gdImageCreateFromXbm (FILE * fd) gdImageDestroy(im); return 0; } +/* }}} */ + +/* {{{ gdCtxPrintf */ +void gdCtxPrintf(gdIOCtx * out, const char *format, ...) +{ + char *buf; + int len; + va_list args; + + va_start(args, format); + len = vspprintf(&buf, 0, format, args); + va_end(args); + out->putBuf(out, buf, len); + efree(buf); +} +/* }}} */ + +/* {{{ gdImageXbmCtx */ +void gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOCtx * out) +{ + int x, y, c, b, sx, sy, p; + char *name, *f; + size_t i, l; + + name = file_name; + if ((f = strrchr(name, '/')) != NULL) name = f+1; + if ((f = strrchr(name, '\\')) != NULL) name = f+1; + name = estrdup(name); + if ((f = strrchr(name, '.')) != NULL && !strcasecmp(f, ".XBM")) *f = '\0'; + if ((l = strlen(name)) == 0) { + efree(name); + name = estrdup("image"); + } else { + for (i=0; i