added filter support to ruby module
[swftools.git] / lib / filters / alpha.c
1 /* alpha.c
2
3    Part of the swftools package.
4
5    Copyright (c) 2010 Matthias Kramm <kramm@quiss.org> 
6  
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20
21 #include <stdlib.h>
22 #include <memory.h>
23 #include "../gfxfilter.h"
24 #include "../gfxtools.h"
25 #include "../types.h"
26 #include "../mem.h"
27
28 typedef struct _internal {
29     U8 alpha;
30 } internal_t;
31
32 static inline gfxcolor_t transform_color(internal_t*i, gfxcolor_t*col)
33 {
34     gfxcolor_t col2;
35     col2.r = col->r;
36     col2.g = col->g;
37     col2.b = col->b;
38     col2.a = (col->a * i->alpha)/255;
39     return col2;
40 }
41
42 static void maketransparent_stroke(gfxfilter_t*f, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit, gfxdevice_t*out)
43 {
44     internal_t*i = (internal_t*)f->internal;
45     gfxcolor_t color2 = transform_color(i, color);
46     out->stroke(out, line, width, &color2, cap_style, joint_style, miterLimit);
47 }
48 static void maketransparent_fill(gfxfilter_t*f, gfxline_t*line, gfxcolor_t*color, gfxdevice_t*out)
49 {
50     internal_t*i = (internal_t*)f->internal;
51     gfxcolor_t color2 = transform_color(i, color);
52     out->fill(out, line, &color2);
53 }
54 static void maketransparent_fillbitmap(gfxfilter_t*f, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform, gfxdevice_t*out)
55 {
56     internal_t*i = (internal_t*)f->internal;
57     gfximage_t img2;
58     img2.width = img->width;
59     img2.height = img->height;
60     img2.data = (gfxcolor_t*)rfx_alloc(img->width*img->height*4);
61     int x,y; 
62     for(y=0;y<img->height;y++)  {
63         gfxcolor_t*in = &img->data[y*img->width];
64         gfxcolor_t*out = &img2.data[y*img->width];
65         for(x=0;x<img->width;x++) {
66             out[x] = transform_color(i, &in[x]);
67         }
68     }
69     out->fillbitmap(out, line, &img2, matrix, cxform);
70     rfx_free(img2.data);
71 }
72 static void maketransparent_drawchar(gfxfilter_t*f, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix, gfxdevice_t*out)
73 {
74     internal_t*i = (internal_t*)f->internal;
75     gfxcolor_t color2 = transform_color(i, color);
76     out->drawchar(out, font, glyphnr, color, matrix);
77 }
78 static void maketransparent_fillgradient(gfxfilter_t*f, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix, gfxdevice_t*out)
79 {
80     internal_t*i = (internal_t*)f->internal;
81     
82     gfxgradient_t*g = 0, *prev = 0;
83
84     while(gradient) {
85         gfxgradient_t*n = rfx_alloc(sizeof(gfxgradient_t));
86         n->pos = gradient->pos;
87         n->color = transform_color(i, &gradient->color);
88         n->next = 0;
89         if(prev) {
90             prev->next = n;
91             prev = n;
92         } else {
93             g = prev = n;
94         }
95         gradient = gradient->next;
96     }
97
98     out->fillgradient(out, line, g, type, matrix);
99     gfxgradient_destroy(g);
100 }
101
102 void gfxfilter_maketransparent_init(gfxfilter_t*f, U8 alpha)
103 {
104     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
105     i->alpha = alpha;
106
107     memset(f, 0, sizeof(gfxfilter_t));
108     f->name = "maketransparent";
109     f->internal = i;
110     f->stroke = maketransparent_stroke;
111     f->fill = maketransparent_fill;
112     f->fillbitmap = maketransparent_fillbitmap;
113     f->fillgradient = maketransparent_fillgradient;
114     f->drawchar = maketransparent_drawchar;
115 }
116