Annotation of parser3/src/classes/gd/gif.C, revision 1.7

1.3       paf         1: /** @file
                      2:        Parser: image manipulations impl1.
                      3: 
                      4:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      5: */
1.7     ! parser      6: static const char *RCSId="$Id: date.C,v 1.6 2001/09/04 10:50:19 parser Exp $"; 
1.3       paf         7: 
1.1       paf         8: #include "gif.h"
                      9: 
1.2       paf        10: //static void BrushApply(int x, int y);
                     11: //static void TileApply(int x, int y);
1.1       paf        12: 
1.2       paf        13: void gdImage::Create(int asx, int asy) {
                     14:        sx = asx;
                     15:        sy = asy;
                     16: 
                     17:        int i;
                     18:        pixels = (unsigned char **) malloc(sizeof(unsigned char *) * sx);
                     19:        polyInts = 0;
                     20:        polyAllocated = 0;
1.6       parser     21:        lineWidth = 1;  lineStyle=0;
1.2       paf        22:        for (i=0; (i<asx); i++)
                     23:                pixels[i] = (unsigned char *) calloc(asy);
                     24:        colorsTotal = 0;
                     25:        transparent = (-1);
                     26:        interlace = 0;
1.1       paf        27: }
                     28: 
1.2       paf        29: int gdImage::ColorClosest(int r, int g, int b)
1.1       paf        30: {
                     31:        int i;
                     32:        long rd, gd, bd;
                     33:        int ct = (-1);
                     34:        long mindist = 0;
1.2       paf        35:        for (i=0; (i<(colorsTotal)); i++) {
1.1       paf        36:                long dist;
1.2       paf        37:                if (open[i]) {
1.1       paf        38:                        continue;
                     39:                }
1.2       paf        40:                rd = (red[i] - r);      
                     41:                gd = (green[i] - g);
                     42:                bd = (blue[i] - b);
1.1       paf        43:                dist = rd * rd + gd * gd + bd * bd;
                     44:                if ((i == 0) || (dist < mindist)) {
                     45:                        mindist = dist; 
                     46:                        ct = i;
                     47:                }
                     48:        }
                     49:        return ct;
                     50: }
                     51: 
1.2       paf        52: int gdImage::ColorExact(int r, int g, int b)
1.1       paf        53: {
                     54:        int i;
1.2       paf        55:        for (i=0; (i<(colorsTotal)); i++) {
                     56:                if (open[i]) {
1.1       paf        57:                        continue;
                     58:                }
1.2       paf        59:                if ((red[i] == r) && 
                     60:                        (green[i] == g) &&
                     61:                        (blue[i] == b)) {
1.1       paf        62:                        return i;
                     63:                }
                     64:        }
                     65:        return -1;
                     66: }
                     67: 
1.2       paf        68: int gdImage::ColorAllocate(int r, int g, int b)
1.1       paf        69: {
                     70:        int i;
                     71:        int ct = (-1);
1.2       paf        72:        for (i=0; (i<(colorsTotal)); i++) {
                     73:                if (open[i]) {
1.1       paf        74:                        ct = i;
                     75:                        break;
                     76:                }
                     77:        }       
                     78:        if (ct == (-1)) {
1.2       paf        79:                ct = colorsTotal;
1.1       paf        80:                if (ct == gdMaxColors) {
                     81:                        return -1;
                     82:                }
1.2       paf        83:                colorsTotal++;
1.1       paf        84:        }
1.2       paf        85:        red[ct] = r;
                     86:        green[ct] = g;
                     87:        blue[ct] = b;
                     88:        open[ct] = 0;
1.1       paf        89:        return ct;
                     90: }
                     91: 
1.2       paf        92: int gdImage::ColorRGB(int r, int g, int b){
                     93:     int idx=ColorExact(r,g,b);
                     94:     return idx<0 ? ColorAllocate(r,g,b) : idx;
1.1       paf        95: }
                     96: 
