switched several parts of the polygon processor to a more compact polygon representation
[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()
118 {
119     gfxline_t*b = 0;
120     unsigned int c = 0;
121     int t;
122     for(t=0;t<30;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         b = gfxline_append(b, gfxline_makecircle(x,y,r,r));
130         //b = gfxline_append(b, gfxline_makerectangle(10,10,100,100));
131     }
132     return b;
133 }
134
135 static windcontext_t onepolygon = {1};
136
137 int test0()
138 {
139     //gfxline_t* b = mkchessboard();
140     //gfxline_t* b = mkrandomshape(100,7);
141     gfxline_t* b = gfxline_makecircle(100,100,100,100);
142
143     gfxmatrix_t m;
144     memset(&m, 0, sizeof(gfxmatrix_t));
145     int t;
146     for(t=0;t<360;t++) {
147         m.m00 = cos(t*M_PI/180.0);
148         m.m01 = sin(t*M_PI/180.0);
149         m.m10 = -sin(t*M_PI/180.0);
150         m.m11 = cos(t*M_PI/180.0);
151         m.tx = 400*1.41/2;
152         m.ty = 400*1.41/2;
153         gfxline_transform(b, &m);
154         gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(b, 0.05);
155
156         gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_evenodd, &onepolygon);
157         gfxcompactpoly_destroy(poly);
158         gfxpoly_destroy(poly2);
159     }
160     gfxline_free(b);
161 }
162
163 int test1(int argn, char*argv[])
164 {
165     gfxline_t*box1 = gfxline_makerectangle(50,50,150,150);
166     gfxline_t*box2 = gfxline_makerectangle(100,100,200,200);
167     gfxline_t*box3 = gfxline_makerectangle(100,100,200,200);
168     gfxline_t*star = mkstar(50,50, 150,150);
169     gfxline_t*b = 0;
170     b = gfxline_append(b, box1);
171     b = gfxline_append(b, box2);
172     b = gfxline_append(b, box3);
173
174     gfxmatrix_t matrix;
175     memset(&matrix, 0, sizeof(gfxmatrix_t));
176     double ua=0.1;
177     matrix.m00=cos(ua);matrix.m10=sin(ua);
178     matrix.m01=-sin(ua);matrix.m11=cos(ua);
179
180     //gfxline_transform(b, &matrix);
181
182     gfxline_dump(b, stderr, "");
183
184     gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(b, 0.05);
185     gfxline_free(box1);
186     gfxline_free(box2);
187     gfxline_free(box3);
188     gfxline_free(star);
189
190     gfxcompactpoly_dump(poly);
191     gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_evenodd, &onepolygon);
192     gfxcompactpoly_destroy(poly);
193     gfxpoly_destroy(poly2);
194 }
195
196 static void test_conversion(gfxline_t*line, double gridsize)
197 {
198     double zoom=1.0;
199     gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(line, gridsize);
200     gfxpoly_t*poly1 = gfxpoly_from_gfxline(line, gridsize);
201     gfxpoly_t*poly2 = gfxpoly_from_gfxcompactpoly(poly);
202     assert(gfxpoly_check(poly1));
203     assert(gfxpoly_check(poly2));
204     assert(gfxcompactpoly_check(poly));
205     intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
206     unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, &windrule_evenodd, &onepolygon);
207     assert(bitmap_ok(&bbox, bitmap1));
208     unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
209     assert(bitmap_ok(&bbox, bitmap2));
210     if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
211         save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
212         assert(!"bitmaps don't match");
213     }
214 }
215
216 int test_square(int width, int height, int num, double gridsize, char bitmaptest)
217 {
218     int t;
219     gfxline_t* line = malloc(sizeof(gfxline_t)*num);
220     for(t=0;t<num;t++) {
221         line[t].type = t?gfx_lineTo:gfx_moveTo;
222         line[t].x = (lrand48()%width);
223         line[t].y = (lrand48()%height);
224         line[t].next = &line[t+1];
225     }
226     line[num-1].x = line[0].x;
227     line[num-1].y = line[0].y;
228     line[num-1].next = 0;
229
230     test_conversion(line, gridsize);
231     
232     gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(line, gridsize);
233     gfxline_free(line);
234     gfxpoly_t*poly1 = gfxpoly_from_gfxcompactpoly(poly);
235
236     windrule_t*rule = &windrule_circular;
237     gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
238     if(bitmaptest) {
239         intbbox_t bbox = intbbox_new(0, 0, width, height);
240         unsigned char*bitmap1 = render_polygon(poly1, &bbox, 1.0, rule, &onepolygon);
241         assert(bitmap_ok(&bbox, bitmap1));
242         unsigned char*bitmap2 = render_polygon(poly2, &bbox, 1.0, &windrule_evenodd, &onepolygon);
243         assert(bitmap_ok(&bbox, bitmap2));
244         if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
245             save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
246             assert(!"bitmaps don't match");
247         }
248     }
249     gfxpoly_destroy(poly1);
250     gfxpoly_destroy(poly2);
251     gfxcompactpoly_destroy(poly);
252 }
253
254 int test2(int argn, char*argv[])
255 {
256     test_square(400,400, 3, 0.05, 1);
257
258     int t;
259     for(t=0;t<400;t++) {
260         fprintf(stderr, "%d\n", t);
261         test_square(400,400, 50, 0.05, 1);
262         test_square(200,3, 1000, 1.0, 0);
263         test_square(3,200, 1000, 1.0, 0);
264         test_square(10,10, 200, 1.0, 0);
265     }
266 }
267
268 #include "../rfxswf.h"
269 void test3(int argn, char*argv[])
270 {
271 #undef N
272 #undef RANGE
273 #define N 100
274 #define RANGE 400
275
276     //gfxline_t*line = mkrandomshape(RANGE, N);
277     //windrule_t*rule = &windrule_circular;
278     //gfxline_t*line = mkchessboard();
279     gfxline_t*line = make_circles();
280     windrule_t*rule = &windrule_evenodd;
281     //windrule_t*rule = &windrule_circular;
282
283     gfxmatrix_t m;
284     memset(&m, 0, sizeof(m));
285
286     SWF swf;
287     memset(&swf, 0, sizeof(SWF));
288     swf.movieSize.xmax = RANGE*20*1.41;
289     swf.movieSize.ymax = RANGE*20*1.41;
290     swf.fileVersion = 9;
291     swf.frameRate = 25*0x100;
292     TAG * tag = swf.firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
293     swf_SetU8(tag, 0);
294     swf_SetU8(tag, 0);
295     swf_SetU8(tag, 0);
296
297     int t;
298     for(t=0;t<360;t++) {
299         fprintf(stderr, "%d\n", t);
300         m.m00 = cos(t*M_PI/180.0);
301         m.m01 = sin(t*M_PI/180.0);
302         m.m10 = -sin(t*M_PI/180.0);
303         m.m11 = cos(t*M_PI/180.0);
304         m.tx = RANGE*1.41/2;
305         m.ty = RANGE*1.41/2;
306
307         gfxline_t*l = gfxline_clone(line);
308         gfxline_transform(l, &m);
309
310         test_conversion(l, 0.05);
311         
312         gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(l, 0.05);
313         gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
314
315         tag = swf_InsertTag(tag, ST_DEFINESHAPE);
316         SHAPE* s;
317         swf_ShapeNew(&s);
318         RGBA rgb;
319         rgb.r = rgb.g = 0x00; rgb.b = 0xff;
320         rgb.a = 255;
321         int fs = swf_ShapeAddSolidFillStyle(s,&rgb);
322         int ls = swf_ShapeAddLineStyle(s,20,&rgb);
323         swf_SetU16(tag,t+1);
324         swf_SetRect(tag,&swf.movieSize);
325         swf_SetShapeHeader(tag,s);
326
327 #define FILL
328 #ifdef FILL
329         swf_ShapeSetAll(tag,s,0,0,0,fs,0);
330         edge_t*e = poly2->edges;
331         while(e) {
332 #define ROTATE
333 #ifdef ROTATE
334             swf_ShapeSetMove(tag, s, e->a.y, e->a.x);
335             swf_ShapeSetLine(tag, s, e->b.y - e->a.y, e->b.x - e->a.x);
336 #else
337             swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
338             swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
339 #endif
340             e = e->next;
341         }
342 #else
343         swf_ShapeSetAll(tag,s,0,0,ls,0,0);
344         edge_t*e = poly2->edges;
345         while(e) {
346             swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
347             swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
348             
349             swf_ShapeSetCircle(tag, s, e->a.x, e->a.y, 5*20, 5*20);
350             swf_ShapeSetCircle(tag, s, e->b.x, e->b.y, 5*20, 5*20);
351             e = e->next;
352         }
353 #endif
354
355         swf_ShapeSetEnd(tag);
356         swf_ShapeFree(s);
357
358         gfxcompactpoly_destroy(poly);
359         gfxpoly_destroy(poly2);
360
361         gfxline_free(l);
362    
363         if(t) {
364             tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
365             swf_SetU16(tag, t);
366         }
367         tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
368         swf_ObjectPlace(tag,t+1,t+1,NULL,NULL,NULL);
369
370         tag = swf_InsertTag(tag, ST_SHOWFRAME);
371     }
372     tag = swf_InsertTag(tag, ST_END);
373
374     swf_SaveSWF(&swf, "test.swf");
375 }
376
377 void rotate90(gfxpoly_t*poly)
378 {
379     edge_t*e = poly->edges;
380     while(e) {
381         point_t a = e->a;
382         point_t b = e->b;
383         e->a.x = a.y;
384         e->a.y = a.x;
385         e->b.x = b.y;
386         e->b.y = b.x;
387         e = e->next;
388     }
389 }
390
391 #include <dirent.h>
392 void test4(int argn, char*argv[])
393 {
394     char*dir = "ps";
395     DIR*_dir = opendir(dir);
396     if(!_dir) return;
397     struct dirent*file;
398     while(1) {
399         file = readdir(_dir);
400         if (!file) 
401             break;
402         if(!strstr(file->d_name, ".ps")) 
403             continue;
404
405         char* filename;
406
407         if(argn<2)
408             filename = allocprintf("%s/%s", dir, file->d_name);
409         else
410             filename = argv[1];
411
412         windrule_t*rule = &windrule_evenodd;
413         gfxcompactpoly_t*poly = gfxcompactpoly_from_file(filename, 1.0);//0.01);
414
415         if(argn!=2)
416             free(filename);
417
418         double zoom = 1.0;
419
420         if(!gfxcompactpoly_check(poly)) {
421             printf("bad polygon\n");
422             continue;
423         }
424
425         gfxpoly_t*poly1 = gfxpoly_from_gfxcompactpoly(poly);
426         gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
427
428         int pass;
429         for(pass=0;pass<2;pass++) {
430             intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
431             unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
432             unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
433             if(!bitmap_ok(&bbox, bitmap1) || !bitmap_ok(&bbox, bitmap2)) {
434                 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
435                 assert(!"error in bitmaps");
436             }
437             if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
438                 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
439                 assert(!"bitmaps don't match");
440             }
441             free(bitmap1);
442             free(bitmap2);
443             
444             // second pass renders the 90° rotated version
445             rotate90(poly1);
446             rotate90(poly2);
447         }
448
449         gfxpoly_destroy(poly1);
450         gfxpoly_destroy(poly2);
451         gfxcompactpoly_destroy(poly);
452         if(argn==2) 
453             break;
454     }
455     closedir(_dir);
456 }
457
458 #include "../gfxdevice.h"
459 #include "../pdf/pdf.h"
460
461 void extract_polygons_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color) 
462 {
463     //gfxcompactpoly_t*c = gfxcompactpoly_from_gfxline(line, 0.05);
464     //gfxcompactpoly_free(c);
465
466     gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(line, 0.05);
467
468     //gfxline_dump(line, stderr, "");
469     //gfxcompactpoly_dump(poly);
470
471     if(gfxcompactpoly_size(poly)>100000) {
472         fprintf(stderr, "%d segments (skipping)\n", gfxcompactpoly_size(poly));
473         return;
474     } else {
475         //fprintf(stderr, "%d segments\n", gfxpoly_size(poly));
476     }
477
478     if(!gfxcompactpoly_check(poly)) {
479         gfxcompactpoly_destroy(poly);
480         fprintf(stderr, "bad polygon\n");
481         return;
482     }
483
484     windrule_t*rule = &windrule_evenodd;
485
486     gfxpoly_t*poly1 = gfxpoly_from_gfxcompactpoly(poly);
487         
488     double zoom = 1.0;
489     intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
490     unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
491     if(!bitmap_ok(&bbox, bitmap1)) {
492         fprintf(stderr, "bad polygon or error in renderer\n");
493         return;
494     }
495     gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
496     unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
497     if(!bitmap_ok(&bbox, bitmap2)) {
498         save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
499         assert(!"error in bitmap");
500     }
501     if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
502         save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
503         assert(!"bitmaps don't match");
504     }
505     free(bitmap1);
506     free(bitmap2);
507
508     gfxpoly_destroy(poly1);
509     gfxpoly_destroy(poly2);
510     gfxcompactpoly_destroy(poly);
511 }
512 int extract_polygons_setparameter(gfxdevice_t*dev, const char*key, const char*value) {
513     return 0;
514 }
515 void extract_polygons_startclip(gfxdevice_t*dev, gfxline_t*line) 
516 {
517     extract_polygons_fill(dev, line, 0);
518 }
519 void extract_polygons_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*imgcoord2devcoord, gfxcxform_t*cxform)
520 {
521     extract_polygons_fill(dev, line, 0);
522 }
523 void extract_polygons_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*gradcoord2devcoord)
524 {
525     extract_polygons_fill(dev, line, 0);
526 }
527 void extract_polygons_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
528 {
529     extract_polygons_fill(dev, line, 0);
530 }
531 void extract_polygons_addfont(gfxdevice_t*dev, gfxfont_t*font)
532 {
533     int t;
534     for(t=0;t<font->num_glyphs;t++) {
535         //extract_polygons_fill(dev, font->glyphs[t].line, 0);
536     }
537 }
538 void extract_polygons_endclip(gfxdevice_t*dev)
539 {
540 }
541 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)
542 {
543 }
544 void extract_polygons_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
545 {
546 }
547     
548 gfxdevice_t extract_polygons = 
549 {
550 name: "extract polygons",
551 setparameter:extract_polygons_setparameter,
552 startclip: extract_polygons_startclip,
553 endclip: extract_polygons_endclip,
554 stroke: extract_polygons_stroke,
555 fill: extract_polygons_fill,
556 fillbitmap: extract_polygons_fillbitmap,
557 fillgradient: extract_polygons_fillgradient,
558 addfont: extract_polygons_addfont,
559 drawchar: extract_polygons_drawchar,
560 drawlink: extract_polygons_drawlink,
561 startpage: 0,
562 endpage: 0,
563 geterror: 0,
564 finish: 0,
565 internal: 0
566 };
567
568 void test5(int argn, char*argv[])
569 {
570     gfxsource_t*driver = gfxsource_pdf_create();
571     char*dir = "pdfs";
572     DIR*_dir = opendir(dir);
573     if(!_dir) return;
574     struct dirent*file;
575     while(1) {
576         file = readdir(_dir);
577         if (!file) 
578             break;
579         if(!strstr(file->d_name, ".pdf")) 
580             continue;
581         char* filename = allocprintf("%s/%s", dir, file->d_name);
582
583         if(argn>1) 
584             filename = argv[1];
585
586         gfxdocument_t*doc = driver->open(driver, filename);
587         gfxdevice_t*out = &extract_polygons;
588         int t;
589         for(t=1;t<=doc->num_pages;t++) {
590             fprintf(stderr, "%s (page %d)\n", filename, t);
591             gfxpage_t* page = doc->getpage(doc, t);
592             page->render(page, out);
593             page->destroy(page);
594         }
595         doc->destroy(doc);
596         if(argn>1) 
597             break;
598         free(filename);
599     }
600     closedir(_dir);
601     driver->destroy(driver);
602 }
603
604 int main(int argn, char*argv[])
605 {
606     test4(argn, argv);
607 }
608