📄 gd.c
字号:
} /* Many (perhaps most) of these colors will remain marked open. */ im->colorsTotal = gdMaxColors; /* ** Initialize the Compression routines */ if (! ReadOK(fd,&c,1)) { return; } if (LWZReadByte(fd, TRUE, c) < 0) { return; } /* ** If this is an "uninteresting picture" ignore it. */ if (ignore) { while (LWZReadByte(fd, FALSE, c) >= 0) ; return; } while ((v = LWZReadByte(fd,FALSE,c)) >= 0 ) { /* This how we recognize which colors are actually used. */ if (im->open[v]) { im->open[v] = 0; } gdImageSetPixel(im, xpos, ypos, v); ++xpos; if (xpos == len) { xpos = 0; if (interlace) { switch (pass) { case 0: case 1: ypos += 8; break; case 2: ypos += 4; break; case 3: ypos += 2; break; } if (ypos >= height) { ++pass; switch (pass) { case 1: ypos = 4; break; case 2: ypos = 2; break; case 3: ypos = 1; break; default: goto fini; } } } else { ++ypos; } } if (ypos >= height) break; }fini: if (LWZReadByte(fd,FALSE,c)>=0) { /* Ignore extra */ }}void gdImageRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color){ gdImageLine(im, x1, y1, x2, y1, color); gdImageLine(im, x1, y2, x2, y2, color); gdImageLine(im, x1, y1, x1, y2, color); gdImageLine(im, x2, y1, x2, y2, color);}void gdImageFilledRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color){ int x, y; for (y=y1; (y<=y2); y++) { for (x=x1; (x<=x2); x++) { gdImageSetPixel(im, x, y, color); } }}void gdImageCopy(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h){ int c; int x, y; int tox, toy; int i; int colorMap[gdMaxColors]; 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; 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 (colorMap[c] == (-1)) { /* If it's the same image, mapping is trivial */ if (dst == src) { nc = c; } else { /* First look for an exact match */ nc = gdImageColorExact(dst, src->red[c], src->green[c], src->blue[c]); } if (nc == (-1)) { /* No, so try to allocate it */ nc = gdImageColorAllocate(dst, src->red[c], src->green[c], src->blue[c]); /* If we're out of colors, go for the closest color */ if (nc == (-1)) { nc = gdImageColorClosest(dst, src->red[c], src->green[c], src->blue[c]); } } colorMap[c] = nc; } gdImageSetPixel(dst, tox, toy, colorMap[c]); 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; int *sty; /* We only need to use floating point to determine the correct stretch vector for one line's worth. */ double accum; stx = (int *) malloc(sizeof(int) * srcW); sty = (int *) malloc(sizeof(int) * srcH); accum = 0; for (i=0; (i < srcW); i++) { int got; accum += (double)dstW/(double)srcW; got = floor(accum); stx[i] = got; accum -= got; } accum = 0; for (i=0; (i < srcH); i++) { int got; accum += (double)dstH/(double)srcH; got = floor(accum); sty[i] = got; accum -= got; } 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; if (!stx[x - srcX]) { continue; } c = gdImageGetPixel(src, x, y); /* Added 7/24/95: support transparent copies */ if (gdImageGetTransparent(src) == c) { tox += stx[x-srcX]; continue; } /* 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 { /* First look for an exact match */ nc = gdImageColorExact(dst, src->red[c], src->green[c], src->blue[c]); } if (nc == (-1)) { /* No, so try to allocate it */ nc = gdImageColorAllocate(dst, src->red[c], src->green[c], src->blue[c]); /* If we're out of colors, go for the closest color */ if (nc == (-1)) { nc = gdImageColorClosest(dst, src->red[c], src->green[c], src->blue[c]); } } colorMap[c] = nc; } for (i=0; (i < stx[x - srcX]); i++) { gdImageSetPixel(dst, tox, toy, colorMap[c]); tox++; } } toy++; } } free(stx); free(sty);}int gdGetWord(int *result, FILE *in){ int r; r = getc(in); if (r == EOF) { return 0; } *result = r << 8; r = getc(in); if (r == EOF) { return 0; } *result += r; return 1;}void gdPutWord(int w, FILE *out){ putc((unsigned char)(w >> 8), out); putc((unsigned char)(w & 0xFF), out);}int gdGetByte(int *result, FILE *in){ int r; r = getc(in); if (r == EOF) { return 0; } *result = r; return 1;}gdImagePtr gdImageCreateFromGd(FILE *in){ int sx, sy; int x, y; int i; gdImagePtr im; if (!gdGetWord(&sx, in)) { goto fail1; } if (!gdGetWord(&sy, in)) { goto fail1; } im = gdImageCreate(sx, sy); if (!gdGetByte(&im->colorsTotal, in)) { goto fail2; } if (!gdGetWord(&im->transparent, in)) { goto fail2; } if (im->transparent == 257) { im->transparent = (-1); } for (i=0; (i<gdMaxColors); i++) { if (!gdGetByte(&im->red[i], in)) { goto fail2; } if (!gdGetByte(&im->green[i], in)) { goto fail2; } if (!gdGetByte(&im->blue[i], in)) { goto fail2; } } for (y=0; (y<sy); y++) { for (x=0; (x<sx); x++) { int ch; ch = getc(in); if (ch == EOF) { gdImageDestroy(im); return 0; } /* ROW-MAJOR IN GD 1.3 */ im->pixels[y][x] = ch; } } return im;fail2: gdImageDestroy(im);fail1: return 0;} void gdImageGd(gdImagePtr im, FILE *out){ int x, y; int i; int trans; gdPutWord(im->sx, out); gdPutWord(im->sy, out); putc((unsigned char)im->colorsTotal, out); trans = im->transparent; if (trans == (-1)) { trans = 257; } gdPutWord(trans, out); for (i=0; (i<gdMaxColors); i++) { putc((unsigned char)im->red[i], out); putc((unsigned char)im->green[i], out); putc((unsigned char)im->blue[i], out); } for (y=0; (y < im->sy); y++) { for (x=0; (x < im->sx); x++) { /* ROW-MAJOR IN GD 1.3 */ putc((unsigned char)im->pixels[y][x], out); } }}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;}void gdImagePolygon(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); void gdImageFilledPolygon(gdImagePtr im, gdPointPtr p, int n, int c){ int i; int y; int y1, y2; int ints; if (!n) { return; } if (!im->polyAllocated) { im->polyInts = (int *) malloc(sizeof(int) * n); im->polyAllocated = n; } if (im->polyAllocated < n) { while (im->polyAllocated < n) { im->polyAllocated *= 2; } im->polyInts = (int *) realloc(im->polyInts, sizeof(int) * im->polyAllocated); } y1 = p[0].y; y2 = p[0].y; for (i=1; (i < n); i++) { if (p[i].y < y1) { y1 = p[i].y; } if (p[i].y > y2) { y2 = p[i].y; } } /* Fix in 1.3: count a vertex only once */ for (y=y1; (y < y2); y++) { int interLast = 0; int dirLast = 0; int interFirst = 1; ints = 0; for (i=0; (i <= n); i++) { int x1, x2; int y1, y2; int dir; int ind1, ind2; int lastInd1 = 0; if ((i == n) || (!i)) { ind1 = n-1; ind2 = 0; } else { ind1 = i-1; ind2 = i; } y1 = p[ind1].y; y2 = p[ind2].y; if (y1 < y2) { y1 = p[ind1].y; y2 = p[ind2].y; x1 = p[ind1].x; x2 = p[ind2].x; dir = -1; } else if (y1 > y2) { y2 = p[ind1].y; y1 = p[ind2].y; x2 = p[ind1].x; x1 = p[ind2].x; dir = 1; } else { /* Horizontal; just draw it */ gdImageLine(im, p[ind1].x, y1, p[ind2].x, y1, c); continue; } if ((y >= y1) && (y <= y2)) { int inter = (y-y1) * (x2-x1) / (y2-y1) + x1; /* Only count intersections once except at maxima and minima. Also, if two consecutive intersections are endpoints of the same horizontal line that is not at a maxima or minima, discard the leftmost of the two. */ if (!interFirst) { if ((p[ind1].y == p[lastInd1].y) && (p[ind1].x != p[lastInd1].x)) { if (dir == dirLast) { if (inter > interLast) { /* Replace the old one */ im->polyInts[ints] = inter; } else { /* Discard this one */ } continue; } } if (inter == interLast) { if (dir == dirLast) { continue; } } } if (i > 0) { im->polyInts[ints++] = inter; } lastInd1 = i; dirLast = dir; interLast = inter; interFirst = 0; } } qsort(im->polyInts, ints, sizeof(int), gdCompareInt); for (i=0; (i < (ints-1)); i+=2) { gdImageLine(im, im->polyInts[i], y, im->polyInts[i+1], y, c); } }} int gdCompareInt(const void *a, const void *b){ return (*(const int *)a) - (*(const int *)b);}void gdImageSetStyle(gdImagePtr im, int *style, int noOfPixels){ if (im->style) { free(im->style); } im->style = (int *) malloc(sizeof(int) * noOfPixels); memcpy(im->style, style, sizeof(int) * noOfPixels); im->styleLength = noOfPixels; im->stylePos = 0;}void gdImageSetBrush(gdImagePtr im, gdImagePtr brush){ int i; im->brush = brush; for (i=0; (i < gdImageColorsTotal(brush)); i++) { int index; index = gdImageColorExact(im, gdImageRed(brush, i), gdImageGreen(brush, i), gdImageBlue(brush, i)); if (index == (-1)) { index = gdImageColorAllocate(im, gdImageRed(brush, i), gdImageGreen(brush, i), gdImageBlue(brush, i)); if (index == (-1)) { index = gdImageColorClosest(im, gdImageRed(brush, i), gdImageGreen(brush, i), gdImageBlue(brush, i)); } } im->brushColorMap[i] = index; }} void gdImageSetTile(gdImagePtr im, gdImagePtr tile){ int i; im->tile = tile; for (i=0; (i < gdImageColorsTotal(tile)); i++) { int index; index = gdImageColorExact(im, gdImageRed(tile, i), gdImageGreen(tile, i), gdImageBlue(tile, i)); if (index == (-1)) { index = gdImageColorAllocate(im, gdImageRed(tile, i), gdImageGreen(tile, i), gdImageBlue(tile, i)); if (index == (-1)) { index = gdImageColorClosest(im, gdImageRed(tile, i), gdImageGreen(tile, i), gdImageBlue(tile, i)); } } im->tileColorMap[i] = index; }}void gdImageInterlace(gdImagePtr im, int interlaceArg){ im->interlace = interlaceArg;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -