Subversion Repositories wimsdev

Rev

Rev 1139 | Rev 3837 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 1
/*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
2
 *
3
 *  This program is free software; you can redistribute it and/or modify
4
 *  it under the terms of the GNU General Public License as published by
5
 *  the Free Software Foundation; either version 2 of the License, or
6
 *  (at your option) any later version.
7
 *
8
 *  This program is distributed in the hope that it will be useful,
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 *  GNU General Public License for more details.
12
 *
13
 *  You should have received a copy of the GNU General Public License
14
 *  along with this program; if not, write to the Free Software
15
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 */
17
 
18
#include <errno.h>
19
 
20
        /* File opening: with security */
21
FILE *open4read(char *n)
22
{
23
    char *p, *p1, *p2, namebuf[2048];
24
    int t;
25
    FILE *f;
26
    n=find_word_start(n);
27
    if(*n==0) return NULL;
28
    p=getenv("flydraw_filebase");
29
    p1=n+strlen(n)-4;if(p1<n || strcasecmp(p1,".gif")!=0) t=1; else t=0;
30
    if(p!=NULL && *p!=0) {
31
        char pbuf[MAX_LINELEN+1];
32
        snprintf(pbuf,sizeof(pbuf),"%s",p);
33
        p=find_word_start(pbuf); if(strstr(p,"..")!=NULL) return NULL;
34
        if(*n=='/' || strstr(n,"..")!=NULL) return NULL;
35
                /* prohibit unusual file/dir names */
36
        for(p1=p;*p1;p1++)
37
          if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL)
38
            return NULL;
39
        for(p1=n;*p1;p1++)
40
          if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL)
41
            return NULL;
42
        f=NULL;
43
        for(p1=p; *p1; p1=find_word_start(p2)) {
44
            p2=find_word_end(p1);
45
            if(*p2) *p2++=0;
46
            snprintf(namebuf,sizeof(namebuf),"%s/%s",p1,n);
47
            f=fopen(namebuf,"r"); if(f!=NULL) goto imgtype;
48
        }
49
        p1=getenv("w_wims_session");
50
        if(p1!=NULL && strncmp(n,"insert",6)==0) {
51
            snprintf(namebuf,sizeof(namebuf),"../s2/%s/%s",p1,n);
52
            f=fopen(namebuf,"r");
53
        }
54
    }
55
    else {
56
        snprintf(namebuf,sizeof(namebuf),"%s",n);
57
        f=fopen(namebuf,"r");
58
    }
59
    imgtype:
60
    if(t && f!=NULL) {
61
        char tbuf[1024],sbuf[4096];
62
        fclose(f); f=NULL;
63
        p1=getenv("TMPDIR"); if(p1==NULL || *p1==0) p1=".";
64
        snprintf(tbuf,sizeof(tbuf),"%s/drawfile_.gif",p1);
65
        snprintf(sbuf,sizeof(sbuf),"convert %s %s",namebuf,tbuf);
66
        system(sbuf); f=fopen(tbuf,"r");       
67
    }
68
    return f;
69
}
70
 
71
        /* Does nothing; just a comment. */
72
void obj_comment(objparm *pm)
73
{
74
    return;
75
}
76
 
77
        /* define image size */
78
void obj_size(objparm *pm)
79
{
80
    sizex=rint(pm->pd[0]); sizey=rint(pm->pd[1]);
81
    if(sizex<0 || sizey<0 || sizex>MAX_SIZE || sizey>MAX_SIZE) {
82
        error("bad_size"); return;
83
    }
84
    if(image!=NULL) {
85
        error("size_already_defined"); return;
86
    }
87
    image=gdImageCreate(sizex,sizey);
88
    if(image==NULL) error("image_creation_failure");
89
    else {
90
        color_white=gdImageColorAllocate(image,255,255,255);
91
        color_black=gdImageColorAllocate(image,0,0,0);
92
        color_bounder=gdImageColorAllocate(image,1,2,3);
93
    }
94
}
95
 
96
        /* new image */
97
void obj_new(objparm *pm)
98
{
99
    if(image) {
100
        gdImageDestroy(image);image=NULL;
101
    }
102
    if(pm->pcnt>=2) obj_size(pm);
103
    else sizex=sizey=0;
104
    saved=0;
105
}
106
 
107
        /* new image */
108
void obj_existing(objparm *pm)
109
{
110
    FILE *inf;
111
    char *pp;
112
 
113
    if(image) {
114
        gdImageDestroy(image);image=NULL;
115
    }
116
    pp=find_word_start(pm->str);*find_word_end(pp)=0;
117
    inf=open4read(pp);
118
    if(inf==NULL) {
119
        error("file_not_exist"); return;
120
    }
121
    image=gdImageCreateFromGif(inf); fclose(inf);
122
    if(image==NULL) {
123
        error("bad_gif"); return;
124
    }
125
    sizex=image->sx; sizey=image->sy;
126
    saved=0;
127
}
128
 
129
        /* solid line */
130
void obj_line(objparm *pm)
131
{
132
    scale(pm->pd,pm->p,2);
133
    gdImageLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],pm->color[0]);
1024 bpr 134
    if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
10 reyssat 135
}
136
 
1024 bpr 137
void _obj_arrow(objparm *pm, int twoside)
10 reyssat 138
{
139
    int l,ii[6],xx,yy;
140
    double dx,dy,length,dd[6];
141
    scale(pm->pd,pm->p,2);
142
    xx=ii[0]=pm->p[2];yy=ii[1]=pm->p[3];
143
    l=pm->pd[4];if(l<0) l=0; if(l>200) l=200;
144
    scale2(pm->pd[0]-pm->pd[2],pm->pd[1]-pm->pd[3],&dx,&dy);
145
    length=sqrt(dx*dx+dy*dy);
146
    if(length<3 || l<5) goto stem;
147
    dd[0]=l*dx/length; dd[1]=l*dy/length;
148
    #define fat 0.27
149
    dd[2]=dd[0]+dd[1]*fat; dd[3]=dd[1]-dd[0]*fat;
150
    dd[4]=dd[0]-dd[1]*fat; dd[5]=dd[1]+dd[0]*fat;
151
    ii[2]=rint(dd[2])+ii[0]; ii[3]=rint(dd[3])+ii[1];
152
    ii[4]=rint(dd[4])+ii[0]; ii[5]=rint(dd[5])+ii[1];
153
    gdImageFilledPolygon(image,(gdPointPtr) ii,3,pm->color[0]);
154
    xx=rint(dd[0])+ii[0];yy=rint(dd[1])+ii[1];
1024 bpr 155
    if(twoside) {
156
        ii[0]=pm->p[0]; ii[1]=pm->p[1];
157
        ii[2]=-rint(dd[2])+ii[0]; ii[3]=-rint(dd[3])+ii[1];
158
        ii[4]=-rint(dd[4])+ii[0]; ii[5]=-rint(dd[5])+ii[1];
159
        gdImageFilledPolygon(image,(gdPointPtr) ii,3,pm->color[0]);
160
    }
10 reyssat 161
    stem: if(pm->fill)
162
      gdImageDashedLine(image,pm->p[0],pm->p[1],xx,yy,pm->color[0]);
163
    else
164
      gdImageLine(image,pm->p[0],pm->p[1],xx,yy,pm->color[0]);
1024 bpr 165
    if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
10 reyssat 166
}
167
 
