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