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