1024 bpr 168
        /* Arrow */
169
void obj_arrow(objparm *pm)
170
{
171
    _obj_arrow(pm,0);
172
}
173
 
174
        /* 2-sided arrow */
175
void obj_arrow2(objparm *pm)
176
{
177
    _obj_arrow(pm,1);
178
}
179
 
10 reyssat 180
        /* horizontal line */
181
void obj_hline(objparm *pm)
182
{
183
    scale(pm->pd,pm->p,1);
184
    if(pm->fill)
185
      gdImageDashedLine(image,0,pm->p[1],sizex,pm->p[1],pm->color[0]);
186
    else
187
      gdImageLine(image,0,pm->p[1],sizex,pm->p[1],pm->color[0]);
188
}
189
 
190
        /* vertical line */
191
void obj_vline(objparm *pm)
192
{
193
    scale(pm->pd,pm->p,1);
194
    if(pm->fill)
195
      gdImageDashedLine(image,pm->p[0],0,pm->p[0],sizey,pm->color[0]);
196
    else
197
      gdImageLine(image,pm->p[0],0,pm->p[0],sizey,pm->color[0]);
198
}
199
 
200
        /* dashed line */
201
void obj_dline(objparm *pm)
202
{
203
    scale(pm->pd,pm->p,2);
204
    gdImageDashedLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],
205
                      pm->color[0]);
206
}
207
 
208
        /* parallel lines.
209
         * x1,y1,x2,y2,xv,yv,n,color */
210
void obj_parallel(objparm *pm)
211
{
212
    int i, n, xi,yi;
213
    double xv,yv;
214
    n=pm->pd[6]; if(n<0) return; if(n>256) n=256;
1122 bpr 215
    scale(pm->pd,pm->p,3);
10 reyssat 216
    scale2(pm->pd[4],pm->pd[5],&xv,&yv);
217
    for(i=0;i<n;i++) {
218
        xi=rint(i*xv); yi=rint(i*yv);
219
        gdImageLine(image,pm->p[0]+xi,pm->p[1]+yi,pm->p[2]+xi,pm->p[3]+yi,
220
                    pm->color[0]);
1139 bpr 221
        if(vimg_enable) vimg_line(scale_buf[0]+i*(scale_buf[4]-transx),
222
                                  scale_buf[1]+i*(scale_buf[5]-transy),
223
                                  scale_buf[2]+i*(scale_buf[4]-transx),
224
                                  scale_buf[3]+i*(scale_buf[5]-transy));
10 reyssat 225
    }
226
}
227
 
228
        /* rectangle */
229
void obj_rect(objparm *pm)
230
{
231
    int x1,y1,x2,y2;
232
    scale(pm->pd,pm->p,2);
233
    x1=min(pm->p[0],pm->p[2]); x2=max(pm->p[0],pm->p[2]);
234
    y1=min(pm->p[1],pm->p[3]); y2=max(pm->p[1],pm->p[3]);
235
    if(pm->fill)
236
      gdImageFilledRectangle(image,x1,y1,x2,y2,pm->color[0]);
237
    else
238
      gdImageRectangle(image,x1,y1,x2,y2,pm->color[0]);
1024 bpr 239
    if(vimg_enable) vimg_rect(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
10 reyssat 240
}
241
 
242
        /* square */
243
void obj_square(objparm *pm)
244
{
245
    int w,h;
246
    scale(pm->pd,pm->p,1);
247
    w=rint(pm->pd[2]); h=rint(pm->pd[2]);
248
    if(pm->fill)
249
      gdImageFilledRectangle(image,pm->p[0],pm->p[1],
250
                     pm->p[0]+w,pm->p[1]+h,pm->color[0]);
251
    else
252
      gdImageRectangle(image,pm->p[0],pm->p[1],
253
                     pm->p[0]+w,pm->p[1]+h,pm->color[0]);
1024 bpr 254
    if(vimg_enable) vimg_rect(scale_buf[0],scale_buf[1],
255
                              scale_buf[0]+pm->pd[2],scale_buf[1]+pm->pd[2]);
10 reyssat 256
}
257
 
258
        /* triangle */
259
void obj_triangle(objparm *pm)
260
{
261
    scale(pm->pd,pm->p,3);
262
    if(pm->fill)
263
      gdImageFilledPolygon(image,(gdPointPtr) pm->p,3,pm->color[0]);
264
    else
265
      gdImagePolygon(image,(gdPointPtr) pm->p,3,pm->color[0]);
1024 bpr 266
    if(vimg_enable) vimg_polyline(scale_buf,3,1);
10 reyssat 267
}
268
 
269
        /* polygon */
270
void obj_poly(objparm *pm)
271
{
272
    int cnt;
273
    cnt=(pm->pcnt)/2;
274
    scale(pm->pd,pm->p,cnt);
275
    if(pm->fill)
276
      gdImageFilledPolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
277
    else
278
      gdImagePolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
1024 bpr 279
    if(vimg_enable) vimg_polyline(scale_buf,cnt,1);
10 reyssat 280
}
281
 
282
        /* rays */
283
void obj_rays(objparm *pm)
284
{
285
    int i, n;
286
    n=(pm->pcnt)/2;
287
    scale(pm->pd,pm->p,n);
1024 bpr 288
    for(i=2;i<2*n;i+=2) {
289
        gdImageLine(image,pm->p[0],pm->p[1],pm->p[i],pm->p[i+1],pm->color[0]);
290
        if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],
291
                                  scale_buf[i],scale_buf[i+1]);
292
    }
10 reyssat 293
}
294
 
295
        /* segments */
296
void obj_lines(objparm *pm)
297
{
298
    int i, n;
299
    n=(pm->pcnt)/2;
300
    scale(pm->pd,pm->p,n);
1139 bpr 301
    for(i=2;i<2*n;i+=2)
10 reyssat 302
      gdImageLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
1024 bpr 303
    if(vimg_enable) vimg_polyline(scale_buf,n,0);
10 reyssat 304
}
305
 
306
        /* segments */
307
void obj_dlines(objparm *pm)
308
{
309
    int i, n;
310
    n=(pm->pcnt)/2;
311
    scale(pm->pd,pm->p,n);
312
    for(i=2;i<2*n;i+=2)
313
      gdImageDashedLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
1024 bpr 314
    if(vimg_enable) vimg_polyline(scale_buf,n,0);
10 reyssat 315
}
316
 
317
        /* points */
318
void obj_points(objparm *pm)
319
{
320
    int i, n;
321
    n=(pm->pcnt)/2;
322
    scale(pm->pd,pm->p,n);
323
    for(i=0;i<2*n;i+=2)
324
      gdImageSetPixel(image,pm->p[i],pm->p[i+1],pm->color[0]);
325
}
326
 
327
        /* lattice.
328
         * x0,y0,xv1,yv1,xv2,yv2,n1,n2,color */
329
void obj_lattice(objparm *pm)
330
{
331
    int n1,n2,i1,i2,xi1,yi1,xi2,yi2;
332
    double xv1,xv2,yv1,yv2;
333
    n1=pm->pd[6];n2=pm->pd[7]; if(n1<0 || n2<0) return;
334
    if(n1>256) n1=256; if(n2>256) n2=256;
335
    scale(pm->pd,pm->p,1);
336
    scale2(pm->pd[2],pm->pd[3],&xv1,&yv1);
337
    scale2(pm->pd[4],pm->pd[5],&xv2,&yv2);
338
    for(i1=0;i1<n1;i1++) {
339
        xi1=rint(i1*xv1)+pm->p[0]; yi1=rint(i1*yv1)+pm->p[1];
340
        for(i2=0;i2<n2;i2++) {
341
            xi2=i2*xv2+xi1;yi2=i2*yv2+yi1;
342
            gdImageSetPixel(image,xi2,yi2,pm->color[0]);
343
        }
344
    }
345
}
346
 
347
        /* arc */
348
void obj_arc(objparm *pm)
349
{
350
    scale(pm->pd,pm->p,1);
351
    pm->p[2]=rint(pm->pd[2]*xscale); pm->p[3]=rint(pm->pd[3]*yscale);
352
    gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],
353
               pm->pd[4],pm->pd[5],pm->color[0]);
1024 bpr 354
    if(vimg_enable) vimg_arc(scale_buf[0],scale_buf[1],
355
                             0.5*pm->pd[2],0.5*pm->pd[3],pm->pd[4],pm->pd[5]);
10 reyssat 356
}
357
 
358
        /* Ellipse: centre 0,1, width 2, hight 3, color 4,5,6 */
359
void obj_ellipse(objparm *pm)
360
{
361
    scale(pm->pd,pm->p,1);
362
    pm->p[2]=rint(pm->pd[2]*xscale); pm->p[3]=rint(pm->pd[3]*yscale);
363
    if(pm->fill) {
364
        gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
365
                   color_bounder);
366
        gdImageFillToBorder(image,pm->p[0],pm->p[1],
367
                            color_bounder,pm->color[0]);
368
    }
369
    gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,pm->color[0]);
1024 bpr 370
    if(vimg_enable) vimg_ellipse(scale_buf[0],scale_buf[1],0.5*pm->pd[2],0.5*pm->pd[3]);
10 reyssat 371
}
372
 
373
        /* Circle */
374
void obj_circle(objparm *pm)
375
{
376
    scale(pm->pd,pm->p,1);
377
    pm->p[2]=rint(pm->pd[2]); pm->p[3]=rint(pm->pd[2]);
378
    if(pm->fill) {
379
        gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
380
                   color_bounder);
381
        gdImageFillToBorder(image,pm->p[0],pm->p[1],
382
                            color_bounder,pm->color[0]);
383
    }
384
    gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,pm->color[0]);
385
}
386
 
387
        /* flood fill */
388
void obj_fill(objparm *pm)
389
{
390
    scale(pm->pd,pm->p,1);
391
    gdImageFill(image,pm->p[0],pm->p[1],pm->color[0]);
392
}
393
 
394
        /* flood fill to border*/
395
void obj_fillb(objparm *pm)
396
{
397
    scale(pm->pd,pm->p,1);
398
    gdImageFillToBorder(image,pm->p[0],pm->p[1],pm->color[0],pm->color[1]);
399
}
400
 
401
gdImagePtr himg;
402
 
403
int makehatchimage(int x, int y, int px, int py, int col)
404
{
405
    int c1,c2,r,g,b;
406
    gdImagePtr saveimg;
407
    himg=gdImageCreate(x,y);
408
    c1=gdImageGetPixel(image,px,py);
409
    r=gdImageRed(image,c1); g=gdImageGreen(image,c1); b=gdImageBlue(image,c1);
410
    if(r>=255) r--; else r++; if(g>=255) g--; else g++; if(b>=255) b--; else b++;
411
    c1=gdImageColorAllocate(himg,r,g,b);
412
    r=gdImageRed(image,col); g=gdImageGreen(image,col); b=gdImageBlue(image,col);
413
    c2=gdImageColorAllocate(himg,r,g,b);
414
    if(width>1) {
415
        savew=-1; saveimg=image;
416
        image=himg; c2=widthcolor(width,c2); image=saveimg;
417
        c2=gdBrushed; savew=-1;
418
    }
419
    return c2;
420
}
421
 
422
        /* flood fill with hatching */
423
void obj_hatchfill(objparm *pm)
424
{
425
    int nx,ny,ax,ay, dir, c;
426
    scale(pm->pd,pm->p,1);
427
    nx=pm->pd[2]; ny=pm->pd[3]; ax=abs(nx); ay=abs(ny);
428
    if(nx==0 && ny==0) {error("bad displacement vector"); return;}
429
    if((nx>0 && ny>0) || (nx<0 && ny<0)) dir=1; else dir=-1;
430
    if(ax==0) {ax=100; dir=2;}
431
    if(ay==0) {ay=100; dir=3;}
432
    c=makehatchimage(ax,ay,pm->p[0],pm->p[1],pm->color[0]);
433
    switch(dir) {
434
        case -1: {
435
            gdImageLine(himg,0,ay-1,ax-1,0,c);
436
            if(width>1) {
437
                gdImageLine(himg,-ax,ay-1,-1,0,c);
438
                gdImageLine(himg,ax,ay-1,2*ax-1,0,c);
439
                gdImageLine(himg,0,-1,ax-1,-ay,c);
440
                gdImageLine(himg,0,2*ay-1,ax-1,ay,c);
441
            }
442
            break;
443
        }
444
        case 1: {
445
            gdImageLine(himg,0,0,ax-1,ay-1,c);
446
            if(width>1) {
447
                gdImageLine(himg,-ax,0,-1,ay-1,c);
448
                gdImageLine(himg,ax,0,2*ax-1,ay-1,c);
449
                gdImageLine(himg,0,-ay,ax-1,-1,c);
450
                gdImageLine(himg,0,ay,ax-1,2*ay-1,c);
451
            }
452
            break;
453
        }
454
        case 2: gdImageLine(himg,0,ay/2,ax-1,ay/2,c); break;
455
        case 3: gdImageLine(himg,ax/2,0,ax/2,ay-1,c); break;
456
    }
457
    gdImageSetTile(image,himg);
458
    gdImageFill(image,pm->p[0],pm->p[1],gdTiled);
459
    gdImageDestroy(himg);
460
    if(tiled) gdImageSetTile(image,tileimg);
461
}
462
 
463
        /* flood fill with grid */
464
void obj_gridfill(objparm *pm)
465
{
466
    int nx,ny, c;
467
    scale(pm->pd,pm->p,1);
468
    nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
469
    if(nx==0 && ny==0) {error("bad grid size"); return;}
470
    c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
471
    gdImageLine(himg,0,ny/2,nx-1,ny/2,c); gdImageLine(himg,nx/2,0,nx/2,ny-1,c);
472
    gdImageSetTile(image,himg);
473
    gdImageFill(image,pm->p[0],pm->p[1],gdTiled);
474
    gdImageDestroy(himg);
475
    if(tiled) gdImageSetTile(image,tileimg);
476
}
477
 
478
        /* flood fill with double hatching */
479
void obj_diafill(objparm *pm)
480
{
481
    int nx,ny, c;
482
    scale(pm->pd,pm->p,1);
483
    nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
484
    if(nx==0 && ny==0) {error("bad grid size"); return;}
485
    c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
486
    gdImageLine(himg,0,0,nx-1,ny-1,c); gdImageLine(himg,0,ny-1,nx-1,0,c);
487
    gdImageSetTile(image,himg);
488
    gdImageFill(image,pm->p[0],pm->p[1],gdTiled);
489
    gdImageDestroy(himg);
490
    if(tiled) gdImageSetTile(image,tileimg);
491
}
492
 
493
        /* flood fill with double hatching */
494
void obj_dotfill(objparm *pm)
495
{
496
    int nx,ny, c;
497
    scale(pm->pd,pm->p,1);
498
    nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
499
    if(nx==0 && ny==0) {error("bad grid size"); return;}
500
    c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
501
    gdImageSetPixel(himg,nx/2,ny/2,c);
502
    gdImageSetTile(image,himg);
503
    gdImageFill(image,pm->p[0],pm->p[1],gdTiled);
504
    gdImageDestroy(himg);
505
    if(tiled) gdImageSetTile(image,tileimg);
506
}
507
 
508
struct {
509
    char *name;
510
    gdFontPtr *fpt;
511
} fonttab[]={
512
      {"tiny",  &gdFontTiny},
513
      {"small", &gdFontSmall},
514
      {"medium",&gdFontMediumBold},
515
      {"large", &gdFontLarge},
516
      {"giant", &gdFontGiant},
517
      {"huge",  &gdFontGiant}
518
};
519
 
520
#define fonttab_no (sizeof(fonttab)/sizeof(fonttab[0]))
521
 
522
        /* string */
523
void obj_string(objparm *pm)
524
{
525
    char *pp, *pe, *p2;
526
    int i;
527
    pp=pm->str; pe=strchr(pp,','); if(pe==NULL) {
528
        error("too_few_parms"); return;
529
    }
530
    *pe++=0; pp=find_word_start(pp); *find_word_end(pp)=0;
531
    pe=find_word_start(pe); strip_trailing_spaces(pe);
532
    if(*pp) {
533
        for(i=0;i<fonttab_no && strcmp(pp,fonttab[i].name)!=0; i++);
534
        if(i>=fonttab_no) i=1;
535
    }
536
    else i=1;
537
    scale(pm->pd,pm->p,1);
538
    if(*pe=='"') {
539
        p2=strchr(pe+1,'"');
540
        if(p2 && *(p2+1)==0) {*p2=0; pe++;}
541
    }
542
    if(pm->fill)
543
      gdImageStringUp(image,*(fonttab[i].fpt),pm->p[0],pm->p[1],pe,
544
                    pm->color[0]);
545
    else
546
      gdImageString(image,*(fonttab[i].fpt),pm->p[0],pm->p[1],pe,
547
                    pm->color[0]);
548
}
549
 
550
        /* point */
551
void obj_point(objparm *pm)
552
{
553
    scale(pm->pd,pm->p,1);
554
    gdImageSetPixel(image,pm->p[0],pm->p[1],pm->color[0]);
555
}
556
 
557
        /* copy an image file */
558
void obj_copy(objparm *pm)
559
{
560
    char *pp;
561
    FILE *inf;
562
    gdImagePtr  insimg;
563
 
564
    pp=find_word_start(pm->str);*find_word_end(pp)=0;
565
    inf=open4read(pp);
566
    if(inf==NULL) {
567
        error("file_not_exist"); return;
568
    }
569
    insimg=gdImageCreateFromGif(inf); fclose(inf);
570
    if(insimg==NULL) {
571
        error("bad_gif"); return;
572
    }
573
    scale(pm->pd,pm->p,1);
574
    if(pm->pd[2]<0 && pm->pd[3]<0 && pm->pd[4]<0 && pm->pd[5]<0)
575
      gdImageCopy(image,insimg,pm->p[0],pm->p[1],0,0,
576
                  insimg->sx,insimg->sy);
577
    else
578
      gdImageCopy(image,insimg,pm->p[0],pm->p[1],pm->pd[2],pm->pd[3],
579
                  pm->pd[4]-pm->pd[2],pm->pd[5]-pm->pd[3]);
580
    gdImageDestroy(insimg);
581
}
582
 
583
        /* copy an image file, with resizing */
584
void obj_copyresize(objparm *pm)
585
{
586
    char *pp;
587
    FILE *inf;
588
    gdImagePtr  insimg;
589
 
590
    pp=find_word_start(pm->str);*find_word_end(pp)=0;
591
    inf=open4read(pp);
592
    if(inf==NULL) {
593
        error("file_not_found"); return;
594
    }
595
    insimg=gdImageCreateFromGif(inf); fclose(inf);
596
    if(insimg==NULL) {
597
        error("bad_gif"); return;
598
    }
599
    scale(pm->pd+4,pm->p+4,2);
600
    if(pm->pd[0]<0 && pm->pd[1]<0 && pm->pd[2]<0 && pm->pd[3]<0)
601
      gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],0,0,
602
                         pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
603
                         insimg->sx,insimg->sy);
604
    else
605
      gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],pm->pd[0],pm->pd[1],
606
                         pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
607
                         pm->pd[2]-pm->pd[0]+1,pm->pd[3]-pm->pd[1]+1);
608
    gdImageDestroy(insimg);
609
}
610
 
611
        /* set brush or tile */
612
void obj_setbrush(objparm *pm)
613
{
614
    char *pp;
615
    FILE *inf;
616
    gdImagePtr insimg;
617
 
618
    pp=find_word_start(pm->str); *find_word_end(pp)=0;
619
    inf=open4read(pp); if(inf==NULL) {
620
        error("file_not_found"); return;
621
    }
622
    insimg=gdImageCreateFromGif(inf); fclose(inf);
623
    if(insimg==NULL) {
624
        error("bad_gif"); return;
625
    }
626
    if(pm->fill) {
627
        gdImageSetTile(image,insimg); tiled=1; tileimg=insimg;
628
    }
629
    else {
630
        gdImageSetBrush(image,insimg);brushed=1; brushimg=insimg;
631
    }
632
}
633
 
634
        /* kill brush */
635
void obj_killbrush(objparm *pm)
636
{
637
    if(brushimg) gdImageDestroy(brushimg);
638
    brushed=0; brushimg=NULL;
639
}
640
 
641
        /* kill tile */
642
void obj_killtile(objparm *pm)
643
{
644
    if(tileimg) gdImageDestroy(tileimg);
645
    tiled=0; tileimg=NULL;
646
}
647
 
648
        /* set style */
649
void obj_setstyle(objparm *pm)
650
{
651
    int i,t;
652
    t=pm->pcnt/3; if(t<=0) {
653
        error("too_few_parms"); return;
654
    }
655
    for(i=0;i<t;i++) {
656
        if(pm->pd[3*i]<0 || pm->pd[3*i+1]<0 || pm->pd[3*i+2]<0)
657
          pm->p[i]=gdTransparent;
658
        else
659
          pm->p[i]=getcolor(pm->pd[3*i],pm->pd[3*i+1],pm->pd[3*i+2]);
660
    }
661
    gdImageSetStyle(image,pm->p,t); styled=1;
662
}
663
 
664
        /* kill style */
665
void obj_killstyle(objparm *pm)
666
{
667
    styled=0;
668
}
669
 
670
        /* set transparent */
671
void obj_transp(objparm *pm)
672
{
673
    gdImageColorTransparent(image,pm->color[0]);
674
}
675
 
676
        /* set interlace */
677
void obj_interlace(objparm *pm)
678
{
679
    gdImageInterlace(image,1);
680
}
681
 
682
        /* set linewidth */
683
void obj_linewidth(objparm *pm)
684
{
685
    if(pm->pd[0]<1 || pm->pd[0]>255) error("bad_parms");
686
    else width=pm->pd[0];
687
}
688
 
689
        /* set x range */
690
void obj_xrange(objparm *pm)
691
{
692
    double dd;
693
    dd=pm->pd[1]-pm->pd[0];
694
    xstart=pm->pd[0]; xscale=sizex/dd;
695
}
696
 
697
        /* set y range */
698
void obj_yrange(objparm *pm)
699
{
700
    double dd;
701
    dd=pm->pd[1]-pm->pd[0];
702
    ystart=pm->pd[1]; yscale=-sizey/dd;
703
}
704
 
705
        /* set x et y range */
706
void obj_range(objparm *pm)
707
{
708
    double d1,d2;
709
    d1=pm->pd[1]-pm->pd[0]; d2=pm->pd[3]-pm->pd[2];
710
    xstart=pm->pd[0]; xscale=(sizex-1)/d1;
711
    ystart=pm->pd[3]; yscale=-(sizey-1)/d2;
712
}
713
 
714
        /* set t range */
715
void obj_trange(objparm *pm)
716
{
717
    double dd;
718
    dd=pm->pd[1]-pm->pd[0];
719
    tstart=pm->pd[0]; tend=pm->pd[1];
720
    tranged=1;
721
}
722
 
723
        /* set tstep (plotting step) */
724
void obj_tstep(objparm *pm)
725
{
726
    int dd;
727
    dd=pm->pd[0];
728
    if(dd<3) {
729
        error("bad_step"); return;
730
    }
731
    if(dd>2000) dd=2000;
732
    tstep=dd;
733
}
734
 
735
        /* set plotjump (plot jump break threashold) */
736
void obj_plotjump(objparm *pm)
737
{
738
    int dd;
739
    dd=pm->pd[0];
740
    if(dd<3) dd=3; if(dd>MAX_SIZE) dd=MAX_SIZE;
741
    plotjump=dd;
742
}
743
 
744
        /* plot a curve, either parametric or explicit */
745
void obj_plot(objparm *pm)
746
{
747
    int i,j,n,dist,xx,yy,varpos;
748
    char p1[MAX_LINELEN+1], p2[MAX_LINELEN+1];
749
    char *varn, *pp;
750
    double dc[2],t,v;
751
    int ic[2],oc[2];
752
 
753
    n=itemnum(pm->str);
754
    if(n<1) {
755
        error("bad_parms"); return;
756
    }
757
    fnd_item(pm->str,1,p1); fnd_item(pm->str,2,p2);
758
    if(n==1 && !tranged) v=sizex/xscale/tstep;
759
    else v=(tend-tstart)/tstep;
760
    if(n==1) varn="x"; else varn="t";
761
    for(pp=varchr(p1,varn); pp; pp=varchr(pp,varn)) {
762
        string_modify(p1,pp,pp+strlen(varn),EV_T);
763
        pp+=strlen(EV_T);
764
    }
765
    for(pp=varchr(p2,varn); pp; pp=varchr(pp,varn)) {
766
        string_modify(p2,pp,pp+strlen(varn),EV_T);
767
        pp+=strlen(EV_T);
768
    }
769
    varpos=eval_getpos(EV_T);
770
    if(varpos<0) return; /* unknown error */
771
    evalue_compile(p1); evalue_compile(p2);
1024 bpr 772
    if(vimg_enable) vimg_plotstart();
10 reyssat 773
    for(i=j=0;i<=tstep;i++) {
774
        if(n==1) {
775
            if(tranged) t=tstart+i*v; else t=xstart+i*v;
776
            eval_setval(varpos,t); dc[0]=t; dc[1]=evalue(p1);
777
        }
778
        else {
779
            t=tstart+i*v; eval_setval(varpos,t);
780
            dc[0]=evalue(p1); dc[1]=evalue(p2);
781
        }
782
        if(!finite(dc[0]) || !finite(dc[1])) ic[0]=ic[1]=-BOUND;
783
        else scale(dc,ic,1);
1024 bpr 784
        if(vimg_enable) vimg_plot1 (scale_buf[0],scale_buf[1]);
10 reyssat 785
        if(j==0) {
786
            gdImageSetPixel(image,ic[0],ic[1],pm->color[0]); j++;
787
        }
788
        else {
789
            xx=ic[0]-oc[0]; yy=ic[1]-oc[1];
790
            dist=sqrt(xx*xx+yy*yy);
791
            if(dist>0) {
792
                if(dist>plotjump || dist==1)
793
                  gdImageSetPixel(image,ic[0],ic[1],pm->color[0]);
794
                else
795
                  gdImageLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
796
 
797
            }
798
        }
799
        memmove(oc,ic,sizeof(oc));
800
    }
1024 bpr 801
    if(vimg_enable) vimg_plotend();
10 reyssat 802
}
803
 
804
        /* set levelcurve granularity */
805
void obj_levelstep(objparm *pm)
806
{
807
    int dd;
808
    dd=pm->pd[0];
809
    if(dd<1) return; if(dd>16) dd=16;
810
    lstep=dd;
811
}
812
 
813
        /* level curve */
814
void obj_levelcurve(objparm *pm)
815
{
816
    char fn[MAX_LINELEN+1],tc[MAX_LINELEN+1];
817
    int n,i;
818
    double d;
819
    leveldata *ld;
820
 
821
    ld=xmalloc(sizeof(leveldata)+16);
822
    ld->xname="x"; ld->yname="y";
823
    ld->xsize=sizex; ld->ysize=sizey; ld->datacnt=0;
824
    ld->xrange[0]=xstart; ld->xrange[1]=sizex/xscale+xstart;
825
    ld->yrange[0]=sizey/yscale+ystart; ld->yrange[1]=ystart;
826
    ld->grain=lstep;
827
    fnd_item(pm->str,1,fn); ld->fn=fn;
828
    n=itemnum(pm->str);
829
    if(n<=1) {ld->levelcnt=1; ld->levels[0]=0;}
830
    else {
831
        if(n>LEVEL_LIM+1) n=LEVEL_LIM+1;
832
        for(i=0;i<n-1;i++) {
833
            fnd_item(pm->str,i+2,tc);
834
            d=evalue(tc);
835
            if(finite(d)) ld->levels[i]=d; else ld->levels[i]=0;
836
        }
837
        ld->levelcnt=n-1;
838
    }
839
    levelcurve(ld);
840
    for(i=0;i<ld->datacnt;i++) {
841
        gdImageSetPixel(image,ld->xdata[i],ld->ydata[i],pm->color[0]);
842
    }
843
    free(ld);
844
}
845
 
846
        /* set animation step */
847
void obj_animstep(objparm *pm)
848
{
849
    animstep=pm->pd[0];
850
    setvar("animstep",pm->pd[0]);
851
}
852
 
853
        /* Starts a line counting */
854
void obj_linecount(objparm *pm)
855
{
856
    linecnt=pm->pd[0];
857
}
858
 
859
        /* marks end of execution */
860
void obj_end(objparm *pm)
861
{
862
    error("successful_end_of_execution");
863
}
864
 
865
        /* output */
866
void obj_output(objparm *pm)
867
{
868
    char *p, namebuf[1024];
869
    p=find_word_start(pm->str); *find_word_end(p)=0;
870
    snprintf(namebuf,sizeof(namebuf),"%s",imagefilename);
871
    snprintf(imagefilename,sizeof(imagefilename),"%s",p);
872
    output();
873
    snprintf(imagefilename,sizeof(imagefilename),"%s",namebuf);
874
}
875
 
1024 bpr 876
        /* vimgfile */
877
void obj_vimgfile(objparm *pm)
878
{
879
    char *p;
880
    p=find_word_start(pm->str); *find_word_end(p)=0;
881
    snprintf(vimgfilename,sizeof(vimgfilename),"%s",p);
882
    if(vimg_ready) vimg_close();
883
}
884
 
885
        /* vimg enable/disable */
886
void obj_vimg(objparm *pm)
887
{
888
    vimg_enable=pm->pd[0];
889
    if(vimg_enable>0 && vimg_ready==0) {
890
        vimg_init();
891
    }
892
}
893
 
10 reyssat 894
        /* Set affine transformation */
895
void obj_affine(objparm *pm)
896
{
897
    int i;
898
    for(i=0;i<4;i++) matrix[i]=pm->pd[i];
899
    transx=pm->pd[4]; transy=pm->pd[5];
900
    transform=1;
901
}
902
 
903
        /* Set affine transformation */
904
void obj_rotation(objparm *pm)
905
{
906
    double r;
907
    r=M_PI*pm->pd[0]/180;
908
    matrix[0]=matrix[3]=cos(r);
909
    matrix[1]=-sin(r); matrix[2]=sin(r);
910
    transform=1;
911
}
912
 
913
        /* Set affine transformation */
914
void obj_linear(objparm *pm)
915
{
916
    int i;
917
    for(i=0;i<4;i++) matrix[i]=pm->pd[i];
918
    transform=1;
919
}
920
 
921
        /* Set affine transformation */
922
void obj_translation(objparm *pm)
923
{
924
    transx=pm->pd[0]; transy=pm->pd[1];
925
}
926
 
927
        /* Set affine transformation */
928
void obj_killaffine(objparm *pm)
929
{
930
    matrix[0]=matrix[3]=1;
931
    matrix[1]=matrix[2]=transx=transy=0;
932
    transform=0;
933
}
934
 
935
        /* Set affine transformation */
936
void obj_killlinear(objparm *pm)
937
{
938
    matrix[0]=matrix[3]=1;
939
    matrix[1]=matrix[2]=0;
940
    transform=0;
941
}
942
 
943
        /* Set affine transformation */
944
void obj_killtranslation(objparm *pm)
945
{
946
    transx=transy=0;
947
}
948
 
949
/***** Les modifs de Jean-Christophe Leger Fev 2006 *****/
950
 
951
/* arguments: un numero de matrice entre 1 et JC_NB_MATRICES, une liste de 4 nombres reels pour la matrice */
952
void obj_setmatrix(objparm *pm)
953
{
954
  int nummatrix = (int) (pm->pd[0]);
955
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
956
    error("bad_matrix_number");
957
    return;
958
  }
959
  matrices_pavage[nummatrix][0] = pm->pd[1];
960
  matrices_pavage[nummatrix][1] = pm->pd[2];
961
  matrices_pavage[nummatrix][2] = pm->pd[3];
962
  matrices_pavage[nummatrix][3] = pm->pd[4];
963
}
964
 
965
/* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
966
void obj_resetmatrix(objparm *pm)
967
{
968
  int nummatrix = (int) (pm->pd[0]);
969
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
970
    error("bad_matrix_number");
971
    return;
972
  }
973
  matrices_pavage[nummatrix][0] = 1;
974
  matrices_pavage[nummatrix][1] = 0;
975
  matrices_pavage[nummatrix][2] = 0;
976
  matrices_pavage[nummatrix][3] = 1;
977
 
978
}
979
/* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 2 nombres reels pour le vecteur */
980
void obj_setvector(objparm *pm)
981
{
982
  int nummatrix = (int) (pm->pd[0]);
983
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
984
    error("bad_vector_number");
985
    return;
986
  }
987
  vecteurs_pavage[nummatrix][0] = pm->pd[1];
988
  vecteurs_pavage[nummatrix][1] = pm->pd[2];
989
}
990
 
991
/* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
992
void obj_resetvector(objparm *pm)
993
{
994
  int nummatrix = (int) (pm->pd[0]);
995
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
996
    error("bad_vector_number");
997
    return;
998
  }
999
  vecteurs_pavage[nummatrix][0] = 0;
1000
  vecteurs_pavage[nummatrix][1] = 0;
1001
}
1002
 
1003
/* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 6 nombres reels pour la matrice et le vecteur */
1004
void obj_settransform(objparm *pm)
1005
{
1006
  int nummatrix = (int) (pm->pd[0]);
1007
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
1008
    error("bad_vector_number");
1009
    return;
1010
  }
1011
  matrices_pavage[nummatrix][0] = pm->pd[1];
1012
  matrices_pavage[nummatrix][1] = pm->pd[2];
1013
  matrices_pavage[nummatrix][2] = pm->pd[3];
1014
  matrices_pavage[nummatrix][3] = pm->pd[4];
1015
  vecteurs_pavage[nummatrix][0] = pm->pd[5];
1016
  vecteurs_pavage[nummatrix][1] = pm->pd[6];
1017
}
1018
 
1019
 
1020
/* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
1021
void obj_resettransform(objparm *pm)
1022
{
1023
  int nummatrix = (int) (pm->pd[0]);
1024
  if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
1025
    error("bad_vector_number");
1026
    return;
1027
  }
1028
  vecteurs_pavage[nummatrix][0] = 0;
1029
  vecteurs_pavage[nummatrix][1] = 0;
1030
  matrices_pavage[nummatrix][0] = 1;
1031
  matrices_pavage[nummatrix][1] = 0;
1032
  matrices_pavage[nummatrix][2] = 0;
1033
  matrices_pavage[nummatrix][3] = 1;
1034
}
1035
 
1036
/* arguments: une liste de 6 nombres reels pour les coordonnees de l'origine et des vecteurs de base */
1037
void obj_setparallelogram(objparm *pm)
1038
{
1039
  int i;
1040
  for(i=0;i<6;i++)  parallogram_fonda[i]=pm->pd[i];
1041
}
1042
 
1043
/* arguments :aucun */
1044
void obj_resetparallelogram(objparm *pm)
1045
{
1046
  parallogram_fonda[0]=0;
1047
  parallogram_fonda[1]=0;
1048
  parallogram_fonda[2]=100;
1049
  parallogram_fonda[3]=0;
1050
  parallogram_fonda[4]=0;
1051
  parallogram_fonda[5]=100;
1052
}
1053
 
1054
/* argument : la liste des numeros des matrices a faire agir, le nom du fichier de l'image a inclure */
1055
void obj_multicopy(objparm *pm)
1056
{
1057
    char *pp,*pe,*cptr;
1058
    FILE *inf;
1059
    gdImagePtr  insimg;
1060
    int i,j;
1061
    int imax,jmax;
1062
    int c; /* la couleur reperee */
1063
    int mat;
1064
    int nummatrices[JC_NB_MATRICES];
1065
    double pt[2]; /* les coordonnees math. du point repere dans le parallelogramme */
1066
    double ptr[2]; /* les coordonnees math. du point transforme */
1067
    int pti[2]; /* les coordonnees img du point a placer sur le canevas */
1068
    int t;
1069
    /* gestion palette de couleur de l'image a inserer */
1070
    int colorMap[gdMaxColors];
1071
    int comptmat=0; /* combien de matrices en tout ? */
1072
 
1073
    for (i=0; (i<gdMaxColors); i++) {
1074
      colorMap[i] = (-1);
1075
    }
1076
 
1077
    /* Gestion des parametres la liste est censee finir avec le nom du fichier */
1078
    t=pm->pcnt-1; /* il faut enlever le nom de fichier ! c'est le nombre de parametres en plus */
1079
    pp=find_word_start(pm->str);
1080
    /* visiblement find_word_start n'arrive pas a trouver le dernier mot dans un contexte ou le nombre de parameters est variable
1081
     * on cherche donc l'emplacement de la derniere virgule:
1082
     * il y en a une car t>0, on retrouve ensuite le debut de mot
1083
     */
1084
    for(pe=pp;*pe!=0;pe++);/* trouve la fin de la chaine */
1085
    if(t>0){
1086
      for(cptr = pe; cptr > pp && *cptr != ','; cptr--);
1087
      pp=find_word_start(cptr+1);
1088
    }
1089
    pe=find_word_end(pp);*pe=0; /* strip junk final */
1090
    //      printf("pp contient %s\n",pp);
1091
 
1092
 
1093
    inf=open4read(pp);
1094
    if(inf==NULL) {
1095
        error("file_not_exist"); return;
1096
    }
1097
    insimg=gdImageCreateFromGif(inf); fclose(inf);
1098
    if(insimg==NULL) {
1099
        error("bad_gif"); return;
1100
    }
1101
 
1102
    /* On recupere les numeros des matrices/vecteurs a faire agir,
1103
     * s'il n'y en a pas, on les fait toutes agir
1104
     */
1105
    for(i=0;i<t && i< JC_NB_MATRICES;i++) {
1106
      if(pm->pd[i]>=1 && pm->pd[i]<=JC_NB_MATRICES){
1107
          nummatrices[comptmat] = pm->pd[i];
1108
          comptmat++;
1109
      }
1110
    }
1111
    if(t<=0){
1112
      for(i=0;i<JC_NB_MATRICES;i++) {
1113
        nummatrices[i] = i+1;
1114
      }
1115
      comptmat=JC_NB_MATRICES;
1116
    }
1117
 
1118
 
1119
    imax = gdImageSX(insimg);
1120
    jmax = gdImageSY(insimg);
1121
 
1122
    for(i=0;i<imax;i++){
1123
      for(j=0;j<jmax;j++){
1124
        int nc;
1125
        c=gdImageGetPixel(insimg,i,j);
1126
        /* Le code suivant est une copie du code dans gdImageCopy  Added 7/24/95: support transparent copies */
1127
        if (gdImageGetTransparent(insimg) != c) {
1128
          /* Have we established a mapping for this color? */
1129
          if (colorMap[c] == (-1)) {
1130
            /* If it's the same image, mapping is trivial, dans notre cas ca n'arrive jamais... */
1131
            if (image == insimg) {
1132
              nc = c;
1133
            } else {
1134
              /* First look for an exact match */
1135
              nc = gdImageColorExact(image,
1136
                                     insimg->red[c], insimg->green[c],
1137
                                     insimg->blue[c]);
1138
            }  
1139
            if (nc == (-1)) {
1140
              /* No, so try to allocate it */
1141
              nc = gdImageColorAllocate(image,
1142
                                        insimg->red[c], insimg->green[c],
1143
                                        insimg->blue[c]);
1144
              /* If we're out of colors, go for the
1145
                 closest color */
1146
              if (nc == (-1)) {
1147
                nc = gdImageColorClosest(image,
1148
                                         insimg->red[c], insimg->green[c],
1149
                                         insimg->blue[c]);
1150
              }
1151
            }
1152
            colorMap[c] = nc;
1153
          }
1154
 
1155
          pt[0]= i*(parallogram_fonda[2])/(imax-1)+(jmax-j)*(parallogram_fonda[4])/(jmax-1)+parallogram_fonda[0];
1156
          pt[1]= i*(parallogram_fonda[3])/(imax-1)+(jmax-j)*(parallogram_fonda[5])/(jmax-1)+parallogram_fonda[1];
1157
          for(mat=0;mat<comptmat;mat++){
1158
            double *matricecourante = matrices_pavage[nummatrices[mat]];
1159
            double *vecteurcourant = vecteurs_pavage[nummatrices[mat]];
1160
            ptr[0] = matricecourante[0]*pt[0]+matricecourante[1]*pt[1]+vecteurcourant[0];
1161
            ptr[1] = matricecourante[2]*pt[0]+matricecourante[3]*pt[1]+vecteurcourant[1];
1162
            scale(ptr,pti,1);
1163
            gdImageSetPixel(image,pti[0],pti[1],colorMap[c]);
1164
          }
1165
        }
1166
      }
1167
    }
1168
    gdImageDestroy(insimg);
1169
}
1170
 
