1 /* Panorama_Tools - Generate, Edit and Convert Panoramic Images
2 Copyright (C) 1998,1999 - Helmut Dersch der@fh-furtwangen.de
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this software; see the file COPYING. If not, a copy
16 can be downloaded from http://www.gnu.org/licenses/gpl.html, or
17 obtained by writing to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
20 /*------------------------------------------------------------*/
21
22 #include "filter.h"
23
24
25 // -------------------------- Functions ------------------------------------------------------
26
perspective(TrformStr * TrPtr,pPrefs * prefs)27 void perspective(TrformStr *TrPtr, pPrefs *prefs)
28 {
29 double v[3]; // auxilliary vector
30 double points_per_degree;
31 double mt[3][3];
32 double alpha, beta, gammar; // Angles in rad
33 double x_off, y_off, d;
34 double a;
35 uint32_t destwidth, destheight;
36 fDesc fD;
37
38 void *params[4]; // Parameters for perspective control functions
39
40 params[0] = (void*)mt;
41 params[1] = (void*)&d;
42 params[2] = (void*)&x_off;
43 params[3] = (void*)&y_off;
44
45
46
47 // Set destination image parameters
48
49
50 destwidth = prefs->width;
51 destheight = prefs->height;
52
53 if( destwidth <= 0 || destheight <= 0 )
54 {
55 TrPtr->success = 0;
56 PrintError( "Zero Destination Image Size" );
57 return;
58 }
59
60
61 if( SetDestImage(TrPtr, destwidth, destheight) != 0 )
62 {
63 TrPtr->success = 0;
64 PrintError( "Not enough Memory.");
65 return;
66 }
67
68
69 // Set parameters for perspective transformation
70
71
72 a = DEG_TO_RAD( prefs->hfov );
73
74 alpha = DEG_TO_RAD( prefs->y_beta );
75 beta = DEG_TO_RAD( prefs->x_alpha );
76 gammar = DEG_TO_RAD( prefs->gamma );
77
78 fD.func =(trfn)NULL;
79
80 switch(prefs->format)
81 {
82 case _rectilinear:
83 if( a >= PI )
84 {
85 TrPtr->success = 0;
86 PrintError("HFOV must be smaller than 180 degrees");
87 return;
88 }
89 d = TrPtr->src->width / ( 2.0 * tan( a/2.0 ) );
90 if( prefs->unit_is_cart )
91 {
92 alpha = atan( (prefs->y_beta - TrPtr->src->height/2.0 ) / d );
93 beta = - atan( (prefs->x_alpha - TrPtr->src->width/2.0 ) / d );
94 }
95 fD.func = persp_rect;
96 break;
97 case _spherical_tp:
98 d = TrPtr->src->width / a;
99 if( prefs->unit_is_cart )
100 {
101 points_per_degree = ((double) TrPtr->src->width) / (a * 360 / (2 * PI)) ;
102 alpha = ((TrPtr->src->height/2.0 - prefs->y_beta)/ points_per_degree) * 2 * PI / 360;
103 beta = -((TrPtr->src->width/2.0 - prefs->x_alpha)/ points_per_degree) * 2 * PI / 360;
104
105 }
106 fD.func = persp_sphere;
107 break;
108 }
109
110 SetMatrix( alpha, beta, gammar , mt, 1 );
111
112 // Offset
113
114 v[0] = 0.0;
115 v[1] = 0.0;
116 v[2] = d;
117 matrix_mult( mt, v );
118 x_off = v[0]*d/v[2];
119 y_off = v[1]*d/v[2];
120
121
122 // Do transformation
123
124
125 if( fD.func != NULL)
126 {
127 fD.param = params;
128 //transFormEx( TrPtr, &fD, &fD, 0, 1 );
129 transForm( TrPtr, &fD, 0 );
130 }
131 else
132 TrPtr->success = 0;
133
134 if( TrPtr->success == 0 && ! (TrPtr->mode & _destSupplied)) // Moved here
135 myfree( (void**)TrPtr->dest->data );
136
137 }
138
139
140
141
SetPerspectiveDefaults(pPrefs * pP)142 void SetPerspectiveDefaults( pPrefs *pP )
143 {
144 pP->magic = 40;
145 pP->format = _rectilinear;
146 pP->hfov = 60;
147 pP->x_alpha = 0.0;
148 pP->y_beta = 0.0;
149 pP->gamma = 0.0;
150 pP->unit_is_cart = FALSE;
151 pP->width = 500;
152 pP->height = 300;
153
154 }
155
156
157
158