911bc477bad163493bbcc3c89305f83cdc4e790c
[swftools.git] / lib / devices / ops.c
1 /* ops.c
2
3    Part of the swftools package.
4
5    Copyright (c) 2006 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 <stdio.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <memory.h>
26 #include "../types.h"
27 #include "../mem.h"
28 #include "../gfxdevice.h"
29 #include "../gfxtools.h"
30
31 typedef struct _internal {
32     gfxdevice_t*out;
33     U8 alpha;
34 } internal_t;
35
36 static int verbose = 1;
37 static void dbg(char*format, ...)
38 {
39     if(!verbose)
40         return;
41     char buf[1024];
42     int l;
43     va_list arglist;
44     va_start(arglist, format);
45     vsprintf(buf, format, arglist);
46     va_end(arglist);
47     l = strlen(buf);
48     while(l && buf[l-1]=='\n') {
49         buf[l-1] = 0;
50         l--;
51     }
52     printf("(device-ops) %s\n", buf);
53     fflush(stdout);
54 }
55
56 inline gfxcolor_t transform_color(internal_t*i, gfxcolor_t*col)
57 {
58     gfxcolor_t col2;
59     /*col2.r = (col->r * i->alpha)>>8;
60     col2.g = (col->g * i->alpha)>>8;
61     col2.b = (col->b * i->alpha)>>8;*/
62     col2.r = col->r;
63     col2.g = col->g;
64     col2.b = col->b;
65     col2.a = (col->a * i->alpha)>>8;
66     return col2;
67 }
68
69 int ops_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
70 {
71     internal_t*i = (internal_t*)dev->internal;
72     return i->out->setparameter(i->out,key,value);
73 }
74
75 void ops_startpage(struct _gfxdevice*dev, int width, int height)
76 {
77     internal_t*i = (internal_t*)dev->internal;
78     i->out->startpage(i->out,width,height);
79 }
80
81 void ops_startclip(struct _gfxdevice*dev, gfxline_t*line)
82 {
83     internal_t*i = (internal_t*)dev->internal;
84     i->out->startclip(i->out,line);
85 }
86
87 void ops_endclip(struct _gfxdevice*dev)
88 {
89     internal_t*i = (internal_t*)dev->internal;
90     i->out->endclip(i->out);
91 }
92
93 void ops_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
94 {
95     internal_t*i = (internal_t*)dev->internal;
96     gfxcolor_t color2 = transform_color(i, color);
97     i->out->stroke(i->out, line, width, &color2, cap_style, joint_style, miterLimit);
98 }
99
100 void ops_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
101 {
102     internal_t*i = (internal_t*)dev->internal;
103     gfxcolor_t color2 = transform_color(i, color);
104     i->out->fill(i->out, line, &color2);
105 }
106
107 void ops_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
108 {
109     internal_t*i = (internal_t*)dev->internal;
110     gfximage_t img2;
111     img2.width = img->width;
112     img2.height = img->height;
113     img2.data = malloc(img->width*img->height*4);
114     int x,y; 
115     for(y=0;y<img->height;y++)  {
116         gfxcolor_t*in = &img->data[y*img->width];
117         gfxcolor_t*out = &img2.data[y*img->width];
118         for(x=0;x<img->width;x++) {
119             out[x] = transform_color(i, &in[x]);
120         }
121     }
122     i->out->fillbitmap(i->out, line, &img2, matrix, cxform);
123     free(img2.data);
124 }
125
126 void ops_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
127 {
128     internal_t*i = (internal_t*)dev->internal;
129     i->out->fillgradient(i->out, line, gradient, type, matrix);
130 }
131
132 void ops_addfont(struct _gfxdevice*dev, gfxfont_t*font)
133 {
134     internal_t*i = (internal_t*)dev->internal;
135     i->out->addfont(i->out, font);
136 }
137
138 void ops_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
139 {
140     internal_t*i = (internal_t*)dev->internal;
141     gfxcolor_t color2 = transform_color(i, color);
142     i->out->drawchar(i->out, font, glyphnr, color, matrix);
143 }
144
145 void ops_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
146 {
147     internal_t*i = (internal_t*)dev->internal;
148     i->out->drawlink(i->out, line, action);
149 }
150
151 void ops_endpage(struct _gfxdevice*dev)
152 {
153     internal_t*i = (internal_t*)dev->internal;
154     i->out->endpage(i->out);
155 }
156
157 gfxresult_t* ops_finish(struct _gfxdevice*dev)
158 {
159     free(dev->internal);dev->internal = 0;
160     return 0;
161 }
162
163 void gfxdevice_ops_init(gfxdevice_t*dev, gfxdevice_t*out, U8 alpha)
164 {
165     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
166     memset(dev, 0, sizeof(gfxdevice_t));
167
168     dev->name = "ops";
169
170     dev->internal = i;
171
172     dev->setparameter = ops_setparameter;
173     dev->startpage = ops_startpage;
174     dev->startclip = ops_startclip;
175     dev->endclip = ops_endclip;
176     dev->stroke = ops_stroke;
177     dev->fill = ops_fill;
178     dev->fillbitmap = ops_fillbitmap;
179     dev->fillgradient = ops_fillgradient;
180     dev->addfont = ops_addfont;
181     dev->drawchar = ops_drawchar;
182     dev->drawlink = ops_drawlink;
183     dev->endpage = ops_endpage;
184     dev->finish = ops_finish;
185
186     i->out = out;
187     i->alpha = alpha;
188 }
189