📄 gd.c
字号:
if (gdImageGetTransparent (src) == mapTo) { tox++; 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 */ mapTo = gdImageColorResolveAlpha (dst, gdTrueColorGetRed (c), gdTrueColorGetGreen (c), gdTrueColorGetBlue (c), gdTrueColorGetAlpha (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 */voidgdImageCopyResampled (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH){ int x, y; float sx, sy; if (!dst->trueColor) { gdImageCopyResized ( dst, src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH); return; } for (y = dstY; (y < dstY + dstH); y++) { for (x = dstX; (x < dstX + dstW); x++) { int pd = gdImageGetPixel (dst, x, y); float sy1, sy2, sx1, sx2; float sx, sy; float spixels = 0; float red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; sy1 = ((float) y - (float) dstY) * (float) srcH / (float) dstH; sy2 = ((float) (y + 1) - (float) dstY) * (float) srcH / (float) dstH; sy = sy1; do { float yportion; if (floor (sy) == floor (sy1)) { yportion = 1.0 - (sy - floor (sy)); if (yportion > sy2 - sy1) { yportion = sy2 - sy1; } sy = floor (sy); } else if (sy == floor (sy2)) { yportion = sy2 - floor (sy2); } else { yportion = 1.0; } sx1 = ((float) x - (float) dstX) * (float) srcW / dstW; sx2 = ((float) (x + 1) - (float) dstX) * (float) srcW / dstW; sx = sx1; do { float xportion; float pcontribution; int p; if (floor (sx) == floor (sx1)) { xportion = 1.0 - (sx - floor (sx)); if (xportion > sx2 - sx1) { xportion = sx2 - sx1; } sx = floor (sx); } else if (sx == floor (sx2)) { xportion = sx2 - floor (sx2); } else { xportion = 1.0; } pcontribution = xportion * yportion; p = gdImageGetTrueColorPixel ( src, (int) sx, (int) sy); red += gdTrueColorGetRed (p) * pcontribution; green += gdTrueColorGetGreen (p) * pcontribution; blue += gdTrueColorGetBlue (p) * pcontribution; alpha += gdTrueColorGetAlpha (p) * pcontribution; spixels += xportion * yportion; sx += 1.0; } while (sx < sx2); sy += 1.0; } while (sy < sy2); if (spixels != 0.0) { red /= spixels; green /= spixels; blue /= spixels; alpha /= spixels; } /* Clamping to allow for rounding errors above */ if (red > 255.0) { red = 255.0; } if (green > 255.0) { green = 255.0; } if (blue > 255.0) { blue = 255.0; } if (alpha > gdAlphaMax) { alpha = gdAlphaMax; } gdImageSetPixel (dst, x, y, gdTrueColorAlpha ((int) red, (int) green, (int) blue, (int) alpha)); } }}gdImagePtrgdImageCreateFromXbm (FILE * fd){ gdImagePtr im; int bit; int w, h; int bytes; int ch; int i, x, y; char *sp; char s[161]; if (!fgets (s, 160, fd)) { return 0; } sp = &s[0]; /* Skip #define */ sp = strchr (sp, ' '); if (!sp) { return 0; } /* Skip width label */ sp++; sp = strchr (sp, ' '); if (!sp) { return 0; } /* Get width */ w = atoi (sp + 1); if (!w) { return 0; } if (!fgets (s, 160, fd)) { return 0; } sp = s; /* Skip #define */ sp = strchr (sp, ' '); if (!sp) { return 0; } /* Skip height label */ sp++; sp = strchr (sp, ' '); if (!sp) { return 0; } /* Get height */ h = atoi (sp + 1); if (!h) { return 0; } /* Skip declaration line */ if (!fgets (s, 160, fd)) { return 0; } bytes = (w * h / 8) + 1; im = gdImageCreate (w, h); gdImageColorAllocate (im, 255, 255, 255); gdImageColorAllocate (im, 0, 0, 0); x = 0; y = 0; for (i = 0; (i < bytes); i++) { char h[3]; int b; /* Skip spaces, commas, CRs, 0x */ while (1) { ch = getc (fd); if (ch == EOF) { goto fail; } if (ch == 'x') { break; } } /* Get hex value */ ch = getc (fd); if (ch == EOF) { goto fail; } h[0] = ch; ch = getc (fd); if (ch == EOF) { goto fail; } h[1] = ch; h[2] = '\0'; sscanf (h, "%x", &b); for (bit = 1; (bit <= 128); (bit = bit << 1)) { gdImageSetPixel (im, x++, y, (b & bit) ? 1 : 0); if (x == im->sx) { x = 0; y++; if (y == im->sy) { return im; } /* Fix 8/8/95 */ break; } } } /* Shouldn't happen */ fprintf (stderr, "Error: bug in gdImageCreateFromXbm!\n"); return 0;fail: gdImageDestroy (im); return 0;}voidgdImagePolygon (gdImagePtr im, gdPointPtr p, int n, int c){ int i; int lx, ly; if (!n) { return; } lx = p->x; ly = p->y; gdImageLine (im, lx, ly, p[n - 1].x, p[n - 1].y, c); for (i = 1; (i < n); i++) { p++; gdImageLine (im, lx, ly, p->x, p->y, c); lx = p->x; ly = p->y; }}int gdCompareInt (const void *a, const void *b);/* THANKS to Kirsten Schulz for the polygon fixes! *//* The intersection finding technique of this code could be improved *//* by remembering the previous intertersection, and by using the slope. *//* That could help to adjust intersections to produce a nice *//* interior_extrema. */voidgdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c){ int i; int y; int miny, maxy; int x1, y1; int x2, y2; int ind1, ind2; int ints; if (!n) { return; } if (!im->polyAllocated) { im->polyInts = (int *) gdMalloc (sizeof (int) * n); im->polyAllocated = n; } if (im->polyAllocated < n) { while (im->polyAllocated < n) { im->polyAllocated *= 2; } im->polyInts = (int *) gdRealloc (im->polyInts, sizeof (int) * im->polyAllocated); } miny = p[0].y; maxy = p[0].y; for (i = 1; (i < n); i++) { if (p[i].y < miny) { miny = p[i].y; } if (p[i].y > maxy) { maxy = p[i].y; } } /* Fix in 1.3: count a vertex only once */ for (y = miny; (y <= maxy); y++) {/*1.4 int interLast = 0; *//* int dirLast = 0; *//* int interFirst = 1; */ ints = 0; for (i = 0; (i < n); i++) { if (!i) { ind1 = n - 1; ind2 = 0; } else { ind1 = i - 1; ind2 = i; } y1 = p[ind1].y; y2 = p[ind2].y; if (y1 < y2) { x1 = p[ind1].x; x2 = p[ind2].x; } else if (y1 > y2) { y2 = p[ind1].y; y1 = p[ind2].y; x2 = p[ind1].x; x1 = p[ind2].x; } else { continue; } if ((y >= y1) && (y < y2)) { im->polyInts[ints++] = (y - y1) * (x2 - x1) / (y2 - y1) + x1; } else if ((y == maxy) && (y > y1) && (y <= y2)) { im->polyInts[ints++] = (y - y1) * (x2 - x1) / (y2 - y1) + x1; } } qsort (im->polyInts, ints, sizeof (int), gdCompareInt); for (i = 0; (i < (ints)); i += 2) { gdImageLine (im, im->polyInts[i], y, im->polyInts[i + 1], y, c); } }}intgdCompareInt (const void *a, const void *b){ return (*(const int *) a) - (*(const int *) b);}voidgdImageSetStyle (gdImagePtr im, int *style, int noOfPixels){ if (im->style) { gdFree (im->style); } im->style = (int *) gdMalloc (sizeof (int) * noOfPixels); memcpy (im->style, style, sizeof (int) * noOfPixels); im->styleLength = noOfPixels; im->stylePos = 0;}voidgdImageSetThickness (gdImagePtr im, int thickness){ im->thick = thickness;}voidgdImageSetBrush (gdImagePtr im, gdImagePtr brush){ int i; im->brush = brush; if ((!im->trueColor) && (!im->brush->trueColor)) { for (i = 0; (i < gdImageColorsTotal (brush)); i++) { int index; index = gdImageColorResolveAlpha (im, gdImageRed (brush, i), gdImageGreen (brush, i), gdImageBlue (brush, i), gdImageAlpha (brush, i)); im->brushColorMap[i] = index; } }}voidgdImageSetTile (gdImagePtr im, gdImagePtr tile){ int i; im->tile = tile; if ((!im->trueColor) && (!im->tile->trueColor)) { for (i = 0; (i < gdImageColorsTotal (tile)); i++) { int index; index = gdImageColorResolveAlpha (im, gdImageRed (tile, i), gdImageGreen (tile, i), gdImageBlue (tile, i), gdImageAlpha (tile, i)); im->tileColorMap[i] = index; } }}voidgdImageInterlace (gdImagePtr im, int interlaceArg){ im->interlace = interlaceArg;}intgdImageCompare (gdImagePtr im1, gdImagePtr im2){ int x, y; int p1, p2; int cmpStatus = 0; int sx, sy; if (im1->interlace != im2->interlace) { cmpStatus |= GD_CMP_INTERLACE; } if (im1->transparent != im2->transparent) { cmpStatus |= GD_CMP_TRANSPARENT; } if (im1->trueColor != im2->trueColor) { cmpStatus |= GD_CMP_TRUECOLOR; } sx = im1->sx; if (im1->sx != im2->sx) { cmpStatus |= GD_CMP_SIZE_X + GD_CMP_IMAGE; if (im2->sx < im1->sx) { sx = im2->sx; } } sy = im1->sy; if (im1->sy != im2->sy) { cmpStatus |= GD_CMP_SIZE_Y + GD_CMP_IMAGE; if (im2->sy < im1->sy) { sy = im2->sy; } } if (im1->colorsTotal != im2->colorsTotal) { cmpStatus |= GD_CMP_NUM_COLORS; } for (y = 0; (y < sy); y++) { for (x = 0; (x < sx); x++) { p1 = im1->pixels[y][x]; p2 = im2->pixels[y][x]; if (gdImageRed (im1, p1) != gdImageRed (im2, p2)) { cmpStatus |= GD_CMP_COLOR + GD_CMP_IMAGE; break; } if (gdImageGreen (im1, p1) != gdImageGreen (im2, p2)) { cmpStatus |= GD_CMP_COLOR + GD_CMP_IMAGE; break; } if (gdImageBlue (im1, p1) != gdImageBlue (im2, p2)) { cmpStatus |= GD_CMP_COLOR + GD_CMP_IMAGE; break; }#if 0 /* Soon we'll add alpha channel to palettes */ if (gdImageAlpha (im1, p1) != gdImageAlpha (im2, p2)) { cmpStatus |= GD_CMP_COLOR + GD_CMP_IMAGE; break; }#endif } if (cmpStatus & GD_CMP_COLOR) { break; }; } return cmpStatus;}intgdAlphaBlend (int dst, int src){ return (((((gdAlphaTransparent - gdTrueColorGetAlpha (src)) * gdTrueColorGetRed (src) / gdAlphaMax) + (gdTrueColorGetAlpha (src) * gdTrueColorGetRed (dst)) / gdAlphaMax) << 16) + ((((gdAlphaTransparent - gdTrueColorGetAlpha (src)) * gdTrueColorGetGreen (src) / gdAlphaMax) + (gdTrueColorGetAlpha (src) * gdTrueColorGetGreen (dst)) / gdAlphaMax) << 8) + (((gdAlphaTransparent - gdTrueColorGetAlpha (src)) * gdTrueColorGetBlue (src) / gdAlphaMax) + (gdTrueColorGetAlpha (src) * gdTrueColorGetBlue (dst)) / gdAlphaMax));}voidgdImageAlphaBlending (gdImagePtr im, int alphaBlendingArg){ im->alphaBlendingFlag = alphaBlendingArg;}voidgdImageSaveAlpha (gdImagePtr im, int saveAlphaArg){ im->saveAlphaFlag = saveAlphaArg;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -