]> granicus.if.org Git - php/commitdiff
Add ImageXBM
authorMarcus Boerger <helly@php.net>
Sun, 15 Jun 2003 20:00:08 +0000 (20:00 +0000)
committerMarcus Boerger <helly@php.net>
Sun, 15 Jun 2003 20:00:08 +0000 (20:00 +0000)
ext/gd/gd.c
ext/gd/gd_ctx.c
ext/gd/libgd/gd.h
ext/gd/libgd/xbm.c
ext/gd/php_gd.h

index 3d12901f5a3ff017f8bae2359cbd29bce0020222..e2ea2c483a46ca021ee76a525566ef87ef0daef8 100644 (file)
@@ -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 */
index 89853c1e60d0128ca2fbc483c5dffb96db2f4d6d..ae318051c9c33422877474e3ca80426e47705cbc 100644 (file)
@@ -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 <stas@php.net>                           |
+   +----------------------------------------------------------------------+
+ */
+
+/* $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<type>().
+        */
        
+       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
+ */
index 2f982916ed0b976fd4cc56d1b59f5a16fc3e6797..4742fd5ab94de1058e59427b8a733e311641fc87 100644 (file)
@@ -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);
 
index f3a22e6617809fe7bbb9cb1681d130fe29eab769..100fe9b950fe6e42ffe707724f2959c21b48d536 100644 (file)
@@ -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<l; i++) {
+                       /* only in C-locale isalnum() would work */
+                       if (!isupper(name[i]) && !islower(name[i]) && !isdigit(name[i])) {
+                               name[i] = '_';
+                       }
+               }
+       }
+
+       gdCtxPrintf(out, "#define %s_width %d\n", name, gdImageSX(image));
+       gdCtxPrintf(out, "#define %s_height %d\n", name, gdImageSY(image));
+       gdCtxPrintf(out, "static unsigned char %s_bits[] = {\n  ", name);
+
+       efree(name);
+
+       b = 1;
+       p = 0;
+       c = 0;
+       sx = gdImageSX(image);
+       sy = gdImageSY(image);
+       for (y = 0; y < sy; y++) {
+               for (x = 0; x < sx; x++) {
+                       if (gdImageGetPixel(image, x, y) == fg) {
+                               c |= b;
+                       }
+                       if ((b == 128) || (x == sx && y == sy)) {
+                               b = 1;
+                               if (p) {
+                                       gdCtxPrintf(out, ", ");
+                                       if (!(p%12)) {
+                                               gdCtxPrintf(out, "\n  ");
+                                               p = 12;
+                                       }
+                               }
+                               p++;
+                               gdCtxPrintf(out, "0x%02X", c);
+                               c = 0;
+                       } else {
+                               b <<= 1;
+                       }
+               }
+       }
+       gdCtxPrintf(out, "};\n");
+}
+/* }}} */
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: sw=4 ts=4 fdm=marker
+ * vim<600: sw=4 ts=4
+ */
index 0b8342e3dd3571c25a6440ccfb75772a34cf7f9f..ba1d600e355eb4f32845d8740f4e267945026a10 100644 (file)
@@ -175,6 +175,7 @@ PHP_FUNCTION(image2wbmp);
 PHP_FUNCTION(imagelayereffect);
 PHP_FUNCTION(imagecolormatch);
 PHP_FUNCTION(imagefilter);
+PHP_FUNCTION(imagexbm);
 #endif
 
 PHP_GD_API int phpi_get_le_gd(void);