Removed unused code.
[libopano.git] / src / filter.h
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 program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 /*------------------------------------------------------------*/
19
20 #ifndef FILTER_H
21 #define FILTER_H
22
23
24 #include <math.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <limits.h>
31
32 #include "panorama.h"
33
34 #ifndef TRUE
35         #define TRUE 1
36 #endif
37
38 #ifndef FALSE
39         #define FALSE 0
40 #endif
41
42
43 //---------------------- Types ---------------------------------------------
44
45 #define UCHAR   unsigned char
46 #define USHORT  unsigned short
47 #define ULONG   unsigned long
48
49 enum{
50         _UCHAR,
51         _USHORT,
52         _ULONG
53         };
54
55 //---------------------- Some useful math defines --------------------------
56
57 #ifndef PI
58         #define PI 3.14159265358979323846
59 #endif
60
61 // Normalize an angle to +/-180degrees
62
63 #define NORM_ANGLE( x )  while( x >180.0 ) x -= 360.0; while( x < -180.0 ) x += 360.0;
64
65 // Convert double x to unsigned char/short c
66
67
68
69 #define DBL_TO_UC( c, x )       if(x>255.0) c=255U;                                                             \
70                                                                 else if(x<0.0) c=0;                                                     \
71                                                                 else c=(unsigned char)floor(x+0.5);
72
73 #define DBL_TO_US( c, x )       if(x>65535.0) c=65535U;                                                 \
74                                                                 else if(x<0.0) c=0;                                                     \
75                                                                 else c=(unsigned short)floor(x+0.5);
76
77
78
79
80
81
82
83 // A large rectangle
84
85 typedef struct{
86         long    top;
87         long    bottom;
88         long    left;
89         long    right;
90         }       PTRect;
91
92
93 struct PTPoint
94 {
95         double x;
96         double y;
97 };
98
99 typedef struct PTPoint PTPoint;
100
101 #define CopyPTPoint( to, from )       memcpy( &to, &from, sizeof( PTPoint ))
102 #define SamePTPoint( p, s )                       ((p).x == (s).x && (p).y == (s).y)
103
104 struct PTLine
105 {
106         PTPoint v[2];
107 };
108
109 typedef struct PTLine PTLine;
110
111
112 struct PTTriangle
113 {
114         PTPoint v[3];
115 };
116
117 typedef struct PTTriangle PTTriangle;
118
119
120
121
122 // Maximum number of controlpoints in a pair of images, which can be read
123 // via Barcodes
124
125 #define NUMPTS 21
126
127 // Randomization of feather in stitching tools
128
129 #define BLEND_RANDOMIZE         0.1
130
131
132
133
134 //----------------------- Structures -------------------------------------------
135
136 struct remap_Prefs{                                                             // Preferences Structure for remap
137                 long                    magic;                                  //  File validity check, must be 30
138                 int                             from;                                   // Image format source image
139                 int                             to;                                             // Image format destination image
140                 double                  hfov;                                   // horizontal field of view /in degrees
141                 double                  vfov;                                   // vertical field of view (usually ignored)
142                 } ;
143
144 typedef struct remap_Prefs rPrefs;
145
146 struct perspective_Prefs{                                               //  Preferences structure for tool perspective
147                 long                    magic;                                  //  File validity check, must be 40
148                 int                             format;                                 //  rectilinear or fisheye?
149                 double                  hfov;                                   //  Horizontal field of view (in degree)
150                 double                  x_alpha;                                //  New viewing direction (x coordinate or angle)
151                 double                  y_beta;                                 //  New viewing direction (y coordinate or angle)
152                 double                  gamma;                                  //  Angle of rotation
153                 int                             unit_is_cart;                   //  true, if viewing direction is specified in coordinates
154                 int                             width;                                  //  new width
155                 int                             height;                                 //  new height
156                 } ;
157                 
158 typedef struct perspective_Prefs pPrefs;
159
160
161 struct optVars{                                                                 //  Indicate to optimizer which variables to optimize
162                 int hfov;                                                               //  optimize hfov? 0-no 1-yes , etc
163                 int yaw;                                
164                 int pitch;                              
165                 int roll;                               
166                 int a;
167                 int b;
168                 int c;                                  
169                 int d;
170                 int e;
171                 };
172                 
173 typedef struct optVars optVars;
174
175
176 enum{                                                                           // Enumerates for stBuf.seam
177         _middle,                                                                // seam is placed in the middle of the overlap
178         _dest                                                                   // seam is places at the edge of the image to be inserted
179         };
180
181 enum{                                                                           // Enumerates for colcorrect
182         _colCorrectImage        = 1,
183         _colCorrectBuffer       = 2,
184         _colCorrectBoth         = 3,
185         };
186
187 struct stitchBuffer{                                            // Used describe how images should be merged
188         char                            srcName[256];           // Buffer should be merged to image; 0 if not.
189         char                            destName[256];          // Converted image (ie pano) should be saved to buffer; 0 if not
190         int                                     feather;                        // Width of feather
191         int                                     colcorrect;                     // Should the images be color corrected?
192         int                                     seam;                           // Where to put the seam (see above)
193         };
194
195 typedef struct stitchBuffer stBuf;
196
197 struct panControls{                                                     // Structure for realtime Panoeditor
198                 double panAngle;                                        // The amount by which yaw/pitch are changed per click
199                 double zoomFactor;                                      // The percentage for zoom in/out
200                 };
201                 
202                 
203 typedef struct panControls panControls;
204
205
206
207 enum{                                                                           // Enumerates for aPrefs.mode
208                 _readControlPoints,
209                 _runOptimizer,
210                 _insert,
211                 _extract,
212                 _useScript = 8,                                         // else use options
213         };
214
215 struct adjust_Prefs{                                            //      Preferences structure for tool adjust
216                 long                    magic;                          //      File validity check, must be 50
217                 long                    mode;                           //  What to do: create Panorama etc?
218                 Image                   im;                                     //  Image to be inserted/extracted
219                 Image                   pano;                           //  Panorama to be created/ used for extraction
220                 
221                 stBuf                   sBuf;
222                 fullPath                scriptFile;     // On Mac: Cast to FSSpec; else: full path to scriptFile
223                 };
224                 
225                 
226 typedef struct adjust_Prefs aPrefs;
227                 
228
229
230 union panoPrefs{
231                 cPrefs  cP;
232                 pPrefs  pP;
233                 rPrefs  rP;
234                 aPrefs  aP;
235                 panControls pc;
236                 };
237                 
238 typedef union panoPrefs panoPrefs;
239
240
241 struct size_Prefs{                                                              // Preferences structure for 'pref' dialog
242                 long                    magic;                                  //  File validity check; must be 70
243                 int                             displayPart;                    // Display cropped/framed image ?
244                 int                             saveFile;                               // Save to tempfile? 0-no, 1-yes
245                 fullPath                sFile;                                  // Full path to file (short name)
246                 int                             launchApp;                              // Open sFile ?
247                 fullPath                lApp;                                   // the Application to launch
248                 int                             interpolator;                   // Which interpolator to use 
249                 double                  gamma;                                  // Gamma correction value
250                 int                             noAlpha;                                // If new file is created: Don't save mask (Photoshop LE)
251                 int                             optCreatePano;                  // Optimizer creates panos? 0  no/ 1 yes
252                 } ;
253
254 typedef struct size_Prefs sPrefs;
255                 
256                 
257
258 #if 0
259 struct controlPoint{                                                    // Control Points to adjust images
260                 int  num[2];                                                    // Indices of Images 
261                 int      x[2];                                                          // x - Coordinates 
262                 int  y[2];                                                              // y - Coordinates 
263                 int  type;                                                              // What to optimize: 0-r, 1-x, 2-y
264                 } ;
265 #endif
266 struct controlPoint{                                                    // Control Points to adjust images
267                 int  num[2];                                                    // Indices of Images 
268                 double x[2];                                                            // x - Coordinates 
269                 double y[2];                                                            // y - Coordinates 
270                 int  type;                                                              // What to optimize: 0-r, 1-x, 2-y
271                 } ;
272
273 typedef struct controlPoint controlPoint;
274
275 struct CoordInfo{                                                               // Real World 3D coordinates
276                 int  num;                                                               // auxilliary index
277                 double x[3];
278                 int  set[3];
279                 };
280                 
281 typedef struct CoordInfo CoordInfo;
282
283 // Some useful macros for vectors
284
285 #define SCALAR_PRODUCT( v1, v2 )        ( (v1)->x[0]*(v2)->x[0] + (v1)->x[1]*(v2)->x[1] + (v1)->x[2]*(v2)->x[2] ) 
286 #define ABS_SQUARED( v )                        SCALAR_PRODUCT( v, v )
287 #define ABS_VECTOR( v )                         sqrt( ABS_SQUARED( v ) )
288 #define CROSS_PRODUCT( v1, v2, r )  { (r)->x[0] = (v1)->x[1] * (v2)->x[2] - (v1)->x[2]*(v2)->x[1];  \
289                                                                           (r)->x[1] = (v1)->x[2] * (v2)->x[0] - (v1)->x[0]*(v2)->x[2];  \
290                                                                           (r)->x[2] = (v1)->x[0] * (v2)->x[1] - (v1)->x[1]*(v2)->x[0]; }
291 #define DIFF_VECTOR( v1, v2, r )        {       (r)->x[0] = (v1)->x[0] - (v2)->x[0];  \
292                                                                                 (r)->x[1] = (v1)->x[1] - (v2)->x[1];  \
293                                                                                 (r)->x[2] = (v1)->x[2] - (v2)->x[2]; }
294 #define DIST_VECTOR( v1, v2 )           sqrt( ((v1)->x[0] - (v2)->x[0]) * ((v1)->x[0] - (v2)->x[0]) + \
295                                                                                   ((v1)->x[1] - (v2)->x[1]) * ((v1)->x[1] - (v2)->x[1]) + \
296                                                                                   ((v1)->x[2] - (v2)->x[2]) * ((v1)->x[2] - (v2)->x[2]) )
297
298 struct transformCoord{                                                  // 
299                 int nump;                                                               // Number of p-coordinates
300                 CoordInfo  *p;                                                  // Coordinates "as is"
301                 int numr;                                                               // Number of r-coordinates
302                 CoordInfo  *r;                                                  // Requested values for coordinates
303                 } ;
304         
305 typedef struct transformCoord transformCoord;
306
307 struct  tMatrix{
308                 double alpha;
309                 double beta;
310                 double gamma;
311                 double x_shift[3];
312                 double scale;
313                 };
314                 
315 typedef struct tMatrix tMatrix;
316
317                 
318                 
319         
320         
321
322
323 struct MakeParams{                                                              // Actual parameters used by Xform functions for pano-creation
324         double  scale[2];                                                       // scaling factors for resize;
325         double  shear[2];                                                       // shear values
326         double  rot[2];                                                         // horizontal rotation params
327         void    *perspect[2];                                           // Parameters for perspective control functions
328         double  rad[6];                                                         // coefficients for polynomial correction (0,...3) and source width/2 (4) and correction radius (5)     
329         double  mt[3][3];                                                       // Matrix
330         double  distance;
331         double  horizontal;
332         double  vertical;
333         };
334
335 struct LMStruct{                                                                // Parameters used by the Levenberg Marquardt-Solver
336         int                     m;                                                              
337         int                     n;
338         double          *x;
339         double          *fvec;
340         double          ftol;
341         double          xtol;
342         double          gtol;
343         int             maxfev; 
344         double          epsfcn;
345         double          *diag;
346         int             mode;   
347         double          factor;
348         int                     nprint;
349         int                     info;
350         int                     nfev;
351         double          *fjac;
352         int                     ldfjac;
353         int             *ipvt;
354         double          *qtf;
355         double          *wa1;
356         double          *wa2;
357         double          *wa3;
358         double          *wa4;
359         };
360
361 // function to minimize in Levenberg-Marquardt solver
362
363 typedef         int (*lmfunc)();        
364
365 struct triangle
366 {
367         int vert[3];    // Three vertices from list
368         int nIm;                // number of image for texture mapping
369 };
370
371 typedef struct triangle triangle;
372
373
374
375
376 struct AlignInfo{                                                       // Global data structure used by alignment optimization
377         Image                           *im;                            // Array of Pointers to Image Structs
378         optVars                         *opt;                           // Mark variables to optimize
379         int                                     numIm;                          // Number of images 
380         controlPoint            *cpt;                           // List of Control points
381         triangle                        *t;                                     // List of triangular faces
382         int                                     nt;                                     // Number of triangular faces
383         int                             numPts;                         // Number of Control Points
384         int                                     numParam;                       // Number of parameters to optimize
385         Image                           pano;                           // Panoramic Image decription
386         stBuf                           st;                                     // Info on how to stitch the panorama
387         void                            *data;
388         lmfunc                          fcn;
389         sPrefs                          sP;     
390         CoordInfo                       *cim;                           // Real World coordinates
391         };  
392
393 typedef struct AlignInfo AlignInfo;
394
395 struct OptInfo{
396         int numVars;                                    // Number of variables to fit
397         int numData;                                    // Number of data to fit to
398         int (*SetVarsToX)(double *x);   // Translate variables to x-values
399         int (*SetXToVars)(double *x);   // and reverse
400         lmfunc fcn;                                             // Levenberg Marquardt function measuring quality
401         char message[256];                              // info returned by LM-optimizer
402         };
403         
404 typedef struct OptInfo OptInfo;
405
406
407
408 struct VRPanoOptions
409 {
410         int                     width;
411         int                     height;
412         double          pan;
413         double          tilt;
414         double          fov;
415         int             codec;
416         int             cquality;
417         int                     progressive;
418 };
419
420 typedef struct VRPanoOptions VRPanoOptions;
421
422
423 struct MultiLayerImage
424 {
425         Image   im;
426         int             numLayers;
427         Image   *Layer;
428         PTRect  *LayerRect;
429 };
430
431 typedef struct MultiLayerImage MultiLayerImage;
432
433
434         
435         
436
437
438 // Transformation function type (we have only one...)
439
440 typedef         void (*trfn)( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );
441
442
443 // Function descriptor to be executed by exec_function
444 struct fDesc {
445         trfn    func;                   // The function to be called
446         void    *param;                 // The parameters to be used
447         };              
448
449 typedef struct fDesc fDesc;
450
451 #define SetDesc(fD,f,p)         fD.func = f; fD.param = p
452
453 // Panorama tool type
454
455 typedef         void (*fnPtr)(TrformStr *TrPtr);
456
457
458 // Filter function type
459
460 typedef unsigned char (*flfn)( unsigned char srcPixel, int xc, int yc, void *params );
461
462
463 // Interpolating functions for resampler
464
465 typedef         void (*intFunc)( unsigned char *dst,    unsigned char **rgb,
466                                                         register double Dx, 
467                                                         register double Dy,
468                                                         int color, int SamplesPerPixel);
469
470
471
472
473
474 // Gamma Correction
475
476 struct PTGamma{
477         double *DeGamma;
478         unsigned short *Gamma;
479         int             ChannelSize;
480         int     ChannelStretch;
481         int             GammaSize;
482         };
483
484 typedef struct PTGamma PTGamma;
485
486 extern PTGamma glu;
487
488
489 // Some macros to find out more about images
490
491 #define GetBitsPerChannel( im, x )              switch( (im)->bitsPerPixel )    \
492                                                                         {                                                                       \
493                                                                                 case 24:        x =  8; break;          \
494                                                                                 case 32:        x =  8; break;          \
495                                                                                 case 48:        x = 16; break;          \
496                                                                                 case 64:        x = 16; break;          \
497                                                                                 default:        x =  8; break;          \
498                                                                         }                                                                                               
499
500 #define GetChannels( im, x )            switch( (im)->bitsPerPixel )            \
501                                                                         {                                                                       \
502                                                                                 case 24:        x =  3; break;          \
503                                                                                 case 32:        x =  4; break;          \
504                                                                                 case 48:        x =  3; break;          \
505                                                                                 case 64:        x =  4; break;          \
506                                                                                 default:        x =  3; break;          \
507                                                                         }                                                                                               
508
509                                                                         
510
511 //---------------------------------- Functions identical in all platforms ------------------------
512
513
514 void    dispatch        (TrformStr *TrPtr, sPrefs *s);     // Entry into platform independent code
515 void    DoTransForm     (TrformStr *TrPtr, panoPrefs *p );
516
517 void setLibToResFile  ( void );                 // MacOS: Get resources from shared lib
518 void unsetLibToResFile( void );                 // MacOS: Don't get resources from shared lib
519
520 enum{                                   // Enumerates used by Progress and infoDlg
521         _initProgress,          // display message "argument"
522         _setProgress,           // display progress (argument is percentage converted to string)
523         _disposeProgress,       // dispose progress indicator
524         _idleProgress           // do nothing; on Mac: call waitnextevent;
525         };
526
527 int     Progress( int command, char* argument );        // Progress Reporting 
528 int     infoDlg ( int command, char* argument );        // Display info: same argumenmts as progress
529 void    PrintError( char* fmt, ...);                            // Error Reporting
530
531 int     ccommand( char ***argvPtr);                                     // Shell for standalone programs
532
533
534 //  Panorama Tool functions
535
536
537 void    perspective     (TrformStr *TrPtr, pPrefs *p);  
538 void    correct         (TrformStr *TrPtr, cPrefs *c);  
539 void    remap           (TrformStr *TrPtr, rPrefs *r); 
540 void    adjust          (TrformStr *TrPtr, aPrefs *a); 
541 void    pan                     (TrformStr *TrPtr, panControls *pc);
542
543
544
545
546 // Set Struct defaults
547
548 void    SetPrefDefaults                 (panoPrefs *prPtr,  int selector);
549 void    SetCorrectDefaults              ( cPrefs *p );
550 void    SetAdjustDefaults               ( aPrefs *p );
551 void    SetRemapDefaults                ( rPrefs *p );
552 void    SetPerspectiveDefaults  ( pPrefs *p );
553 void    SetImageDefaults                ( Image *im);
554 void    SetOptDefaults                  ( optVars *opt );
555 void    SetPanDefaults                  ( panControls *pc);
556 void    SetSizeDefaults                 ( sPrefs *pref);
557 void    SetStitchDefaults               ( stBuf *sbuf);
558 void    SetVRPanoOptionsDefaults( VRPanoOptions *v);
559 void    SettMatrixDefaults              ( tMatrix *t );
560 void    SetCoordDefaults                ( CoordInfo *c, int num);
561
562 int             SetAlignParams                  ( double *x );
563 int     SetLMParams                             ( double *x );
564 void    SetGlobalPtr                    ( AlignInfo *p );
565
566
567
568 // Dialogs
569 int     SetPrefs                        ( panoPrefs *p );
570 int             SetPanPrefs                     ( panControls *p );
571 int     SetCorrectPrefs         ( cPrefs *p );
572 int     SetRadialOptions        ( cPrefs *p );
573 int     SetHorizontalOptions( cPrefs *p );
574 int     SetVerticalOptions      ( cPrefs *p );
575 int     SetShearOptions         ( cPrefs *p );
576 int     SetScaleOptions         ( cPrefs *p );
577 int     SetLumOptions           ( cPrefs *p );
578 int     setSizePrefs            ( sPrefs *p, int can_resize );
579 int     SetRemapPrefs           ( rPrefs *p );
580 int     SetPerspectivePrefs     ( pPrefs *p );
581 int     SetAdjustPrefs          ( aPrefs *p );
582 int     SetInterpolator         ( sPrefs *p );
583 int     SetCreateOptions        ( aPrefs *p );
584 int     SetCutOptions           ( cPrefs *p );
585 int     SetFourierOptions       ( cPrefs *p );
586
587
588
589 // File I/O
590
591 int     readPrefs                       (char* p, int selector );                       // Preferences, same selector as dispatch
592 void    writePrefs                      (char* p, int selector );                       // Preferences, same selector as dispatch
593
594 int             LoadBufImage            ( Image *image, char *fname, int mode);
595 int             SaveBufImage            ( Image *image, char *fname );
596 int             writeTIFF                       ( Image *im, fullPath* fname);                  // On Mac: fname is FSSpec*                             
597 void    SaveOptions                     ( struct correct_Prefs * thePrefs );
598 int     LoadOptions                     ( struct correct_Prefs * thePrefs );
599 void    FindScript                      ( struct adjust_Prefs *thePrefs );
600 char*   LoadScript                      ( fullPath* scriptFile  );
601 int     WriteScript                     ( char* res, fullPath* scriptFile, int launch );
602 int     writePSD                        ( Image *im, fullPath* fname);                  // On Mac: fname is FSSpec*     
603 int     readPSD                         ( Image *im, fullPath* fname, int mode);
604 int     FindFile                        ( fullPath *fname );
605 int     SaveFileAs                      ( fullPath *fname, char *prompt, char *name );
606 void    ConvFileName            ( fullPath *fname,char *string);
607 int     writePSDwithLayer       ( Image *im, fullPath *fname);
608 int     addLayerToFile          ( Image *im, fullPath* sfile, fullPath* dfile, stBuf *sB);
609 void    showScript                      ( fullPath* scriptFile );
610 void    MakeTempName            ( fullPath *fspec, char *fname );
611 void    makePathForResult       ( fullPath *path );
612 int     makePathToHost          ( fullPath *path );
613 void    open_selection          ( fullPath *path );
614 int     readPSDMultiLayerImage( MultiLayerImage *mim, fullPath* sfile);
615 int     GetFullPath             (fullPath *path, char *filename); // Somewhat confusing, for compatibility easons
616 int     StringtoFullPath        (fullPath *path, char *filename);
617 int     IsTextFile                      ( char* fname );
618 int     readPositions           ( char* script, transformCoord *tP );
619 int     readImage                       ( Image *im, fullPath *sfile );
620 int     writeImage                      ( Image *im, fullPath *sfile );
621 int     writeJPEG                       ( Image *im, fullPath *sfile,   int quality, int progressive );
622 int     makeTempPath            ( fullPath *path );
623 int     writePNG                        ( Image *im, fullPath *sfile );
624 int     readPNG                         ( Image *im, fullPath *sfile );
625
626 #define FullPathtoString( path, string )                GetFullPath( path, string)
627
628
629
630
631 // Image manipulation
632
633 void    addAlpha                        ( Image *im ); 
634 void    transForm                       ( TrformStr *TrPtr, fDesc *fD, int color);
635 void    filter                          ( TrformStr *TrPtr, flfn func, void* params, int color);                
636 void    CopyImageData           ( Image *dest, Image *src );
637 void    laplace                         ( Image *im );
638 void    blurr                           ( Image *im );
639 void    MakePano                        ( TrformStr *TrPtr, aPrefs *aP, int nt, PTTriangle *ts,  PTTriangle *td);
640 void    ExtractStill            ( TrformStr *TrPtr , aPrefs *p );
641 int     HaveEqualSize           ( Image *im1, Image *im2 );
642 int     merge                           ( Image *dst, Image *src, int feather, int showprogress, int seam );
643 void    mergeAlpha                      ( Image *im, unsigned char *alpha, int feather, PTRect *theRect );
644 void    SetEquColor                     ( cPrefs *p );
645 void    CopyPosition            ( Image *to, Image *from );
646 int     isColorSpecific         ( cPrefs *p );
647 void    ThreeToFourBPP          ( Image *im );
648 void    FourToThreeBPP          ( Image *im );
649 int     SetUpGamma                      ( double pgamma, int psize);
650 int     cutTheFrame                     ( Image *dest, Image *src, int width, int height, int showprogress );
651 int     PositionCmp                     ( Image *im1, Image *im2 );
652 int     MorphImage                      ( Image *src, Image *dst, PTTriangle *ts, PTTriangle *td, int nt );
653 int     MorphImageFile          ( fullPath *sfile, fullPath *dfile, AlignInfo *g,int nIm );
654 int     blendImages                     ( fullPath *f0,  fullPath *f1, fullPath *result, double s );
655 int     InterpolateImage        ( Image *src, Image *dst, PTTriangle *ts, PTTriangle *td, int nt );
656 int     InterpolateTrianglesPerspective( AlignInfo *g, int nIm, double s, PTTriangle** t  );
657 int     InterpolateImageFile( fullPath *sfile, fullPath *dfile, AlignInfo *g,int nIm );
658 void    OneToTwoByte            ( Image *im );
659 void    TwoToOneByte            ( Image *im );
660 void    SetMakeParams           ( struct fDesc *stack, struct MakeParams *mp, Image *im , Image *pn, int color );
661 void    SetInvMakeParams        ( struct fDesc *stack, struct MakeParams *mp, Image *im , Image *pn, int color );
662 void    GetControlPointCoordinates(int i, double *x, double *y, AlignInfo *gl );
663
664
665 // Script Reading/Parsing/Writing
666
667 int     ParseScript                     ( char* script, AlignInfo *gl );
668 void    WriteResults            ( char* script, fullPath *sfile, AlignInfo *g, double ds( int i) , int launch);
669 int     readAdjust                      ( aPrefs *p,  fullPath* sfile , int insert);
670 void    readControlPoints       (char* script, controlPoint *c );
671 int             getVRPanoOptions        ( VRPanoOptions *v, char *line );
672 void    nextWord                        ( register char* word, char** ch );
673 void    nextLine                        ( register char* line, char** ch );
674 int     numLines                        ( char* script, char first );
675
676 // Memory
677
678 void    DisposeAlignInfo        ( AlignInfo *g );
679 void**  mymalloc                        ( long numBytes );                                      // Memory allocation, use Handles
680 void    myfree                          ( void** Hdl );                                         // free Memory, use Handles
681 int     SetDestImage            ( TrformStr *TrPtr, int width, int height) ;
682 void    DisposeMultiLayerImage( MultiLayerImage *mim );
683
684
685 // Math
686
687 void    RunLMOptimizer          ( OptInfo       *g);
688 void    RunBROptimizer          ( OptInfo       *g, double minStepWidth);
689 void    RunOverlapOptimizer ( AlignInfo *g);
690
691 void    SetMatrix                       ( double a, double b, double c , double m[3][3], int cl );
692 void    matrix_mult                     ( double m[3][3], double vector[3] );
693 void    matrix_inv_mult         ( double m[3][3], double vector[3] );
694 double  smallestRoot            ( double *p );
695 void    SetCorrectionRadius     ( cPrefs *cP );
696 int             lmdif                           ();
697 void    fourier                         ( TrformStr *TrPtr, cPrefs *cP );
698 unsigned short  gamma_correct( double pix );
699 int     EqualCPrefs( cPrefs *c1, cPrefs *c2 );
700 double  OverlapRMS                      ( MultiLayerImage *mim );
701 double  distSquared                     ( int num ); 
702 int             fcnPano();
703 void    doCoordinateTransform( CoordInfo *c, tMatrix *t );
704 void    findOptimumtMatrix( transformCoord *tP, tMatrix *tM, lmfunc f);
705 int     SolveLinearEquation2( double a[2][2], double b[2], double x[2] );
706 void    SortControlPoints( AlignInfo *g , int nIm);
707 void    noisefilter                     ( Image *dest, Image *src );    
708 void    fwiener                         ( TrformStr *TrPtr, Image *nf, Image *psf, double gamma, double frame );
709
710
711 // Triangulation
712 int     PointInTriangle( double x, double y, PTTriangle *T, double c[2] );
713 int     SetSourceTriangles( AlignInfo *g, int nIm, PTTriangle** t  );
714 int     SetDestTriangles( AlignInfo *g, int nIm, PTTriangle** t  );
715 int     InterpolateTriangles( AlignInfo *g, int nIm, double s, PTTriangle** t  );
716 int     DelaunayIteration( AlignInfo *g, int nIm );
717 int     PointInCircumcircle( double x, double y, PTTriangle *tC );
718 int     TriangulatePoints( AlignInfo *g, int nIm );
719 int     AddTriangle( triangle *t, AlignInfo *g );
720 int     RemoveTriangle( int nt, AlignInfo *g );
721 void    OrderVerticesInTriangle( int nt, AlignInfo *g );
722 void    SetTriangleCoordinates( triangle *t, PTTriangle *tC, AlignInfo *g );
723 int     TrianglesOverlap( PTTriangle *t0, PTTriangle *t1 );
724 int     LinesIntersect( PTLine *s0, PTLine *s1) ; 
725 double  PTDistance( PTPoint *s0, PTPoint *s1 );
726 int     PTPointInRectangle(  PTPoint *p, PTLine *r );
727 int     PTElementOf(  double x, double a, double b );
728 int     PTNormal( double *a, double *b, double *c, PTLine *s );
729 int     PTGetLineCrossing( PTLine *s0, PTLine *s1, PTPoint *ps );
730 int     ReduceTriangles( AlignInfo *g, int nIm );
731 double  PTAreaOfTriangle( PTTriangle *t );
732 int     normalToTriangle( CoordInfo *n, CoordInfo *v, triangle *t );
733
734
735
736
737 double GetBlendfactor( int d, int s, int feather );
738
739
740
741
742
743 void execute_stack              ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
744
745 void resize                             ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );           
746 void shear                              ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
747 void horiz                              ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
748 void vert                               ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
749 void radial                             ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
750
751
752 void persp_sphere               ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
753 void persp_rect                 ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
754
755
756 void rect_pano                  ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
757 void pano_rect                  ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
758 void pano_erect                 ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
759 void erect_pano                 ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
760 void sphere_cp_erect    ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
761 void sphere_tp_erect    ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
762 void erect_sphere_cp    ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
763 void rect_sphere_tp             ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
764 void sphere_tp_rect             ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
765 void sphere_cp_pano             ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
766 void rect_erect                 ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
767 void erect_rect                 ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
768 void erect_sphere_tp    ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
769 void mirror_erect               ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );    
770 void mirror_sphere_cp   ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
771 void mirror_pano                ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
772 void sphere_cp_mirror   ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );   
773 void sphere_tp_pano             ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );
774
775 void pano_sphere_tp             ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );
776
777 void rotate_erect               ( double x_dest, double y_dest, double* x_src, double* y_src, void* params );
778 void inv_radial                 ( double x_dest, double y_dest, double* x_src, double* y_src, void* params );
779
780 void vertical                   ( double x_dest, double y_dest, double* x_src, double* y_src, void* params );
781 void inv_vertical               ( double x_dest, double y_dest, double* x_src, double* y_src, void* params );
782 void deregister                 ( double x_dest, double y_dest, double* x_src, double* y_src, void* params );
783 void tmorph                             ( double x_dest,double  y_dest, double* x_src, double* y_src, void* params );
784
785
786
787 unsigned char radlum    ( unsigned char srcPixel, int xc, int yc, void *params );
788
789
790 extern TrformStr                *gTrPtr;
791 extern sPrefs                   *gsPrPtr;
792
793
794
795
796 // Endian stuff: Read and write numbers from and to memory (ptr)
797
798 #ifdef BIGENDIAN
799         #define LONGNUMBER( number, ptr )                                       *ptr++ = ((char*)(&number))[0]; \
800                                                                                                                 *ptr++ = ((char*)(&number))[1]; \
801                                                                                                                 *ptr++ = ((char*)(&number))[2]; \
802                                                                                                                 *ptr++ = ((char*)(&number))[3]; 
803
804         #define NUMBERLONG( number, ptr )                                       ((char*)(&number))[0] = *ptr++; \
805                                                                                                                 ((char*)(&number))[1] = *ptr++; \
806                                                                                                                 ((char*)(&number))[2] = *ptr++; \
807                                                                                                                 ((char*)(&number))[3] = *ptr++; 
808
809         #define SHORTNUMBER( number, ptr )                                      *ptr++ = ((char*)(&number))[0]; \
810                                                                                                                 *ptr++ = ((char*)(&number))[1]; \
811
812         #define NUMBERSHORT( number, ptr )                                      ((char*)(&number))[0] = *ptr++; \
813                                                                                                                 ((char*)(&number))[1] = *ptr++; \
814
815 #else
816         #define LONGNUMBER( number, ptr )                                       *ptr++ = ((char*)(&number))[3]; \
817                                                                                                                 *ptr++ = ((char*)(&number))[2]; \
818                                                                                                                 *ptr++ = ((char*)(&number))[1]; \
819                                                                                                                 *ptr++ = ((char*)(&number))[0]; 
820
821         #define NUMBERLONG( number, ptr )                                       ((char*)(&number))[3] = *ptr++; \
822                                                                                                                 ((char*)(&number))[2] = *ptr++; \
823                                                                                                                 ((char*)(&number))[1] = *ptr++; \
824                                                                                                                 ((char*)(&number))[0] = *ptr++; 
825
826         #define SHORTNUMBER( number, ptr )                                      *ptr++ = ((char*)(&number))[1]; \
827                                                                                                                 *ptr++ = ((char*)(&number))[0]; \
828
829         #define NUMBERSHORT( number, ptr )                                      ((char*)(&number))[1] = *ptr++; \
830                                                                                                                 ((char*)(&number))[0] = *ptr++; \
831
832
833
834 #endif // BIGENDIAN
835
836 // Cross platform file functions
837
838 #ifdef __Mac__
839
840         #include <Files.h>
841         #include "sys_mac.h"
842         
843         #define                 file_spec                                                       short
844         #define                 myopen( path, perm, fspec )                     ( FSpOpenDF( path, perm, &fspec ) != noErr )
845         #define                 mywrite( fspec, count, data )           FSWrite (fspec, &count, data) 
846         #define                 myread(  fspec, count, data )           FSRead  (fspec, &count, data) 
847         #define         myclose( fspec )                                        FSClose (fspec )
848         #define                 mycreate( path, creator, type )         FSpCreate( path, creator, type,0)
849         #define                 mydelete( path )                                        FSpDelete( path )
850         #define                 myrename( path, newpath )                       FSpRename (path, (newpath)->name)
851         #define                 write_text                                                      fsWrPerm
852         #define                 write_bin                                                       fsWrPerm
853         #define                 read_text                                                       fsRdPerm
854         #define                 read_bin                                                        fsRdPerm
855         #define                 read_write_text                                         fsRdWrPerm
856                         
857 #else // __Mac__, use ANSI-filefunctions
858         #define                 file_spec                                                       FILE*
859         #define                 myopen( path, perm, fspec )                     ( (fspec = fopen( (path)->name, perm )) == NULL)
860         #define                 mywrite( fspec, count, data )           count = fwrite( data, 1, count, fspec)
861         #define                 myread( fspec, count, data )            count = fread( data, 1, count, fspec ) 
862         #define         myclose( fspec )                                        fclose (fspec )
863         #define                 mycreate( path, creator, type )         
864         #define                 mydelete( path )                                        remove((path)->name )
865         #define                 myrename( path, newpath )                       rename ((path)->name, (newpath)->name)
866         #define                 write_text                                                      "w"
867         #define                 write_bin                                                       "wb"
868         #define                 read_text                                                       "r"
869         #define                 read_bin                                                        "rb"
870         #define                 read_write_text                                         "rw"
871         #define                 p2cstr( x )     
872         #define                 c2pstr( x )
873                                                                                                                         
874
875 #endif
876
877
878
879
880 #endif
881
882
883
884
885
886