several flash 8 fixes
[swftools.git] / lib / modules / swffilter.c
1 char* filtername[] = {"dropshadow","blur","glow","bevel","gradientglow","convolution","colormatrix","gradientbevel", 0};
2
3 void swf_SetFilter(TAG*tag, FILTER*filter)
4 {
5     swf_SetU8(tag, filter->type);
6     if(filter->type == FILTERTYPE_BLUR) {
7         FILTER_BLUR*f = (FILTER_BLUR*)filter;
8         swf_SetFixed(tag, f->blurx);
9         swf_SetFixed(tag, f->blury);
10         U8 flags = f->passes << 3;
11         swf_SetU8(tag, flags);
12     } else if(filter->type == FILTERTYPE_GLOW) {
13         FILTER_GLOW*f = (FILTER_GLOW*)filter;
14     } else if(filter->type == FILTERTYPE_DROPSHADOW) {
15         FILTER_DROPSHADOW*f = (FILTER_DROPSHADOW*)filter;
16         swf_SetRGBA(tag, &f->color);
17         swf_SetFixed(tag, f->blurx);
18         swf_SetFixed(tag, f->blury);
19         swf_SetFixed(tag, f->angle);
20         swf_SetFixed(tag, f->distance);
21         swf_SetFixed8(tag, f->strength);
22         U8 flags = f->innershadow<<7|f->knockout<<6|f->composite<<5|f->passes;
23         swf_SetU8(tag, flags);
24     } else if(filter->type == FILTERTYPE_GRADIENTGLOW) {
25         FILTER_GRADIENTGLOW*f = (FILTER_GRADIENTGLOW*)filter;
26         swf_SetU8(tag, f->gradient->num);
27         int s;
28         for(s=0;s<f->gradient->num;s++)
29             swf_SetRGBA(tag, &f->gradient->rgba[s]);
30         for(s=0;s<f->gradient->num;s++)
31             swf_SetU8(tag, f->gradient->ratios[s]);
32
33         swf_SetFixed(tag, f->blurx);
34         swf_SetFixed(tag, f->blury);
35         swf_SetFixed(tag, f->angle);
36         swf_SetFixed(tag, f->distance);
37         swf_SetFixed8(tag, f->strength);
38         U8 flags = f->passes|f->innershadow<<7|f->knockout<<6|f->composite<<5|f->ontop<<4;
39         swf_SetU8(tag, flags);
40     } else if(filter->type == FILTERTYPE_BEVEL) {
41         FILTER_BEVEL*f = (FILTER_BEVEL*)filter;
42         swf_SetRGBA(tag, &f->shadow);
43         swf_SetRGBA(tag, &f->highlight);
44         swf_SetFixed(tag, f->blurx);
45         swf_SetFixed(tag, f->blury);
46         swf_SetFixed(tag, f->angle);
47         swf_SetFixed(tag, f->distance);
48         swf_SetFixed8(tag, f->strength);
49         U8 flags = f->passes|f->innershadow<<7|f->knockout<<6|f->composite<<5|f->ontop<<4;
50         swf_SetU8(tag, flags);
51     } else {
52         fprintf(stderr, "Writing of filter type %02x not supported yet\n", filter->type);
53     }
54 }
55
56 FILTER*swf_GetFilter(TAG*tag)
57 {
58     U8 type = swf_GetU8(tag);
59     FILTER*filter;
60     if(type == FILTERTYPE_BLUR) {
61         FILTER_BLUR* f = (FILTER_BLUR*)rfx_calloc(sizeof(FILTER_BLUR));
62         f->type = type;
63         f->blurx = swf_GetFixed(tag);
64         f->blury = swf_GetFixed(tag);
65         U8 flags = swf_GetU8(tag);
66         f->passes = (flags&15)<<3;
67         return (FILTER*)f;
68     } else if(type == FILTERTYPE_GLOW) {
69         FILTER_GLOW* f = (FILTER_GLOW*)rfx_calloc(sizeof(FILTER_GLOW));
70         f->type = type;
71         swf_GetRGBA(tag, &f->rgba);
72         f->blurx = swf_GetFixed(tag);
73         f->blury = swf_GetFixed(tag);
74         f->strength = swf_GetFixed8(tag);
75         U8 flags = swf_GetU8(tag);
76         f->passes = flags&31;
77         f->innerglow = (flags>>7)&1;
78         f->knockout = (flags>>6)&1;
79         f->composite = (flags>>5)&1;
80         return (FILTER*)f;
81     } else if(type == FILTERTYPE_GRADIENTGLOW) {
82         FILTER_GRADIENTGLOW* f = (FILTER_GRADIENTGLOW*)rfx_calloc(sizeof(FILTER_GRADIENTGLOW));
83         f->type = type;
84         f->gradient = (GRADIENT*)rfx_calloc(sizeof(GRADIENT));
85         f->gradient->num = swf_GetU8(tag);
86         f->gradient->rgba = (RGBA*)rfx_calloc(sizeof(RGBA)*f->gradient->num);
87         f->gradient->ratios = (U8*)rfx_calloc(sizeof(U8)*f->gradient->num);
88         int s;
89         for(s=0;s<f->gradient->num;s++)
90             swf_GetRGBA(tag, &f->gradient->rgba[s]);
91         for(s=0;s<f->gradient->num;s++)
92             f->gradient->ratios[s] = swf_GetU8(tag);
93         
94         f->blurx = swf_GetFixed(tag);
95         f->blury = swf_GetFixed(tag);
96         f->angle = swf_GetFixed(tag);
97         f->distance = swf_GetFixed(tag);
98         f->strength = swf_GetFixed8(tag);
99         U8 flags = swf_GetU8(tag);
100         f->passes = flags&15;
101         f->innershadow = (flags>>7)&1;
102         f->knockout = (flags>>6)&1;
103         f->composite = (flags>>5)&1;
104         f->ontop = (flags>>4)&1;
105         return (FILTER*)f;
106     } else if(type == FILTERTYPE_DROPSHADOW) {
107         FILTER_DROPSHADOW* f = (FILTER_DROPSHADOW*)rfx_calloc(sizeof(FILTER_DROPSHADOW));
108         f->type = type;
109         swf_GetRGBA(tag, &f->color);
110         f->blurx = swf_GetFixed(tag);
111         f->blury = swf_GetFixed(tag);
112         f->angle = swf_GetFixed(tag);
113         f->distance = swf_GetFixed(tag);
114         f->strength = swf_GetFixed8(tag);
115         U8 flags = swf_GetU8(tag);
116         f->passes = flags&31;
117         f->innershadow = (flags>>7)&1;
118         f->knockout = (flags>>6)&1;
119         f->composite = (flags>>5)&1;
120         return (FILTER*)f;
121     } else if(type == FILTERTYPE_BEVEL) {
122         FILTER_BEVEL* f = (FILTER_BEVEL*)rfx_calloc(sizeof(FILTER_BEVEL));
123         f->type = type;
124         swf_GetRGBA(tag, &f->shadow);
125         swf_GetRGBA(tag, &f->highlight);
126         f->blurx = swf_GetFixed(tag);
127         f->blury = swf_GetFixed(tag);
128         f->angle = swf_GetFixed(tag);
129         f->distance = swf_GetFixed(tag);
130         f->strength = swf_GetFixed8(tag);
131         U8 flags = swf_GetU8(tag);
132         f->passes = flags&15;
133         f->innershadow = (flags>>7)&1;
134         f->knockout = (flags>>6)&1;
135         f->composite = (flags>>5)&1;
136         f->ontop = (flags>>4)&1;
137         return (FILTER*)f;
138     } else {
139         fprintf(stderr, "Reading of filter type %02x not supported yet\n", type);
140     }
141     return 0;
142 }
143
144 FILTER*swf_NewFilter(U8 type)
145 {
146     FILTER*f = 0;
147     if(type == FILTERTYPE_BLUR)
148         f = (FILTER*)rfx_calloc(sizeof(FILTER_BLUR));
149     else if(type == FILTERTYPE_GRADIENTGLOW)
150         f = (FILTER*)rfx_calloc(sizeof(FILTER_GRADIENTGLOW));
151     else if(type == FILTERTYPE_DROPSHADOW)
152         f = (FILTER*)rfx_calloc(sizeof(FILTER_DROPSHADOW));
153     else if(type == FILTERTYPE_BEVEL)
154         f = (FILTER*)rfx_calloc(sizeof(FILTER_BEVEL));
155     else 
156         fprintf(stderr, "Creation of filter type %02x not supported yet\n", type);
157     if(f)
158         f->type = type;
159     return f;
160 }
161
162 void swf_DeleteFilter(FILTER*f)
163 {
164     //FIXME
165     free(f);
166 }