b9062f5b8aab0bd25410662abf9af4cf4c1256b1
[swftools.git] / lib / gfxpoly / test.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <assert.h>
4 #include <memory.h>
5 #include <math.h>
6 #include "../gfxtools.h"
7 #include "poly.h"
8 #include "convert.h"
9 #include "renderpoly.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 int test1()
30 {
31     gfxline_t*box1 = gfxline_makerectangle(50,50,150,150);
32     // put box2 and box3 on top of each other *snicker*
33     gfxline_t*box2 = gfxline_makerectangle(100,100,200,200);
34     gfxline_t*box3 = gfxline_makerectangle(100,100,200,200);
35     gfxline_t*star = mkstar(50,50, 150,150);
36     gfxline_t*b = 0;
37     b = gfxline_append(b, box1);
38     b = gfxline_append(b, box2);
39     b = gfxline_append(b, box3);
40     b = gfxline_append(b, star);
41
42     gfxmatrix_t matrix;
43     memset(&matrix, 0, sizeof(gfxmatrix_t));
44     double ua=0.1;
45     matrix.m00=cos(ua);matrix.m10=sin(ua);
46     matrix.m01=-sin(ua);matrix.m11=cos(ua);
47
48     gfxline_transform(b, &matrix);
49     gfxpoly_t*poly = gfxpoly_fillToPoly(b, 0.05);
50     gfxline_free(box1);
51     gfxline_free(box2);
52     gfxline_free(box3);
53     gfxline_free(star);
54
55     gfxpoly_dump(poly);
56     gfxpoly_process(poly, &windrule_evenodd);
57 }
58
59 int test_square(int width, int height, int num, double gridsize, char bitmaptest)
60 {
61     int t;
62     gfxline_t* line = malloc(sizeof(gfxline_t)*num);
63     for(t=0;t<num;t++) {
64         line[t].type = t?gfx_lineTo:gfx_moveTo;
65         line[t].x = (lrand48()%width);
66         line[t].y = (lrand48()%height);
67         line[t].next = &line[t+1];
68     }
69     line[num-1].x = line[0].x;
70     line[num-1].y = line[0].y;
71     line[num-1].next = 0;
72     
73     windrule_t*rule = &windrule_circular;
74     
75     gfxpoly_t*poly = gfxpoly_fillToPoly(line, gridsize);
76     gfxpoly_t*poly2 = gfxpoly_process(poly, rule);
77     gfxline_free(line);
78
79     if(bitmaptest) {
80         intbbox_t bbox = intbbox_new(0, 0, width, height);
81         unsigned char*bitmap1 = render_polygon(poly, &bbox, 1.0, rule);
82         unsigned char*bitmap2 = render_polygon(poly2, &bbox, 1.0, &windrule_evenodd);
83         if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
84             save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
85             assert(!"bitmaps don't match");
86         }
87     }
88
89     gfxpoly_destroy(poly);
90     gfxpoly_destroy(poly2);
91 }
92
93 int test2()
94 {
95     test_square(400,400, 3, 0.05, 1);
96
97     int t;
98     for(t=0;t<400;t++) {
99         test_square(400,400, 50, 0.05, 1);
100         test_square(200,3, 1000, 1.0, 0);
101         test_square(3,200, 1000, 1.0, 0);
102         test_square(10,10, 200, 1.0, 0);
103     }
104 }
105
106 #include "../rfxswf.h"
107 void test3()
108 {
109 #undef N
110 #undef RANGE
111 #define N 100
112 #define RANGE 400
113
114     int i;
115     gfxline_t* line = malloc(sizeof(gfxline_t)*N*2);
116     for(i=0;i<N;i++) {
117         line[i].type = i?gfx_lineTo:gfx_moveTo;
118         line[i].x = lrand48()%RANGE - RANGE/2;
119         line[i].y = lrand48()%RANGE - RANGE/2;
120         line[i].next = &line[i+1];
121         line[N*2-i-1].type = gfx_lineTo;
122         line[N*2-i-1].x = line[i].x;
123         line[N*2-i-1].y = line[i].y;
124         line[N*2-i-1].next = &line[N*2-i];
125     }
126     line[N*2-1].next = 0;
127
128     line[N-1].x = line[0].x;
129     line[N-1].y = line[0].y;
130     line[N-1].next = 0;
131
132     gfxmatrix_t m;
133     memset(&m, 0, sizeof(m));
134
135     SWF swf;
136     memset(&swf, 0, sizeof(SWF));
137     swf.movieSize.xmax = RANGE*20*1.41;
138     swf.movieSize.ymax = RANGE*20*1.41;
139     swf.fileVersion = 9;
140     swf.frameRate = 25*0x100;
141     TAG * tag = swf.firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
142     swf_SetU8(tag, 0);
143     swf_SetU8(tag, 0);
144     swf_SetU8(tag, 0);
145
146     int t;
147     for(t=0;t<360;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 = RANGE*1.41/2;
153         m.ty = RANGE*1.41/2;
154         gfxline_t*l = gfxline_clone(line);
155         gfxline_transform(l, &m);
156         
157         gfxpoly_t*poly = gfxpoly_fillToPoly(l, 0.05);
158         gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_circular);
159
160         tag = swf_InsertTag(tag, ST_DEFINESHAPE);
161         SHAPE* s;
162         swf_ShapeNew(&s);
163         RGBA rgb;
164         rgb.r = rgb.g = 0x00; rgb.b = 0xff;
165         rgb.a = 255;
166         int fs = swf_ShapeAddSolidFillStyle(s,&rgb);
167         int ls = swf_ShapeAddLineStyle(s,20,&rgb);
168         swf_SetU16(tag,t+1);
169         swf_SetRect(tag,&swf.movieSize);
170         swf_SetShapeHeader(tag,s);
171
172 #define FILL
173 #ifdef FILL
174         swf_ShapeSetAll(tag,s,0,0,0,fs,0);
175         edge_t*e = poly2->edges;
176         while(e) {
177             swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
178             swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
179             e = e->next;
180         }
181 #else
182         swf_ShapeSetAll(tag,s,0,0,ls,0,0);
183         edge_t*e = poly2->edges;
184         while(e) {
185             swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
186             swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
187             
188             swf_ShapeSetCircle(tag, s, e->a.x, e->a.y, 5*20, 5*20);
189             swf_ShapeSetCircle(tag, s, e->b.x, e->b.y, 5*20, 5*20);
190             e = e->next;
191         }
192 #endif
193
194         swf_ShapeSetEnd(tag);
195         swf_ShapeFree(s);
196
197         gfxpoly_destroy(poly);
198         gfxpoly_destroy(poly2);
199
200         gfxline_free(l);
201    
202         if(t) {
203             tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
204             swf_SetU16(tag, t);
205         }
206         tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
207         swf_ObjectPlace(tag,t+1,t+1,NULL,NULL,NULL);
208
209         tag = swf_InsertTag(tag, ST_SHOWFRAME);
210     }
211     tag = swf_InsertTag(tag, ST_END);
212
213     swf_SaveSWF(&swf, "test.swf");
214 }
215
216 int main()
217 {
218     test3();
219 }