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

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

E-mail: