Subversion Repositories wimsdev

Rev

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