home *** CD-ROM | disk | FTP | other *** search
/ ftp.hitl.washington.edu / ftp.hitl.washington.edu.tar / ftp.hitl.washington.edu / pub / people / habib / kodak / distortionJohn.cc < prev    next >
C/C++ Source or Header  |  2000-04-18  |  5KB  |  119 lines

  1. #include <stdio.h>
  2. #include <iostream.h>
  3. #include <math.h>
  4. #include <malloc.h>
  5. #include <string.h>
  6. #include "define.h"
  7.  
  8. int imm_width[1];
  9. int imm_height[1];
  10. double R =  56.5 ; //cm Dome Radius
  11. double D = 280;    // cm DomeCenterDistanceToProjector
  12.  
  13. struct DistTex 
  14. {
  15.     float x[DefNumOfEdgesInX][DefNumOfEdgesInY];
  16.     float y[DefNumOfEdgesInX][DefNumOfEdgesInY];
  17. } ;
  18.  
  19. extern struct DistTex Tex;
  20.  
  21. // Look at the hard file Distortion.
  22. correct_Textured_image(int image_width, int image_height)
  23. {
  24.     extern double cam_angle;
  25.     // extern unsigned char Distortion_buffer[1024][768][3];
  26.     // extern unsigned char Distortion_buffer_corrected[1024][768][3];
  27.     extern int NumOfSquaresInX;
  28.     extern int NumOfSquaresInY;
  29.     /* FILE *file;
  30.     if ((file = fopen ("res.txt", "w")) == NULL)
  31.         printf ("\n pas de chance l'ami ton ffile iest emmerde...\n");  */
  32.     
  33.     float phi = acos (R/D);
  34.   cout << "FOV possible du to the distance of the projector from the dome , phi = " << phi*180/3.14159 << endl;
  35.   //cam_angle = phi*360/3.14159;
  36.     float PhiImage = cam_angle * 3.14159 / 360.0; // phiImage is half the angle of the camera FOV (Not the Dome FOV, but the camera FOV).
  37.     int half_image_width  = image_width  / 2;
  38.     int half_image_height = image_height / 2;
  39.     int W = half_image_width;
  40.   float T = R*R/D; // See Hard Copy!!
  41.  
  42.     int ct = -1; // indice for the Tex.[ct][rt]
  43.     int rt = -1; // indice for the Tex.[ct][rt]
  44.     for (float PPx = 0 ; PPx<=image_width; PPx = PPx+ (image_width / (float) NumOfSquaresInX)) { // <= because 512 / 2 = 256 I want c to be = 0 then 255 then 511 (not 512!!).
  45.         ct++; // Incrementing the colone of the Edge of the grid used for texturing the dist. Correction
  46.         for ( float PPy=0 ; PPy<=image_height; PPy = PPy+(image_height / (float) NumOfSquaresInY)){
  47.             if (PPy == 0) rt = 0; else rt++; // Incrementing the row of the Edge of the grid used for texturing the dist. Correction
  48.             float Px = PPx - half_image_width;
  49.             float Py = PPy - half_image_height;
  50.             float P = sqrt (Px*Px + Py*Py) * T * tan(PhiImage) / W;
  51.       float teta = atan (P / (D-T));
  52.       float alpha = asin(D*sin(teta)/R);
  53.       float I = tan (alpha) * W / tan(PhiImage);
  54.       float Ix= I * Px / (sqrt (Px*Px + Py*Py));
  55.       float Iy= I * Py / (sqrt (Px*Px + Py*Py));
  56.       int  IIx= Ix + half_image_width;
  57.       int  IIy= Iy + half_image_height;
  58.             if ((IIx >= 0) && (IIx <image_width) && (IIy>=0) && (IIy < image_height)){
  59.                 Tex.x[ct][rt]   = IIx/(float)image_width;
  60.         Tex.y[ct][rt]   = IIy/(float)image_height;
  61.             }
  62.             /*fprintf (file, "%f %f\n", r, p);
  63.             if (ro == image_height/3)
  64.                 fprintf (file, "%d %d\n", co, xp);
  65.             /*if (ro == image_height/2)
  66.                 fprintf (file, "%d %d\n", x, xp); */
  67.         }
  68.     }
  69.      //fclose (file);
  70. }
  71.  
  72. // Look at the hard file Distortion.
  73. correct_image(int image_width, int image_height)
  74. {
  75.     extern double cam_angle;
  76.   extern unsigned char Distortion_buffer [DistWidth][DistHeight][3];
  77.   extern unsigned char Distortion_buffer_corrected [DistWidth][DistHeight][3];
  78.     // extern unsigned char Distortion_buffer[1024][768][3];
  79.     // extern unsigned char Distortion_buffer_corrected[1024][768][3];
  80.     FILE *file;
  81.   /*    if ((file = fopen ("res.txt", "w")) == NULL)
  82.       printf ("\n pas de chance l'ami ton ffile iest emmerde...\n"); */
  83.     
  84.     float phi = acos (R/D);
  85.     float PhiImage = cam_angle * 3.14159 / 360.0; // phiImage is half the angle of the camera FOV (Not the Dome FOV, but the camera FOV).
  86.     int half_image_width  = image_width  / 2;
  87.     int half_image_height = image_height / 2;
  88.     int W = half_image_width;
  89.   float T = (R*R)/D;
  90.  
  91.     for (int PPx= 0 ; PPx<image_width; PPx++)
  92.         for (int PPy=0 ; PPy<image_height; PPy++){
  93.             int Px = PPx - half_image_width;
  94.             int Py = PPy - half_image_height;
  95.             float P = sqrt (Px*Px + Py*Py) * T * tan(PhiImage) / W;
  96.       float teta = atan(P/(D-T));
  97.       float alpha = asin (D*sin(teta)/R);
  98.       float I = tan (alpha) * W / tan(PhiImage);
  99.       int Ix = I * Px / sqrt(Px*Px+Py*Py);
  100.       int Iy = I * Py / sqrt(Px*Px+Py*Py);
  101.       int IIx= Ix + half_image_width;
  102.       int IIy= Iy + half_image_height;
  103.             if ((IIx >= 0) && (IIx <image_width) && (IIy>=0) && (IIy < image_height)){
  104.         Distortion_buffer_corrected[PPx][PPy][0] = Distortion_buffer[IIx][IIy][0];
  105.                 Distortion_buffer_corrected[PPx][PPy][1] = Distortion_buffer[IIx][IIy][1];
  106.                 Distortion_buffer_corrected[PPx][PPy][2] = Distortion_buffer[IIx][IIy][2]; 
  107.             }
  108.             /*fprintf (file, "%f %f\n", r, p);
  109.             if (ro == image_height/3)
  110.                 fprintf (file, "%d %d\n", co, xp);
  111.             /*if (ro == image_height/2)
  112.                 fprintf (file, "%d %d\n", x, xp); */
  113.  
  114.  
  115.         }
  116.   //fclose (file);
  117. }
  118.  
  119.