Huge change, and it's almost working, too :)
[libopano.git] / src / panolib.c
index 8b9f30b..425a839 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "filter.h"
 #include "utils_math.h"
+#include "utils_image.h"
 
 #define DEG_TO_RAD(x) ((x) * 2.0 * M_PI / 360.0 )
 
 #define NATAN 2048
 #define NSQRT 2048
 
-int *atan_LU;
-int *sqrt_LU;
-
-
-void   matrix_matrix_mult      ( double m1[3][3],double m2[3][3],double result[3][3]);
-void   PV_transForm( TrformStr *TrPtr, double dist_r, double dist_e, int mt[3][3]);
-int    PV_sqrt( int x1, int x2 );
-
-
-/*
- * Extract image from pano in TrPtr->src using parameters in prefs (ignore
- * image parameters in TrPtr)
- */
-void PV_ExtractStill( TrformStr *TrPtr )
+static void matrix_matrix_mult( double m1[3][3],double m2[3][3],double result[3][3])
 {
-       /* field of view in rad */
-       double a;
-       double b;
-
-       double      p[2];
-       double          mt[3][3];
-       int             mi[3][3],i,k;
-
-       a =      DEG_TO_RAD( TrPtr->dest->hfov );       // field of view in rad         
-       b =      DEG_TO_RAD( TrPtr->src->hfov );
+       int i,k;
+       
+       for(i=0;i<3;i++)
+               for(k=0; k<3; k++)
+                       result[i][k] = m1[i][0] * m2[0][k] + m1[i][1] * m2[1][k] + m1[i][2] * m2[2][k];
+} /* void matrix_matrix_mult */
 
-       /* Set up the transformation matrix `mt' using Euler angles (somehow..) */
-       SetMatrix (DEG_TO_RAD (TrPtr->dest->pitch), /* alpha */
-                       DEG_TO_RAD (TrPtr->dest->yaw), /* beta */
-                       0.0, /* gamma */
-                       mt, /* output */
-                       1);
+/* Set matrix elements based on Euler angles a, b, c */
+static void set_transformation_matrix (double m[3][3],
+               double a, double b, double c)
+{
+       double mx[3][3], my[3][3], mz[3][3], dummy[3][3];
+       
 
+       // Calculate Matrices;
 
-       p[0] = (double)TrPtr->dest->width/ (2.0 * tan( a / 2.0 ) );
-       p[1] = (double) TrPtr->src->width / b;
+       mx[0][0] = 1.0 ;                                mx[0][1] = 0.0 ;                                mx[0][2] = 0.0;
+       mx[1][0] = 0.0 ;                                mx[1][1] = cos(a) ;                     mx[1][2] = sin(a);
+       mx[2][0] = 0.0 ;                                mx[2][1] =-mx[1][2] ;                   mx[2][2] = mx[1][1];
        
-       for(i=0; i<3; i++){
-               for(k=0; k<3; k++){
-                       mi[i][k] = 256 * mt[i][k];
-               }
-       }
+       my[0][0] = cos(b);                              my[0][1] = 0.0 ;                                my[0][2] =-sin(b);
+       my[1][0] = 0.0 ;                                my[1][1] = 1.0 ;                                my[1][2] = 0.0;
+       my[2][0] = -my[0][2];                   my[2][1] = 0.0 ;                                my[2][2] = my[0][0];
+       
+       mz[0][0] = cos(c) ;                     mz[0][1] = sin(c) ;                     mz[0][2] = 0.0;
+       mz[1][0] =-mz[0][1] ;                   mz[1][1] = mz[0][0] ;                   mz[1][2] = 0.0;
+       mz[2][0] = 0.0 ;                                mz[2][1] = 0.0 ;                                mz[2][2] = 1.0;
 
+       /* Calculate `m = mz * mx * my' */
 
-       PV_transForm( TrPtr, p[0], p[1], mi);
-       return;
-}
+       matrix_matrix_mult( mz, mx,     dummy);
+       matrix_matrix_mult( dummy, my, m);
+} /* void SetMatrix */
 
+/*
+ * Extract image from pano in TrPtr->src using parameters in prefs (ignore
+ * image parameters in TrPtr)
+ */
 typedef enum interpolator_e
 {
        NNEIGHBOUR,
        BILINEAR
 } interpolator_t;
 
-static int copy_pixel (Image *dest, Image *src,
+static int copy_pixel (ui_image_t *dest, const ui_image_t *src,
                int x_dest, int y_dest,
                double x_src_fp, double y_src_fp,
                interpolator_t interp)
 {
-       uint32_t pixel_value;
-       uint32_t *src_data  = (uint32_t *) (*src->data);
-       uint32_t *dest_data = (uint32_t *) (*dest->data);
+       uint32_t pixel_dest = (y_dest * dest->width) + x_dest;
 
        interp = NNEIGHBOUR;
 
@@ -102,12 +93,20 @@ static int copy_pixel (Image *dest, Image *src,
 
                if ((x_src < 0) || (x_src >= src->width)
                                || (y_src < 0) || (y_src >= src->height))
-                       pixel_value = 0x000000FF;
+               {
+                       dest->data[0][pixel_dest] = 0;
+                       dest->data[1][pixel_dest] = 0;
+                       dest->data[2][pixel_dest] = 0;
+               }
                else
-                       pixel_value = src_data[(y_src * src->width) + x_src];
-       }
+               {
+                       uint32_t pixel_src = (y_src * src->width) + x_src;
 
-       dest_data[(y_dest * dest->width) + x_dest] = pixel_value;
+                       dest->data[0][pixel_dest] = src->data[0][pixel_src];
+                       dest->data[1][pixel_dest] = src->data[1][pixel_src];
+                       dest->data[2][pixel_dest] = src->data[2][pixel_src];
+               }
+       }
 
        return (0);
 } /* int copy_pixel */
@@ -117,33 +116,37 @@ static int copy_pixel (Image *dest, Image *src,
 //    determined. If successful, TrPtr->success = 1. Memory for destination image
 //    must have been allocated and locked!
 
-void PV_transForm( TrformStr *TrPtr, double dist_r, double dist_e, int mt[3][3])
+int pl_extract_view (ui_image_t *view, const ui_image_t *pano,
+               double pitch, double yaw, double fov)
 {
-       int x_dest, y_dest;                     // Loop through destination image
-       unsigned char           *dest,*src;// Source and destination image data
-
-       // Variables used to convert screen coordinates to cartesian coordinates
+       int x_dest, y_dest; // Loop through destination image
 
-               
-       int                             dest_width_left = TrPtr->dest->width  / 2 ;  
-       int                             dest_height_top = TrPtr->dest->height / 2 ;
-       int                             src_width_left =  TrPtr->src->width   / 2 ;
-       int                             src_height_top =  TrPtr->src->height  / 2 ;
+       int dest_width_left = view->width  / 2 ;  
+       int dest_height_top = view->height / 2 ;
+       int src_width_left =  pano->width   / 2 ;
+       int src_height_top =  pano->height  / 2 ;
        
-       int                             v[3];
-       int                                     x_min, x_max, y_min, y_max;
+       double v[3];
+       int     x_min, x_max, y_min, y_max;
 
-       int                                     dr1, dr2, dr3;
+       /* The transformation matrix */
+       double tm[3][3];
 
-       dr1 = mt[2][0] * dist_r;
-       dr2 = mt[2][1] * dist_r;
-       dr3 = mt[2][2] * dist_r;
-       
-       dest = *TrPtr->dest->data;
-       src  = *TrPtr->src->data; // is locked
+       double dist_r;
+       double dist_e;
 
-       x_min = -dest_width_left; x_max = TrPtr->dest->width - dest_width_left;
-       y_min = -dest_height_top; y_max = TrPtr->dest->height - dest_height_top;
+       { /* What the fuck does this? -octo */
+               double a = DEG_TO_RAD (fov);
+               double b = 2.0 * M_PI; /* DEG_TO_RAD (360.0) */
+
+               dist_r = ((double) view->width) / (2.0 * tan (a / 2.0));
+               dist_e = ((double) pano->width) / b;
+       }
+
+       set_transformation_matrix (tm, DEG_TO_RAD (pitch), DEG_TO_RAD (yaw), 0.0);
+
+       x_min = -dest_width_left; x_max = view->width - dest_width_left;
+       y_min = -dest_height_top; y_max = view->height - dest_height_top;
 
        for(y_dest = y_min; y_dest < y_max; y_dest++)
        {
@@ -152,24 +155,21 @@ void PV_transForm( TrformStr *TrPtr, double dist_r, double dist_e, int mt[3][3])
                        double x_src_fp;
                        double y_src_fp;
 
-                       v[0] = mt[0][0] * x_dest + mt[1][0] * y_dest + dr1;
-                       v[1] = mt[0][1] * x_dest + mt[1][1] * y_dest + dr2;
-                       v[2] = mt[0][2] * x_dest + mt[1][2] * y_dest + dr3;
-
-                       v[0] = v[0] >> 8; v[2] = v[2] >> 8;
-                       v[1] = v[1] >> 8;
+                       v[0] = tm[0][0] * x_dest + tm[1][0] * y_dest + tm[2][0] * dist_r;
+                       v[1] = tm[0][1] * x_dest + tm[1][1] * y_dest + tm[2][1] * dist_r;
+                       v[2] = tm[0][2] * x_dest + tm[1][2] * y_dest + tm[2][2] * dist_r;
 
                        x_src_fp = dist_e * atan2 (v[0], v[2]);
                        y_src_fp = dist_e * atan2 (v[1], sqrt (v[2] * v[2] + v[0] * v[0]));
 
-                       copy_pixel (TrPtr->dest, TrPtr->src,
+                       copy_pixel (view, pano,
                                        dest_width_left + x_dest, dest_height_top + y_dest,
                                        src_width_left + x_src_fp, src_height_top + y_src_fp,
                                        NNEIGHBOUR);
                }
        }
        
-       return;
+       return (0);
 }
 
 #if 0
@@ -187,45 +187,6 @@ void matrix_inv_mult( double m[3][3], double vector[3] )
 }
 #endif
 
-// Set matrix elements based on Euler angles a, b, c
-
-void SetMatrix( double a, double b, double c , double m[3][3], int cl )
-{
-       double mx[3][3], my[3][3], mz[3][3], dummy[3][3];
-       
-
-       // Calculate Matrices;
-
-       mx[0][0] = 1.0 ;                                mx[0][1] = 0.0 ;                                mx[0][2] = 0.0;
-       mx[1][0] = 0.0 ;                                mx[1][1] = cos(a) ;                     mx[1][2] = sin(a);
-       mx[2][0] = 0.0 ;                                mx[2][1] =-mx[1][2] ;                   mx[2][2] = mx[1][1];
-       
-       my[0][0] = cos(b);                              my[0][1] = 0.0 ;                                my[0][2] =-sin(b);
-       my[1][0] = 0.0 ;                                my[1][1] = 1.0 ;                                my[1][2] = 0.0;
-       my[2][0] = -my[0][2];                   my[2][1] = 0.0 ;                                my[2][2] = my[0][0];
-       
-       mz[0][0] = cos(c) ;                     mz[0][1] = sin(c) ;                     mz[0][2] = 0.0;
-       mz[1][0] =-mz[0][1] ;                   mz[1][1] = mz[0][0] ;                   mz[1][2] = 0.0;
-       mz[2][0] = 0.0 ;                                mz[2][1] = 0.0 ;                                mz[2][2] = 1.0;
-
-       /* Calculate `m = mz * mx * my' */
-
-       if( cl )
-               matrix_matrix_mult( mz, mx,     dummy);
-       else
-               matrix_matrix_mult( mx, mz,     dummy);
-       matrix_matrix_mult( dummy, my, m);
-} /* void SetMatrix */
-
-void matrix_matrix_mult( double m1[3][3],double m2[3][3],double result[3][3])
-{
-       register int i,k;
-       
-       for(i=0;i<3;i++)
-               for(k=0; k<3; k++)
-                       result[i][k] = m1[i][0] * m2[0][k] + m1[i][1] * m2[1][k] + m1[i][2] * m2[2][k];
-} /* void matrix_matrix_mult */
-
 #define ID_0 0xff
 #define ID_1 0xd8
 #define ID_2 0xff