1.2       paf        97: int gdImage::Color(unsigned int rgb){
1.1       paf        98:     unsigned int b=rgb, g=b>>8, r=g>>8;
1.2       paf        99:     return ColorRGB(r & 0xFF,g & 0xFF,b & 0xFF);
1.1       paf       100: }
                    101: 
1.2       paf       102: void gdImage::ColorDeallocate(int color)
1.1       paf       103: {
                    104:        /* Mark it open. */
1.2       paf       105:        open[color] = 1;
1.1       paf       106: }
                    107: 
1.2       paf       108: void gdImage::SetColorTransparent(int color)
1.1       paf       109: {
1.2       paf       110:        transparent = color;
1.1       paf       111: }
                    112: 
                    113: 
1.2       paf       114: void gdImage::SetPixel(int x, int y, int color)
1.1       paf       115: {
                    116: //paf  int p;
                    117: 
1.6       parser    118:        switch (lineWidth){
1.1       paf       119:            case 1: {
1.2       paf       120:                DoSetPixel(x, y,color);
1.1       paf       121:                return;
                    122:            }
                    123:            case 2: {
1.2       paf       124:                DoSetPixel(x, y-1,color);
                    125:                DoSetPixel(x-1, y,color);
                    126:                DoSetPixel(x, y,color);
                    127:                DoSetPixel(x+1, y,color);
                    128:                DoSetPixel(x, y+1,color);
1.1       paf       129:                return;
                    130:            }
                    131:            default:{
                    132:                int i,j;
1.2       paf       133:                for (i=-1;i<=1;i++) DoSetPixel(x+i, y-2,color);
                    134:                for (j=-1;j<=1;j++) for (i=-2;i<=2;i++) DoSetPixel(x+i, y+j,color);
                    135:                for (i=-1;i<=1;i++) DoSetPixel(x+i, y+2,color);
1.1       paf       136:                return;
                    137:            }
                    138:        }
                    139: }
                    140: 
1.2       paf       141: int gdImage::GetPixel(int x, int y)
1.1       paf       142: {
1.2       paf       143:        return BoundsSafe(x, y) ? pixels[x][y]:0;
1.1       paf       144: }
                    145: 
                    146: /* Bresenham as presented in Foley & Van Dam */
                    147: 
                    148: /* As above, plus dashing */
                    149: 
1.6       parser    150: #define styledSet \
1.1       paf       151:        { \
1.6       parser    152:                if (lineStyle) { \
                    153:                        if(!lineStyle[styleStep]) \
                    154:                                styleStep = 0; \
                    155:                        on=lineStyle[styleStep++]!=' '; \
1.1       paf       156:                } \
                    157:                if (on) { \
1.2       paf       158:                        SetPixel(x, y, color); \
1.1       paf       159:                } \
                    160:        }
                    161: 
