fixed mem leak
[swftools.git] / lib / filters / rescale_images.c
blob83b98e39fb909bdc4a148e96151b12c9c8a0c1fc
1 /* rescale_images.c
3 Part of the swftools package.
5 Copyright (c) 2011 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <memory.h>
25 #include <string.h>
26 #include <math.h>
27 #include "../mem.h"
28 #include "../gfxdevice.h"
29 #include "../gfxfilter.h"
30 #include "../gfxtools.h"
31 #include "../gfximage.h"
32 #include "../log.h"
34 typedef struct _internal {
35 double config_subpixels;
36 } internal_t;
38 int rescale_images_setparameter(gfxfilter_t*dev, const char*key, const char*value, gfxdevice_t*out)
40 internal_t*i = (internal_t*)dev->internal;
41 if(!strcmp(key, "subpixels")) {
42 i->config_subpixels = atof(value);
44 return out->setparameter(out,key,value);
47 void rescale_images_startpage(gfxfilter_t*dev, int width, int height, gfxdevice_t*out)
49 internal_t*i = (internal_t*)dev->internal;
50 out->startpage(out,width,height);
53 void rescale_images_fillbitmap(gfxfilter_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform, gfxdevice_t*out)
55 internal_t*i = (internal_t*)dev->internal;
57 if(img->width<=0 || img->height<=0)
58 return;
60 int target_width = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
61 int target_height = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
63 int new_width = (int)ceil(target_width*i->config_subpixels);
64 int new_height = (int)ceil(target_height*i->config_subpixels);
66 if(new_width<=0)
67 new_width = 1;
68 if(new_height<=0)
69 new_height = 1;
71 if(new_width < img->width || new_height < img->height) {
72 msg("<verbose> Scaling %dx%d image to %dx%d", img->width, img->height, new_width, new_height);
73 gfximage_t*new_image = gfximage_rescale(img, new_width, new_height);
74 gfxmatrix_t m = *matrix;
75 m.m00 = (m.m00 * img->width) / new_width;
76 m.m01 = (m.m01 * img->width) / new_width;
77 m.m10 = (m.m10 * img->height) / new_height;
78 m.m11 = (m.m11 * img->height) / new_height;
79 out->fillbitmap(out, line, img, &m, cxform);
80 gfximage_free(new_image);
81 } else {
82 out->fillbitmap(out, line, img, matrix, cxform);
86 gfxresult_t* rescale_images_finish(gfxfilter_t*dev, gfxdevice_t*out)
88 internal_t*i = (internal_t*)dev->internal;
89 free(i);dev->internal = 0;
90 return out->finish(out);
93 void gfxfilter_rescale_images_init(gfxfilter_t*f)
95 memset(f, 0, sizeof(gfxfilter_t));
96 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
98 i->config_subpixels = 1.0;
100 f->internal = i;
101 f->name = "rescale_images";
102 f->type = gfxfilter_onepass;
104 f->fillbitmap = rescale_images_fillbitmap;
105 f->finish = rescale_images_finish;