Subversion Repositories wimsdev

Rev

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