more bugfixes in stroke code
[swftools.git] / lib / gfxpoly / test.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <memory.h>
4 #include <math.h>
5 #include "../gfxtools.h"
6 #include "poly.h"
7 #include "convert.h"
8 #include "renderpoly.h"
9 #include "stroke.h"
10
11 gfxline_t*mkstar(int x1, int y1, int x2, int y2)
12 {
13     gfxline_t*l=0,*line = 0;
14     int x;
15     for(x=x1;x<=x2;x+=50) {
16         l = rfx_calloc(sizeof(gfxline_t));
17         l->type = gfx_moveTo;
18         l->x = x;l->y = y1;
19         line = gfxline_append(line, l);
20
21         l = rfx_calloc(sizeof(gfxline_t));
22         l->type = gfx_lineTo;
23         l->x = x2-x;l->y = y2;
24         line = gfxline_append(line, l);
25     }
26     return line;
27 }
28
29 gfxline_t* mkrandomshape(int range, int n)
30 {
31     int i;
32     gfxline_t* line = malloc(sizeof(gfxline_t)*n);
33     for(i=0;i<n;i++) {
34         line[i].type = i?gfx_lineTo:gfx_moveTo;
35         line[i].x = lrand48()%range - range/2;
36         line[i].y = lrand48()%range - range/2;
37         line[i].next = &line[i+1];
38     }
39     line[n-1].x = line[0].x;
40     line[n-1].y = line[0].y;
41     line[n-1].next = 0;
42     return line;
43 }
44
45 gfxline_t* mkchessboard()
46 {
47     gfxline_t*b = 0;
48     int x,y;
49     unsigned int r = 0;
50     int spacing = 20;
51
52     int num_caros = 40;
53     int l = 5;
54     char do_centerpiece=1;
55
56     //int num_caros = 4;
57     //int l=1;
58     //char do_centerpiece=0;
59
60     for(x=-l;x<=l;x++) 
61     for(y=-l;y<=l;y++) {
62         /* pseudo random */ 
63         r = crc32_add_byte(r, x);r = crc32_add_byte(r, y);
64         if(r&1) {
65             gfxline_t*box;
66             if(r&2) {
67                 box = gfxline_makerectangle(x*spacing,y*spacing,(x+1)*spacing,(y+1)*spacing);
68             } else {
69                 box = gfxline_makerectangle((x+1)*spacing,y*spacing,x*spacing,(y+1)*spacing);
70             }
71             b = gfxline_append(b, box);
72         }
73     }
74
75     int t;
76     for(t=0;t<num_caros;t++) {
77         r = crc32_add_byte(r, t);
78         int x=(r%10-5)*spacing;
79         int y=((r>>4)%10-5)*spacing;
80         int sizex = ((r>>8)%4)*spacing;
81         int sizey = sizex;
82         if(r&65536)
83             sizex = -sizex;
84         gfxline_t*l = malloc(sizeof(gfxline_t)*5);
85         l[0].type = gfx_moveTo;l[0].next = &l[1];
86         l[1].type = gfx_lineTo;l[1].next = &l[2];
87         l[2].type = gfx_lineTo;l[2].next = &l[3];
88         l[3].type = gfx_lineTo;l[3].next = &l[4];
89         l[4].type = gfx_lineTo;l[4].next = 0;
90         l[0].x = x;
91         l[0].y = y-sizey;
92         l[1].x = x+sizex;
93         l[1].y = y;
94         l[2].x = x;
95         l[2].y = y+sizey;
96         l[3].x = x-sizex;
97         l[3].y = y;
98         l[4].x = x;
99         l[4].y = y-sizey;
100         gfxline_append(b, l);
101     }
102     if(do_centerpiece) {
103         for(t=0;t<5;t++) {
104             gfxline_t*l = gfxline_makerectangle(-9*spacing,-10,9*spacing,10);
105             gfxmatrix_t matrix;
106             memset(&matrix, 0, sizeof(gfxmatrix_t));
107             double ua=t*0.43;
108             matrix.m00=cos(ua);matrix.m10=sin(ua);
109             matrix.m01=-sin(ua);matrix.m11=cos(ua);
110             gfxline_transform(l, &matrix);
111             gfxline_append(b, l);
112         }
113         gfxline_append(b, gfxline_makecircle(100,100,100,100));
114     }
115     return b;
116 }
117
118 gfxline_t* make_circles(int n)
119 {
120     gfxline_t*b = 0;
121     unsigned int c = 0;
122     int t;
123     for(t=0;t<n;t++) {
124         c = crc32_add_byte(c, t);
125         int x = c%200;
126         c = crc32_add_byte(c, t);
127         int y = c%200;;
128         c = crc32_add_byte(c, t^0x55);
129         int r = c%100;
130         gfxline_t*c = gfxline_makecircle(x,y,r,r);
131         b = gfxline_append(b, c);
132         //b = gfxline_append(b, gfxline_makerectangle(10,10,100,100));
133     }
134     return b;
135 }
136
137 static windcontext_t onepolygon = {1};
138 static windcontext_t twopolygons = {2};
139
140 int test_speed()
141 {
142     //gfxline_t* b = mkchessboard();
143     //gfxline_t* b = mkrandomshape(100,7);
144     gfxline_t* b = make_circles(30);
145
146     gfxmatrix_t m;
147     memset(&m, 0, sizeof(gfxmatrix_t));
148     int t;
149     for(t=0;t<360;t++) {
150         printf("%d\n", t);
151         m.m00 = cos(t*M_PI/180.0);
152         m.m01 = sin(t*M_PI/180.0);
153         m.m10 = -sin(t*M_PI/180.0);
154         m.m11 = cos(t*M_PI/180.0);
155         m.tx = 400*1.41/2;
156         m.ty = 400*1.41/2;
157         gfxline_t*l = gfxline_clone(b);
158         gfxline_transform(l, &m);
159         gfxpoly_t*poly = gfxpoly_from_fill(b, 0.05);
160
161         gfxpoly_t*poly2 = gfxpoly_process(poly, 0, &windrule_evenodd, &onepolygon);
162         gfxpoly_destroy(poly);
163         gfxpoly_destroy(poly2);
164         gfxline_free(l);
165     }
166     gfxline_free(b);
167 }
168
169 int test0(int argn, char*argv[])
170 {
171     gfxline_t*box1 = gfxline_makerectangle(-100,-100,100,100);
172     gfxline_t*box2 = gfxline_makerectangle(-100,-100,100,100);
173     gfxline_t*box3 = gfxline_makerectangle(-100,-100,100,100);
174     //gfxline_append(box2, box3);
175
176     gfxpoly_check(gfxpoly_from_stroke(box1, 2.0, gfx_capRound, gfx_joinRound, 0, 0.05));
177
178     gfxmatrix_t matrix;
179     memset(&matrix, 0, sizeof(gfxmatrix_t));
180     double ua=M_PI/4;
181     matrix.m00=cos(ua);matrix.m10=sin(ua);
182     matrix.m01=-sin(ua);matrix.m11=cos(ua);
183     //gfxline_transform(box1, &matrix);
184     
185     //gfxline_t*b = 0;
186     //b = gfxline_append(b, box1);
187     //b = gfxline_append(b, box2);
188     //gfxline_dump(b, stderr, "");
189
190     gfxpoly_t*poly1 = gfxpoly_from_fill(box1, 0.05);
191     gfxpoly_t*poly2 = gfxpoly_from_fill(box2, 0.05);
192     
193     gfxline_free(box1);
194     gfxline_free(box2);
195     gfxpoly_t*poly3 = gfxpoly_process(poly1, poly2, &windrule_intersect, &twopolygons);
196     gfxpoly_dump(poly3);
197     gfxline_t*line = gfxline_from_gfxpoly(poly3);
198     gfxline_dump(line, stdout, "");
199     gfxline_free(line);
200     gfxpoly_destroy(poly1);
201     gfxpoly_destroy(poly2);
202     gfxpoly_destroy(poly3);
203 }
204
205
206 int test1(int argn, char*argv[])
207 {
208     gfxline_t*box1 = gfxline_makerectangle(50,50,150,150);
209     gfxline_t*box2 = gfxline_makerectangle(100,100,200,200);
210     gfxline_t*box3 = gfxline_makerectangle(100,100,200,200);
211     gfxline_t*star = mkstar(50,50, 150,150);
212     gfxline_t*b = 0;
213     b = gfxline_append(b, box1);
214     b = gfxline_append(b, box2);
215     b = gfxline_append(b, box3);
216
217     gfxmatrix_t matrix;
218     memset(&matrix, 0, sizeof(gfxmatrix_t));
219     double ua=0.1;
220     matrix.m00=cos(ua);matrix.m10=sin(ua);
221     matrix.m01=-sin(ua);matrix.m11=cos(ua);
222
223     //gfxline_transform(b, &matrix);
224
225     gfxline_dump(b, stderr, "");
226     gfxpoly_t*poly = gfxpoly_from_fill(b, 0.05);
227     
228     gfxline_free(box1);
229     gfxline_free(box2);
230     gfxline_free(box3);
231     gfxline_free(star);
232
233     gfxpoly_dump(poly);
234     gfxpoly_t*poly2 = gfxpoly_process(poly, 0, &windrule_evenodd, &onepolygon);
235     gfxpoly_destroy(poly);
236     gfxpoly_destroy(poly2);
237 }
238
239 int test_square(int width, int height, int num, double gridsize, char bitmaptest)
240 {
241     int t;
242     gfxline_t* line = malloc(sizeof(gfxline_t)*num);
243     for(t=0;t<num;t++) {
244         line[t].type = t?gfx_lineTo:gfx_moveTo;
245         line[t].x = (lrand48()%width);
246         line[t].y = (lrand48()%height);
247         line[t].next = &line[t+1];
248     }
249     line[num-1].x = line[0].x;
250     line[num-1].y = line[0].y;
251     line[num-1].next = 0;
252
253     gfxpoly_t*poly1 = gfxpoly_from_fill(line, gridsize);
254     gfxline_free(line);
255
256     windrule_t*rule = &windrule_circular;
257     gfxpoly_t*poly2 = gfxpoly_process(poly1, 0, rule, &onepolygon);
258     if(bitmaptest) {
259         intbbox_t bbox = intbbox_new(0, 0, width, height);
260         unsigned char*bitmap1 = render_polygon(poly1, &bbox, 1.0, rule, &onepolygon);
261         assert(bitmap_ok(&bbox, bitmap1));
262         unsigned char*bitmap2 = render_polygon(poly2, &bbox, 1.0, &windrule_evenodd, &onepolygon);
263         assert(bitmap_ok(&bbox, bitmap2));
264         if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
265             save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
266             assert(!"bitmaps don't match");
267         }
268     }
269     gfxpoly_destroy(poly1);
270     gfxpoly_destroy(poly2);
271 }
272
273 int test2(int argn, char*argv[])
274 {
275     test_square(400,400, 3, 0.05, 1);
276
277     int t;
278     for(t=0;t<400;t++) {
279         fprintf(stderr, "%d\n", t);
280         test_square(400,400, 50, 0.05, 1);
281         test_square(200,3, 1000, 1.0, 0);
282         test_square(3,200, 1000, 1.0, 0);
283         test_square(10,10, 200, 1.0, 0);
284     }
285 }
286
287 #include "../rfxswf.h"
288 void test3(int argn, char*argv[])
289 {
290 #undef N
291 #undef RANGE
292 #define N 100
293 #define RANGE 400
294
295     //gfxline_t*line = mkrandomshape(RANGE, N);
296     //windrule_t*rule = &windrule_circular;
297     //gfxline_t*line = mkchessboard();
298     gfxline_t*line = make_circles(30);
299     windrule_t*rule = &windrule_evenodd;
300     //windrule_t*rule = &windrule_circular;
301
302     gfxmatrix_t m;
303     memset(&m, 0, sizeof(m));
304
305     SWF swf;
306     memset(&swf, 0, sizeof(SWF));
307     swf.movieSize.xmax = RANGE*20*1.41;
308     swf.movieSize.ymax = RANGE*20*1.41;
309     swf.fileVersion = 9;
310     swf.frameRate = 25*0x100;
311     TAG * tag = swf.firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
312     swf_SetU8(tag, 0);
313     swf_SetU8(tag, 0);
314     swf_SetU8(tag, 0);
315
316     int t;
317     for(t=0;t<360;t++) {
318         fprintf(stderr, "%d\n", t);
319         m.m00 = cos(t*M_PI/180.0);
320         m.m01 = sin(t*M_PI/180.0);
321         m.m10 = -sin(t*M_PI/180.0);
322         m.m11 = cos(t*M_PI/180.0);
323         m.tx = RANGE*1.41/2;
324         m.ty = RANGE*1.41/2;
325
326         gfxline_t*l = gfxline_clone(line);
327         gfxline_transform(l, &m);
328
329         gfxpoly_t*poly1 = gfxpoly_from_fill(l, 0.05);
330
331         gfxpoly_t*poly2 = gfxpoly_process(poly1, 0, rule, &onepolygon);
332
333         tag = swf_InsertTag(tag, ST_DEFINESHAPE);
334         SHAPE* s;
335         swf_ShapeNew(&s);
336         RGBA rgb;
337         rgb.r = rgb.g = 0x00; rgb.b = 0xff;
338         rgb.a = 255;
339         int fs = swf_ShapeAddSolidFillStyle(s,&rgb);
340         int ls = swf_ShapeAddLineStyle(s,20,&rgb);
341         swf_SetU16(tag,t+1);
342         swf_SetRect(tag,&swf.movieSize);
343         swf_SetShapeHeader(tag,s);
344
345 #define FILL
346 #ifdef FILL
347         swf_ShapeSetAll(tag,s,UNDEFINED_COORD,UNDEFINED_COORD,0,fs,0);
348
349         int i,j;
350         gfxpolystroke_t*stroke = poly2->strokes;
351         for(;stroke;stroke=stroke->next) {
352             for(j=0;j<stroke->num_points-1;j++) {
353                 point_t a = stroke->points[j];
354                 point_t b = stroke->points[j+1];
355 #define ROTATE
356 #ifdef ROTATE
357                 swf_ShapeSetMove(tag, s, a.y, a.x);
358                 swf_ShapeSetLine(tag, s, b.y - a.y, b.x - a.x);
359 #else
360                 swf_ShapeSetMove(tag, s, a.x, a.y);
361                 swf_ShapeSetLine(tag, s, b.x - a.x, b.y - a.y);
362 #endif
363             }
364         }
365 #else
366         swf_ShapeSetAll(tag,s,0,0,ls,0,0);
367         edge_t*e = poly2->edges;
368         while(e) {
369             swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
370             swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
371             
372             swf_ShapeSetCircle(tag, s, e->a.x, e->a.y, 5*20, 5*20);
373             swf_ShapeSetCircle(tag, s, e->b.x, e->b.y, 5*20, 5*20);
374             e = e->next;
375         }
376 #endif
377
378         swf_ShapeSetEnd(tag);
379         swf_ShapeFree(s);
380
381         gfxpoly_destroy(poly1);
382         gfxpoly_destroy(poly2);
383
384         gfxline_free(l);
385    
386         if(t) {
387             tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
388             swf_SetU16(tag, t);
389         }
390         tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
391         swf_ObjectPlace(tag,t+1,t+1,NULL,NULL,NULL);
392
393         tag = swf_InsertTag(tag, ST_SHOWFRAME);
394     }
395     tag = swf_InsertTag(tag, ST_END);
396
397     swf_SaveSWF(&swf, "test.swf");
398 }
399
400 void rotate90(gfxpoly_t*poly)
401 {
402     int i,j;
403     gfxpolystroke_t*stroke = poly->strokes;
404     for(;stroke;stroke=stroke->next) {
405         for(j=0;j<stroke->num_points;j++) {
406             point_t a = stroke->points[j];
407             stroke->points[j].x = a.y;
408             stroke->points[j].y = a.x;
409         }
410     }
411 }
412
413 #include <dirent.h>
414 void test4(int argn, char*argv[])
415 {
416     char*dir = "ps";
417     DIR*_dir = opendir(dir);
418     if(!_dir) return;
419     struct dirent*file;
420     while(1) {
421         file = readdir(_dir);
422         if (!file) 
423             break;
424         if(!strstr(file->d_name, ".ps")) 
425             continue;
426
427         char* filename;
428
429         if(argn<2)
430             filename = allocprintf("%s/%s", dir, file->d_name);
431         else
432             filename = argv[1];
433
434         windrule_t*rule = &windrule_evenodd;
435         gfxpoly_t*poly1 = gfxpoly_from_file(filename, 1.0);//0.01);
436
437         if(argn!=2)
438             free(filename);
439
440         double zoom = 1.0;
441
442         if(!gfxpoly_check(poly1)) {
443             printf("bad polygon\n");
444             continue;
445         }
446
447         gfxpoly_t*poly2 = gfxpoly_process(poly1, 0, rule, &onepolygon);
448
449         int pass;
450         for(pass=0;pass<2;pass++) {
451             intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
452             unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
453             unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
454             if(!bitmap_ok(&bbox, bitmap1) || !bitmap_ok(&bbox, bitmap2)) {
455                 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
456                 assert(!"error in bitmaps");
457             }
458             if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
459                 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
460                 assert(!"bitmaps don't match");
461             }
462             free(bitmap1);
463             free(bitmap2);
464             
465             // second pass renders the 90° rotated version
466             rotate90(poly1);
467             rotate90(poly2);
468         }
469
470         gfxpoly_destroy(poly1);
471         gfxpoly_destroy(poly2);
472         if(argn==2) 
473             break;
474     }
475     closedir(_dir);
476 }
477
478 #include "../gfxdevice.h"
479 #include "../pdf/pdf.h"
480
481 static int max_segments = 0;
482 static int max_any_segments = 0;
483 void extract_polygons_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color) 
484 {
485     //gfxpoly_t*c = gfxpoly_from_fill(line, 0.05);
486     //gfxpoly_free(c);
487
488     //gfxpoly_t*poly1 = gfxpoly_from_fill(line, 0.05);
489     gfxpoly_t*poly1 = gfxpoly_from_fill(line, 0.05);
490
491     //gfxline_dump(line, stderr, "");
492     //gfxpoly_dump(poly);
493
494     int size = gfxpoly_size(poly1);
495     if(size == 4) {
496         //rectangles are boring.
497         gfxpoly_destroy(poly1);
498         return;
499     }
500
501     max_any_segments = size > max_any_segments? size : max_any_segments;
502     if(size>100000) {
503         fprintf(stderr, "%d segments (skipping)\n", size);
504         return;
505     } else {
506         max_segments = size > max_segments? size : max_segments;
507         fprintf(stderr, "%d segments (max so far: %d/%d)\n", size, max_segments, max_any_segments);
508     }
509
510     if(!gfxpoly_check(poly1)) {
511         gfxpoly_destroy(poly1);
512         fprintf(stderr, "bad polygon\n");
513         return;
514     }
515
516     windrule_t*rule = &windrule_evenodd;
517
518     double zoom = 1.0;
519     intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
520     unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
521     if(!bitmap_ok(&bbox, bitmap1)) {
522         fprintf(stderr, "bad polygon or error in renderer\n");
523         return;
524     }
525     gfxpoly_t*poly2 = gfxpoly_process(poly1, 0, rule, &onepolygon);
526     unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
527     if(!bitmap_ok(&bbox, bitmap2)) {
528         save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
529         assert(!"error in bitmap");
530     }
531     if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
532         save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
533         assert(!"bitmaps don't match");
534     }
535     free(bitmap1);
536     free(bitmap2);
537
538     gfxpoly_destroy(poly1);
539     gfxpoly_destroy(poly2);
540 }
541 int extract_polygons_setparameter(gfxdevice_t*dev, const char*key, const char*value) {
542     return 0;
543 }
544 void extract_polygons_startclip(gfxdevice_t*dev, gfxline_t*line) 
545 {
546     extract_polygons_fill(dev, line, 0);
547 }
548 void extract_polygons_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*imgcoord2devcoord, gfxcxform_t*cxform)
549 {
550     extract_polygons_fill(dev, line, 0);
551 }
552 void extract_polygons_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*gradcoord2devcoord)
553 {
554     extract_polygons_fill(dev, line, 0);
555 }
556 void extract_polygons_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
557 {
558     extract_polygons_fill(dev, line, 0);
559 }
560 void extract_polygons_addfont(gfxdevice_t*dev, gfxfont_t*font)
561 {
562     int t;
563     for(t=0;t<font->num_glyphs;t++) {
564         //extract_polygons_fill(dev, font->glyphs[t].line, 0);
565     }
566 }
567 void extract_polygons_endclip(gfxdevice_t*dev)
568 {
569 }
570 void extract_polygons_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
571 {
572 }
573 void extract_polygons_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
574 {
575 }
576     
577 gfxdevice_t extract_polygons = 
578 {
579 name: "extract polygons",
580 setparameter:extract_polygons_setparameter,
581 startclip: extract_polygons_startclip,
582 endclip: extract_polygons_endclip,
583 stroke: extract_polygons_stroke,
584 fill: extract_polygons_fill,
585 fillbitmap: extract_polygons_fillbitmap,
586 fillgradient: extract_polygons_fillgradient,
587 addfont: extract_polygons_addfont,
588 drawchar: extract_polygons_drawchar,
589 drawlink: extract_polygons_drawlink,
590 startpage: 0,
591 endpage: 0,
592 geterror: 0,
593 finish: 0,
594 internal: 0
595 };
596
597 void test5(int argn, char*argv[])
598 {
599     gfxsource_t*driver = gfxsource_pdf_create();
600     char*dir = "pdfs";
601     DIR*_dir = opendir(dir);
602     if(!_dir) return;
603     struct dirent*file;
604     while(1) {
605         file = readdir(_dir);
606         if (!file) 
607             break;
608         if(!strstr(file->d_name, ".pdf")) 
609             continue;
610         char* filename = allocprintf("%s/%s", dir, file->d_name);
611
612         if(argn>1) 
613             filename = argv[1];
614
615         gfxdocument_t*doc = driver->open(driver, filename);
616         gfxdevice_t*out = &extract_polygons;
617         int t;
618         for(t=1;t<=doc->num_pages;t++) {
619             fprintf(stderr, "%s (page %d)\n", filename, t);
620             gfxpage_t* page = doc->getpage(doc, t);
621             page->render(page, out);
622             page->destroy(page);
623         }
624         doc->destroy(doc);
625         if(argn>1) 
626             break;
627         free(filename);
628     }
629     closedir(_dir);
630     driver->destroy(driver);
631 }
632
633 int main(int argn, char*argv[])
634 {
635     test0(argn, argv);
636 }
637