Subversion Repositories wimsdev

Rev

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