⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gd.c

📁 PHP v6.0 For Linux 运行环境:Win9X/ WinME/ WinNT/ Win2K/ WinXP
💻 C
📖 第 1 页 / 共 5 页
字号:
	if (dst->trueColor) {		/* 2.0: much easier when the destination is truecolor. */		/* 2.0.10: needs a transparent-index check that is still valid if		 * the source is not truecolor. Thanks to Frank Warmerdam.		 */		if (src->trueColor) {			for (y = 0; (y < h); y++) {				for (x = 0; (x < w); x++) {					int c = gdImageGetTrueColorPixel (src, srcX + x, srcY + y);					gdImageSetPixel (dst, dstX + x, dstY + y, c);				}			}		} else {			/* source is palette based */			for (y = 0; (y < h); y++) {				for (x = 0; (x < w); x++) {					int c = gdImageGetPixel (src, srcX + x, srcY + y);					if (c != src->transparent) {						gdImageSetPixel(dst, dstX + x, dstY + y, gdTrueColorAlpha(src->red[c], src->green[c], src->blue[c], src->alpha[c]));					}				}			}		}		return;	}	/* Destination is palette based */	if (src->trueColor) { /* But source is truecolor (Ouch!) */		toy = dstY;		for (y = srcY; (y < (srcY + h)); y++) {			tox = dstX;			for (x = srcX; x < (srcX + w); x++) {				int nc;				c = gdImageGetPixel (src, x, y);				/* Get best match possible. */				nc = gdImageColorResolveAlpha(dst, gdTrueColorGetRed(c), gdTrueColorGetGreen(c), gdTrueColorGetBlue(c), gdTrueColorGetAlpha(c));				gdImageSetPixel(dst, tox, toy, nc);				tox++;			}			toy++;		}		return;	}	/* Palette based to palette based */	for (i = 0; i < gdMaxColors; i++) {		colorMap[i] = (-1);	}	toy = dstY;	for (y = srcY; y < (srcY + h); y++) {		tox = dstX;		for (x = srcX; x < (srcX + w); x++) {			int nc;			int mapTo;			c = gdImageGetPixel (src, x, y);			/* Added 7/24/95: support transparent copies */			if (gdImageGetTransparent (src) == c) {				tox++;				continue;			}			/* Have we established a mapping for this color? */			if (src->trueColor) {				/* 2.05: remap to the palette available in the destination image. This is slow and				 * works badly, but it beats crashing! Thanks to Padhrig McCarthy.				 */				mapTo = gdImageColorResolveAlpha (dst, gdTrueColorGetRed (c), gdTrueColorGetGreen (c), gdTrueColorGetBlue (c), gdTrueColorGetAlpha (c));			} else if (colorMap[c] == (-1)) {				/* If it's the same image, mapping is trivial */				if (dst == src) {					nc = c;				} else {					/* Get best match possible. This function never returns error. */					nc = gdImageColorResolveAlpha (dst, src->red[c], src->green[c], src->blue[c], src->alpha[c]);				}				colorMap[c] = nc;				mapTo = colorMap[c];			} else {				mapTo = colorMap[c];			}			gdImageSetPixel (dst, tox, toy, mapTo);			tox++;		}		toy++;	}}/* This function is a substitute for real alpha channel operations,   so it doesn't pay attention to the alpha channel. */void gdImageCopyMerge (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h, int pct){	int c, dc;	int x, y;	int tox, toy;	int ncR, ncG, ncB;	toy = dstY;		for (y = srcY; y < (srcY + h); y++) {		tox = dstX;		for (x = srcX; x < (srcX + w); x++) {			int nc;			c = gdImageGetPixel(src, x, y);			/* Added 7/24/95: support transparent copies */			if (gdImageGetTransparent(src) == c) {				tox++;				continue;			}			/* If it's the same image, mapping is trivial */			if (dst == src) {				nc = c;			} else {				dc = gdImageGetPixel(dst, tox, toy); 				ncR = (int)(gdImageRed (src, c) * (pct / 100.0) + gdImageRed (dst, dc) * ((100 - pct) / 100.0)); 				ncG = (int)(gdImageGreen (src, c) * (pct / 100.0) + gdImageGreen (dst, dc) * ((100 - pct) / 100.0)); 				ncB = (int)(gdImageBlue (src, c) * (pct / 100.0) + gdImageBlue (dst, dc) * ((100 - pct) / 100.0));				/* Find a reasonable color */				nc = gdImageColorResolve (dst, ncR, ncG, ncB);			}			gdImageSetPixel (dst, tox, toy, nc);			tox++;		}		toy++;	}}/* This function is a substitute for real alpha channel operations,   so it doesn't pay attention to the alpha channel. */void gdImageCopyMergeGray (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h, int pct){	int c, dc;	int x, y;	int tox, toy;	int ncR, ncG, ncB;	float g;	toy = dstY;	for (y = srcY; (y < (srcY + h)); y++) {		tox = dstX;		for (x = srcX; (x < (srcX + w)); x++) {			int nc;			c = gdImageGetPixel (src, x, y);			/* Added 7/24/95: support transparent copies */			if (gdImageGetTransparent(src) == c) {				tox++;				continue;			}			/*			 * If it's the same image, mapping is NOT trivial since we			 * merge with greyscale target, but if pct is 100, the grey			 * value is not used, so it becomes trivial. pjw 2.0.12.			 */			if (dst == src && pct == 100) {				nc = c;			} else {				dc = gdImageGetPixel(dst, tox, toy);				g = (0.29900f * gdImageRed(dst, dc)) + (0.58700f * gdImageGreen(dst, dc)) + (0.11400f * gdImageBlue(dst, dc));                                ncR = (int)(gdImageRed (src, c) * (pct / 100.0f) + g * ((100 - pct) / 100.0));                                ncG = (int)(gdImageGreen (src, c) * (pct / 100.0f) + g * ((100 - pct) / 100.0));                                ncB = (int)(gdImageBlue (src, c) * (pct / 100.0f) + g * ((100 - pct) / 100.0));				/* First look for an exact match */				nc = gdImageColorExact(dst, ncR, ncG, ncB);				if (nc == (-1)) {					/* No, so try to allocate it */					nc = gdImageColorAllocate(dst, ncR, ncG, ncB);					/* If we're out of colors, go for the closest color */					if (nc == (-1)) {						nc = gdImageColorClosest(dst, ncR, ncG, ncB);					}				}			}			gdImageSetPixel(dst, tox, toy, nc);			tox++;		}		toy++;	}}void gdImageCopyResized (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH){	int c;	int x, y;	int tox, toy;	int ydest;	int i;	int colorMap[gdMaxColors];	/* Stretch vectors */	int *stx, *sty;	/* We only need to use floating point to determine the correct stretch vector for one line's worth. */	double accum;	stx = (int *) gdMalloc (sizeof (int) * srcW);	sty = (int *) gdMalloc (sizeof (int) * srcH);	accum = 0;	/* Fixed by Mao Morimoto 2.0.16 */	for (i = 0; (i < srcW); i++) {		stx[i] = dstW * (i+1) / srcW - dstW * i / srcW ;	}	for (i = 0; (i < srcH); i++) {		sty[i] = dstH * (i+1) / srcH - dstH * i / srcH ;	}	for (i = 0; (i < gdMaxColors); i++) {		colorMap[i] = (-1);	}	toy = dstY;	for (y = srcY; (y < (srcY + srcH)); y++) {		for (ydest = 0; (ydest < sty[y - srcY]); ydest++) {			tox = dstX;			for (x = srcX; (x < (srcX + srcW)); x++) {				int nc = 0;				int mapTo;				if (!stx[x - srcX]) {					continue;				}				if (dst->trueColor) {					/* 2.0.9: Thorben Kundinger: Maybe the source image is not a truecolor image */					if (!src->trueColor) {					  	int tmp = gdImageGetPixel (src, x, y);		  				mapTo = gdImageGetTrueColorPixel (src, x, y);					  	if (gdImageGetTransparent (src) == tmp) {							/* 2.0.21, TK: not tox++ */							tox += stx[x - srcX];					  		continue;					  	}					} else {						/* TK: old code follows */					  	mapTo = gdImageGetTrueColorPixel (src, x, y);						/* Added 7/24/95: support transparent copies */						if (gdImageGetTransparent (src) == mapTo) {							/* 2.0.21, TK: not tox++ */							tox += stx[x - srcX];							continue;						}					}				} else {					c = gdImageGetPixel (src, x, y);					/* Added 7/24/95: support transparent copies */					if (gdImageGetTransparent (src) == c) {					      tox += stx[x - srcX];					      continue;					}					if (src->trueColor) {					      /* Remap to the palette available in the destination image. This is slow and works badly. */					      mapTo = gdImageColorResolveAlpha(dst, gdTrueColorGetRed(c),					      					    gdTrueColorGetGreen(c),					      					    gdTrueColorGetBlue(c),					      					    gdTrueColorGetAlpha (c));					} else {						/* Have we established a mapping for this color? */						if (colorMap[c] == (-1)) {							/* If it's the same image, mapping is trivial */							if (dst == src) {								nc = c;							} else {								/* Find or create the best match */								/* 2.0.5: can't use gdTrueColorGetRed, etc with palette */								nc = gdImageColorResolveAlpha(dst, gdImageRed(src, c),												   gdImageGreen(src, c),												   gdImageBlue(src, c),												   gdImageAlpha(src, c));							}							colorMap[c] = nc;						}						mapTo = colorMap[c];					}				}				for (i = 0; (i < stx[x - srcX]); i++) {					gdImageSetPixel (dst, tox, toy, mapTo);					tox++;				}			}			toy++;		}	}	gdFree (stx);	gdFree (sty);}/* When gd 1.x was first created, floating point was to be avoided.   These days it is often faster than table lookups or integer   arithmetic. The routine below is shamelessly, gloriously   floating point. TBB */void gdImageCopyResampled (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH){	int x, y;	double sy1, sy2, sx1, sx2;	if (!dst->trueColor) {		gdImageCopyResized (dst, src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);		return;	}	for (y = dstY; (y < dstY + dstH); y++) {		sy1 = ((double) y - (double) dstY) * (double) srcH / (double) dstH;		sy2 = ((double) (y + 1) - (double) dstY) * (double) srcH / (double) dstH;		for (x = dstX; (x < dstX + dstW); x++) {			double sx, sy;			double spixels = 0;			double red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;			double alpha_factor, alpha_sum = 0.0, contrib_sum = 0.0;			sx1 = ((double) x - (double) dstX) * (double) srcW / dstW;			sx2 = ((double) (x + 1) - (double) dstX) * (double) srcW / dstW;			sy = sy1;			do {				double yportion;				if (floor_cast(sy) == floor_cast(sy1)) {					yportion = 1.0f - (sy - floor_cast(sy));					if (yportion > sy2 - sy1) {						yportion = sy2 - sy1;					}					sy = floor_cast(sy);				} else if (sy == floorf(sy2)) {					yportion = sy2 - floor_cast(sy2);				} else {					yportion = 1.0f;				}				sx = sx1;				do {					double xportion;					double pcontribution;					int p;					if (floorf(sx) == floor_cast(sx1)) {						xportion = 1.0f - (sx - floor_cast(sx));						if (xportion > sx2 - sx1) {							xportion = sx2 - sx1;						}						sx = floor_cast(sx);					} else if (sx == floorf(sx2)) {						xportion = sx2 - floor_cast(sx2);					} else {						xportion = 1.0f;					}					pcontribution = xportion * yportion;					p = gdImageGetTrueColorPixel(src, (int) sx + srcX, (int) sy + srcY);					alpha_factor = ((gdAlphaMax - gdTrueColorGetAlpha(p))) * pcontribution;					red += gdTrueColorGetRed (p) * alpha_factor;					green += gdTrueColorGetGreen (p) * alpha_factor;					blue += gdTrueColorGetBlue (p) * alpha_factor;					alpha += gdTrueColorGetAlpha (p) * pcontribution;					alpha_sum += alpha_factor;					contrib_sum += pcontribution;					spixels += xportion * yportion;					sx += 1.0f;				}				while (sx < sx2);				sy += 1.0f;			}			while (sy < sy2);			if (spixels != 0.0f) {				red /= spixels;				green /= spixels;				blue /= spixels;				alpha /= spixels;			}			if ( alpha_sum != 0.0f) {				if( contrib_sum != 0.0f) {					alpha_sum /= contrib_sum;				}				red /= alpha_sum;				green /= alpha_sum;				blue /= alpha_sum;			}			/* Clamping to allow for rounding errors above */			if (red > 255.0f) {				red = 255.0f;			}			if (green > 255.0f) {				green = 255.0f;			}			if (blue > 255.0f) {				blue = 255.0f;			}			if (alpha > gdAlphaMax) {				alpha = gdAlphaMax;			}			gdImageSetPixel(dst, x, y, gdTrueColorAlpha ((int) red, (int) green, (int) blue, (int) alpha));		}	}}/* * Rotate function Added on 2003/12 * by Pierre-Alain Joye (pajoye@pearfr.org) **//* Begin rotate function */#ifdef ROTATE_PI#undef ROTATE_PI#endif /* ROTATE_PI */#define ROTATE_DEG2RAD  3.1415926535897932384626433832795/180void gdImageSkewX (gdImagePtr dst, gdImagePtr src, int uRow, int iOffset, double dWeight, int clrBack, int ignoretransparent){	typedef int (*FuncPtr)(gdImagePtr, int, int);	int i, r, g, b, a, clrBackR, clrBackG, clrBackB, clrBackA;	FuncPtr f;	int pxlOldLeft, pxlLeft=0, pxlSrc;	/* Keep clrBack as color index if required */	if (src->trueColor) {		pxlOldLeft = clrBack;		f = gdImageGetTrueColorPixel;	} else {		pxlOldLeft = clrBack;		clrBackR = gdImageRed(src, clrBack);		clrBackG = gdImageGreen(src, clrBack);		clrBackB = gdImageBlue(src, clrBack);		clrBackA = gdImageAlpha(src, clrBack);		clrBack =  gdTrueColorAlpha(clrBackR, clrBackG, clrBackB, clrBackA);		f = gdImageGetPixel;	}	for (i = 0; i < iOffset; i++) {		gdImageSetPixel (dst, i, uRow, clrBack);	}	if (i < dst->sx) {		gdImageSetPixel (dst, i, uRow, clrBack);	}	for (i = 0; i < src->sx; i++) {		pxlSrc = f (src,i,uRow);		r = (int)(gdImageRed(src,pxlSrc) * dWeight);		g = (int)(gdImageGreen(src,pxlSrc) * dWeight);		b = (int)(gdImageBlue(src,pxlSrc) * dWeight);		a = (int)(gdImageAlpha(src,pxlSrc) * dWeight);		pxlLeft = gdImageColorAllocateAlpha(src, r, g, b, a);		if (pxlLeft == -1) {			pxlLeft = gdImageColorClosestAlpha(src, r, g, b, a);		}		r = gdImageRed(src,pxlSrc) - (gdImageRed(src,pxlLeft) - gdImageRed(src,pxlOldLeft));		g = gdImageGreen(src,pxlSrc) - (gdImageGreen(src,pxlLeft) - gdImageGreen(src,pxlOldLeft));		b = gdImageBlue(src,pxlSrc) - (gdImageBlue(src,pxlLeft) - gdImageBlue(src,pxlOldLeft));		a = gdImageAlpha(src,pxlSrc) - (gdImageAlpha(src,pxlLeft) - gdImageAlpha(src,pxlOldLeft));        if (r>255) {        	r = 255;        }		if (g>255) {			g = 255;		}		if (b>255) {			b = 255;		}		if (a>127) {			a = 127;		}		if (ignoretransparent && pxlSrc == dst->transparent) {			pxlSrc = dst->transparent;		} else {			pxlSrc = gdImageColorAllocateAlpha(dst, r, g, b, a);			if (pxlSrc == -1) {				pxlSrc = gdImageColorClosestAlpha(dst, r, g, b, a);			}		}		if ((i + iOffset >= 0) && (i + iOffset < dst->sx)) {			gdImageSetPixel (dst, i+iOffset, uRow,  pxlSrc);		}		pxlOldLeft = pxlLeft;	}	i += iOffset;	if (i < dst->sx) {		gdImageSetPixel (dst, i, uRow, pxlLeft);	}	gdImageSetPixel (dst, iOffset, uRow, clrBack);	i--;	while (++i < dst->sx) {		gdImageSetPixel (dst, i, uRow, clrBack);	}}void gdImageSkewY (gdImagePtr dst, gdImagePtr src, int uCol, int iOffset, double dWeight, int clrBack, int ignoretransparent){	typedef int (*FuncPtr)(gdImagePtr, int, int);	int i, iYPos=0, r, g, b, a;	FuncPtr f;	int pxlOldLeft, pxlLeft=0, pxlSrc;	if (src->trueColor) {		f = gdImageGetTrueColorPixel;	} else {		f = gdImageGetPixel;	}	for (i = 0; i<=iOffset; i++) {		gdImageSetPixel (dst, uCol, i, clrBack);	}	r = (int)((double)gdImageRed(src,clrBack) * dWeight);	g = (int)((double)gdImageGreen(src,clrBack) * dWeight);	b = (int)((double)gdImageBlue(src,clrBack) * dWeight);	a = (int)((double)gdImageAlpha(src,clrBack) * dWeight);	pxlOldLeft = gdImageColorAllocateAlpha(dst, r, g, b, a);	for (i = 0; i < src->sy; i++) {		pxlSrc = f (src, uCol, i);		iYPos = i + iOffset;		r = (int)((double)gdImageRed(src,pxlSrc) * dWeight);		g = (int)((double)gdImageGreen(src,pxlSrc) * dWeight);		b = (int)((double)gdImageBlue(src,pxlSrc) * dWeight);		a = (int)((double)gdImageAlpha(src,pxlSrc) * dWeight);		pxlLeft = gdImageColorAllocateAlpha(src, r, g, b, a);		if (pxlLeft == -1) {			pxlLeft = gdImageColorClosestAlpha(src, r, g, b, a);		}		r = gdImageRed(src,pxlSrc) - (gdImageRed(src,pxlLeft) - gdImageRed(src,pxlOldLeft));		g = gdImageGreen(src,pxlSrc) - (gdImageGreen(src,pxlLeft) - gdImageGreen(src,pxlOldLeft));		b = gdImageBlue(src,pxlSrc) - (gdImageBlue(src,pxlLeft) - gdImageBlue(src,pxlOldLeft));		a = gdImageAlpha(src,pxlSr

⌨️ 快捷键说明

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