Subversion Repositories wimsdev

Rev

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