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

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.10    ! parser      7:        $Id: gif.C,v 1.9 2001/09/26 10:32:25 parser Exp $
1.9       parser      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: {
1.10    ! parser    422:        if(x1>x2) {
        !           423:                int t=x1;
        !           424:                x1=x2;
        !           425:                x2=t;
        !           426:        }
        !           427:        if(y1>y2) {
        !           428:                int t=y1;
        !           429:                y1=y2;
        !           430:                y2=t;
        !           431:        }
1.1       paf       432:        int x, y;
1.10    ! parser    433: 
1.2       paf       434:        for (y=y1; (y<=y2); y++)
                    435:                for (x=x1; (x<=x2); x++)
                    436:                        SetPixel(x, y, color);
1.1       paf       437: }
                    438: 
1.2       paf       439: void gdImage::Copy(gdImage& dst, int dstX, int dstY, int srcX, int srcY, int w, int h)
1.1       paf       440: {
                    441:        int c;
                    442:        int x, y;
                    443:        int tox, toy;
                    444:        int i;
                    445:        int colorMap[gdMaxColors];
                    446:        for (i=0; (i<gdMaxColors); i++) {
                    447:                colorMap[i] = (-1);
                    448:        }
                    449:        toy = dstY;
                    450:        for (y=srcY; (y < (srcY + h)); y++) {
                    451:                tox = dstX;
                    452:                for (x=srcX; (x < (srcX + w)); x++) {
                    453:                        int nc;
1.2       paf       454:                        c = GetPixel(x, y);
1.1       paf       455:                        /* Added 7/24/95: support transparent copies */
1.2       paf       456:                        if (GetTransparent() == c) {
1.1       paf       457:                                tox++;
                    458:                                continue;
                    459:                        }
                    460:                        /* Have we established a mapping for this color? */
                    461:                        if (colorMap[c] == (-1)) {
                    462:                                /* If it's the same image, mapping is trivial */
1.2       paf       463:                                if (&dst == this) {
1.1       paf       464:                                        nc = c;
                    465:                                } else { 
                    466:                                        /* First look for an exact match */
1.2       paf       467:                                        nc = dst.ColorExact(
                    468:                                                red[c], green[c],
                    469:                                                blue[c]);
1.1       paf       470:                                }       
                    471:                                if (nc == (-1)) {
                    472:                                        /* No, so try to allocate it */
1.2       paf       473:                                        nc = dst.ColorAllocate(
                    474:                                                red[c], green[c],
                    475:                                                blue[c]);
1.1       paf       476:                                        /* If we're out of colors, go for the
                    477:                                                closest color */
                    478:                                        if (nc == (-1)) {
1.2       paf       479:                                                nc = dst.ColorClosest(
                    480:                                                        red[c], green[c],
                    481:                                                        blue[c]);
1.1       paf       482:                                        }
                    483:                                }
                    484:                                colorMap[c] = nc;
                    485:                        }
1.2       paf       486:                        dst.SetPixel(tox, toy, colorMap[c]);
1.1       paf       487:                        tox++;
                    488:                }
                    489:                toy++;
                    490:        }
                    491: }                      
                    492: 
1.2       paf       493: static int gdGetWord(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 << 8;
                    501:        r = getc(in);   
                    502:        if (r == EOF) {
                    503:                return 0;
                    504:        }
                    505:        *result += r;
                    506:        return 1;
                    507: }
                    508: 
1.2       paf       509: static void gdPutWord(int w, FILE *out)
1.1       paf       510: {
                    511:        putc((unsigned char)(w >> 8), out);
                    512:        putc((unsigned char)(w & 0xFF), out);
                    513: }
                    514: 
1.2       paf       515: static int gdGetByte(int *result, FILE *in)
1.1       paf       516: {
                    517:        int r;
                    518:        r = getc(in);
                    519:        if (r == EOF) {
                    520:                return 0;
                    521:        }
                    522:        *result = r;
                    523:        return 1;
                    524: }
                    525: 