1.6       parser    162: void gdImage::Line(int x1, int y1, int x2, int y2, int color)
1.1       paf       163: {
                    164:        int dx, dy, incr1, incr2, d, x, y, xend, yend, xdirflag, ydirflag;
1.6       parser    165:        int styleStep = 0;
1.1       paf       166:        int on = 1;
                    167:        dx = abs(x2-x1);
                    168:        dy = abs(y2-y1);
                    169:        if (dy <= dx) {
                    170:                d = 2*dy - dx;
                    171:                incr1 = 2*dy;
                    172:                incr2 = 2 * (dy - dx);
                    173:                if (x1 > x2) {
                    174:                        x = x2;
                    175:                        y = y2;
                    176:                        ydirflag = (-1);
                    177:                        xend = x1;
                    178:                } else {
                    179:                        x = x1;
                    180:                        y = y1;
                    181:                        ydirflag = 1;
                    182:                        xend = x2;
                    183:                }
1.6       parser    184:                styledSet;
1.1       paf       185:                if (((y2 - y1) * ydirflag) > 0) {
                    186:                        while (x < xend) {
                    187:                                x++;
                    188:                                if (d <0) {
                    189:                                        d+=incr1;
                    190:                                } else {
                    191:                                        y++;
                    192:                                        d+=incr2;
                    193:                                }
1.6       parser    194:                                styledSet;
1.1       paf       195:                        }
                    196:                } else {
                    197:                        while (x < xend) {
                    198:                                x++;
                    199:                                if (d <0) {
                    200:                                        d+=incr1;
                    201:                                } else {
                    202:                                        y--;
                    203:                                        d+=incr2;
                    204:                                }
1.6       parser    205:                                styledSet;
1.1       paf       206:                        }
                    207:                }               
                    208:        } else {
                    209:                d = 2*dx - dy;
                    210:                incr1 = 2*dx;
                    211:                incr2 = 2 * (dx - dy);
                    212:                if (y1 > y2) {
                    213:                        y = y2;
                    214:                        x = x2;
                    215:                        yend = y1;
                    216:                        xdirflag = (-1);
                    217:                } else {
                    218:                        y = y1;
                    219:                        x = x1;
                    220:                        yend = y2;
                    221:                        xdirflag = 1;
                    222:                }
1.6       parser    223:                styledSet;
1.1       paf       224:                if (((x2 - x1) * xdirflag) > 0) {
                    225:                        while (y < yend) {
                    226:                                y++;
                    227:                                if (d <0) {
                    228:                                        d+=incr1;
                    229:                                } else {
                    230:                                        x++;
                    231:                                        d+=incr2;
                    232:                                }
1.6       parser    233:                                styledSet;
1.1       paf       234:                        }
                    235:                } else {
                    236:                        while (y < yend) {
                    237:                                y++;
                    238:                                if (d <0) {
                    239:                                        d+=incr1;
                    240:                                } else {
                    241:                                        x--;
                    242:                                        d+=incr2;
                    243:                                }
1.6       parser    244:                                styledSet;
1.1       paf       245:                        }
                    246:                }
                    247:        }
                    248: }
                    249: 
                    250: /* s and e are integers modulo 360 (degrees), with 0 degrees
                    251:   being the rightmost extreme and degrees changing clockwise.
                    252:   cx and cy are the center in pixels; w and h are the horizontal 
                    253:   and vertical diameter in pixels. Nice interface, but slow, since
                    254:   I don't yet use Bresenham (I'm using an inefficient but
                    255:   simple solution with too much work going on in it; generalizing
                    256:   Bresenham to ellipses and partial arcs of ellipses is non-trivial,
                    257:   at least for me) and there are other inefficiencies (small circles
                    258:   do far too much work). */
                    259: 
1.2       paf       260: void gdImage::FillToBorder(int x, int y, int border, int color)
1.1       paf       261: {
1.4       paf       262:        if(!BoundsSafe(x, y)) //PAF
                    263:                return;
                    264: 
1.1       paf       265:        int lastBorder;
                    266:        /* Seek left */
                    267:        int leftLimit, rightLimit;
                    268:        int i;
                    269:        leftLimit = (-1);
                    270:        if (border < 0) {
                    271:                /* Refuse to fill to a non-solid border */
                    272:                return;
                    273:        }
                    274:        for (i = x; (i >= 0); i--) {
1.2       paf       275:                if (GetPixel(i, y) == border) {
1.1       paf       276:                        break;
                    277:                }
1.2       paf       278:                SetPixel(i, y, color);
1.1       paf       279:                leftLimit = i;
                    280:        }
                    281:        if (leftLimit == (-1)) {
                    282:                return;
                    283:        }
                    284:        /* Seek right */
                    285:        rightLimit = x;
1.2       paf       286:        for (i = (x+1); (i < sx); i++) {        
                    287:                if (GetPixel(i, y) == border) {
1.1       paf       288:                        break;
                    289:                }
1.2       paf       290:                SetPixel(i, y, color);
1.1       paf       291:                rightLimit = i;
                    292:        }
                    293:        /* Look at lines above and below and start paints */
                    294:        /* Above */
                    295:        if (y > 0) {
                    296:                lastBorder = 1;
                    297:                for (i = leftLimit; (i <= rightLimit); i++) {
                    298:                        int c;
1.2       paf       299:                        c = GetPixel(i, y-1);
1.1       paf       300:                        if (lastBorder) {
                    301:                                if ((c != border) && (c != color)) {    
1.2       paf       302:                                        FillToBorder(i, y-1, 
1.1       paf       303:                                                border, color);         
                    304:                                        lastBorder = 0;
                    305:                                }
                    306:                        } else if ((c == border) || (c == color)) {
                    307:                                lastBorder = 1;
                    308:                        }
                    309:                }
                    310:        }
                    311:        /* Below */
1.2       paf       312:        if (y < ((sy) - 1)) {
1.1       paf       313:                lastBorder = 1;
                    314:                for (i = leftLimit; (i <= rightLimit); i++) {
                    315:                        int c;
1.2       paf       316:                        c = GetPixel(i, y+1);
1.1       paf       317:                        if (lastBorder) {
                    318:                                if ((c != border) && (c != color)) {    
1.2       paf       319:                                        FillToBorder(i, y+1, 
1.1       paf       320:                                                border, color);         
                    321:                                        lastBorder = 0;
                    322:                                }
                    323:                        } else if ((c == border) || (c == color)) {
                    324:                                lastBorder = 1;
                    325:                        }
                    326:                }
                    327:        }
                    328: }
                    329: 
