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