1171
/**** Fin modifs JC Fev 06 ******/
1172
 
1173
        /* get color from value */
1174
int calc_color(char *p, struct objtab *o)
1175
{
1176
    int k, cc[3];
1177
    char buf[MAX_LINELEN+1];
1178
    for(k=0;k<3;k++) {
1179
        fnd_item(p,k+1,buf);
1180
        cc[k]=evalue(buf);
1181
    }
1182
    collapse_item(p,3);
1183
    if(cc[0]==-1 && cc[1]==-1 && cc[2]==-255) {
1184
 
1185
        if(brushed && o->subst&1) return gdBrushed;
1186
        else return color_black;
1187
    }
1188
    if(cc[0]==-1 && cc[1]==-255 && cc[2]==-1) {
1189
        if(styled && o->subst&2)  return gdStyled;
1190
        else return color_black;
1191
    }
1192
    if(cc[0]==-255 && cc[1]==-1 && cc[2]==-1) {
1193
        if(tiled && o->fill_tag==1)  return gdTiled;
1194
        else return color_black;
1195
    }
1196
    return getcolor(cc[0],cc[1],cc[2]);
1197
}
1198
 
1199
        /* Routine to parse parameters */
1200
int parse_parms(char *p,objparm *pm,struct objtab *o)
1201
{
1202
    char buf[MAX_LINELEN+1];
1203
    char *p1, *p2;
1204
    int j,t,c,c1,c2;
1205
 
1206
    c=o->color_pos;c1=c2=0;
1207
    pm->color[0]=pm->color[1]=0;
1208
    if(c>0) c1=c; if(c<0) c2=-c; c=c1+c2;
1209
    t=itemnum(p);if(t<o->required_parms+3*c) return -1;
1210
    if(c1>0 && t>o->required_parms+3*c) t=o->required_parms+3*c;
1211
    pm->pcnt=t-3*c;
1212
    if(pm->pcnt>MAX_PARMS) pm->pcnt=MAX_PARMS;
1213
    if(c2>0) {
1214
        for(j=0;j<2 && j<c2; j++) pm->color[j]=calc_color(p,o);
1215
    }
1216
    snprintf(buf,sizeof(buf),"%s",p);
1217
    for(j=0, p1=buf; j<pm->pcnt; j++, p1=p2) {
1218
        p2=find_item_end(p1); if(*p2) *p2++=0;
1219
        p1=find_word_start(p1);
1220
        if(*p1) pm->pd[j]=evalue(p1); else pm->pd[j]=0;
1221
        if(!finite(pm->pd[j])) {
1222
            if(j<o->required_parms) return -1;
1223
            else {pm->pd[j]=0;break;}
1224
        }
1225
    }
1226
    collapse_item(p,o->required_parms);
1227
    if(c1>0) {
1228
        for(j=0;j<c1 && j<2; j++) pm->color[j]=calc_color(p,o);
1229
    }
1230
    if(width>1 && o->subst&1 && pm->color[0]!=gdBrushed
1231
       && pm->color[0]!=gdStyled && pm->color[0]!=gdTiled) {
1232
        pm->color[1]=pm->color[0];
1233
        pm->color[0]=widthcolor(width,pm->color[0]);
1234
    }
1235
    pm->fill=o->fill_tag;
3718 reyssat 1236
    ovlstrcpy(pm->str,p); return 0;
10 reyssat 1237
}
1238
 
1239
        /* Execute a command. Returns 0 if OK. */
1240
int obj_main(char *p)
1241
{
1242
    int i;
1243
    char *pp,*name,*pt;
1244
    char tbuf2[MAX_LINELEN+1];
1245
    struct objparm pm;
1246
 
1247
    p=find_word_start(p);
1248
    if(*p==exec_prefix_char || *p==0) return 0; /* comment */
1249
    name=p;
1250
    pp=find_name_end(p);
1251
    pt=find_word_start(pp); if(*pt=='=') *pt=' ';
1252
    if(*pt==':' && *(pt+1)=='=') *pt=*(pt+1)=' ';
1253
    pp=find_word_end(p);
1254
    if(*pp!=0) {
1255
        *(pp++)=0; pp=find_word_start(pp);
1256
    }
1257
    if(strlen(p)==1 || (strlen(p)==2 && isdigit(*(p+1)))) {
1258
        setvar(p,evalue(pp)); return 0;
1259
    }
1260
    i=search_list(objtab,obj_no,sizeof(objtab[0]),name);
1261
    if(i<0) {
1262
        error("bad_cmd"); return 1;
1263
    }
1264
    if(image==NULL && (objtab[i].color_pos || objtab[i].required_parms>2)) {
1265
        error("image_not_defined"); return 1;
1266
    }
3718 reyssat 1267
    ovlstrcpy(tbuf2,pp);
10 reyssat 1268
    if(objtab[i].color_pos || objtab[i].routine==obj_setstyle) {
1269
        substit(tbuf2);
1270
    }
1271
    if(parse_parms(tbuf2,&pm,objtab+i)!=0) error("bad_parms");
1272
    else objtab[i].routine(&pm);
1273
    return 0;
1274
}
1275