1.2       paf       330: void gdImage::Fill(int x, int y, int color)
1.1       paf       331: {
1.4       paf       332:        if(!BoundsSafe(x, y)) //PAF
                    333:                return;
                    334: 
1.1       paf       335:        int lastBorder;
                    336:        int old;
                    337:        int leftLimit, rightLimit;
                    338:        int i;
1.2       paf       339:        old = GetPixel(x, y);
1.1       paf       340:        if (old == color) {
                    341:                /* Nothing to be done */
                    342:                return;
                    343:        }
                    344:        /* Seek left */
                    345:        leftLimit = (-1);
                    346:        for (i = x; (i >= 0); i--) {
1.2       paf       347:                if (GetPixel(i, y) != old) {
1.1       paf       348:                        break;
                    349:                }
1.2       paf       350:                SetPixel(i, y, color);
1.1       paf       351:                leftLimit = i;
                    352:        }
                    353:        if (leftLimit == (-1)) {
                    354:                return;
                    355:        }
                    356:        /* Seek right */
                    357:        rightLimit = x;
1.2       paf       358:        for (i = (x+1); (i < sx); i++) {        
                    359:                if (GetPixel(i, y) != old) {
1.1       paf       360:                        break;
                    361:                }
1.2       paf       362:                SetPixel(i, y, color);
1.1       paf       363:                rightLimit = i;
                    364:        }
                    365:        /* Look at lines above and below and start paints */
                    366:        /* Above */
                    367:        if (y > 0) {
                    368:                lastBorder = 1;
                    369:                for (i = leftLimit; (i <= rightLimit); i++) {
                    370:                        int c;
1.2       paf       371:                        c = GetPixel(i, y-1);
1.1       paf       372:                        if (lastBorder) {
                    373:                                if (c == old) { 
1.2       paf       374:                                        Fill(i, y-1, color);            
1.1       paf       375:                                        lastBorder = 0;
                    376:                                }
                    377:                        } else if (c != old) {
                    378:                                lastBorder = 1;
                    379:                        }
                    380:                }
                    381:        }
                    382:        /* Below */
1.2       paf       383:        if (y < ((sy) - 1)) {
1.1       paf       384:                lastBorder = 1;
                    385:                for (i = leftLimit; (i <= rightLimit); i++) {
                    386:                        int c;
1.2       paf       387:                        c = GetPixel(i, y+1);
1.1       paf       388:                        if (lastBorder) {
                    389:                                if (c == old) {
1.2       paf       390:                                        Fill(i, y+1, color);            
1.1       paf       391:                                        lastBorder = 0;
                    392:                                }
                    393:                        } else if (c != old) {
                    394:                                lastBorder = 1;
                    395:                        }
                    396:                }
                    397:        }
                    398: }
                    399:        
