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