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