1.2       paf       400: void gdImage::Rectangle(int x1, int y1, int x2, int y2, int color)
1.1       paf       401: {
1.2       paf       402:        Line(x1, y1, x2, y1, color);            
                    403:        Line(x1, y2, x2, y2, color);            
                    404:        Line(x1, y1, x1, y2, color);
                    405:        Line(x2, y1, x2, y2, color);
1.1       paf       406: }
                    407: 
1.2       paf       408: void gdImage::FilledRectangle(int x1, int y1, int x2, int y2, int color)
1.1       paf       409: {
                    410:        int x, y;
1.2       paf       411:        for (y=y1; (y<=y2); y++)
                    412:                for (x=x1; (x<=x2); x++)
                    413:                        SetPixel(x, y, color);
1.1       paf       414: }
                    415: 
1.2       paf       416: void gdImage::Copy(gdImage& dst, int dstX, int dstY, int srcX, int srcY, int w, int h)
1.1       paf       417: {
                    418:        int c;
                    419:        int x, y;
                    420:        int tox, toy;
                    421:        int i;
                    422:        int colorMap[gdMaxColors];
                    423:        for (i=0; (i<gdMaxColors); i++) {
                    424:                colorMap[i] = (-1);
                    425:        }
                    426:        toy = dstY;
                    427:        for (y=srcY; (y < (srcY + h)); y++) {
                    428:                tox = dstX;
                    429:                for (x=srcX; (x < (srcX + w)); x++) {
                    430:                        int nc;
1.2       paf       431:                        c = GetPixel(x, y);
1.1       paf       432:                        /* Added 7/24/95: support transparent copies */
1.2       paf       433:                        if (GetTransparent() == c) {
1.1       paf       434:                                tox++;
                    435:                                continue;
                    436:                        }
                    437:                        /* Have we established a mapping for this color? */
                    438:                        if (colorMap[c] == (-1)) {
                    439:                                /* If it's the same image, mapping is trivial */
1.2       paf       440:                                if (&dst == this) {
1.1       paf       441:                                        nc = c;
                    442:                                } else { 
                    443:                                        /* First look for an exact match */
1.2       paf       444:                                        nc = dst.ColorExact(
                    445:                                                red[c], green[c],
                    446:                                                blue[c]);
1.1       paf       447:                                }       
                    448:                                if (nc == (-1)) {
                    449:                                        /* No, so try to allocate it */
1.2       paf       450:                                        nc = dst.ColorAllocate(
                    451:                                                red[c], green[c],
                    452:                                                blue[c]);
1.1       paf       453:                                        /* If we're out of colors, go for the
                    454:                                                closest color */
                    455:                                        if (nc == (-1)) {
1.2       paf       456:                                                nc = dst.ColorClosest(
                    457:                                                        red[c], green[c],
                    458:                                                        blue[c]);
1.1       paf       459:                                        }
                    460:                                }
                    461:                                colorMap[c] = nc;
                    462:                        }
1.2       paf       463:                        dst.SetPixel(tox, toy, colorMap[c]);
1.1       paf       464:                        tox++;
                    465:                }
                    466:                toy++;
                    467:        }
                    468: }                      
                    469: 
1.2       paf       470: static int gdGetWord(int *result, FILE *in)
1.1       paf       471: {
                    472:        int r;
                    473:        r = getc(in);
                    474:        if (r == EOF) {
                    475:                return 0;
                    476:        }
                    477:        *result = r << 8;
                    478:        r = getc(in);   
                    479:        if (r == EOF) {
                    480:                return 0;
                    481:        }
                    482:        *result += r;
                    483:        return 1;
                    484: }
                    485: 
1.2       paf       486: static void gdPutWord(int w, FILE *out)
1.1       paf       487: {
                    488:        putc((unsigned char)(w >> 8), out);
                    489:        putc((unsigned char)(w & 0xFF), out);
                    490: }
                    491: 
