[495361]: src / panotools-wrappers / pttransform.c Maximize Restore History

Download this file

pttransform.c    276 lines (231 with data), 9.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* pttransform.c
*
* Copyright Pablo d'Angelo
*
* May 2007
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* Author: Pablo d'Angelo pablo.dangelo@web.de
*
*/
#include "pttransform.h"
// tables to map the script projection numbers
// to internal projection numbers
int srcProj2ProjMap[] = { 0, 1, 2, 3, 4 };
int panoProj2ProjMap[] = {0, 1, 4, 3, 10, 11, 12, 14 };
// internal function to setup panotools data structure
static void srcImage2PTImg(pt_script_image * src, Image * dest)
{
int i,j;
SetImageDefaults(dest);
dest->width = src->width;
dest->height = src->height;
// dummy entry for image storage
dest->bytesPerLine = dest->height*3;
dest->bitsPerPixel = 24;
dest->dataSize = 0;
dest->data = 0;
dest->dataformat = _RGB;
if (src->projection < 0 || src->projection >= sizeof(srcProj2ProjMap)/sizeof(int)) {
fprintf(stderr, "Warning, unknown projection format, using rectilinear\n");
dest->format = 0;
} else {
dest->format = srcProj2ProjMap[src->projection];
}
dest->hfov = src->fHorFOV;
dest->yaw = src->yaw;
dest->roll = src->roll;
dest->pitch = src->pitch;
// copy lens correction parameters
// radial distortion parameters
if (src->coef[0] != 0.0 || src->coef[1] != 0.0 || src->coef[2] != 0.0) {
dest->cP.radial = 1;
dest->cP.correction_mode = 0;
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++) {
dest->cP.radial_params[j][3-i] = src->coef[i];
}
dest->cP.radial_params[j][0] = 1 - src->coef[0] - src->coef[1] - src->coef[2];
}
}
SetCorrectionRadius(&(dest->cP));
// image center shift
if (src->coef[4]) {
dest->cP.vertical = 1;
for (i=0; i < 3; i++)
dest->cP.vertical_params[i] = src->coef[4];
}
if (src->coef[3]) {
dest->cP.horizontal = 1;
for (i=0; i < 3; i++)
dest->cP.horizontal_params[i] = src->coef[3];
}
// shear
if (src->coef[5] != 0.0 || src->coef[6] != 0.0) {
dest->cP.shear = 1;
dest->cP.shear_x = src->coef[5];
dest->cP.shear_y = src->coef[6];
} else {
dest->cP.shear = 0;
}
dest->cP.luminance = 0;
dest->cP.fourier = 0;
dest->name[0] = 0;
// copy selection
dest->selection.left = src->cropArea[0];
dest->selection.top = src->cropArea[1];
dest->selection.right = src->cropArea[2];
dest->selection.bottom = src->cropArea[3];
// type of crop
dest->cP.cutFrame = src->cropType == 'C' ? 1 : 0;
// dest->crop information is not used by the coordinate transforms themself.
}
// internal function to setup panotools data structure
static void destImage2PTImg(pt_script_pano * src, Image * dest)
{
SetImageDefaults(dest);
dest->width = src->width;
dest->height = src->height;
// dummy entry for image storage
dest->bytesPerLine = dest->height*3;
dest->bitsPerPixel = 24;
dest->dataSize = 0;
dest->data = 0;
dest->dataformat = _RGB;
// TODO: map projection format
if (src->projection < 0 || src->projection >= sizeof(panoProj2ProjMap)/sizeof(int)) {
fprintf(stderr, "Warning, unknown projection format, using rectilinear\n");
dest->format = 0;
} else {
dest->format = panoProj2ProjMap[src->projection];
}
dest->format = src->projection;
dest->hfov = src->fHorFOV;
}
pt_transform * panoTransformCreate(pt_script *script, int imgNr)
{
pt_script_image * inputImg = panoScriptGetInputImage(script, imgNr);
if (! inputImg) {
fprintf(stderr, "image number invalid\n");
return NULL;
}
// void SetMakeParams( struct fDesc *stack, struct MakeParams *mp, Image *im , Image *pn, int color )
// create transformation struct
pt_transform * trans = (pt_transform *) malloc(sizeof(pt_transform));
// panotools data structures
Image imSrc;
Image imDest;
srcImage2PTImg(inputImg, &imSrc);
destImage2PTImg(&(script->pano), &imDest);
// create transformation stack. Transfroms using cartesian coordinates
// (origin in image centre)
SetMakeParams(&(trans->stack[0]), &(trans->mp), &imSrc, &imDest, 0);
trans->fD.func = execute_stack_new;
trans->fD.param = trans->stack;
// create transformation stack. Transfroms using cartesian coordinates
// (origin in image centre)
SetMakeParams(&(trans->invstack[0]), &(trans->invmp), &imSrc, &imDest, 0);
trans->fD.func = execute_stack_new;
trans->fD.param = trans->invstack;
// offset to correct for cartesion <-> image coordinate systems
trans->w2 = panoScriptGetPanoWidth(script) / 2.0 - 0.5; // Steve's L
trans->h2 = panoScriptGetPanoHeight(script) / 2.0 - 0.5;
trans->sw2 = inputImg->width / 2.0 - 0.5;
trans->sh2 = inputImg->height / 2.0 - 0.5;
// precalculate crop settings
if (inputImg->cropArea[0] < 0)
trans->crop_left = 0;
else
trans->crop_left = inputImg->cropArea[0];
if (inputImg->cropArea[1] > inputImg->width)
trans->crop_right = inputImg->width;
else
trans->crop_right = inputImg->cropArea[1];
if (inputImg->cropArea[2] < 0)
trans->crop_top = 0;
else
trans->crop_top = inputImg->cropArea[2];
if (inputImg->cropArea[3] > inputImg->height)
trans->crop_bottom = 0;
else
trans->crop_bottom = inputImg->cropArea[3];
if (inputImg->projection == 2) {
double r1, r2;
trans->crop_circular = 1;
// TODO: calculate crop centre and radius
trans->crop_circular_cx = (inputImg->cropArea[0] + inputImg->cropArea[1]) / 2.0;
trans->crop_circular_cy = (inputImg->cropArea[2] + inputImg->cropArea[3]) / 2.0;
r1 = (inputImg->cropArea[1] - inputImg->cropArea[0]) / 2.0;
r2 = (inputImg->cropArea[3] - inputImg->cropArea[2]) / 2.0;
if (r1 < r2)
trans->crop_circular_radius = r1;
else
trans->crop_circular_radius = r2;
} else {
trans->crop_circular = 0;
}
return trans;
}
int panoTransformForward(pt_transform *transform,
double x_in, double y_in, double *x_out, double *y_out )
{
// convert destination screen coordinates to cartesian coordinates
x_in -= transform->sw2;
y_in -= transform->sh2;
// get source cartesian coordinates
int valid = transform->fD.func(x_in, y_in, x_out, y_out, transform->fD.param);
if (! valid) return FALSE;
// convert source cartesian coordinates to screen coordinates
*x_out += transform->w2;
*y_out += transform->h2;
// TODO: calculate distance from image border
// useful for later seam creation
}
int panoTransformInverse(pt_transform *transform,
double x_out, double y_out, double *x_in, double *y_in )
{
// convert destination screen coordinates to cartesian coordinates
x_out -= transform->w2;
y_out -= transform->h2;
// get source cartesian coordinates
int valid = transform->fD.func(x_out, y_out, x_in, y_in, transform->fD.param);
if (! valid) return FALSE;
// convert source cartesian coordinates to screen coordinates
*x_in += transform->sw2;
*y_in += transform->sh2;
// check if this is inside the source image/crop
valid = valid && (! (*x_in < transform->crop_left || *x_in >= transform->crop_right ||
*y_in < transform->crop_top || *y_in >= transform->crop_bottom));
if (transform->crop_circular) {
// circular crop
double rx = *x_in - transform->crop_circular_cx;
double ry = *y_in - transform->crop_circular_cy;
double r = sqrt(rx * rx + ry * ry);
valid = valid && (r < transform->crop_circular_radius);
}
return valid;
// TODO: calculate distance from image border
// useful for later seam creation
}
void panoTransformDispose(pt_transform **transform)
{
if (*transform) {
free(*transform);
}
*transform = 0;
}