applied MSVC compatibility patch from Dwight Kelly
[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_DROPSHADOW) {
13         FILTER_DROPSHADOW*f = (FILTER_DROPSHADOW*)filter;
14         swf_SetRGBA(tag, &f->color);
15         swf_SetFixed(tag, f->blurx);
16         swf_SetFixed(tag, f->blury);
17         swf_SetFixed(tag, f->angle);
18         swf_SetFixed(tag, f->distance);
19         swf_SetFixed8(tag, f->strength);
20         U8 flags = f->innershadow<<7|f->knockout<<6|f->composite<<5|f->passes;
21         swf_SetU8(tag, flags);
22     } else if(filter->type == FILTERTYPE_GRADIENTGLOW) {
23         FILTER_GRADIENTGLOW*f = (FILTER_GRADIENTGLOW*)filter;
24         swf_SetU8(tag, f->gradient->num);
25         int s;
26         for(s=0;s<f->gradient->num;s++)
27             swf_SetRGBA(tag, &f->gradient->rgba[s]);
28         for(s=0;s<f->gradient->num;s++)
29             swf_SetU8(tag, f->gradient->ratios[s]);
30
31         swf_SetFixed(tag, f->blurx);
32         swf_SetFixed(tag, f->blury);
33         swf_SetFixed(tag, f->angle);
34         swf_SetFixed(tag, f->distance);
35         swf_SetFixed8(tag, f->strength);
36         U8 flags = f->passes|f->innershadow<<7|f->knockout<<6|f->composite<<5|f->ontop<<4;
37         swf_SetU8(tag, flags);
38     } else if(filter->type == FILTERTYPE_BEVEL) {
39         FILTER_BEVEL*f = (FILTER_BEVEL*)filter;
40         swf_SetRGBA(tag, &f->shadow);
41         swf_SetRGBA(tag, &f->highlight);
42         swf_SetFixed(tag, f->blurx);
43         swf_SetFixed(tag, f->blury);
44         swf_SetFixed(tag, f->angle);
45         swf_SetFixed(tag, f->distance);
46         swf_SetFixed8(tag, f->strength);
47         U8 flags = f->passes|f->innershadow<<7|f->knockout<<6|f->composite<<5|f->ontop<<4;
48         swf_SetU8(tag, flags);
49     } else {
50         fprintf(stderr, "Writing of filter type %02x not supported yet\n", filter->type);
51     }
52 }
53
54 FILTER*swf_GetFilter(TAG*tag)
55 {
56     U8 type = swf_GetU8(tag);
57     FILTER*filter;
58     if(type == FILTERTYPE_BLUR) {
59         FILTER_BLUR* f = (FILTER_BLUR*)rfx_calloc(sizeof(FILTER_BLUR));
60         f->type = type;
61         f->blurx = swf_GetFixed(tag);
62         f->blury = swf_GetFixed(tag);
63         U8 flags = swf_GetU8(tag);
64         f->passes = (flags&15)<<3;
65         return (FILTER*)f;
66     } else if(type == FILTERTYPE_GRADIENTGLOW) {
67         FILTER_GRADIENTGLOW* f = (FILTER_GRADIENTGLOW*)rfx_calloc(sizeof(FILTER_GRADIENTGLOW));
68         f->type = type;
69         f->gradient = (GRADIENT*)rfx_calloc(sizeof(GRADIENT));
70         f->gradient->num = swf_GetU8(tag);
71         f->gradient->rgba = (RGBA*)rfx_calloc(sizeof(RGBA)*f->gradient->num);
72         f->gradient->ratios = (U8*)rfx_calloc(sizeof(U8)*f->gradient->num);
73         int s;
74         for(s=0;s<f->gradient->num;s++)
75             swf_GetRGBA(tag, &f->gradient->rgba[s]);
76         for(s=0;s<f->gradient->num;s++)
77             f->gradient->ratios[s] = swf_GetU8(tag);
78         
79         f->blurx = swf_GetFixed(tag);
80         f->blury = swf_GetFixed(tag);
81         f->angle = swf_GetFixed(tag);
82         f->distance = swf_GetFixed(tag);
83         f->strength = swf_GetFixed8(tag);
84         U8 flags = swf_GetU8(tag);
85         f->passes = flags&15;
86         f->innershadow = (flags>>7)&1;
87         f->knockout = (flags>>6)&1;
88         f->composite = (flags>>5)&1;
89         f->ontop = (flags>>4)&1;
90         return (FILTER*)f;
91     } else if(type == FILTERTYPE_DROPSHADOW) {
92         FILTER_DROPSHADOW* f = (FILTER_DROPSHADOW*)rfx_calloc(sizeof(FILTER_DROPSHADOW));
93         f->type = type;
94         swf_GetRGBA(tag, &f->color);
95         f->blurx = swf_GetFixed(tag);
96         f->blury = swf_GetFixed(tag);
97         f->angle = swf_GetFixed(tag);
98         f->distance = swf_GetFixed(tag);
99         f->strength = swf_GetFixed8(tag);
100         U8 flags = swf_GetU8(tag);
101         f->passes = flags&31;
102         f->innershadow = (flags>>7)&1;
103         f->knockout = (flags>>6)&1;
104         f->composite = (flags>>5)&1;
105         return (FILTER*)f;
106     } else if(type == FILTERTYPE_BEVEL) {
107         FILTER_BEVEL* f = (FILTER_BEVEL*)rfx_calloc(sizeof(FILTER_BEVEL));
108         f->type = type;
109         swf_GetRGBA(tag, &f->shadow);
110         swf_GetRGBA(tag, &f->highlight);
111         f->blurx = swf_GetFixed(tag);
112         f->blury = swf_GetFixed(tag);
113         f->angle = swf_GetFixed(tag);
114         f->distance = swf_GetFixed(tag);
115         f->strength = swf_GetFixed8(tag);
116         U8 flags = swf_GetU8(tag);
117         f->passes = flags&15;
118         f->innershadow = (flags>>7)&1;
119         f->knockout = (flags>>6)&1;
120         f->composite = (flags>>5)&1;
121         f->ontop = (flags>>4)&1;
122         return (FILTER*)f;
123     } else {
124         fprintf(stderr, "Reading of filter type %02x not supported yet\n", type);
125     }
126     return 0;
127 }
128
129 FILTER*swf_NewFilter(U8 type)
130 {
131     FILTER*f = 0;
132     if(type == FILTERTYPE_BLUR)
133         f = (FILTER*)rfx_calloc(sizeof(FILTER_BLUR));
134     else if(type == FILTERTYPE_GRADIENTGLOW)
135         f = (FILTER*)rfx_calloc(sizeof(FILTER_GRADIENTGLOW));
136     else if(type == FILTERTYPE_DROPSHADOW)
137         f = (FILTER*)rfx_calloc(sizeof(FILTER_DROPSHADOW));
138     else if(type == FILTERTYPE_BEVEL)
139         f = (FILTER*)rfx_calloc(sizeof(FILTER_BEVEL));
140     else 
141         fprintf(stderr, "Creation of filter type %02x not supported yet\n", type);
142     if(f)
143         f->type = type;
144     return f;
145 }