1.2       paf       492: static int gdGetByte(int *result, FILE *in)
1.1       paf       493: {
                    494:        int r;
                    495:        r = getc(in);
                    496:        if (r == EOF) {
                    497:                return 0;
                    498:        }
                    499:        *result = r;
                    500:        return 1;
                    501: }
                    502: 
1.6       parser    503: void gdImage::Polygon(Point *p, int n, int c, bool closed)
1.1       paf       504: {
                    505:        int i;
                    506:        int lx, ly;
                    507:        if (!n) {
                    508:                return;
                    509:        }
                    510:        lx = p->x;
                    511:        ly = p->y;
1.6       parser    512:        if(closed)
                    513:                Line(lx, ly, p[n-1].x, p[n-1].y, c);
1.1       paf       514:        for (i=1; (i < n); i++) {
                    515:                p++;
1.2       paf       516:                Line(lx, ly, p->x, p->y, c);
1.1       paf       517:                lx = p->x;
                    518:                ly = p->y;
                    519:        }
                    520: }      
                    521:        
1.5       paf       522: static int gdCompareInt(const void *a, const void *b)
                    523: {
                    524:        return (*(const int *)a) - (*(const int *)b);
                    525: }
                    526: 
                    527: 
1.1       paf       528:        
1.2       paf       529: void gdImage::FilledPolygon(Point *p, int n, int c)
1.1       paf       530: {
                    531:        int i;
                    532:        int y;
                    533:        int y1, y2;
                    534:        int ints;
                    535:        if (!n) {
                    536:                return;
                    537:        }
1.2       paf       538:        if (!polyAllocated) {
                    539:                polyInts = (int *) malloc(sizeof(int) * n);
                    540:                polyAllocated = n;
1.1       paf       541:        }               
1.2       paf       542:        if (polyAllocated < n) {
                    543:                while (polyAllocated < n) {
                    544:                        polyAllocated *= 2;
1.1       paf       545:                }       
1.2       paf       546:                polyInts = (int *) realloc(polyInts,
                    547:                        sizeof(int) * polyAllocated);
1.1       paf       548:        }
                    549:        y1 = p[0].y;
                    550:        y2 = p[0].y;
                    551:        for (i=1; (i < n); i++) {
                    552:                if (p[i].y < y1) {
                    553:                        y1 = p[i].y;
                    554:                }
                    555:                if (p[i].y > y2) {
                    556:                        y2 = p[i].y;
                    557:                }
                    558:        }
                    559:        for (y=y1; (y <= y2); y++) {
                    560:                int interLast = 0;
                    561:                int dirLast = 0;
                    562:                int interFirst = 1;
                    563:                ints = 0;
                    564:                for (i=0; (i <= n); i++) {
                    565:                        int x1, x2;
                    566:                        int y1, y2;
                    567:                        int dir;
                    568:                        int ind1, ind2;
                    569:                        int lastInd1 = 0;
                    570:                        if ((i == n) || (!i)) {
                    571:                                ind1 = n-1;
                    572:                                ind2 = 0;
                    573:                        } else {
                    574:                                ind1 = i-1;
                    575:                                ind2 = i;
                    576:                        }
                    577:                        y1 = p[ind1].y;
                    578:                        y2 = p[ind2].y;
                    579:                        if (y1 < y2) {
                    580:                                y1 = p[ind1].y;
                    581:                                y2 = p[ind2].y;
                    582:                                x1 = p[ind1].x;
                    583:                                x2 = p[ind2].x;
                    584:                                dir = -1;
                    585:                        } else if (y1 > y2) {
                    586:                                y2 = p[ind1].y;
                    587:                                y1 = p[ind2].y;
                    588:                                x2 = p[ind1].x;
                    589:                                x1 = p[ind2].x;
                    590:                                dir = 1;
                    591:                        } else {
                    592:                                /* Horizontal; just draw it */
1.2       paf       593:                                Line( 
1.1       paf       594:                                        p[ind1].x, y1, 
                    595:                                        p[ind2].x, y1,
                    596:                                        c);
                    597:                                continue;
                    598:                        }
                    599:                        if ((y >= y1) && (y <= y2)) {
                    600:                                int inter = 
                    601:                                        (y-y1) * (x2-x1) / (y2-y1) + x1;
                    602:                                /* Only count intersections once
                    603:                                        except at maxima and minima. Also, 
                    604:                                        if two consecutive intersections are
                    605:                                        endpoints of the same horizontal line
                    606:                                        that is not at a maxima or minima,      
                    607:                                        discard the leftmost of the two. */
                    608:                                if (!interFirst) {
                    609:                                        if ((p[ind1].y == p[lastInd1].y) &&
                    610:                                                (p[ind1].x != p[lastInd1].x)) {
                    611:                                                if (dir == dirLast) {
                    612:                                                        if (inter > interLast) {
                    613:                                                                /* Replace the old one */
1.2       paf       614:                                                                polyInts[ints] = inter;
1.1       paf       615:                                                        } else {
                    616:                                                                /* Discard this one */
                    617:                                                        }       
                    618:                                                        continue;
                    619:                                                }
                    620:                                        }
                    621:                                        if (inter == interLast) {
                    622:                                                if (dir == dirLast) {
                    623:                                                        continue;
                    624:                                                }
                    625:                                        }
                    626:                                } 
                    627:                                if (i > 0) {
1.2       paf       628:                                        polyInts[ints++] = inter;
1.1       paf       629:                                }
                    630:                                lastInd1 = i;
                    631:                                dirLast = dir;
                    632:                                interLast = inter;
                    633:                                interFirst = 0;
                    634:                        }
                    635:                }
1.2       paf       636:                qsort(polyInts, ints, sizeof(int), gdCompareInt);
                    637:                for (i=0; (i < (ints-1)); i+=2)
                    638:                        Line(polyInts[i], y, polyInts[i+1], y, c);
1.1       paf       639:        }
                    640: }
                    641: 
