wbmp support in php4

[email protected] (Rune Nordbøe Skillingstad) Mon, 5 Jun 2000 18:05:55 +0200 (CEST)
Newsgroups php.version4
Message-ID <[email protected]>
I found the imagewbmp() function in php3 somthing usefull,
so I've tried to port it to php4. Is this diff of any use?

Regards,
Rune(sk)
diff (text/plain, 3.7 KB)
--- gd.org/gd.c	Mon Jun  5 16:01:54 2000
+++ gd/gd.c	Mon Jun  5 18:02:08 2000
@@ -117,6 +117,7 @@
 	PHP_FE(imagegif,								NULL)
 	PHP_FE(imagecreatefromjpeg,						NULL)
 	PHP_FE(imagejpeg,								NULL)
+	PHP_FE(imagewbmp,                               NULL)
 	PHP_FE(imagedestroy,							NULL)
 	PHP_FE(imagegammacorrect,						NULL)
 	PHP_FE(imagefill,								NULL)
@@ -779,6 +780,140 @@
 	php_error(E_WARNING, "ImageJpeg: No JPG support in this PHP build");
 	RETURN_FALSE;
 #endif /* HAVE_GD_JPG */
+}
+/* }}} */
+
+/* {{{ proto int imagewbmp(int im [, string filename])
+   Output WBMP image to browser or file */
+PHP_FUNCTION(imagewbmp)
+{
+	zval **imgind, **file;
+	gdImagePtr im;
+	char *fn=NULL;
+	FILE *fp;
+	int argc;
+	int output=1;
+	int x, y;
+	int c, p, width, height;
+	GDLS_FETCH();
+
+	
+	argc = ARG_COUNT(ht);
+	if (argc < 1 || argc > 2 || zend_get_parameters_ex(argc, &imgind, &file) == FAILURE) {
+		WRONG_PARAM_COUNT;
+	}
+
+	ZEND_FETCH_RESOURCE(im, gdImagePtr, imgind, -1, "Image", GDG(le_gd));
+
+	if (argc == 2) {
+		convert_to_string_ex(file);
+		fn = (*file)->value.str.val;
+		if (!fn || fn == empty_string || php_check_open_basedir(fn)) {
+			php_error(E_WARNING, "ImageWbmp: Invalid filename");
+			RETURN_FALSE;
+		}
+	}
+
+	if (argc == 2) {
+		fp = V_FOPEN(fn, "wb");
+		if (!fp) {
+			php_error(E_WARNING, "ImageWbmp: unable to open %s for writing", fn);
+			RETURN_FALSE;
+		}
+		//		gdImageGif(im, fp);
+		/* WBMP header, black and white, no compression */
+		fputc(0,fp); fputc(0,fp);
+		
+		/* Width and height of image */
+		c = 1; width = im->sx;
+		while(width & 0x7f << 7*c) c++;
+		while(c > 1) fputc(0x80 | ((width >> 7*--c) & 0xff), fp);
+		fputc(width & 0x7f,fp);
+		c = 1; height = im->sy;
+		while(height & 0x7f << 7*c) c++;
+		while(c > 1) fputc(0x80 | ((height >> 7*--c) & 0xff), fp);
+		fputc(height & 0x7f,fp);
+		
+		/* Actual image data */
+		for(y = 0; y < im->sy; y++) {
+			p = c = 0;
+			for(x = 0; x < im->sx; x++) {
+#if HAVE_GD_ANCIENT
+				if(im->pixels[x][y] == 0) c = c | (1 << (7-p));
+#else
+				if(im->pixels[y][x] == 0) c = c | (1 << (7-p));
+#endif
+				if(++p == 8) {
+					fputc(c,fp);
+					p = c = 0;
+				}
+			}
+			if(p) fputc(c,fp);
+		}
+		fflush(fp);
+		fclose(fp);
+	}
+	else {
+		output = php_header();
+
+		if (output) {
+#if APACHE && defined(CHARSET_ECDIC)
+			{
+				SLS_FETCH();
+				/* This is a binary file already: avoid EBCDIC->ASCII conversion */
+				ap_bsetflag(((request_rec *) SG(server_context))->connection->client, B_EBCDIC2ASCII, 0);
+			}
+#endif
+			
+			/* WBMP header, black and white, no compression */
+			char buff = '0';
+			(void)PUTC(buff); 
+			(void)PUTC(buff);
+			
+			/* Width and height of image */
+			c = 1; width = im->sx;
+			while(width & 0x7f << 7*c) c++;
+			while(c > 1) {
+				buff = (0x80 | ((width >> 7*--c) & 0xff));
+				(void)PUTC(buff);
+			}
+			buff = (width & 0x7f);
+			(void)PUTC(buff);
+
+			
+			c = 1; height = im->sy;
+			while(height & 0x7f << 7*c) c++;
+			while(c > 1) {
+				buff = (0x80 | ((height >> 7*--c) & 0xff));
+				(void)PUTC(buff);
+			}
+			buff = (height & 0x7f);
+			(void)PUTC(buff);
+			
+			/* Actual image data */
+			for(y = 0; y < im->sy; y++) {
+				p = c = 0;
+				for(x = 0; x < im->sx; x++) {
+#if HAVE_GD_ANCIENT
+					if(im->pixels[x][y] == 0) c = c | (1 << (7-p));
+#else
+					if(im->pixels[y][x] == 0) c = c | (1 << (7-p));
+#endif
+					if(++p == 8) {
+						buff = c;
+						(void)PUTC(buff);
+						p = c = 0;
+					}
+				}
+				if(p) {
+					buff = c;
+					(void)PUTC(buff);
+				}
+			}
+		}
+	}
+
+	RETURN_TRUE;
 }
 /* }}} */