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

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

E-mail: