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

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.13    ! parser      7:        $Id: gif.C,v 1.12 2001/10/08 15:50:22 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: 
                    274: void gdImage::Arc(int cx, int cy, int w, int h, int s, int e, int color)
                    275: {
1.13    ! parser    276:        if(w!=h) {
        !           277:                int i;
        !           278:                int lx = 0, ly = 0;
        !           279:                int w2, h2;
        !           280:                w2 = w/2;
        !           281:                h2 = h/2;
        !           282:                while (e < s) {
        !           283:                        e += 360;
        !           284:                }
        !           285:                for (i=s; (i <= e); i++) {
        !           286:                        int x, y;
        !           287:                        x = ((long)cost[i % 360] * (long)w2 / costScale) + cx; 
        !           288:                        y = ((long)sint[i % 360] * (long)h2 / sintScale) + cy;
        !           289:                        if (i != s) {
        !           290:                                Line(lx, ly, x, y, color);      
        !           291:                        }
        !           292:                        lx = x;
        !           293:                        ly = y;
        !           294:                }
        !           295:        } else {
        !           296:                /* Bresenham octant code, which I should use eventually */
        !           297:                int x, y, d;
        !           298:                x = 0;
        !           299:                y = w/2;
        !           300:                d = 3-w;
        !           301:                while (x <= y) {
        !           302:                        SetPixel(cx+x, cy+y, color);
        !           303:                        SetPixel(cx+x, cy-y, color);
        !           304:                        SetPixel(cx-x, cy+y, color);
        !           305:                        SetPixel(cx-x, cy-y, color);
        !           306:                        SetPixel(cx+y, cy+x, color);
        !           307:                        SetPixel(cx+y, cy-x, color);
        !           308:                        SetPixel(cx-y, cy+x, color);
        !           309:                        SetPixel(cx-y, cy-x, color);
        !           310:                        if (d < 0) {
        !           311:                                d += 4 * x + 6;
        !           312:                        } else {
        !           313:                                d += 4 * (x - y) + 10;
        !           314:                                y--;
        !           315:                        }
        !           316:                        x++;
1.11      parser    317:                }
                    318:        }
                    319: }
                    320: 
1.12      parser    321: 
                    322: void gdImage::Sector(int cx, int cy, int w, int h, int s, int e, int color)
                    323: {
                    324:        int i;
                    325:        int lx = 0, ly = 0;
                    326:        int w2, h2;
                    327:        w2 = w/2;
                    328:        h2 = h/2;
                    329:        while (e < s) {
                    330:                e += 360;
                    331:        }
                    332:        for (i=s; (i <= e); i++) {
                    333:                int x, y;
                    334:                x = ((long)cost[i % 360] * (long)w2 / costScale) + cx; 
                    335:                y = ((long)sint[i % 360] * (long)h2 / sintScale) + cy;
                    336:                if(i==s || i==e)
                    337:                        Line(cx, cy, x, y, color);
                    338:                if (i != s) {
                    339:                        Line(lx, ly, x, y, color);      
                    340:                }
                    341:                lx = x;
                    342:                ly = y;
                    343:        }
                    344: }
1.11      parser    345: 
                    346: 
1.1       paf       347: 
1.2       paf       348: void gdImage::FillToBorder(int x, int y, int border, int color)
1.1       paf       349: {
1.4       paf       350:        if(!BoundsSafe(x, y)) //PAF
                    351:                return;
                    352: 
1.1       paf       353:        int lastBorder;
                    354:        /* Seek left */
                    355:        int leftLimit, rightLimit;
                    356:        int i;
                    357:        leftLimit = (-1);
                    358:        if (border < 0) {
                    359:                /* Refuse to fill to a non-solid border */
                    360:                return;
                    361:        }
                    362:        for (i = x; (i >= 0); i--) {
1.2       paf       363:                if (GetPixel(i, y) == border) {
1.1       paf       364:                        break;
                    365:                }
1.2       paf       366:                SetPixel(i, y, color);
1.1       paf       367:                leftLimit = i;
                    368:        }
                    369:        if (leftLimit == (-1)) {
                    370:                return;
                    371:        }
                    372:        /* Seek right */
                    373:        rightLimit = x;
1.2       paf       374:        for (i = (x+1); (i < sx); i++) {        
                    375:                if (GetPixel(i, y) == border) {
1.1       paf       376:                        break;
                    377:                }
1.2       paf       378:                SetPixel(i, y, color);
1.1       paf       379:                rightLimit = i;
                    380:        }
                    381:        /* Look at lines above and below and start paints */
                    382:        /* Above */
                    383:        if (y > 0) {
                    384:                lastBorder = 1;
                    385:                for (i = leftLimit; (i <= rightLimit); i++) {
                    386:                        int c;
1.2       paf       387:                        c = GetPixel(i, y-1);
1.1       paf       388:                        if (lastBorder) {
                    389:                                if ((c != border) && (c != color)) {    
1.2       paf       390:                                        FillToBorder(i, y-1, 
1.1       paf       391:                                                border, color);         
                    392:                                        lastBorder = 0;
                    393:                                }
                    394:                        } else if ((c == border) || (c == color)) {
                    395:                                lastBorder = 1;
                    396:                        }
                    397:                }
                    398:        }
                    399:        /* Below */
1.2       paf       400:        if (y < ((sy) - 1)) {
1.1       paf       401:                lastBorder = 1;
                    402:                for (i = leftLimit; (i <= rightLimit); i++) {
                    403:                        int c;
1.2       paf       404:                        c = GetPixel(i, y+1);
1.1       paf       405:                        if (lastBorder) {
                    406:                                if ((c != border) && (c != color)) {    
1.2       paf       407:                                        FillToBorder(i, y+1, 
1.1       paf       408:                                                border, color);         
                    409:                                        lastBorder = 0;
                    410:                                }
                    411:                        } else if ((c == border) || (c == color)) {
                    412:                                lastBorder = 1;
                    413:                        }
                    414:                }
                    415:        }
                    416: }
                    417: 
1.2       paf       418: void gdImage::Fill(int x, int y, int color)
1.1       paf       419: {
1.4       paf       420:        if(!BoundsSafe(x, y)) //PAF
                    421:                return;
                    422: 
1.1       paf       423:        int lastBorder;
                    424:        int old;
                    425:        int leftLimit, rightLimit;
                    426:        int i;
1.2       paf       427:        old = GetPixel(x, y);
1.1       paf       428:        if (old == color) {
                    429:                /* Nothing to be done */
                    430:                return;
                    431:        }
                    432:        /* Seek left */
                    433:        leftLimit = (-1);
                    434:        for (i = x; (i >= 0); i--) {
1.2       paf       435:                if (GetPixel(i, y) != old) {
1.1       paf       436:                        break;
                    437:                }
1.2       paf       438:                SetPixel(i, y, color);
1.1       paf       439:                leftLimit = i;
                    440:        }
                    441:        if (leftLimit == (-1)) {
                    442:                return;
                    443:        }
                    444:        /* Seek right */
                    445:        rightLimit = x;
1.2       paf       446:        for (i = (x+1); (i < sx); i++) {        
                    447:                if (GetPixel(i, y) != old) {
1.1       paf       448:                        break;
                    449:                }
1.2       paf       450:                SetPixel(i, y, color);
1.1       paf       451:                rightLimit = i;
                    452:        }
                    453:        /* Look at lines above and below and start paints */
                    454:        /* Above */
                    455:        if (y > 0) {
                    456:                lastBorder = 1;
                    457:                for (i = leftLimit; (i <= rightLimit); i++) {
                    458:                        int c;
1.2       paf       459:                        c = GetPixel(i, y-1);
1.1       paf       460:                        if (lastBorder) {
                    461:                                if (c == old) { 
1.2       paf       462:                                        Fill(i, y-1, color);            
1.1       paf       463:                                        lastBorder = 0;
                    464:                                }
                    465:                        } else if (c != old) {
                    466:                                lastBorder = 1;
                    467:                        }
                    468:                }
                    469:        }
                    470:        /* Below */
1.2       paf       471:        if (y < ((sy) - 1)) {
1.1       paf       472:                lastBorder = 1;
                    473:                for (i = leftLimit; (i <= rightLimit); i++) {
                    474:                        int c;
1.2       paf       475:                        c = GetPixel(i, y+1);
1.1       paf       476:                        if (lastBorder) {
                    477:                                if (c == old) {
1.2       paf       478:                                        Fill(i, y+1, color);            
1.1       paf       479:                                        lastBorder = 0;
                    480:                                }
                    481:                        } else if (c != old) {
                    482:                                lastBorder = 1;
                    483:                        }
                    484:                }
                    485:        }
                    486: }
                    487:        
1.2       paf       488: void gdImage::Rectangle(int x1, int y1, int x2, int y2, int color)
1.1       paf       489: {
1.2       paf       490:        Line(x1, y1, x2, y1, color);            
                    491:        Line(x1, y2, x2, y2, color);            
                    492:        Line(x1, y1, x1, y2, color);
                    493:        Line(x2, y1, x2, y2, color);
1.1       paf       494: }
                    495: 
1.2       paf       496: void gdImage::FilledRectangle(int x1, int y1, int x2, int y2, int color)
1.1       paf       497: {
1.10      parser    498:        if(x1>x2) {
                    499:                int t=x1;
                    500:                x1=x2;
                    501:                x2=t;
                    502:        }
                    503:        if(y1>y2) {
                    504:                int t=y1;
                    505:                y1=y2;
                    506:                y2=t;
                    507:        }
1.1       paf       508:        int x, y;
1.10      parser    509: 
1.2       paf       510:        for (y=y1; (y<=y2); y++)
                    511:                for (x=x1; (x<=x2); x++)
                    512:                        SetPixel(x, y, color);
1.1       paf       513: }
                    514: 
1.2       paf       515: void gdImage::Copy(gdImage& dst, int dstX, int dstY, int srcX, int srcY, int w, int h)
1.1       paf       516: {
                    517:        int c;
                    518:        int x, y;
                    519:        int tox, toy;
                    520:        int i;
                    521:        int colorMap[gdMaxColors];
                    522:        for (i=0; (i<gdMaxColors); i++) {
                    523:                colorMap[i] = (-1);
                    524:        }
                    525:        toy = dstY;
                    526:        for (y=srcY; (y < (srcY + h)); y++) {
                    527:                tox = dstX;
                    528:                for (x=srcX; (x < (srcX + w)); x++) {
                    529:                        int nc;
1.2       paf       530:                        c = GetPixel(x, y);
1.1       paf       531:                        /* Added 7/24/95: support transparent copies */
1.2       paf       532:                        if (GetTransparent() == c) {
1.1       paf       533:                                tox++;
                    534:                                continue;
                    535:                        }
                    536:                        /* Have we established a mapping for this color? */
                    537:                        if (colorMap[c] == (-1)) {
                    538:                                /* If it's the same image, mapping is trivial */
1.2       paf       539:                                if (&dst == this) {
1.1       paf       540:                                        nc = c;
                    541:                                } else { 
                    542:                                        /* First look for an exact match */
1.2       paf       543:                                        nc = dst.ColorExact(
                    544:                                                red[c], green[c],
                    545:                                                blue[c]);
1.1       paf       546:                                }       
                    547:                                if (nc == (-1)) {
                    548:                                        /* No, so try to allocate it */
1.2       paf       549:                                        nc = dst.ColorAllocate(
                    550:                                                red[c], green[c],
                    551:                                                blue[c]);
1.1       paf       552:                                        /* If we're out of colors, go for the
                    553:                                                closest color */
                    554:                                        if (nc == (-1)) {
1.2       paf       555:                                                nc = dst.ColorClosest(
                    556:                                                        red[c], green[c],
                    557:                                                        blue[c]);
1.1       paf       558:                                        }
                    559:                                }
                    560:                                colorMap[c] = nc;
                    561:                        }
1.2       paf       562:                        dst.SetPixel(tox, toy, colorMap[c]);
1.1       paf       563:                        tox++;
                    564:                }
                    565:                toy++;
                    566:        }
                    567: }                      
                    568: 
