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