Subversion Repositories wimsdev

Rev

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