| 1 | /* perspective.c |
| 2 | * Copyright (C) 2008 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 | |
| 21 | #include "frei0r.h" |
| 22 | |
| 23 | |
| 24 | void sub_vec2( f0r_param_position_t* r, f0r_param_position_t* a, f0r_param_position_t* b ) |
| 25 | { |
| 26 | r->x = a->x - b->x; |
| 27 | r->y = a->y - b->y; |
| 28 | } |
| 29 | void add_vec2( f0r_param_position_t* r, f0r_param_position_t* a, f0r_param_position_t* b ) |
| 30 | { |
| 31 | r->x = a->x + b->x; |
| 32 | r->y = a->y + b->y; |
| 33 | } |
| 34 | |
| 35 | void mul_vec2( f0r_param_position_t* r, f0r_param_position_t* a, double scalar ) |
| 36 | { |
| 37 | r->x = a->x * scalar; |
| 38 | r->y = a->y * scalar; |
| 39 | } |
| 40 | |
| 41 | void get_pixel_position( f0r_param_position_t* r, f0r_param_position_t* t, f0r_param_position_t* b, f0r_param_position_t* tl, f0r_param_position_t* bl,f0r_param_position_t* in ) |
| 42 | { |
| 43 | f0r_param_position_t t_x; |
| 44 | f0r_param_position_t b_x; |
| 45 | f0r_param_position_t k; |
| 46 | mul_vec2( &t_x, t, in->x ); |
| 47 | mul_vec2( &b_x, b, in->x ); |
| 48 | |
| 49 | add_vec2( &t_x, &t_x, tl ); |
| 50 | add_vec2( &b_x, &b_x, bl ); |
| 51 | |
| 52 | sub_vec2( &k, &b_x, &t_x ); |
| 53 | mul_vec2( &k, &k, in->y ); |
| 54 | |
| 55 | add_vec2( r, &k, &t_x ); |
| 56 | |
| 57 | } |
| 58 | |
| 59 | |
| 60 | #include <math.h> |
| 61 | #include <stdlib.h> |
| 62 | |
| 63 | typedef struct perspective_instance { |
| 64 | int w, h; |
| 65 | f0r_param_position_t tl; |
| 66 | f0r_param_position_t tr; |
| 67 | f0r_param_position_t bl; |
| 68 | f0r_param_position_t br; |
| 69 | } perspective_instance_t; |
| 70 | |
| 71 | |
| 72 | int f0r_init() |
| 73 | { |
| 74 | return 1; |
| 75 | } |
| 76 | void f0r_deinit() |
| 77 | { /* empty */ } |
| 78 | |
| 79 | void f0r_get_plugin_info( f0r_plugin_info_t* info ) |
| 80 | { |
| 81 | info->name = "Perspective"; |
| 82 | info->author = "Richard Spindler"; |
| 83 | info->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 84 | info->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 85 | info->frei0r_version = FREI0R_MAJOR_VERSION; |
| 86 | info->major_version = 0; |
| 87 | info->minor_version = 1; |
| 88 | info->num_params = 4; |
| 89 | info->explanation = "Distorts the image for a pseudo perspective"; |
| 90 | |
| 91 | } |
| 92 | void f0r_get_param_info( f0r_param_info_t* info, int param_index ) |
| 93 | { |
| 94 | switch ( param_index ) { |
| 95 | case 0: |
| 96 | info->name = "Top Left"; |
| 97 | info->type = F0R_PARAM_POSITION; |
| 98 | info->explanation = ""; |
| 99 | break; |
| 100 | case 1: |
| 101 | info->name = "Top Right"; |
| 102 | info->type = F0R_PARAM_POSITION; |
| 103 | info->explanation = ""; |
| 104 | break; |
| 105 | case 2: |
| 106 | info->name = "Bottom Left"; |
| 107 | info->type = F0R_PARAM_POSITION; |
| 108 | info->explanation = ""; |
| 109 | break; |
| 110 | case 3: |
| 111 | info->name = "Bottom Right"; |
| 112 | info->type = F0R_PARAM_POSITION; |
| 113 | info->explanation = ""; |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 119 | { |
| 120 | perspective_instance_t* inst = (perspective_instance_t*)calloc(1, sizeof(*inst)); |
| 121 | inst->w = width; |
| 122 | inst->h = height; |
| 123 | inst->tl.x = 0.0; |
| 124 | inst->tl.y = 0.0; |
| 125 | inst->tr.x = 1.0; |
| 126 | inst->tr.y = 0.0; |
| 127 | inst->bl.x = 0.0; |
| 128 | inst->bl.y = 1.0; |
| 129 | inst->br.x = 1.0; |
| 130 | inst->br.y = 1.0; |
| 131 | return (f0r_instance_t)inst; |
| 132 | } |
| 133 | void f0r_destruct(f0r_instance_t instance) |
| 134 | { |
| 135 | perspective_instance_t* inst = (perspective_instance_t*)instance; |
| 136 | free(inst); |
| 137 | } |
| 138 | void f0r_set_param_value(f0r_instance_t instance, |
| 139 | f0r_param_t param, int param_index) |
| 140 | { |
| 141 | perspective_instance_t* inst = (perspective_instance_t*)instance; |
| 142 | switch ( param_index ) { |
| 143 | case 0: |
| 144 | inst->tl = *((f0r_param_position_t*)param); |
| 145 | break; |
| 146 | case 1: |
| 147 | inst->tr = *((f0r_param_position_t*)param); |
| 148 | break; |
| 149 | case 2: |
| 150 | inst->bl = *((f0r_param_position_t*)param); |
| 151 | break; |
| 152 | case 3: |
| 153 | inst->br = *((f0r_param_position_t*)param); |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | void f0r_get_param_value(f0r_instance_t instance, |
| 158 | f0r_param_t param, int param_index) |
| 159 | { |
| 160 | perspective_instance_t* inst = (perspective_instance_t*)instance; |
| 161 | switch ( param_index ) { |
| 162 | case 0: |
| 163 | *((f0r_param_position_t*)param) = inst->tl; |
| 164 | break; |
| 165 | case 1: |
| 166 | *((f0r_param_position_t*)param) = inst->tr; |
| 167 | break; |
| 168 | case 2: |
| 169 | *((f0r_param_position_t*)param) = inst->bl; |
| 170 | break; |
| 171 | case 3: |
| 172 | *((f0r_param_position_t*)param) = inst->br; |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void f0r_update(f0r_instance_t instance, double time, |
| 178 | const uint32_t* inframe, uint32_t* outframe) |
| 179 | { |
| 180 | perspective_instance_t* inst = (perspective_instance_t*)instance; |
| 181 | |
| 182 | uint32_t* dst = outframe; |
| 183 | const uint32_t* src = inframe; |
| 184 | int len = inst->w * inst->h; |
| 185 | |
| 186 | int i; |
| 187 | for ( i = 0; i < len; i++ ) { |
| 188 | *dst = 0x00000000; |
| 189 | dst++; |
| 190 | } |
| 191 | dst = outframe; |
| 192 | |
| 193 | int w = inst->w; |
| 194 | int h = inst->h; |
| 195 | |
| 196 | int rx; |
| 197 | int ry; |
| 198 | int x; |
| 199 | int y; |
| 200 | f0r_param_position_t top; |
| 201 | f0r_param_position_t bot; |
| 202 | f0r_param_position_t r; |
| 203 | f0r_param_position_t in; |
| 204 | sub_vec2( &top, &inst->tr, &inst->tl ); |
| 205 | sub_vec2( &bot, &inst->br, &inst->bl ); |
| 206 | for( y = 0; y < h; y++ ) { |
| 207 | for ( x = 0; x < w; x++ ) { |
| 208 | in.x = (double)x / (double)w; |
| 209 | in.y = (double)y / (double)h; |
| 210 | get_pixel_position( &r, &top, &bot, &inst->tl, &inst->bl, &in ); |
| 211 | rx = lrint(r.x * (float)w); |
| 212 | ry = lrint(r.y * (float)h); |
| 213 | if ( rx < 0 || rx >= w || ry < 0 || ry >= h ) { |
| 214 | src++; |
| 215 | continue; |
| 216 | } |
| 217 | dst[rx + w * ry] = *src; |
| 218 | src++; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | } |
| 223 |