1.2       paf       642: //001005paf this used in drawing straight lines in gdImage::FilledPolygonReplaceColor
                    643: void gdImage::LineReplaceColor(int x1, int y1, int x2, int y2, int a, int b) {
1.1       paf       644:        if(y1!=y2)
                    645:                return;
                    646: 
                    647:        for(int x=x1; x<=x2; x++) {
1.2       paf       648:                unsigned char *pixel=&pixels[x][y1];
1.1       paf       649:                if(*pixel==a)
                    650:                        *pixel=b;
                    651:        }
                    652: }
                    653: 
1.2       paf       654: void gdImage::FilledPolygonReplaceColor(Point *p, int n, int a, int b)
1.1       paf       655: {
                    656:        int i;
                    657:        int y;
                    658:        int y1, y2;
                    659:        int ints;
                    660:        if (!n) {
                    661:                return;
                    662:        }
1.2       paf       663:        if (!polyAllocated) {
                    664:                polyInts = (int *) malloc(sizeof(int) * n);
                    665:                polyAllocated = n;
1.1       paf       666:        }               
1.2       paf       667:        if (polyAllocated < n) {
                    668:                while (polyAllocated < n) {
                    669:                        polyAllocated *= 2;
1.1       paf       670:                }       
1.2       paf       671:                polyInts = (int *) realloc(polyInts,
                    672:                        sizeof(int) * polyAllocated);
1.1       paf       673:        }
                    674:        y1 = p[0].y;
                    675:        y2 = p[0].y;
                    676:        for (i=1; (i < n); i++) {
                    677:                if (p[i].y < y1) {
                    678:                        y1 = p[i].y;
                    679:                }
                    680:                if (p[i].y > y2) {
                    681:                        y2 = p[i].y;
                    682:                }
                    683:        }
                    684:        for (y=y1; (y <= y2); y++) {
                    685:                int interLast = 0;
                    686:                int dirLast = 0;
                    687:                int interFirst = 1;
                    688:                ints = 0;
                    689:                for (i=0; (i <= n); i++) {
                    690:                        int x1, x2;
                    691:                        int y1, y2;
                    692:                        int dir;
                    693:                        int ind1, ind2;
                    694:                        int lastInd1 = 0;
                    695:                        if ((i == n) || (!i)) {
                    696:                                ind1 = n-1;
                    697:                                ind2 = 0;
                    698:                        } else {
                    699:                                ind1 = i-1;
                    700:                                ind2 = i;
                    701:                        }
                    702:                        y1 = p[ind1].y;
                    703:                        y2 = p[ind2].y;
                    704:                        if (y1 < y2) {
                    705:                                y1 = p[ind1].y;
                    706:                                y2 = p[ind2].y;
                    707:                                x1 = p[ind1].x;
                    708:                                x2 = p[ind2].x;
                    709:                                dir = -1;
                    710:                        } else if (y1 > y2) {
                    711:                                y2 = p[ind1].y;
                    712:                                y1 = p[ind2].y;
                    713:                                x2 = p[ind1].x;
                    714:                                x1 = p[ind2].x;
                    715:                                dir = 1;
                    716:                        } else {
                    717:                                /* Horizontal; just draw it */
1.2       paf       718:                                LineReplaceColor( 
1.1       paf       719:                                        p[ind1].x, y1, 
                    720:                                        p[ind2].x, y1,
                    721:                                        a,b);
                    722:                                continue;
                    723:                        }
                    724:                        if ((y >= y1) && (y <= y2)) {
                    725:                                int inter = 
                    726:                                        (y-y1) * (x2-x1) / (y2-y1) + x1;
                    727:                                /* Only count intersections once
                    728:                                        except at maxima and minima. Also, 
                    729:                                        if two consecutive intersections are
                    730:                                        endpoints of the same horizontal line
                    731:                                        that is not at a maxima or minima,      
                    732:                                        discard the leftmost of the two. */
                    733:                                if (!interFirst) {
                    734:                                        if ((p[ind1].y == p[lastInd1].y) &&
                    735:                                                (p[ind1].x != p[lastInd1].x)) {
                    736:                                                if (dir == dirLast) {
                    737:                                                        if (inter > interLast) {
                    738:                                                                /* Replace the old one */
1.2       paf       739:                                                                polyInts[ints] = inter;
1.1       paf       740:                                                        } else {
                    741:                                                                /* Discard this one */
                    742:                                                        }       
                    743:                                                        continue;
                    744:                                                }
                    745:                                        }
                    746:                                        if (inter == interLast) {
                    747:                                                if (dir == dirLast) {
                    748:                                                        continue;
                    749:                                                }
                    750:                                        }
                    751:                                } 
                    752:                                if (i > 0) {
1.2       paf       753:                                        polyInts[ints++] = inter;
1.1       paf       754:                                }
                    755:                                lastInd1 = i;
                    756:                                dirLast = dir;
                    757:                                interLast = inter;
                    758:                                interFirst = 0;
                    759:                        }
                    760:                }
1.2       paf       761:                qsort(polyInts, ints, sizeof(int), gdCompareInt);
1.1       paf       762:                for (i=0; (i < (ints-1)); i+=2) {
1.2       paf       763:                        LineReplaceColor(polyInts[i], y,
                    764:                                polyInts[i+1], y, a,b);
1.1       paf       765:                }
                    766:        }
                    767: }
                    768: 
1.2       paf       769: void gdImage::SetInterlace(int interlaceArg)
1.1       paf       770: {
1.2       paf       771:        interlace = interlaceArg;
1.1       paf       772: }
                    773: 
1.6       parser    774: void gdImage::SetLineWidth(int width)
                    775: {
                    776:        lineWidth=width;
                    777: }
                    778: 
                    779: void gdImage::SetLineStyle(const char *alineStyle)
1.1       paf       780: {
1.6       parser    781:        lineStyle=alineStyle;
1.1       paf       782: }
                    783: 

E-mail: