| 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"cxx . . $ `<file>/include/frei0r.h`/ |
| 22 | |
| 23 | |
| 24 | void sub_vec2cxx . . $ sub_vec2(745f637666904c2d).( f0r_param_position_tcxx . . $ f0r_param_position_t#* r, f0r_param_position_tcxx . . $ f0r_param_position_t#* a, f0r_param_position_tcxx . . $ f0r_param_position_t#* b ) |
| 25 | { |
| 26 | r->xcxx . . $ f0r_param_position#x. = a->xcxx . . $ f0r_param_position#x. - b->xcxx . . $ f0r_param_position#x.; |
| 27 | r->ycxx . . $ f0r_param_position#y. = a->ycxx . . $ f0r_param_position#y. - b->ycxx . . $ f0r_param_position#y.; |
| 28 | } |
| 29 | void add_vec2cxx . . $ add_vec2(745f637666904c2d).( f0r_param_position_tcxx . . $ f0r_param_position_t#* r, f0r_param_position_tcxx . . $ f0r_param_position_t#* a, f0r_param_position_tcxx . . $ f0r_param_position_t#* b ) |
| 30 | { |
| 31 | r->xcxx . . $ f0r_param_position#x. = a->xcxx . . $ f0r_param_position#x. + b->xcxx . . $ f0r_param_position#x.; |
| 32 | r->ycxx . . $ f0r_param_position#y. = a->ycxx . . $ f0r_param_position#y. + b->ycxx . . $ f0r_param_position#y.; |
| 33 | } |
| 34 | |
| 35 | void mul_vec2cxx . . $ mul_vec2(bdabcd9c5f983a1c).( f0r_param_position_tcxx . . $ f0r_param_position_t#* r, f0r_param_position_tcxx . . $ f0r_param_position_t#* a, double scalar ) |
| 36 | { |
| 37 | r->xcxx . . $ f0r_param_position#x. = a->xcxx . . $ f0r_param_position#x. * scalar; |
| 38 | r->ycxx . . $ f0r_param_position#y. = a->ycxx . . $ f0r_param_position#y. * scalar; |
| 39 | } |
| 40 | |
| 41 | void get_pixel_positioncxx . . $ get_pixel_position(1a1ea8ca7c28108b).( f0r_param_position_tcxx . . $ f0r_param_position_t#* r, f0r_param_position_tcxx . . $ f0r_param_position_t#* t, f0r_param_position_tcxx . . $ f0r_param_position_t#* b, f0r_param_position_tcxx . . $ f0r_param_position_t#* tl, f0r_param_position_tcxx . . $ f0r_param_position_t#* bl,f0r_param_position_tcxx . . $ f0r_param_position_t#* in ) |
| 42 | { |
| 43 | f0r_param_position_tcxx . . $ f0r_param_position_t# t_x; |
| 44 | f0r_param_position_tcxx . . $ f0r_param_position_t# b_x; |
| 45 | f0r_param_position_tcxx . . $ f0r_param_position_t# k; |
| 46 | mul_vec2cxx . . $ mul_vec2(bdabcd9c5f983a1c).( &t_x, t, in->xcxx . . $ f0r_param_position#x. ); |
| 47 | mul_vec2cxx . . $ mul_vec2(bdabcd9c5f983a1c).( &b_x, b, in->xcxx . . $ f0r_param_position#x. ); |
| 48 | |
| 49 | add_vec2cxx . . $ add_vec2(745f637666904c2d).( &t_x, &t_x, tl ); |
| 50 | add_vec2cxx . . $ add_vec2(745f637666904c2d).( &b_x, &b_x, bl ); |
| 51 | |
| 52 | sub_vec2cxx . . $ sub_vec2(745f637666904c2d).( &k, &b_x, &t_x ); |
| 53 | mul_vec2cxx . . $ mul_vec2(bdabcd9c5f983a1c).( &k, &k, in->ycxx . . $ f0r_param_position#y. ); |
| 54 | |
| 55 | add_vec2cxx . . $ add_vec2(745f637666904c2d).( r, &k, &t_x ); |
| 56 | |
| 57 | } |
| 58 | |
| 59 | |
| 60 | #include <math.h> |
| 61 | #include <stdlib.h> |
| 62 | |
| 63 | typedef struct perspective_instancecxx . . $ perspective_instance# { |
| 64 | int wcxx . . $ perspective_instance#w., hcxx . . $ perspective_instance#h.; |
| 65 | f0r_param_position_tcxx . . $ f0r_param_position_t# tlcxx . . $ perspective_instance#tl.; |
| 66 | f0r_param_position_tcxx . . $ f0r_param_position_t# trcxx . . $ perspective_instance#tr.; |
| 67 | f0r_param_position_tcxx . . $ f0r_param_position_t# blcxx . . $ perspective_instance#bl.; |
| 68 | f0r_param_position_tcxx . . $ f0r_param_position_t# brcxx . . $ perspective_instance#br.; |
| 69 | } perspective_instance_tcxx . . $ perspective_instance_t#; |
| 70 | |
| 71 | |
| 72 | int f0r_initcxx . . $ f0r_init(b126dc7c1de90089).() |
| 73 | { |
| 74 | return 1; |
| 75 | } |
| 76 | void f0r_deinitcxx . . $ f0r_deinit(49f6e7a06ebc5aa8).() |
| 77 | { /* empty */ } |
| 78 | |
| 79 | void f0r_get_plugin_infocxx . . $ f0r_get_plugin_info(35d753456c0ea59c).( f0r_plugin_info_tcxx . . $ f0r_plugin_info_t#* info ) |
| 80 | { |
| 81 | info->namecxx . . $ f0r_plugin_info#name. = "Perspective"; |
| 82 | info->authorcxx . . $ f0r_plugin_info#author. = "Richard Spindler"; |
| 83 | info->plugin_typecxx . . $ f0r_plugin_info#plugin_type. = F0R_PLUGIN_TYPE_FILTERcxx . . $ `include/frei0r.h:247:9`!; |
| 84 | info->color_modelcxx . . $ f0r_plugin_info#color_model. = F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!; |
| 85 | info->frei0r_versioncxx . . $ f0r_plugin_info#frei0r_version. = FREI0R_MAJOR_VERSIONcxx . . $ `include/frei0r.h:217:9`!; |
| 86 | info->major_versioncxx . . $ f0r_plugin_info#major_version. = 0; |
| 87 | info->minor_versioncxx . . $ f0r_plugin_info#minor_version. = 1; |
| 88 | info->num_paramscxx . . $ f0r_plugin_info#num_params. = 4; |
| 89 | info->explanationcxx . . $ f0r_plugin_info#explanation. = "Distorts the image for a pseudo perspective"; |
| 90 | |
| 91 | } |
| 92 | void f0r_get_param_infocxx . . $ f0r_get_param_info(a9f9aa7954cc4a37).( f0r_param_info_tcxx . . $ f0r_param_info_t#* info, int param_index ) |
| 93 | { |
| 94 | switch ( param_index ) { |
| 95 | case 0: |
| 96 | info->namecxx . . $ f0r_param_info#name. = "Top Left"; |
| 97 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_POSITIONcxx . . $ `include/frei0r.h:394:9`!; |
| 98 | info->explanationcxx . . $ f0r_param_info#explanation. = ""; |
| 99 | break; |
| 100 | case 1: |
| 101 | info->namecxx . . $ f0r_param_info#name. = "Top Right"; |
| 102 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_POSITIONcxx . . $ `include/frei0r.h:394:9`!; |
| 103 | info->explanationcxx . . $ f0r_param_info#explanation. = ""; |
| 104 | break; |
| 105 | case 2: |
| 106 | info->namecxx . . $ f0r_param_info#name. = "Bottom Left"; |
| 107 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_POSITIONcxx . . $ `include/frei0r.h:394:9`!; |
| 108 | info->explanationcxx . . $ f0r_param_info#explanation. = ""; |
| 109 | break; |
| 110 | case 3: |
| 111 | info->namecxx . . $ f0r_param_info#name. = "Bottom Right"; |
| 112 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_POSITIONcxx . . $ `include/frei0r.h:394:9`!; |
| 113 | info->explanationcxx . . $ f0r_param_info#explanation. = ""; |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | f0r_instance_tcxx . . $ f0r_instance_t# f0r_constructcxx . . $ f0r_construct(da3f471cd007a696).(unsigned int width, unsigned int height) |
| 119 | { |
| 120 | perspective_instance_tcxx . . $ perspective_instance_t#* inst = (perspective_instance_tcxx . . $ perspective_instance_t#*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*inst)); |
| 121 | inst->wcxx . . $ perspective_instance#w. = width; |
| 122 | inst->hcxx . . $ perspective_instance#h. = height; |
| 123 | inst->tlcxx . . $ perspective_instance#tl..xcxx . . $ f0r_param_position#x. = 0.0; |
| 124 | inst->tlcxx . . $ perspective_instance#tl..ycxx . . $ f0r_param_position#y. = 0.0; |
| 125 | inst->trcxx . . $ perspective_instance#tr..xcxx . . $ f0r_param_position#x. = 1.0; |
| 126 | inst->trcxx . . $ perspective_instance#tr..ycxx . . $ f0r_param_position#y. = 0.0; |
| 127 | inst->blcxx . . $ perspective_instance#bl..xcxx . . $ f0r_param_position#x. = 0.0; |
| 128 | inst->blcxx . . $ perspective_instance#bl..ycxx . . $ f0r_param_position#y. = 1.0; |
| 129 | inst->brcxx . . $ perspective_instance#br..xcxx . . $ f0r_param_position#x. = 1.0; |
| 130 | inst->brcxx . . $ perspective_instance#br..ycxx . . $ f0r_param_position#y. = 1.0; |
| 131 | return (f0r_instance_tcxx . . $ f0r_instance_t#)inst; |
| 132 | } |
| 133 | void f0r_destructcxx . . $ f0r_destruct(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance) |
| 134 | { |
| 135 | perspective_instance_tcxx . . $ perspective_instance_t#* inst = (perspective_instance_tcxx . . $ perspective_instance_t#*)instance; |
| 136 | freecxx . . $ free(5d22bdacc48458e8).(inst); |
| 137 | } |
| 138 | void f0r_set_param_valuecxx . . $ f0r_set_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, |
| 139 | f0r_param_tcxx . . $ f0r_param_t# param, int param_index) |
| 140 | { |
| 141 | perspective_instance_tcxx . . $ perspective_instance_t#* inst = (perspective_instance_tcxx . . $ perspective_instance_t#*)instance; |
| 142 | switch ( param_index ) { |
| 143 | case 0: |
| 144 | inst->tlcxx . . $ perspective_instance#tl. = *((f0r_param_position_tcxx . . $ f0r_param_position_t#*)param); |
| 145 | break; |
| 146 | case 1: |
| 147 | inst->trcxx . . $ perspective_instance#tr. = *((f0r_param_position_tcxx . . $ f0r_param_position_t#*)param); |
| 148 | break; |
| 149 | case 2: |
| 150 | inst->blcxx . . $ perspective_instance#bl. = *((f0r_param_position_tcxx . . $ f0r_param_position_t#*)param); |
| 151 | break; |
| 152 | case 3: |
| 153 | inst->brcxx . . $ perspective_instance#br. = *((f0r_param_position_tcxx . . $ f0r_param_position_t#*)param); |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | void f0r_get_param_valuecxx . . $ f0r_get_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, |
| 158 | f0r_param_tcxx . . $ f0r_param_t# param, int param_index) |
| 159 | { |
| 160 | perspective_instance_tcxx . . $ perspective_instance_t#* inst = (perspective_instance_tcxx . . $ perspective_instance_t#*)instance; |
| 161 | switch ( param_index ) { |
| 162 | case 0: |
| 163 | *((f0r_param_position_tcxx . . $ f0r_param_position_t#*)param) = inst->tlcxx . . $ perspective_instance#tl.; |
| 164 | break; |
| 165 | case 1: |
| 166 | *((f0r_param_position_tcxx . . $ f0r_param_position_t#*)param) = inst->trcxx . . $ perspective_instance#tr.; |
| 167 | break; |
| 168 | case 2: |
| 169 | *((f0r_param_position_tcxx . . $ f0r_param_position_t#*)param) = inst->blcxx . . $ perspective_instance#bl.; |
| 170 | break; |
| 171 | case 3: |
| 172 | *((f0r_param_position_tcxx . . $ f0r_param_position_t#*)param) = inst->brcxx . . $ perspective_instance#br.; |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void f0r_updatecxx . . $ f0r_update(96cd7b27f2fca8a7).(f0r_instance_tcxx . . $ f0r_instance_t# instance, double time, |
| 178 | const uint32_tcxx . . $ uint32_t#* inframe, uint32_tcxx . . $ uint32_t#* outframe) |
| 179 | { |
| 180 | perspective_instance_tcxx . . $ perspective_instance_t#* inst = (perspective_instance_tcxx . . $ perspective_instance_t#*)instance; |
| 181 | |
| 182 | uint32_tcxx . . $ uint32_t#* dst = outframe; |
| 183 | const uint32_tcxx . . $ uint32_t#* src = inframe; |
| 184 | int len = inst->wcxx . . $ perspective_instance#w. * inst->hcxx . . $ perspective_instance#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->wcxx . . $ perspective_instance#w.; |
| 194 | int h = inst->hcxx . . $ perspective_instance#h.; |
| 195 | |
| 196 | int rx; |
| 197 | int ry; |
| 198 | int x; |
| 199 | int y; |
| 200 | f0r_param_position_tcxx . . $ f0r_param_position_t# top; |
| 201 | f0r_param_position_tcxx . . $ f0r_param_position_t# bot; |
| 202 | f0r_param_position_tcxx . . $ f0r_param_position_t# r; |
| 203 | f0r_param_position_tcxx . . $ f0r_param_position_t# in; |
| 204 | sub_vec2cxx . . $ sub_vec2(745f637666904c2d).( &top, &inst->trcxx . . $ perspective_instance#tr., &inst->tlcxx . . $ perspective_instance#tl. ); |
| 205 | sub_vec2cxx . . $ sub_vec2(745f637666904c2d).( &bot, &inst->brcxx . . $ perspective_instance#br., &inst->blcxx . . $ perspective_instance#bl. ); |
| 206 | for( y = 0; y < h; y++ ) { |
| 207 | for ( x = 0; x < w; x++ ) { |
| 208 | in.xcxx . . $ f0r_param_position#x. = (double)x / (double)w; |
| 209 | in.ycxx . . $ f0r_param_position#y. = (double)y / (double)h; |
| 210 | get_pixel_positioncxx . . $ get_pixel_position(1a1ea8ca7c28108b).( &r, &top, &bot, &inst->tlcxx . . $ perspective_instance#tl., &inst->blcxx . . $ perspective_instance#bl., &in ); |
| 211 | rx = lrint(r.xcxx . . $ f0r_param_position#x. * (float)w); |
| 212 | ry = lrint(r.ycxx . . $ f0r_param_position#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 |