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

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

E-mail: