Subversion Repositories wimsdev

Rev

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