index.html

来自「CA仿真模型中SLEUTH模型」· HTML 代码 · 共 1,606 行 · 第 1/5 页

HTML
1,606
字号
<p>In addition, a diagonal line made up of tiny space shuttleimages should appear from the lower left to the upperright corner.   A blue frame with green interior trim should surround the picture.<P>(If you are missing the demoin.gif file, the other itemsshould appear anyway.)<P>Look at demoin.gif to see the original space shuttleimage which was scaled and copied into the output image.<P><A NAME="basics"><H3>gd basics: using gd in your program</H3></A>gd lets you create GIF images on the fly. To use gd in yourprogram, include the file gd.h, and link with the libgd.alibrary produced by "make libgd.a", under Unix. You willneed to adapt the makefile for your needs if you are usinga non-Unix operating system, but this is very straightforward.<P>If you want to use the provided fonts, includegdfontt.h, gdfonts.h, gdfontmb.h, gdfontl.h and/or gdfontg.h. If you are not using the provided Makefile and/or a library-based approach, besure to include the source modules as well in yourproject. (They may be too large for 16-bit memory models,that is, 16-bit DOS and Windows.)<P>Here is a short example program. <strong>(For a more advanced example,see gddemo.c, included in the distribution. gddemo.c is NOT the same program; it demonstrates additional features!)</strong><P><PRE>/* Bring in gd library functions */#include "gd.h"/* Bring in standard I/O so we can output the GIF to a file */#include &lt;stdio.h&gt;int main() {	/* Declare the image */	<A HREF="#gdImagePtr">gdImagePtr</A> im;	/* Declare an output file */	FILE *out;	/* Declare color indexes */	int black;	int white;	/* Allocate the image: 64 pixels across by 64 pixels tall */	im = <A HREF="#gdImageCreate">gdImageCreate</A>(64, 64);	/* Allocate the color black (red, green and blue all minimum).		Since this is the first color in a new image, it will		be the background color. */	black = <A HREF="#gdImageColorAllocate">gdImageColorAllocate</A>(im, 0, 0, 0);		/* Allocate the color white (red, green and blue all maximum). */	white = <A HREF="#gdImageColorAllocate">gdImageColorAllocate</A>(im, 255, 255, 255);			/* Draw a line from the upper left to the lower right,		using white color index. */	<A HREF="#gdImageLine">gdImageLine</A>(im, 0, 0, 63, 63, white);		/* Open a file for writing. "wb" means "write binary", important		under MSDOS, harmless under Unix. */	out = fopen("test.gif", "wb");	/* Output the image to the disk file. */	<A HREF="#gdImageGif">gdImageGif</A>(im, out);		/* Close the file. */	fclose(out);	/* Destroy the image in memory. */	<A HREF="#gdImageDestroy">gdImageDestroy</A>(im);}</PRE>When executed, this program creates an image, allocatestwo colors (the first color allocated becomes the backgroundcolor), draws a diagonal line (note that 0, 0 is the upperleft corner), writes the image to a GIF file, anddestroys the image.<P>The above example program shouldgive you an idea of how the package works.gd provides many additional functions, which are listedin the following reference chapters, complete with codesnippets demonstrating each. There is also an<A HREF="#index">alphabetical index</A>. <H3><A NAME="webgif">Webgif: a more powerful gd example</A></H3>Webgif is a simple utility program to manipulate GIFs from thecommand line. It is written for Unix and similar command-linesystems, but should be easily adapted for other environments. Webgif allows you to set transparency and interlacing andoutput interesting information about the GIF in question.<P>webgif.c is provided in the distribution. Unix users cansimply type "make webgif" to compile the program. Type"webgif" with no arguments to see the available options.A discussion of the code follows.<PRE>/* Bring in the gd library functions */#include "gd.h"/* Bring in standard I/O and string manipulation functions */#include <stdio.h>#include <string.h>int main(argc, argv) 	int argc;	char *argv[];{	FILE *in;	FILE *out;	/* Declare our image pointer */	<A HREF="#gdImagePtr">gdImagePtr</A> im = 0;	int i;       /* We'll clear 'no' once we know the user has made a		reasonable request. */	int no = 1;	/* We'll set 'write' once we know the user's request		requires that the image be written back to disk. */	int write = 0;	/* C programs always get at least one argument; we want at		least one more (the image), more in practice. */	if (argc < 2) {		no = 1;			goto usage;	}	/* The last argument should be the image. Open the file. */	in = fopen(argv[argc-1], "rb");		if (!in) {		fprintf(stderr,			"Error: can't open file %s.\n", argv[argc-1]);	}	/* Now load the image. */		im = <A HREF="#gdImageCreateFromGif">gdImageCreateFromGif</A>(in);	fclose(in);	/* If the load failed, it must not be a GIF file. */	if (!im) {		fprintf(stderr,			"Error: %s is not a valid gif file.\n", argv[1]);		exit(1);		}	/* Consider each argument in turn. */	for (i=1; (i < (argc-1)); i++) {		/* -i turns on and off interlacing. */		if (!strcmp(argv[i], "-i")) {			if (i == (argc-2)) {				fprintf(stderr, 				"Error: -i specified without y or n.\n");				no = 1;				goto usage;			}			if (!strcmp(argv[i+1], "y")) {				/* Set interlace. */				<A HREF="#gdImageInterlace">gdImageInterlace</A>(im, 1);			} else if (!strcmp(argv[i+1], "n")) {				/* Clear interlace. */				<A HREF="#gdImageInterlace">gdImageInterlace</A>(im, 0);			} else {				fprintf(stderr,				"Error: -i specified without y or n.\n");				no = 1;				goto usage;			}			i++;			no = 0;			write = 1;		} else if (!strcmp(argv[i], "-t")) {			/* Set transparent index (or none). */			int index;			if (i == (argc-2)) {				fprintf(stderr,		"Error: -t specified without a color table index.\n");				no = 1;				goto usage;			}			if (!strcmp(argv[i+1], "none")) {				/* -1 means not transparent. */				<A HREF="#gdImageColorTransparent">gdImageColorTransparent</A>(im, -1);			} else {				/* OK, get an integer and set the index. */				index = atoi(argv[i+1]);				<A HREF="#gdImageColorTransparent">gdImageColorTransparent</A>(im, index);			}			i++;			write = 1;			no = 0;		} else if (!strcmp(argv[i], "-l")) {			/* List the colors in the color table. */			int j;			/* Tabs used below. */			printf("Index	Red	Green	Blue\n");			for (j=0; (j < <A HREF="#gdImageColorsTotal">gdImageColorsTotal</A>(im)); j++) {				/* Use access macros to learn colors. */				printf("%d	%d	%d	%d\n",					j, 					<A HREF="#gdImageRed">gdImageRed</A>(im, j),					<A HREF="#gdImageGreen">gdImageGreen</A>(im, j),					<A HREF="#gdImageBlue">gdImageBlue</A>(im, j));			}			no = 0;		} else if (!strcmp(argv[i], "-d")) {			/* Output dimensions, etc. */			int t;			printf("Width: %d Height: %d Colors: %d\n",				<A HREF="#gdImageSX">gdImageSX</A>(im), <A HREF="#gdImageSY">gdImageSY</A>(im),				<A HREF="#gdImageColorsTotal">gdImageColorsTotal</A>(im));			t = <A HREF="#gdImageGetTransparent">gdImageGetTransparent</A>(im);			if (t != (-1)) {				printf("Transparent index: %d\n", t);			} else {				/* -1 means the image is not transparent. */				printf("Transparent index: none\n");			}			if (<A HREF="#gdImageGetInterlaced">gdImageGetInterlaced</A>(im)) {				printf("Interlaced: yes\n");				} else {				printf("Interlaced: no\n");				}			no = 0;		} else {			fprintf(stderr, "Unknown argument: %s\n", argv[i]);			break;			}	}usage:	if (no) {		/* If the command failed, output an explanation. */		fprintf(stderr, 	"Usage: webgif [-i y|n ] [-l] [-t index|off ] [-d] gifname.gif\n");		fprintf(stderr, 	"Where -i controls interlace (specify y or n for yes or no),\n");		fprintf(stderr, 	"-l outputs a table of color indexes, -t sets the specified\n");		fprintf(stderr, 	"color index (0-255 or none) to be the transparent color, and\n");		fprintf(stderr,	"-d reports the dimensions and other characteristics of the image.\n");		fprintf(stderr, 	"Note: you may wish to pipe to \"more\" when using the -l option.\n");	} 	if (write) {		/* Open a temporary file. */		out = fopen("temp.tmp", "wb");		if (!out) {			fprintf(stderr,				"Unable to write to temp.tmp -- exiting\n");			exit(1);		}		/* Write the new gif. */		<A HREF="#gdImageGif">gdImageGif</A>(im, out);		fclose(out);		/* Erase the old gif. */		unlink(argv[argc-1]);		/* Rename the new to the old. */		rename("temp.tmp", argv[argc-1]);	}	/* Delete the image from memory. */	if (im) {		<A HREF="#gdImageDestroy">gdImageDestroy</A>(im);	}	/* All's well that ends well. */	return 0;}</PRE><H2><A NAME="reference">Function and type reference</A></H2><UL><LI><A HREF="#types">Types</A></LI><LI><A HREF="#creating">Image creation, destruction, loading and saving</A></LI><LI><A HREF="#drawing">Drawing, styling, brushing, tiling and filling functions</A></LI><LI><A HREF="#query">Query functions (not color-related)</A></LI><LI><A HREF="#fonts">Font and text-handling functions</A></LI><LI><A HREF="#colors">Color handling functions</A></LI><LI><A HREF="#copying">Copying and resizing functions</A></LI><LI><A HREF="#misc">Miscellaneous Functions</A></LI><LI><A HREF="#constants">Constants</A></LI></UL><H3><A NAME="types">Types</A></H3><DL><DT><A NAME="gdImage"><code>gdImage</code><strong>(TYPE)</strong></A><DD>The data structure in which gd stores images. <A HREF="#gdImageCreate">gdImageCreate</A> returnsa pointer to this type, and the other functions expect to receivea pointer to this type as their first argument. You mayread the members <code>sx</code> (size on X axis),<code>sy</code> (size on Y axis), <code>colorsTotal</code>(total colors), <code>red</code> (red component of colors;an array of 256 integers between 0 and 255), <code>green</code>(green component of colors, as above), <code>blue</code>(blue component of colors, as above), and <code>transparent</code>(index of transparent color, -1 if none); please do sousing the macros provided. Do NOT set the members directlyfrom your code; use the functions provided.<PRE>typedef struct {	unsigned char ** pixels;	int sx;	int sy;	int colorsTotal;	int red[gdMaxColors];	int green[gdMaxColors];	int blue[gdMaxColors]; 	int open[gdMaxColors];	int transparent;} gdImage;</PRE><DT><A NAME="gdImagePtr">gdImagePtr</A> <strong>(TYPE)</strong><DD>A pointer to an image structure. <A HREF="#gdImageCreate">gdImageCreate</A>returns this type, and the other functions expect it as the firstargument.<DT><A NAME="gdFont">gdFont</A> <strong>(TYPE)</strong><DD>A font structure. Used to declare the characteristics of a font.Plese see the files gdfontl.c and gdfontl.h for an example of theproper declaration of this structure. You can provide yourown font data by providing such a structure and the associatedpixel array. You can determine the width and height of a singlecharacter in a font by examining the w and h members of thestructure. If you will not be creating your own fonts, you will

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?