1.2       paf       569: static int gdGetWord(int *result, FILE *in)
1.1       paf       570: {
                    571:        int r;
                    572:        r = getc(in);
                    573:        if (r == EOF) {
                    574:                return 0;
                    575:        }
                    576:        *result = r << 8;
                    577:        r = getc(in);   
                    578:        if (r == EOF) {
                    579:                return 0;
                    580:        }
                    581:        *result += r;
                    582:        return 1;
                    583: }
                    584: 
1.2       paf       585: static void gdPutWord(int w, FILE *out)
1.1       paf       586: {
                    587:        putc((unsigned char)(w >> 8), out);
                    588:        putc((unsigned char)(w & 0xFF), out);
                    589: }
                    590: 
1.2       paf       591: static int gdGetByte(int *result, FILE *in)
1.1       paf       592: {
                    593:        int r;
                    594:        r = getc(in);
                    595:        if (r == EOF) {
                    596:                return 0;
                    597:        }
                    598:        *result = r;
                    599:        return 1;
                    600: }
                    601: 
1.6       parser    602: void gdImage::Polygon(Point *p, int n, int c, bool closed)
1.1       paf       603: {
                    604:        int i;
                    605:        int lx, ly;
                    606:        if (!n) {
                    607:                return;
                    608:        }
                    609:        lx = p->x;
                    610:        ly = p->y;
1.6       parser    611:        if(closed)
                    612:                Line(lx, ly, p[n-1].x, p[n-1].y, c);
1.1       paf       613:        for (i=1; (i < n); i++) {
                    614:                p++;
1.2       paf       615:                Line(lx, ly, p->x, p->y, c);
1.1       paf       616:                lx = p->x;
                    617:                ly = p->y;
                    618:        }
                    619: }      
                    620:        
1.5       paf       621: static int gdCompareInt(const void *a, const void *b)
                    622: {
                    623:        return (*(const int *)a) - (*(const int *)b);
                    624: }
                    625: 
                    626: 
