Subversion Repositories wimsdev

Rev

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