Subversion Repositories wimsdev

Rev

Rev 17587 | Rev 17608 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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