1.1       paf       627:        
1.2       paf       628: void gdImage::FilledPolygon(Point *p, int n, int c)
1.1       paf       629: {
                    630:        int i;
                    631:        int y;
                    632:        int y1, y2;
                    633:        int ints;
                    634:        if (!n) {
                    635:                return;
                    636:        }
1.2       paf       637:        if (!polyAllocated) {
                    638:                polyInts = (int *) malloc(sizeof(int) * n);
                    639:                polyAllocated = n;
1.1       paf       640:        }               
1.2       paf       641:        if (polyAllocated < n) {
                    642:                while (polyAllocated < n) {
                    643:                        polyAllocated *= 2;
1.1       paf       644:                }       
1.2       paf       645:                polyInts = (int *) realloc(polyInts,
                    646:                        sizeof(int) * polyAllocated);
1.1       paf       647:        }
                    648:        y1 = p[0].y;
                    649:        y2 = p[0].y;
                    650:        for (i=1; (i < n); i++) {
                    651:                if (p[i].y < y1) {
                    652:                        y1 = p[i].y;
                    653:                }
                    654:                if (p[i].y > y2) {
                    655:                        y2 = p[i].y;
                    656:                }
                    657:        }
                    658:        for (y=y1; (y <= y2); y++) {
                    659:                int interLast = 0;
                    660:                int dirLast = 0;
                    661:                int interFirst = 1;
                    662:                ints = 0;
                    663:                for (i=0; (i <= n); i++) {
                    664:                        int x1, x2;
                    665:                        int y1, y2;
                    666:                        int dir;
                    667:                        int ind1, ind2;
                    668:                        int lastInd1 = 0;
                    669:                        if ((i == n) || (!i)) {
                    670:                                ind1 = n-1;
                    671:                                ind2 = 0;
                    672:                        } else {
                    673:                                ind1 = i-1;
                    674:                                ind2 = i;
                    675:                        }
                    676:                        y1 = p[ind1].y;
                    677:                        y2 = p[ind2].y;
                    678:                        if (y1 < y2) {
                    679:                                y1 = p[ind1].y;
                    680:                                y2 = p[ind2].y;
                    681:                                x1 = p[ind1].x;
                    682:                                x2 = p[ind2].x;
                    683:                                dir = -1;
                    684:                        } else if (y1 > y2) {
                    685:                                y2 = p[ind1].y;
                    686:                                y1 = p[ind2].y;
                    687:                                x2 = p[ind1].x;
                    688:                                x1 = p[ind2].x;
                    689:                                dir = 1;
                    690:                        } else {
                    691:                                /* Horizontal; just draw it */
1.2       paf       692:                                Line( 
1.1       paf       693:                                        p[ind1].x, y1, 
                    694:                                        p[ind2].x, y1,
                    695:                                        c);
                    696:                                continue;
                    697:                        }
                    698:                        if ((y >= y1) && (y <= y2)) {
                    699:                                int inter = 
                    700:                                        (y-y1) * (x2-x1) / (y2-y1) + x1;
                    701:                                /* Only count intersections once
                    702:                                        except at maxima and minima. Also, 
                    703:                                        if two consecutive intersections are
                    704:                                        endpoints of the same horizontal line
                    705:                                        that is not at a maxima or minima,      
                    706:                                        discard the leftmost of the two. */
                    707:                                if (!interFirst) {
                    708:                                        if ((p[ind1].y == p[lastInd1].y) &&
                    709:                                                (p[ind1].x != p[lastInd1].x)) {
                    710:                                                if (dir == dirLast) {
                    711:                                                        if (inter > interLast) {
                    712:                                                                /* Replace the old one */
1.2       paf       713:                                                                polyInts[ints] = inter;
1.1       paf       714:                                                        } else {
                    715:                                                                /* Discard this one */
                    716:                                                        }       
                    717:                                                        continue;
                    718:                                                }
                    719:                                        }
                    720:                                        if (inter == interLast) {
                    721:                                                if (dir == dirLast) {
                    722:                                                        continue;
                    723:                                                }
                    724:                                        }
                    725:                                } 
                    726:                                if (i > 0) {
1.2       paf       727:                                        polyInts[ints++] = inter;
1.1       paf       728:                                }
                    729:                                lastInd1 = i;
                    730:                                dirLast = dir;
                    731:                                interLast = inter;
                    732:                                interFirst = 0;
                    733:                        }
                    734:                }
1.2       paf       735:                qsort(polyInts, ints, sizeof(int), gdCompareInt);
                    736:                for (i=0; (i < (ints-1)); i+=2)
                    737:                        Line(polyInts[i], y, polyInts[i+1], y, c);
1.1       paf       738:        }
                    739: }
                    740: 
1.2       paf       741: //001005paf this used in drawing straight lines in gdImage::FilledPolygonReplaceColor
                    742: void gdImage::LineReplaceColor(int x1, int y1, int x2, int y2, int a, int b) {
1.1       paf       743:        if(y1!=y2)
                    744:                return;
                    745: 
                    746:        for(int x=x1; x<=x2; x++) {
1.2       paf       747:                unsigned char *pixel=&pixels[x][y1];
1.1       paf       748:                if(*pixel==a)
                    749:                        *pixel=b;
                    750:        }
                    751: }
                    752: 
1.2       paf       753: void gdImage::FilledPolygonReplaceColor(Point *p, int n, int a, int b)
1.1       paf       754: {
                    755:        int i;
                    756:        int y;
                    757:        int y1, y2;
                    758:        int ints;
                    759:        if (!n) {
                    760:                return;
                    761:        }
1.2       paf       762:        if (!polyAllocated) {
                    763:                polyInts = (int *) malloc(sizeof(int) * n);
                    764:                polyAllocated = n;
1.1       paf       765:        }               
1.2       paf       766:        if (polyAllocated < n) {
                    767:                while (polyAllocated < n) {
                    768:                        polyAllocated *= 2;
1.1       paf       769:                }       
1.2       paf       770:                polyInts = (int *) realloc(polyInts,
                    771:                        sizeof(int) * polyAllocated);
1.1       paf       772:        }
                    773:        y1 = p[0].y;
                    774:        y2 = p[0].y;
                    775:        for (i=1; (i < n); i++) {
                    776:                if (p[i].y < y1) {
                    777:                        y1 = p[i].y;
                    778:                }
                    779:                if (p[i].y > y2) {
                    780:                        y2 = p[i].y;
                    781:                }
                    782:        }
                    783:        for (y=y1; (y <= y2); y++) {
                    784:                int interLast = 0;
                    785:                int dirLast = 0;
                    786:                int interFirst = 1;
                    787:                ints = 0;
                    788:                for (i=0; (i <= n); i++) {
                    789:                        int x1, x2;
                    790:                        int y1, y2;
                    791:                        int dir;
                    792:                        int ind1, ind2;
                    793:                        int lastInd1 = 0;
                    794:                        if ((i == n) || (!i)) {
                    795:                                ind1 = n-1;
                    796:                                ind2 = 0;
                    797:                        } else {
                    798:                                ind1 = i-1;
                    799:                                ind2 = i;
                    800:                        }
                    801:                        y1 = p[ind1].y;
                    802:                        y2 = p[ind2].y;
                    803:                        if (y1 < y2) {
                    804:                                y1 = p[ind1].y;
                    805:                                y2 = p[ind2].y;
                    806:                                x1 = p[ind1].x;
                    807:                                x2 = p[ind2].x;
                    808:                                dir = -1;
                    809:                        } else if (y1 > y2) {
                    810:                                y2 = p[ind1].y;
                    811:                                y1 = p[ind2].y;
                    812:                                x2 = p[ind1].x;
                    813:                                x1 = p[ind2].x;
                    814:                                dir = 1;
                    815:                        } else {
                    816:                                /* Horizontal; just draw it */
1.2       paf       817:                                LineReplaceColor( 
1.1       paf       818:                                        p[ind1].x, y1, 
                    819:                                        p[ind2].x, y1,
                    820:                                        a,b);
                    821:                                continue;
                    822:                        }
                    823:                        if ((y >= y1) && (y <= y2)) {
                    824:                                int inter = 
                    825:                                        (y-y1) * (x2-x1) / (y2-y1) + x1;
                    826:                                /* Only count intersections once
                    827:                                        except at maxima and minima. Also, 
                    828:                                        if two consecutive intersections are
                    829:                                        endpoints of the same horizontal line
                    830:                                        that is not at a maxima or minima,      
                    831:                                        discard the leftmost of the two. */
                    832:                                if (!interFirst) {
                    833:                                        if ((p[ind1].y == p[lastInd1].y) &&
                    834:                                                (p[ind1].x != p[lastInd1].x)) {
                    835:                                                if (dir == dirLast) {
                    836:                                                        if (inter > interLast) {
                    837:                                                                /* Replace the old one */
1.2       paf       838:                                                                polyInts[ints] = inter;
1.1       paf       839:                                                        } else {
                    840:                                                                /* Discard this one */
                    841:                                                        }       
                    842:                                                        continue;
                    843:                                                }
                    844:                                        }
                    845:                                        if (inter == interLast) {
                    846:                                                if (dir == dirLast) {
                    847:                                                        continue;
                    848:                                                }
                    849:                                        }
                    850:                                } 
                    851:                                if (i > 0) {
1.2       paf       852:                                        polyInts[ints++] = inter;
1.1       paf       853:                                }
                    854:                                lastInd1 = i;
                    855:                                dirLast = dir;
                    856:                                interLast = inter;
                    857:                                interFirst = 0;
                    858:                        }
                    859:                }
1.2       paf       860:                qsort(polyInts, ints, sizeof(int), gdCompareInt);
1.1       paf       861:                for (i=0; (i < (ints-1)); i+=2) {
1.2       paf       862:                        LineReplaceColor(polyInts[i], y,
                    863:                                polyInts[i+1], y, a,b);
1.1       paf       864:                }
                    865:        }
                    866: }
                    867: 
1.2       paf       868: void gdImage::SetInterlace(int interlaceArg)
1.1       paf       869: {
1.2       paf       870:        interlace = interlaceArg;
1.1       paf       871: }
                    872: 
1.6       parser    873: void gdImage::SetLineWidth(int width)
                    874: {
                    875:        lineWidth=width;
                    876: }
                    877: 
                    878: void gdImage::SetLineStyle(const char *alineStyle)
1.1       paf       879: {
1.6       parser    880:        lineStyle=alineStyle;
1.1       paf       881: }
                    882: 

E-mail: