added keepratio parameter
[swftools.git] / lib / devices / rescale.c
1 /* rescale.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 <math.h>
27 #include "../types.h"
28 #include "../mem.h"
29 #include "../gfxdevice.h"
30 #include "../gfxtools.h"
31
32 typedef struct _internal {
33     gfxdevice_t*out;
34     int origwidth;
35     int origheight;
36     int targetwidth;
37     int targetheight;
38     int centerx, centery;
39     gfxmatrix_t matrix;
40     double zoomwidth;
41     int keepratio;
42 } internal_t;
43
44 static int verbose = 1;
45 static void dbg(char*format, ...)
46 {
47     if(!verbose)
48         return;
49     char buf[1024];
50     int l;
51     va_list arglist;
52     va_start(arglist, format);
53     vsprintf(buf, format, arglist);
54     va_end(arglist);
55     l = strlen(buf);
56     while(l && buf[l-1]=='\n') {
57         buf[l-1] = 0;
58         l--;
59     }
60     printf("(device-rescale) %s\n", buf);
61     fflush(stdout);
62 }
63
64 char gfxline_isRect(gfxline_t*line)
65 {
66     return 1;
67     if(!line)
68         return 0;
69     double x1=line->x,y1=line->x,x2=line->x,y2=line->y;
70     int nx1=0,nx2=0,ny1=0,ny2=0;
71     line = line->next;
72     while(line) {
73         if(line->type != gfx_lineTo)
74             return 0;
75         if(line->x < x1) {
76             x1 = line->x;
77             nx1++;
78         } else if(line->y < y1) {
79             y1 = line->y;
80             ny1++;
81         } else if(line->x > x2) {
82             x2 = line->x;
83             nx2++;
84         } else if(line->y > y1) {
85             y2 = line->y;
86             ny2++;
87         }
88         line = line->next;
89     }
90     return (nx1+nx2)==1 && (ny1+ny2)==1;
91 }
92
93 gfxline_t*transformgfxline(internal_t*i, gfxline_t*line)
94 {
95     /* special case: transformed rectangle 
96     if(gfxline_isRect(line)) {
97         gfxbbox_t bbox = gfxline_getbbox(line);
98         if(fabs(bbox.xmin)<0.1 && fabs(bbox.ymin)<0.1 &&
99            fabs(bbox.ymax-i->origwidth)<0.1 && fabs(bbox.ymax-i->origheight)<0.1) {
100             gfxline_t r[5];
101             r[0].x = 0;             r[0].y = 0;           r[0].type = gfx_moveTo;r[0].next = &r[1];
102             r[1].x = i->targetwidth;r[1].y = 0;           r[1].type = gfx_lineTo;r[1].next = &r[2];
103             r[2].x = i->targetwidth;r[2].y = i->targetheight;r[2].type = gfx_lineTo;r[2].next = &r[3];
104             r[3].x = 0;             r[3].y = i->targetheight;r[3].type = gfx_lineTo;r[3].next = &r[4];
105             r[4].x = 0;             r[4].y = 0;           r[4].type = gfx_lineTo;r[4].next = 0;
106             return gfxline_clone(r);
107         }
108     } */
109     gfxline_t*line2 = gfxline_clone(line);
110     gfxline_transform(line2, &i->matrix);
111     return line2;
112 }
113
114 int rescale_setparameter(gfxdevice_t*dev, const char*key, const char*value)
115 {
116     internal_t*i = (internal_t*)dev->internal;
117     if(!strcmp(key, "keepratio")) {
118         i->keepratio = atoi(value);
119         return 1;
120     } else return i->out->setparameter(i->out,key,value);
121 }
122
123 void rescale_startpage(gfxdevice_t*dev, int width, int height)
124 {
125     internal_t*i = (internal_t*)dev->internal;
126
127     i->origwidth = width;
128     i->origheight = height;
129
130     if(i->keepratio) {
131         double rx = (double)i->targetwidth / (double)width;
132         double ry = (double)i->targetheight / (double)height;
133         if(rx<ry) {
134             i->matrix.m00 = rx;
135             i->matrix.m11 = rx;
136             i->matrix.tx = 0;
137             if(i->centery) {
138                 i->matrix.ty = (i->targetheight - height*rx) / 2;
139             }
140         } else {
141             i->matrix.m00 = ry;
142             i->matrix.m11 = ry;
143             if(i->centerx) {
144                 i->matrix.tx = (i->targetwidth - width*ry) / 2;
145             }
146             i->matrix.ty = 0;
147         }
148     } else {
149         i->matrix.m00 = (double)i->targetwidth / (double)width;
150         i->matrix.m11 = (double)i->targetheight / (double)height;
151     }
152     i->out->startpage(i->out,i->targetwidth,i->targetheight);
153 }
154
155 void rescale_startclip(gfxdevice_t*dev, gfxline_t*line)
156 {
157     internal_t*i = (internal_t*)dev->internal;
158     gfxline_t*line2 = transformgfxline(i, line);
159     i->out->startclip(i->out,line2);
160     gfxline_free(line2);
161 }
162
163 void rescale_endclip(gfxdevice_t*dev)
164 {
165     internal_t*i = (internal_t*)dev->internal;
166     i->out->endclip(i->out);
167 }
168
169 void rescale_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
170 {
171     internal_t*i = (internal_t*)dev->internal;
172     gfxline_t*line2 = transformgfxline(i, line);
173     i->out->stroke(i->out, line2, width*i->zoomwidth, color, cap_style, joint_style, miterLimit);
174     gfxline_free(line2);
175 }
176
177 void rescale_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
178 {
179     internal_t*i = (internal_t*)dev->internal;
180     gfxline_t*line2 = transformgfxline(i, line);
181     i->out->fill(i->out, line2, color);
182     gfxline_free(line2);
183 }
184
185 void rescale_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
186 {
187     internal_t*i = (internal_t*)dev->internal;
188     gfxline_t*line2 = transformgfxline(i, line);
189     gfxmatrix_t m2;
190     gfxmatrix_multiply(&i->matrix, matrix, &m2);
191     i->out->fillbitmap(i->out, line2, img, &m2, cxform);
192     gfxline_free(line2);
193 }
194
195 void rescale_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
196 {
197     internal_t*i = (internal_t*)dev->internal;
198     gfxline_t*line2 = transformgfxline(i, line);
199     i->out->fillgradient(i->out, line, gradient, type, matrix);
200     gfxline_free(line2);
201 }
202
203 void rescale_addfont(gfxdevice_t*dev, gfxfont_t*font)
204 {
205     internal_t*i = (internal_t*)dev->internal;
206     i->out->addfont(i->out, font);
207 }
208
209 void rescale_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
210 {
211     internal_t*i = (internal_t*)dev->internal;
212     gfxmatrix_t m2;
213     gfxmatrix_multiply(&i->matrix, matrix, &m2);
214     i->out->drawchar(i->out, font, glyphnr, color, &m2);
215 }
216
217 void rescale_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action)
218 {
219     internal_t*i = (internal_t*)dev->internal;
220     gfxline_t*line2 = transformgfxline(i, line);
221     i->out->drawlink(i->out, line, action);
222     gfxline_free(line2);
223 }
224
225 void rescale_endpage(gfxdevice_t*dev)
226 {
227     internal_t*i = (internal_t*)dev->internal;
228     i->out->endpage(i->out);
229 }
230
231 gfxresult_t* rescale_finish(gfxdevice_t*dev)
232 {
233     internal_t*i = (internal_t*)dev->internal;
234     gfxdevice_t*out = i->out;
235     free(dev->internal);dev->internal = 0;i=0;
236     return out->finish(out);
237 }
238
239 void gfxdevice_rescale_init(gfxdevice_t*dev, gfxdevice_t*out, int width, int height)
240 {
241     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
242     memset(dev, 0, sizeof(gfxdevice_t));
243
244     dev->name = "rescale";
245
246     dev->internal = i;
247
248     dev->setparameter = rescale_setparameter;
249     dev->startpage = rescale_startpage;
250     dev->startclip = rescale_startclip;
251     dev->endclip = rescale_endclip;
252     dev->stroke = rescale_stroke;
253     dev->fill = rescale_fill;
254     dev->fillbitmap = rescale_fillbitmap;
255     dev->fillgradient = rescale_fillgradient;
256     dev->addfont = rescale_addfont;
257     dev->drawchar = rescale_drawchar;
258     dev->drawlink = rescale_drawlink;
259     dev->endpage = rescale_endpage;
260     dev->finish = rescale_finish;
261
262     gfxmatrix_unit(&i->matrix);
263     i->targetwidth = width;
264     i->targetheight = height;
265     i->zoomwidth = 1.0;
266     i->centerx = 1;
267
268     i->out = out;
269 }
270