[743724]: src / hugin_base / panotools / PanoToolsInterface.cpp Maximize Restore History

Download this file

PanoToolsInterface.cpp    841 lines (727 with data), 28.1 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
// -*- c-basic-offset: 4 -*-
/** @file PanoToolsInterface.cpp
*
* @brief implementation of PanoToolsInterface Class
*
* @author Pablo d'Angelo <pablo.dangelo@web.de>
*
* $Id$
*
* 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
*
*/
#include <hugin_config.h>
#include <stdlib.h>
#include "PanoToolsInterface.h"
#include <vector>
#include <set>
#include <hugin_utils/utils.h>
#include <hugin_utils/stl_utils.h>
#include <panodata/PanoImage.h>
#include <panodata/PanoramaData.h>
// This function is not defined in the pano13 headers
extern "C" {
int CheckParams( AlignInfo *g );
}
namespace HuginBase { namespace PTools {
Transform::~Transform()
{
if (!m_initialized) {
freeImage(m_srcImage);
freeImage(m_dstImage);
}
}
void Transform::updatePTData(const vigra::Diff2D &srcSize,
const VariableMap & srcVars,
Lens::LensProjectionFormat & srcProj,
const vigra::Diff2D & destSize,
PanoramaOptions::ProjectionFormat & destProj,
const std::vector<double> & destProjParam,
double destHFOV)
{
if (m_initialized) {
freeImage(m_srcImage);
freeImage(m_dstImage);
}
// fill our data into the Pano Tools structures.
setFullImage(m_srcImage, srcSize, 0,
srcVars, srcProj, true);
setDestImage(m_dstImage, destSize, 0, destProj, destProjParam, destHFOV);
}
void Transform::createTransform(const PanoramaData & pano, unsigned int imgNr,
const PanoramaOptions & dest, vigra::Diff2D srcSize)
{
const PanoImage & img = pano.getImage(imgNr);
if (srcSize.x == 0 && srcSize.y == 0) {
srcSize.x = img.getWidth();
srcSize.y = img.getHeight();
}
createTransform(srcSize,
pano.getImageVariables(imgNr),
pano.getLens(img.getLensNr()).getProjection(),
vigra::Diff2D(dest.getWidth(), dest.getHeight()),
dest.getProjection(),
dest.getProjectionParameters(),
dest.getHFOV(),
vigra::Diff2D(img.getWidth(), img.getHeight()));
}
void Transform::createInvTransform(const SrcPanoImage & src, const PanoramaOptions & dest)
{
VariableMap vars;
// not very nice, but I don't like to change all the stuff in this file..
vars.insert(make_pair(std::string("v"), Variable(std::string("v"), src.getHFOV())));
vars.insert(make_pair(std::string("a"), Variable("a", src.getRadialDistortion()[0])));
vars.insert(make_pair(std::string("b"), Variable("b", src.getRadialDistortion()[1])));
vars.insert(make_pair(std::string("c"), Variable("c", src.getRadialDistortion()[2])));
vars.insert(make_pair(std::string("d"), Variable("d", src.getRadialDistortionCenterShift().x)));
vars.insert(make_pair(std::string("e"), Variable("e", src.getRadialDistortionCenterShift().y)));
vars.insert(make_pair(std::string("g"), Variable("g", src.getShear().x)));
vars.insert(make_pair(std::string("t"), Variable("t", src.getShear().y)));
vars.insert(make_pair(std::string("r"), Variable("r", src.getRoll())));
vars.insert(make_pair(std::string("p"), Variable("p", src.getPitch())));
vars.insert(make_pair(std::string("y"), Variable("y", src.getYaw())));
createInvTransform(src.getSize(),
vars,
(Lens::LensProjectionFormat) src.getProjection(),
dest.getSize(),
dest.getProjection(),
dest.getProjectionParameters(),
dest.getHFOV(),
src.getSize());
}
void Transform::createTransform(const SrcPanoImage & src, const PanoramaOptions & dest)
{
VariableMap vars;
// not very nice, but I don't like to change all the stuff in this file..
vars.insert(make_pair(std::string("v"), Variable(std::string("v"), src.getHFOV())));
vars.insert(make_pair(std::string("a"), Variable("a", src.getRadialDistortion()[0])));
vars.insert(make_pair(std::string("b"), Variable("b", src.getRadialDistortion()[1])));
vars.insert(make_pair(std::string("c"), Variable("c", src.getRadialDistortion()[2])));
vars.insert(make_pair(std::string("d"), Variable("d", src.getRadialDistortionCenterShift().x)));
vars.insert(make_pair(std::string("e"), Variable("e", src.getRadialDistortionCenterShift().y)));
vars.insert(make_pair(std::string("g"), Variable("g", src.getShear().x)));
vars.insert(make_pair(std::string("t"), Variable("t", src.getShear().y)));
vars.insert(make_pair(std::string("r"), Variable("r", src.getRoll())));
vars.insert(make_pair(std::string("p"), Variable("p", src.getPitch())));
vars.insert(make_pair(std::string("y"), Variable("y", src.getYaw())));
createTransform(src.getSize(),
vars,
(Lens::LensProjectionFormat) src.getProjection(),
dest.getSize(),
dest.getProjection(),
dest.getProjectionParameters(),
dest.getHFOV(),
src.getSize());
}
void Transform::createTransform(const vigra::Diff2D & srcSize,
VariableMap srcVars,
Lens::LensProjectionFormat srcProj,
const vigra::Diff2D &destSize,
PanoramaOptions::ProjectionFormat destProj,
const std::vector<double> & destProjParam,
double destHFOV,
const vigra::Diff2D & originalSrcSize)
{
m_srcTX = destSize.x/2.0;
m_srcTY = destSize.y/2.0;
m_destTX = srcSize.x/2.0;
m_destTY = srcSize.y/2.0;
// adjust parameters dependant on the image size.
if (originalSrcSize.x != 0 && originalSrcSize.y != 0) {
double rx = srcSize.x / (double)originalSrcSize.x;
double ry = srcSize.y / (double)originalSrcSize.y;
map_get(srcVars,"d").setValue( map_get(srcVars,"d").getValue() * rx );
map_get(srcVars,"e").setValue( map_get(srcVars,"e").getValue() * ry );
}
updatePTData(srcSize, srcVars, srcProj,
destSize, destProj, destProjParam, destHFOV);
// create the actual stack
SetMakeParams( m_stack, &m_mp, &m_srcImage , &m_dstImage, 0 );
}
void Transform::createInvTransform(const PanoramaData & pano, unsigned int imgNr,
const PanoramaOptions & dest, vigra::Diff2D srcSize)
{
const PanoImage & img = pano.getImage(imgNr);
if (srcSize.x == 0 && srcSize.y == 0) {
srcSize.x = img.getWidth();
srcSize.y = img.getHeight();
}
createInvTransform(srcSize,
pano.getImageVariables(imgNr),
pano.getLens(img.getLensNr()).getProjection(),
vigra::Diff2D(dest.getWidth(), dest.getHeight()),
dest.getProjection(),
dest.getProjectionParameters(),
dest.getHFOV(),
vigra::Diff2D(img.getWidth(), img.getHeight()));
}
void Transform::createInvTransform(const vigra::Diff2D & srcSize,
VariableMap srcVars,
Lens::LensProjectionFormat srcProj,
const vigra::Diff2D & destSize,
PanoramaOptions::ProjectionFormat destProj,
const std::vector<double> & destProjParam,
double destHFOV,
const vigra::Diff2D & originalSrcSize)
{
m_srcTX = srcSize.x/2.0;
m_srcTY = srcSize.y/2.0;
m_destTX = destSize.x/2.0;
m_destTY = destSize.y/2.0;
// adjust parameters dependant on the image size.
if (originalSrcSize.x != 0 && originalSrcSize.y != 0) {
double rx = srcSize.x / (double)originalSrcSize.x;
double ry = srcSize.y / (double)originalSrcSize.y;
map_get(srcVars,"d").setValue( map_get(srcVars,"d").getValue() * rx );
map_get(srcVars,"e").setValue( map_get(srcVars,"e").getValue() * ry );
}
updatePTData(srcSize, srcVars, srcProj,
destSize, destProj,
destProjParam,
destHFOV);
// create the actual stack
SetInvMakeParams( m_stack, &m_mp, &m_srcImage , &m_dstImage, 0 );
}
bool Transform::transform(double & x_dest, double & y_dest,
double x_src, double y_src) const
{
void * params= (void *) (&m_stack);
return execute_stack_new(x_src, y_src, &x_dest, &y_dest, params) != 0;
}
///
bool Transform::transform(hugin_utils::FDiff2D& dest, const hugin_utils::FDiff2D & src) const
{
double x_dest, y_dest;
void * params= (void *) (&m_stack);
bool ok = execute_stack_new(src.x, src.y, &x_dest, &y_dest, params) != 0;
dest.x = x_dest;
dest.y = y_dest;
return ok;
}
/** like transform, but return image coordinates, not cartesian
* coordinates
*/
bool Transform::transformImgCoord(double & x_dest, double & y_dest,
double x_src, double y_src) const
{
x_src -= m_srcTX - 0.5 ;
y_src -= m_srcTY - 0.5;
void * params= (void *) (&m_stack);
bool ok = execute_stack_new(x_src, y_src, &x_dest, &y_dest, params) != 0;
x_dest += m_destTX - 0.5;
y_dest += m_destTY - 0.5;
return ok;
}
AlignInfoWrap::AlignInfoWrap()
{
gl.im = NULL;
gl.opt = NULL;
gl.cpt = NULL;
gl.t = NULL;
gl.cim = NULL;
gl.numIm = 0;
gl.numPts = 0;
gl.nt = 0;
}
AlignInfoWrap::~AlignInfoWrap()
{
// DisposeAlignInfo(&gl);
// delete[](gl.img);
// delete[](gl.opt);
// delete[](gl.cpt);
// delete[](gl.t);
// delete[](gl.cim);
if (gl.im) free(gl.im);
if (gl.opt) free(gl.opt);
if (gl.cpt) free(gl.cpt);
if (gl.t) free(gl.t);
if (gl.cim) free(gl.cim);
}
bool AlignInfoWrap::setInfo(const PanoramaData & pano)
{
const VariableMapVector & variables = pano.getVariables();
const CPVector & controlPoints = pano.getCtrlPoints();
const OptimizeVector & optvec = pano.getOptimizeVector();
// based on code from ParseScript by H. Dersch
/*
delete(gl.im);
delete(gl.opt);
delete(gl.cpt);
delete(gl.t);
delete(gl.cim);
*/
if (gl.im) free(gl.im);
if (gl.opt) free(gl.opt);
if (gl.cpt) free(gl.cpt);
if (gl.t) free(gl.t);
if (gl.cim) free(gl.cim);
// Determine number of images and control points
gl.numIm = int(variables.size());
gl.nt = 0;
int optVarCounter=0;
// be careful. linked variables should not be specified multiple times.
std::vector<std::set<std::string> > linkvars(pano.getNrOfLenses());
for (unsigned imgNr = 0; imgNr < variables.size(); imgNr++)
{
unsigned int lensNr = pano.getImage(imgNr).getLensNr();
const Lens & lens = pano.getLens(lensNr);
const std::set<std::string> & optvar = optvec[imgNr];
for (std::set<std::string>::const_iterator sit = optvar.begin();
sit != optvar.end(); ++sit )
{
if (set_contains(lens.variables,*sit)) {
// it is a lens variable
if (const_map_get(lens.variables,*sit).isLinked()) {
if (! set_contains(linkvars[lensNr], *sit))
{
// linked, count only once
optVarCounter++;
linkvars[lensNr].insert(*sit);
}
} else {
// unlinked lens variable, count as usual
optVarCounter++;
}
} else {
// not a lens variable, count as usual
optVarCounter++;
}
}
}
gl.numParam = optVarCounter;
gl.numPts = int(controlPoints.size());
// Allocate Space for Pointers to images, preferences and control points
// use memory allocation routines from pano12.dll
gl.im = (Image*) malloc( gl.numIm * sizeof(::Image) );
gl.opt = (optVars*) malloc( gl.numIm * sizeof(::optVars) );
gl.t = (triangle*) malloc( gl.nt * sizeof(::triangle) );
gl.cim = (CoordInfo*) malloc( gl.numIm * sizeof(::CoordInfo) );
gl.cpt = (controlPoint*) malloc( gl.numPts * sizeof(::controlPoint) );
if( gl.im == NULL || gl.opt == NULL || gl.cpt == NULL || gl.t == NULL || gl.cim == NULL )
{
DEBUG_FATAL("Not enough memory");
}
const PanoramaOptions & opts = pano.getOptions();
setDestImage(gl.pano, vigra::Diff2D(opts.getWidth(), opts.getHeight()),
0, opts.getProjection(),
opts.getProjectionParameters(),
opts.getHFOV());
// Default: Use buffer 'buf' for stitching
SetStitchDefaults(&(gl.st)); strcpy( gl.st.srcName, "buf" );
for(int imgNr=0; imgNr<gl.numIm; imgNr++)
{
SetImageDefaults( &(gl.im[imgNr]) );
SetOptDefaults ( &(gl.opt[imgNr]));
SetCoordDefaults( &(gl.cim[imgNr]), imgNr);
}
std::map<unsigned int, unsigned int> linkAnchors;
for(unsigned imgNr=0; (int)imgNr< gl.numIm; imgNr++)
{
const PanoImage & pimg = pano.getImage(imgNr);
const VariableMap & vars = variables[imgNr];
unsigned lensNr = pimg.getLensNr();
//pano.getImageVariables(*it);
const Lens & lens = pano.getLens(lensNr);
// set the image information, with pointer to dummy image data
setFullImage(gl.im[imgNr],
vigra::Diff2D(pimg.getWidth(), pimg.getHeight()),
0, vars, lens.getProjection(), true);
// macro to set optimisation vars without a lot of cut n paste
#define PT_SET_OPT(n,v) \
{ if (set_contains(optvec[imgNr], #v )) { \
if (set_contains(lens.variables, #v) \
&& const_map_get(lens.variables, #v ).isLinked()) {\
if (set_contains(linkAnchors, lensNr) \
&& linkAnchors[lensNr] != imgNr) \
{ \
gl.opt[imgNr]. n = linkAnchors[lensNr] + 2; \
}else { \
gl.opt[imgNr]. n = 1; \
linkAnchors[lensNr] = imgNr; \
} \
} else { \
gl.opt[imgNr]. n = 1; \
} \
} else { \
gl.opt[imgNr]. n = 0; \
} }
// set optimisation flags
PT_SET_OPT(hfov, v);
PT_SET_OPT(yaw, y);
PT_SET_OPT(pitch, p);
PT_SET_OPT(roll, r);
PT_SET_OPT(a, a);
PT_SET_OPT(b, b);
PT_SET_OPT(c, c);
PT_SET_OPT(d, d);
PT_SET_OPT(e, e);
PT_SET_OPT(shear_x, g);
PT_SET_OPT(shear_y, t);
#undef PT_SET_OPT
}
unsigned i=0;
for (CPVector::const_iterator it = controlPoints.begin(); it != controlPoints.end(); ++it) {
// control point stuff.
gl.cpt[i].type = it->mode;
gl.cpt[i].num[0] = it->image1Nr;
gl.cpt[i].x[0] = it->x1;
gl.cpt[i].y[0] = it->y1;
gl.cpt[i].num[1] = it->image2Nr;
gl.cpt[i].x[1] = it->x2;
gl.cpt[i].y[1] = it->y2;
i++;
}
if( CheckParams( &gl ) != 0 ) {
DEBUG_FATAL("CheckParams() returned false!");
return false;
}
gl.fcn = fcnPano;
return true;
}
void AlignInfoWrap::setGlobal()
{
SetGlobalPtr( &gl );
}
VariableMapVector AlignInfoWrap::getVariables() const
{
return GetAlignInfoVariables(gl);
}
CPVector AlignInfoWrap::getCtrlPoints() const
{
return GetAlignInfoCtrlPoints(gl);
}
VariableMapVector GetAlignInfoVariables(const AlignInfo& gl)
{
VariableMapVector res;
if (gl.im) {
for (int i = 0; i < gl.numIm; i++) {
VariableMap vars;
vars.insert(make_pair(std::string("v"), Variable("v", gl.im[i].hfov)));
vars.insert(make_pair(std::string("y"), Variable("y", gl.im[i].yaw)));
vars.insert(make_pair(std::string("r"), Variable("r", gl.im[i].roll)));
vars.insert(make_pair(std::string("p"), Variable("p", gl.im[i].pitch)));
vars.insert(make_pair(std::string("a"), Variable("a", gl.im[i].cP.radial_params[0][3])));
vars.insert(make_pair(std::string("b"), Variable("b", gl.im[i].cP.radial_params[0][2])));
vars.insert(make_pair(std::string("c"), Variable("c", gl.im[i].cP.radial_params[0][1])));
vars.insert(make_pair(std::string("e"), Variable("e", gl.im[i].cP.vertical_params[0])));
vars.insert(make_pair(std::string("d"), Variable("d", gl.im[i].cP.horizontal_params[0])));
//Modified by Dev
vars.insert(make_pair(std::string("g"), Variable("g", gl.im[i].cP.shear_x)));
vars.insert(make_pair(std::string("t"), Variable("t", gl.im[i].cP.shear_y)));
res.push_back(vars);
}
}
return res;
}
CPVector GetAlignInfoCtrlPoints(const AlignInfo& gl)
{
CPVector result;
if (gl.cpt) {
for (int i = 0; i < gl.numPts; i++) {
ControlPoint pnt(gl.cpt[i].num[0], gl.cpt[i].x[0], gl.cpt[i].y[0],
gl.cpt[i].num[1], gl.cpt[i].x[1], gl.cpt[i].y[1], (ControlPoint::OptimizeMode) gl.cpt[i].type);
pnt.error = sqrt(distSquared(i));
result.push_back(pnt);
}
}
return result;
}
void setDestImage(Image & image, vigra::Diff2D size,
unsigned char * imageData,
const PanoramaOptions::ProjectionFormat & format,
const std::vector<double> & projParams,
double destHFOV)
{
SetImageDefaults(&image);
image.width = size.x;
image.height = size.y;
image.bytesPerLine = image.width*3;
image.bitsPerPixel = 24;
image.dataSize = image.height * image.bytesPerLine;
// Allocate memory for pointer to pointer to image data
// image.data = (unsigned char**)malloc( sizeof(unsigned char*) );
// if(image.data == NULL) {
// DEBUG_FATAL("Out of memory");
// }
image.data = 0;
#ifdef HasPANO13
pano_projection_features projd;
if (panoProjectionFeaturesQuery((int) format, &projd)) {
image.format = projd.internalFormat;
} else {
image.format = _equirectangular;
PrintError("unsupported projection");
}
image.formatParamCount = projd.numberOfParameters;
assert(image.formatParamCount == (int) projParams.size());
for (int i=0; i < projd.numberOfParameters; i++) {
image.formatParam[i] = projParams[i];
DEBUG_DEBUG("projection parameter " << i << ": " << image.formatParam[i]);
}
#else
switch (format) {
case PanoramaOptions::RECTILINEAR:
image.format = _rectilinear;
break;
case PanoramaOptions::CYLINDRICAL:
image.format= _panorama;
break;
case PanoramaOptions::EQUIRECTANGULAR:
image.format = _equirectangular;
break;
case PanoramaOptions::FULL_FRAME_FISHEYE:
image.format = _fisheye_ff;
break;
case PanoramaOptions::STEREOGRAPHIC:
image.format = _stereographic;
break;
case PanoramaOptions::MERCATOR:
image.format = _mercator;
break;
case PanoramaOptions::TRANSVERSE_MERCATOR:
image.format = _trans_mercator;
break;
case PanoramaOptions::SINUSOIDAL:
image.format = _sinusoidal;
break;
#ifdef HasPANO13
case PanoramaOptions::LAMBERT:
image.format = _lambert;
break;
case PanoramaOptions::LAMBERT_AZIMUTHAL:
image.format = _lambertazimuthal;
break;
case PanoramaOptions::PANINI:
image.format = _panini;
break;
case PanoramaOptions::ARCHITECTURAL:
image.format = _architectural;
break;
case PanoramaOptions::ORTHOGRAPHIC:
image.format = _ortographic;
break;
case PanoramaOptions::EQUISOLID:
image.format = _equisolid;
break;
case PanoramaOptions::EQUI_PANINI:
image.format = _equi_panini;
break;
#endif
default:
PrintError("unsupported projection");
}
#endif
image.hfov = destHFOV;
}
// internal function, used by setFullImage() to set the distortion parameters
void initCPrefs(cPrefs & p, const VariableMap &vars)
{
SetCorrectDefaults(&p);
double val;
double a = const_map_get(vars,"a").getValue();
double b = const_map_get(vars,"b").getValue();
double c = const_map_get(vars,"c").getValue();
if (a != 0.0 || b != 0.0 || c != 0) {
p.radial = 1;
p.radial_params[0][3] = p.radial_params[1][3] = p.radial_params[2][3] = a;
p.radial_params[0][2] = p.radial_params[1][2] = p.radial_params[2][2] = b;
p.radial_params[0][1] = p.radial_params[1][1] = p.radial_params[2][1] = c;
double d = 1.0 - (a+b+c);
p.radial_params[0][0] = p.radial_params[1][0] = p.radial_params[2][0] = d;
} else {
p.radial = 0;
}
val = const_map_get(vars,"e").getValue();
if (val != 0.0) {
p.vertical = TRUE;
p.vertical_params[0] = p.vertical_params[1] = p.vertical_params[2] = val;
} else {
p.vertical = FALSE;
p.vertical_params[0] = p.vertical_params[1] = p.vertical_params[2] = 0;
}
val = const_map_get(vars,"d").getValue();
if (val != 0.0) {
p.horizontal = TRUE;
p.horizontal_params[0] = p.horizontal_params[1] = p.horizontal_params[2] = val;
} else {
p.horizontal = FALSE;
p.horizontal_params[0] = p.horizontal_params[1] = p.horizontal_params[2] = 0;
}
// FIXME add shear parameters
val = const_map_get(vars, "g").getValue();
double val2 = const_map_get(vars, "t").getValue();
if (val2 != 0.0 || val != 0.0) {
p.shear = TRUE;
p.shear_x = val;
p.shear_y = val2;
} else {
p.shear = FALSE;
}
p.resize = FALSE;
p.luminance = FALSE;
p.cutFrame = FALSE;
p.fourier = FALSE;
// calculate correction radius
SetCorrectionRadius(&p);
}
void setFullImage(Image & image, vigra::Diff2D size,
unsigned char * imageData, const VariableMap & vars,
const Lens::LensProjectionFormat format,
bool correctDistortions)
{
SetImageDefaults(&image);
image.width = size.x;
image.height = size.y;
image.bytesPerLine = image.width*3;
image.bitsPerPixel = 24;
image.dataSize = image.height * image.bytesPerLine;
#if 0
// Allocate memory for pointer to pointer to image data
image.data = (unsigned char**)malloc( sizeof(unsigned char*) );
if(image.data == NULL) {
DEBUG_FATAL("Out of memory");
}
*(image.data) = imageData;
#else
image.data = 0;
#endif
image.dataformat = _RGB;
switch (format) {
case Lens::RECTILINEAR:
image.format = _rectilinear;
break;
case Lens::PANORAMIC:
image.format = _panorama;
break;
case Lens::CIRCULAR_FISHEYE:
image.format = _fisheye_circ;
break;
case Lens::FULL_FRAME_FISHEYE:
image.format = _fisheye_ff;
break;
case Lens::EQUIRECTANGULAR:
image.format = _equirectangular;
break;
}
image.hfov = const_map_get(vars,"v").getValue();
image.yaw = const_map_get(vars,"y").getValue();
image.pitch = const_map_get(vars,"p").getValue();
image.roll = const_map_get(vars,"r").getValue();
//fill cPrefs struct
if (correctDistortions) {
initCPrefs(image.cP,vars);
}
// no name
image.name[0]=0;
image.yaw = const_map_get(vars,"y").getValue();
image.yaw = const_map_get(vars,"y").getValue();
image.selection.top = 0;
image.selection.left = 0;
image.selection.right = image.width;
image.selection.bottom = image.height;
}
// ===========================================================================
// ===========================================================================
void createAdjustPrefs(aPrefs & p, TrformStr & transf)
{
SetAdjustDefaults(&p);
p.interpolator = _nn;
p.mode = _insert;
SetImageDefaults(&(p.im));
SetImageDefaults(&(p.pano));
}
// ===========================================================================
// ===========================================================================
// prepare a Trform struct for the adjust operation, image -> pano
void createAdjustTrform(TrformStr & trf)
{
trf.src = (Image *) malloc(sizeof(Image));
SetImageDefaults(trf.src);
trf.dest = (Image *) malloc(sizeof(Image));
SetImageDefaults(trf.dest);
trf.success = TRUE;
trf.tool = _adjust;
trf.mode = _destSupplied | _honor_valid;
trf.data = 0;
trf.interpolator = _nn;
trf.gamma = 1.0;
}
// free the resources associated with a TrformStr.
// createAdjustTrform must have been used to create it.
void freeTrform(TrformStr & trf)
{
if (trf.dest) {
if (trf.dest->data) {
myfree((void**)trf.dest->data);
}
free((void**)trf.dest);
}
if (trf.src) {
if (trf.src->data) {
myfree((void**)trf.src->data);
}
free((void**)trf.src);
}
}
void freeImage(Image &img)
{
if (img.data) {
myfree((void**)img.data);
}
}
void setAdjustSrcImg(TrformStr & trf, aPrefs & ap,
int width, int height, unsigned char * imageData,
const VariableMap & vars,
const Lens::LensProjectionFormat format,
bool correctDistortions)
{
DEBUG_ASSERT(trf.src);
if (trf.src->data) {
myfree((void**)trf.src->data);
}
setFullImage(*(trf.src), vigra::Diff2D(width,height), imageData, vars, format,
correctDistortions);
ap.im = *(trf.src);
}
void setAdjustDestImg(TrformStr & trf, aPrefs & ap,
int width, int height, unsigned char * imageData,
const PanoramaOptions & opts)
{
DEBUG_ASSERT(trf.dest);
if (trf.dest->data) {
myfree((void**)trf.dest->data);
}
setDestImage(*(trf.dest), vigra::Diff2D(width, height), imageData, opts.getProjection(),
opts.getProjectionParameters(),
opts.getHFOV());
ap.pano = *(trf.dest);
}
void setOptVars(optVars & opt, const std::set<std::string> & optvars)
{
// FIXME: BUG does not support linked variables!!!!
// set the optimisation value to img+2, if it is linked to img...
opt.hfov = set_contains(optvars,"v") ? 1 : 0;
opt.yaw = set_contains(optvars,"y") ? 1 : 0;
opt.pitch = set_contains(optvars,"p") ? 1 : 0;
opt.roll = set_contains(optvars,"r") ? 1 : 0;
opt.a = set_contains(optvars,"a") ? 1 : 0;
opt.b = set_contains(optvars,"b") ? 1 : 0;
opt.c = set_contains(optvars,"c") ? 1 : 0;
opt.d = set_contains(optvars,"d") ? 1 : 0;
opt.e = set_contains(optvars,"e") ? 1 : 0;
opt.shear_x = set_contains(optvars,"g") ? 1 : 0;
opt.shear_y = set_contains(optvars,"t") ? 1 : 0;
}
}} // namespace