Debian Codegraph @

src/filter/scale0tilt/scale0tilt.c

raw symbols SCIP
1 /* scale0tilt.c
2 * Copyright (C) 2007 Richard Spindler ([email protected])
3 * This file is a Frei0r plugin.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <math.h>
21 #include "frei0r.h"
22 #include <gavl/gavl.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #define EPSILON 1e-6
27
28 typedef struct scale0tilt_instance {
29 double cl, ct, cr, cb;
30 double sx, sy;
31 double tx, ty;
32 int w, h;
33 gavl_video_scaler_t* video_scaler;
34 gavl_video_frame_t* frame_src;
35 gavl_video_frame_t* frame_dst;
36 int do_scale;
37 gavl_video_format_t format_src;
38 gavl_video_frame_t* padded;
39 } scale0tilt_instance_t;
40
41 void update_scaler( scale0tilt_instance_t* inst )
42 {
43 float dst_x, dst_y, dst_w, dst_h;
44 float src_x, src_y, src_w, src_h;
45
46 inst->do_scale = 1;
47 src_x = inst->w * inst->cl;
48 src_y = inst->h * inst->ct;
49 src_w = inst->w * (1.0 - inst->cl - inst->cr );
50 src_h = inst->h * (1.0 - inst->ct - inst->cb );
51
52 dst_x = inst->w * inst->cl * inst->sx + inst->tx * inst->w;
53 dst_y = inst->h * inst->ct * inst->sy + inst->ty * inst->h;
54 dst_w = inst->w * (1.0 - inst->cl - inst->cr) * inst->sx;
55 dst_h = inst->h * (1.0 - inst->ct - inst->cb) * inst->sy;
56
57 if((dst_w < EPSILON) || (dst_h < EPSILON) ||
58 (src_w < EPSILON) || (src_h < EPSILON)) {
59 inst->do_scale = 0;
60 return;
61 }
62
63 if ( dst_x + dst_w > inst->w ) {
64 src_w = src_w * ( (inst->w-dst_x) / dst_w );
65 dst_w = inst->w - dst_x;
66 }
67 if ( dst_y + dst_h > inst->h ) {
68 src_h = src_h * ( (inst->h-dst_y) / dst_h );
69 dst_h = inst->h - dst_y;
70 }
71 if ( dst_x < 0 ) {
72 src_x = src_x - dst_x * ( src_w / dst_w );
73 src_w = src_w * ( (dst_w+dst_x) / dst_w );
74 dst_w = dst_w + dst_x;
75 dst_x = 0;
76 }
77 if ( dst_y < 0 ) {
78 src_y = src_y - dst_y * ( src_h / dst_h );
79 src_h = src_h * ( (dst_h+dst_y) / dst_h );
80 dst_h = dst_h + dst_y;
81 dst_y = 0;
82 }
83
84 if((dst_w < EPSILON) || (dst_h < EPSILON) ||
85 (src_w < EPSILON) || (src_h < EPSILON)) {
86 inst->do_scale = 0;
87 return;
88 }
89
90 gavl_video_options_t* options = gavl_video_scaler_get_options( inst->video_scaler );
91
92 gavl_video_format_t format_dst;
93
94 memset(&inst->format_src, 0, sizeof(inst->format_src));
95 memset(&format_dst, 0, sizeof(format_dst));
96
97 format_dst.frame_width = inst->w;
98 format_dst.frame_height = inst->h;
99 format_dst.image_width = inst->w;
100 format_dst.image_height = inst->h;
101 format_dst.pixel_width = 1;
102 format_dst.pixel_height = 1;
103 format_dst.pixelformat = GAVL_RGBA_32;
104
105 inst->format_src.frame_width = inst->w;
106 inst->format_src.frame_height = inst->h;
107 inst->format_src.image_width = inst->w;
108 inst->format_src.image_height = inst->h;
109 inst->format_src.pixel_width = 1;
110 inst->format_src.pixel_height = 1;
111 inst->format_src.pixelformat = GAVL_RGBA_32;
112
113 gavl_rectangle_f_t src_rect;
114 gavl_rectangle_i_t dst_rect;
115
116 src_rect.x = src_x;
117 src_rect.y = src_y;
118 src_rect.w = src_w;
119 src_rect.h = src_h;
120
121 dst_rect.x = lroundf(dst_x);
122 dst_rect.y = lroundf(dst_y);
123 dst_rect.w = lroundf(dst_w);
124 dst_rect.h = lroundf(dst_h);
125
126 gavl_video_options_set_rectangles( options, &src_rect, &dst_rect );
127 gavl_video_scaler_init( inst->video_scaler, &inst->format_src, &format_dst );
128 }
129
130 int f0r_init()
131 {
132 return 1;
133 }
134 void f0r_deinit()
135 { /* empty */ }
136
137 void f0r_get_plugin_info( f0r_plugin_info_t* info )
138 {
139 info->name = "Scale0Tilt";
140 info->author = "Richard Spindler";
141 info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
142 info->color_model = F0R_COLOR_MODEL_RGBA8888;
143 info->frei0r_version = FREI0R_MAJOR_VERSION;
144 info->major_version = 0;
145 info->minor_version = 1;
146 info->num_params = 8;
147 info->explanation = "Scales, Tilts and Crops an Image";
148
149 }
150 void f0r_get_param_info( f0r_param_info_t* info, int param_index )
151 {
152 switch ( param_index ) {
153 case 0:
154 info->name = "Clip left";
155 info->type = F0R_PARAM_DOUBLE;
156 info->explanation = "";
157 break;
158 case 1:
159 info->name = "Clip right";
160 info->type = F0R_PARAM_DOUBLE;
161 info->explanation = "";
162 break;
163 case 2:
164 info->name = "Clip top";
165 info->type = F0R_PARAM_DOUBLE;
166 info->explanation = "";
167 break;
168 case 3:
169 info->name = "Clip bottom";
170 info->type = F0R_PARAM_DOUBLE;
171 info->explanation = "";
172 break;
173 case 4:
174 info->name = "Scale X";
175 info->type = F0R_PARAM_DOUBLE;
176 info->explanation = "";
177 break;
178 case 5:
179 info->name = "Scale Y";
180 info->type = F0R_PARAM_DOUBLE;
181 info->explanation = "";
182 break;
183 case 6:
184 info->name = "Tilt X";
185 info->type = F0R_PARAM_DOUBLE;
186 info->explanation = "";
187 break;
188 case 7:
189 info->name = "Tilt Y";
190 info->type = F0R_PARAM_DOUBLE;
191 info->explanation = "";
192 break;
193 }
194 }
195
196 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
197 {
198 scale0tilt_instance_t* inst = (scale0tilt_instance_t*)calloc(1, sizeof(*inst));
199 inst->w = width;
200 inst->h = height;
201 inst->sx = 1.0;
202 inst->sy = 1.0;
203 inst->video_scaler = gavl_video_scaler_create();
204 inst->frame_src = gavl_video_frame_create( 0 );
205 inst->frame_dst = gavl_video_frame_create( 0 );
206 inst->frame_src->strides[0] = width * 4;
207 inst->frame_dst->strides[0] = width * 4;
208 update_scaler(inst);
209 if ( inst->frame_src->strides[0] % 16 )
210 inst->padded = gavl_video_frame_create( &inst->format_src );
211 return (f0r_instance_t)inst;
212 }
213 void f0r_destruct(f0r_instance_t instance)
214 {
215 scale0tilt_instance_t* inst = (scale0tilt_instance_t*)instance;
216 gavl_video_scaler_destroy(inst->video_scaler);
217 gavl_video_frame_null( inst->frame_src );
218 gavl_video_frame_destroy( inst->frame_src );
219 gavl_video_frame_null( inst->frame_dst );
220 gavl_video_frame_destroy( inst->frame_dst );
221 if ( inst->padded )
222 gavl_video_frame_destroy( inst->padded );
223 free(instance);
224 }
225 void f0r_set_param_value(f0r_instance_t instance,
226 f0r_param_t param, int param_index)
227 {
228 scale0tilt_instance_t* inst = (scale0tilt_instance_t*)instance;
229 switch ( param_index ) {
230 case 0:
231 inst->cl = *((double*)param);
232 break;
233 case 1:
234 inst->cr = *((double*)param);
235 break;
236 case 2:
237 inst->ct = *((double*)param);
238 break;
239 case 3:
240 inst->cb = *((double*)param);
241 break;
242 case 4:
243 inst->sx = *((double*)param) * 2.0;
244 break;
245 case 5:
246 inst->sy = *((double*)param) * 2.0;
247 break;
248 case 6:
249 inst->tx = *((double*)param) * 2.0 - 1.0;
250 break;
251 case 7:
252 inst->ty = *((double*)param) * 2.0 - 1.0;
253 break;
254 }
255 update_scaler( inst );
256 }
257 void f0r_get_param_value(f0r_instance_t instance,
258 f0r_param_t param, int param_index)
259 {
260 scale0tilt_instance_t* inst = (scale0tilt_instance_t*)instance;
261 switch ( param_index ) {
262 case 0:
263 *((double*)param) = inst->cl;
264 break;
265 case 1:
266 *((double*)param) = inst->cr;
267 break;
268 case 2:
269 *((double*)param) = inst->ct;
270 break;
271 case 3:
272 *((double*)param) = inst->cb;
273 break;
274 case 4:
275 *((double*)param) = inst->sx / 2.0;
276 break;
277 case 5:
278 *((double*)param) = inst->sy / 2.0;
279 break;
280 case 6:
281 *((double*)param) = (inst->tx + 1.0) / 2.0;
282 break;
283 case 7:
284 *((double*)param) = (inst->ty + 1.0) / 2.0;
285 break;
286 }
287 }
288 void f0r_update(f0r_instance_t instance, double time,
289 const uint32_t* inframe, uint32_t* outframe)
290 {
291 scale0tilt_instance_t* inst = (scale0tilt_instance_t*)instance;
292 gavl_video_frame_t* frame_src = inst->frame_src;
293 inst->frame_src->planes[0] = (uint8_t *)inframe;
294 inst->frame_dst->planes[0] = (uint8_t *)outframe;
295 int len = inst->w * inst->h;
296 int i;
297 for ( i = 0; i < len; i++ ) {
298 outframe[i] = 0;
299 }
300 if ( inst->do_scale ) {
301 if ( inst->padded ) {
302 gavl_video_frame_copy( &inst->format_src, inst->padded, frame_src );
303 frame_src = inst->padded;
304 }
305 gavl_video_scaler_scale( inst->video_scaler, frame_src, inst->frame_dst );
306 }
307 }
308