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