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