1.6       parser    526: void gdImage::Polygon(Point *p, int n, int c, bool closed)
1.1       paf       527: {
                    528:        int i;
                    529:        int lx, ly;
                    530:        if (!n) {
                    531:                return;
                    532:        }
                    533:        lx = p->x;
                    534:        ly = p->y;
1.6       parser    535:        if(closed)
                    536:                Line(lx, ly, p[n-1].x, p[n-1].y, c);
1.1       paf       537:        for (i=1; (i < n); i++) {
                    538:                p++;
1.2       paf       539:                Line(lx, ly, p->x, p->y, c);
1.1       paf       540:                lx = p->x;
                    541:                ly = p->y;
                    542:        }
                    543: }      
                    544:        
1.5       paf       545: static int gdCompareInt(const void *a, const void *b)
                    546: {
                    547:        return (*(const int *)a) - (*(const int *)b);
                    548: }
                    549: 
                    550: 
1.1       paf       551:        
1.2       paf       552: void gdImage::FilledPolygon(Point *p, int n, int c)
1.1       paf       553: {
                    554:        int i;
                    555:        int y;
                    556:        int y1, y2;
                    557:        int ints;
                    558:        if (!n) {
                    559:                return;
                    560:        }
1.2       paf       561:        if (!polyAllocated) {
                    562:                polyInts = (int *) malloc(sizeof(int) * n);
                    563:                polyAllocated = n;
1.1       paf       564:        }               
1.2       paf       565:        if (polyAllocated < n) {
                    566:                while (polyAllocated < n) {
                    567:                        polyAllocated *= 2;
1.1       paf       568:                }       
1.2       paf       569:                polyInts = (int *) realloc(polyInts,
                    570:                        sizeof(int) * polyAllocated);
1.1       paf       571:        }
                    572:        y1 = p[0].y;
                    573:        y2 = p[0].y;
                    574:        for (i=1; (i < n); i++) {
                    575:                if (p[i].y < y1) {
                    576:                        y1 = p[i].y;
                    577:                }
                    578:                if (p[i].y > y2) {
                    579:                        y2 = p[i].y;
                    580:                }
                    581:        }
                    582:        for (y=y1; (y <= y2); y++) {
                    583:                int interLast = 0;
                    584:                int dirLast = 0;
                    585:                int interFirst = 1;
                    586:                ints = 0;
                    587:                for (i=0; (i <= n); i++) {
                    588:                        int x1, x2;
                    589:                        int y1, y2;
                    590:                        int dir;
                    591:                        int ind1, ind2;
                    592:                        int lastInd1 = 0;
                    593:                        if ((i == n) || (!i)) {
                    594:                                ind1 = n-1;
                    595:                                ind2 = 0;
                    596:                        } else {
                    597:                                ind1 = i-1;
                    598:                                ind2 = i;
                    599:                        }
                    600:                        y1 = p[ind1].y;
                    601:                        y2 = p[ind2].y;
                    602:                        if (y1 < y2) {
                    603:                                y1 = p[ind1].y;
                    604:                                y2 = p[ind2].y;
                    605:                                x1 = p[ind1].x;
                    606:                                x2 = p[ind2].x;
                    607:                                dir = -1;
                    608:                        } else if (y1 > y2) {
                    609:                                y2 = p[ind1].y;
                    610:                                y1 = p[ind2].y;
                    611:                                x2 = p[ind1].x;
                    612:                                x1 = p[ind2].x;
                    613:                                dir = 1;
                    614:                        } else {
                    615:                                /* Horizontal; just draw it */
1.2       paf       616:                                Line( 
1.1       paf       617:                                        p[ind1].x, y1, 
                    618:                                        p[ind2].x, y1,
                    619:                                        c);
                    620:                                continue;
                    621:                        }
                    622:                        if ((y >= y1) && (y <= y2)) {
                    623:                                int inter = 
                    624:                                        (y-y1) * (x2-x1) / (y2-y1) + x1;
                    625:                                /* Only count intersections once
                    626:                                        except at maxima and minima. Also, 
                    627:                                        if two consecutive intersections are
                    628:                                        endpoints of the same horizontal line
                    629:                                        that is not at a maxima or minima,      
                    630:                                        discard the leftmost of the two. */
                    631:                                if (!interFirst) {
                    632:                                        if ((p[ind1].y == p[lastInd1].y) &&
                    633:                                                (p[ind1].x != p[lastInd1].x)) {
                    634:                                                if (dir == dirLast) {
                    635:                                                        if (inter > interLast) {
                    636:                                                                /* Replace the old one */
1.2       paf       637:                                                                polyInts[ints] = inter;
1.1       paf       638:                                                        } else {
                    639:                                                                /* Discard this one */
                    640:                                                        }       
                    641:                                                        continue;
                    642:                                                }
                    643:                                        }
                    644:                                        if (inter == interLast) {
                    645:                                                if (dir == dirLast) {
                    646:                                                        continue;
                    647:                                                }
                    648:                                        }
                    649:                                } 
                    650:                                if (i > 0) {
1.2       paf       651:                                        polyInts[ints++] = inter;
1.1       paf       652:                                }
                    653:                                lastInd1 = i;
                    654:                                dirLast = dir;
                    655:                                interLast = inter;
                    656:                                interFirst = 0;
                    657:                        }
                    658:                }
1.2       paf       659:                qsort(polyInts, ints, sizeof(int), gdCompareInt);
                    660:                for (i=0; (i < (ints-1)); i+=2)
                    661:                        Line(polyInts[i], y, polyInts[i+1], y, c);
1.1       paf       662:        }
                    663: }
                    664: 
1.2       paf       665: //001005paf this used in drawing straight lines in gdImage::FilledPolygonReplaceColor
                    666: void gdImage::LineReplaceColor(int x1, int y1, int x2, int y2, int a, int b) {
1.1       paf       667:        if(y1!=y2)
                    668:                return;
                    669: 
                    670:        for(int x=x1; x<=x2; x++) {
1.2       paf       671:                unsigned char *pixel=&pixels[x][y1];
1.1       paf       672:                if(*pixel==a)
                    673:                        *pixel=b;
                    674:        }
                    675: }
                    676: 
1.2       paf       677: void gdImage::FilledPolygonReplaceColor(Point *p, int n, int a, int b)
1.1       paf       678: {
                    679:        int i;
                    680:        int y;
                    681:        int y1, y2;
                    682:        int ints;
                    683:        if (!n) {
                    684:                return;
                    685:        }
1.2       paf       686:        if (!polyAllocated) {
                    687:                polyInts = (int *) malloc(sizeof(int) * n);
                    688:                polyAllocated = n;
1.1       paf       689:        }               
1.2       paf       690:        if (polyAllocated < n) {
                    691:                while (polyAllocated < n) {
                    692:                        polyAllocated *= 2;
1.1       paf       693:                }       
1.2       paf       694:                polyInts = (int *) realloc(polyInts,
                    695:                        sizeof(int) * polyAllocated);
1.1       paf       696:        }
                    697:        y1 = p[0].y;
                    698:        y2 = p[0].y;
                    699:        for (i=1; (i < n); i++) {
                    700:                if (p[i].y < y1) {
                    701:                        y1 = p[i].y;
                    702:                }
                    703:                if (p[i].y > y2) {
                    704:                        y2 = p[i].y;
                    705:                }
                    706:        }
                    707:        for (y=y1; (y <= y2); y++) {
                    708:                int interLast = 0;
                    709:                int dirLast = 0;
                    710:                int interFirst = 1;
                    711:                ints = 0;
                    712:                for (i=0; (i <= n); i++) {
                    713:                        int x1, x2;
                    714:                        int y1, y2;
                    715:                        int dir;
                    716:                        int ind1, ind2;
                    717:                        int lastInd1 = 0;
                    718:                        if ((i == n) || (!i)) {
                    719:                                ind1 = n-1;
                    720:                                ind2 = 0;
                    721:                        } else {
                    722:                                ind1 = i-1;
                    723:                                ind2 = i;
                    724:                        }
                    725:                        y1 = p[ind1].y;
                    726:                        y2 = p[ind2].y;
                    727:                        if (y1 < y2) {
                    728:                                y1 = p[ind1].y;
                    729:                                y2 = p[ind2].y;
                    730:                                x1 = p[ind1].x;
                    731:                                x2 = p[ind2].x;
                    732:                                dir = -1;
                    733:                        } else if (y1 > y2) {
                    734:                                y2 = p[ind1].y;
                    735:                                y1 = p[ind2].y;
                    736:                                x2 = p[ind1].x;
                    737:                                x1 = p[ind2].x;
                    738:                                dir = 1;
                    739:                        } else {
                    740:                                /* Horizontal; just draw it */
1.2       paf       741:                                LineReplaceColor( 
1.1       paf       742:                                        p[ind1].x, y1, 
                    743:                                        p[ind2].x, y1,
                    744:                                        a,b);
                    745:                                continue;
                    746:                        }
                    747:                        if ((y >= y1) && (y <= y2)) {
                    748:                                int inter = 
                    749:                                        (y-y1) * (x2-x1) / (y2-y1) + x1;
                    750:                                /* Only count intersections once
                    751:                                        except at maxima and minima. Also, 
                    752:                                        if two consecutive intersections are
                    753:                                        endpoints of the same horizontal line
                    754:                                        that is not at a maxima or minima,      
                    755:                                        discard the leftmost of the two. */
                    756:                                if (!interFirst) {
                    757:                                        if ((p[ind1].y == p[lastInd1].y) &&
                    758:                                                (p[ind1].x != p[lastInd1].x)) {
                    759:                                                if (dir == dirLast) {
                    760:                                                        if (inter > interLast) {
                    761:                                                                /* Replace the old one */
1.2       paf       762:                                                                polyInts[ints] = inter;
1.1       paf       763:                                                        } else {
                    764:                                                                /* Discard this one */
                    765:                                                        }       
                    766:                                                        continue;
                    767:                                                }
                    768:                                        }
                    769:                                        if (inter == interLast) {
                    770:                                                if (dir == dirLast) {
                    771:                                                        continue;
                    772:                                                }
                    773:                                        }
                    774:                                } 
                    775:                                if (i > 0) {
1.2       paf       776:                                        polyInts[ints++] = inter;
1.1       paf       777:                                }
                    778:                                lastInd1 = i;
                    779:                                dirLast = dir;
                    780:                                interLast = inter;
                    781:                                interFirst = 0;
                    782:                        }
                    783:                }
1.2       paf       784:                qsort(polyInts, ints, sizeof(int), gdCompareInt);
1.1       paf       785:                for (i=0; (i < (ints-1)); i+=2) {
1.2       paf       786:                        LineReplaceColor(polyInts[i], y,
                    787:                                polyInts[i+1], y, a,b);
1.1       paf       788:                }
                    789:        }
                    790: }
                    791: 
1.2       paf       792: void gdImage::SetInterlace(int interlaceArg)
1.1       paf       793: {
1.2       paf       794:        interlace = interlaceArg;
1.1       paf       795: }
                    796: 
1.6       parser    797: void gdImage::SetLineWidth(int width)
                    798: {
                    799:        lineWidth=width;
                    800: }
                    801: 
                    802: void gdImage::SetLineStyle(const char *alineStyle)
1.1       paf       803: {
1.6       parser    804:        lineStyle=alineStyle;
1.1       paf       805: }
                    806: 

E-mail: