VIDEO1.C

Go to the documentation of this file.
00001 /* --
00002 OpenFX version 1.0 - Modelling, Animation and Rendering Package
00003 Copyright (C) 2000 OpenFX Development Team
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019 You may contact the OpenFX development team via elecronic mail
00020 at core@openfx.org, or visit our website at http://openfx.org for
00021 further information and support details.
00022 -- */
00023 
00024 /* video1.c */
00025 
00026 static void SetColour(long *colour, HWND parent){
00027  CHOOSECOLOR cc;
00028  static COLORREF CustColours[16]={
00029    RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00030    RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00031    RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00032    RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00033  cc.lStructSize=sizeof(CHOOSECOLOR);
00034  cc.hwndOwner=parent;
00035  cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00036  cc.lpCustColors=(LPDWORD)CustColours;
00037  cc.Flags= CC_FULLOPEN | CC_RGBINIT;
00038  cc.lCustData=(DWORD)0;
00039  if(ChooseColor(&cc)){
00040    colour[0]=(unsigned char)GetRValue(cc.rgbResult);
00041    colour[1]=(unsigned char)GetGValue(cc.rgbResult);
00042    colour[2]=(unsigned char)GetBValue(cc.rgbResult);
00043  }
00044 }
00045 
00046 /* h is 0->360  s and v are 0-1 */
00047 void Hsv2Rgb(double h, double s, double v,
00048              unsigned char *r, unsigned char *g, unsigned char *b){
00049  double rr,gg,bb,hh,ss,vv,maxcol,mincol;
00050  hh=(double)h; ss=(double)s; vv=(double)v;
00051  mincol=vv*(1.0-ss);
00052  if(hh <= 120.0){bb=mincol;
00053    if(hh <=  60){rr=vv; gg=mincol+hh*(vv-mincol)/(120.0-hh);}
00054    else         {gg=vv; rr=mincol+(120.0-hh)*(vv-mincol)/hh;}
00055  }
00056  else if( hh <= 240){rr=mincol;
00057    if(hh <= 180){gg=vv; bb=mincol+(hh-120.0)*(vv-mincol)/(240.0-hh);}
00058    else         {bb=vv; gg=mincol+(240.0-hh)*(vv-mincol)/(hh-120.0);}
00059  }
00060  else {gg=mincol;
00061    if(hh <= 300){bb=vv; rr=mincol+(hh-240.0)*(vv-mincol)/(360.0-hh);}
00062    else         {rr=vv; bb=mincol+(360.0-hh)*(vv-mincol)/(hh-240.0);}
00063  }
00064  rr = rr*255.0; *r=(unsigned char)min(255,rr);
00065  gg = gg*255.0; *g=(unsigned char)min(255,gg);
00066  bb = bb*255.0; *b=(unsigned char)min(255,bb);
00067 }
00068 
00069 void Rgb2Hsv(unsigned char r, unsigned char g, unsigned char b,
00070              double *h, double *s, double *v){
00071  double rr,gg,bb,hh,ss,vv,maxcol,mincol;
00072  rr=(double)r; gg=(double)g; bb=(double)b;
00073  rr /= 255.0; gg /= 255.0; bb /= 255.0;
00074  maxcol=max(rr,max(gg,bb));
00075  mincol=min(rr,min(gg,bb));
00076  vv=maxcol;
00077  if(maxcol == mincol){ss=0; hh=0;}
00078  else{
00079   ss=(maxcol-mincol)/maxcol;
00080   if(mincol == bb)
00081     hh = 120.0*(gg-mincol)/(rr+gg-2.0*mincol);
00082   else if(mincol == rr)
00083     hh = 120.0*(1.0+(bb-mincol)/(bb+gg-2.0*mincol));
00084   else
00085     hh = 120.0*(2.0+(rr-mincol)/(rr+bb-2.0*mincol));
00086  }
00087   *h=hh;
00088   *s=ss;
00089   *v=vv;
00090 }
00091 
00092 #ifndef PI
00093 #define PI   3.1415926
00094 #endif
00095 #define PI2  PI/2
00096 
00097 typedef double vector[3];
00098 
00099 #define VECCOPY(a,b)    { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; }
00100 #define INVERT(a)       { a[0] = -a[0]; a[1] = -a[1]; a[2] = -a[2]; }
00101 #define VECSUB(a,b,c)   { c[0]=a[0]-b[0]; c[1]=a[1]-b[1]; c[2]=a[2]-b[2];}
00102 #define VECSUM(a,b,c)   { c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2];}
00103 #define VECSCALE(a,b,c) { c[0]=(a)*b[0]; c[1]=(a)*b[1]; c[2]=(a)*b[2];}
00104 #define DOT(a,b)        ( (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]) )
00105 #define CROSS(v1,v2,r)  { \
00106                           r[0] = (v1[1]*v2[2]) - (v2[1]*v1[2]);  \
00107                           r[1] = (v1[2]*v2[0]) - (v1[0]*v2[2]);  \
00108                           r[2] = (v1[0]*v2[1]) - (v2[0]*v1[1]);  \
00109                         }
00110 #define TOL 0.1
00111 
00112 double hitpoint(vector n, vector x, vector y, vector pb, vector pf,
00113                 double * a, double * b){
00114 vector v1,p;
00115 double mu,det,ve1,ve2,ve3,vp1,vp2,vp3,p1,p2,p3;
00116 VECSUB(pb,pf,v1)
00117 mu=DOT(n,v1);
00118 VECSCALE(mu,n,v1)
00119 VECSUM(pf,v1,p)
00120 ve1=x[0]; ve2=x[1]; ve3=x[2];
00121 vp1=y[0]; vp2=y[1]; vp3=y[2];
00122 p1=p[0]-pb[0];
00123 p2=p[1]-pb[1];
00124 p3=p[2]-pb[2];
00125 det=ve1*vp2-vp1*ve2 ;
00126 if(det > TOL || det < -TOL)  /* e-10 appears not to work */
00127   {  *a=( vp2*p1-vp1*p2)/det;
00128      *b=(-ve2*p1+ve1*p2)/det;
00129      goto HIT;
00130   }
00131 det=ve1*vp3-vp1*ve3 ;
00132 if(det > TOL || det < -TOL)
00133   {  *a=( vp3*p1-vp1*p3)/det;
00134      *b=(-ve3*p1+ve1*p3)/det;
00135      goto HIT;
00136   }
00137 det=ve2*vp3-vp2*ve3 ;
00138 if(det > TOL || det < -TOL)
00139   {  *a=( vp3*p2-vp2*p3)/det;
00140      *b=(-ve3*p2+ve2*p3)/det;
00141      goto HIT;
00142   }
00143 *a = *b = 0.0;
00144 HIT:
00145 return mu;
00146 }
00147 
00148 long MapFrom(vector p,
00149              vector Mp, vector Mdx, vector Mdy, vector Mn,
00150              fullscreenbuffer *S,
00151              long Xmax, long Ymax,
00152              double *red,double *green,double *blue){
00153  long  ialpha,ibeta,i,j,k,kl,kr,kt,kb,pixel;
00154  double alpha,beta,a,b,cc0,cc1,cc2,cl0,cl1,cl2,
00155            cr0,cr1,cr2,ct0,ct1,ct2,cb0,cb1,cb2;
00156  unsigned char *pP,*pR,*pG,*pB;
00157  j=Xmax;
00158  hitpoint(Mn,Mdx,Mdy,Mp,p,&alpha,&beta);
00159  if(alpha < -0.01 || alpha > 1.01)return -1;
00160  if(beta  < -0.02 || beta  > 1.02)return -1;
00161  alpha=Xmax*alpha;
00162  beta =Ymax*beta;
00163  ialpha = (long)alpha; alpha -= (double)ialpha;
00164  ibeta  = (long)beta;  beta  -= (double)ibeta;
00165  if(ialpha < 0 || ialpha >= Xmax)return 0;
00166  if(ibeta  < 0 || ibeta  >= Ymax)return 0;
00167  k =ibeta*j+ialpha;
00168  if(ialpha <   1)kl=k; else kl=k-1;
00169  if(ialpha > j-2)kr=k; else kr=k+1;
00170  if(ibeta  <   1)kb=k; else kb=k-j;
00171  if(ibeta  > Ymax-2)kt=k; else kt=k+j;
00172  cc0 = (double)S[k].R;  cc1 = (double)S[k].G;  cc2 = (double)S[k].B;
00173  cl0 = (double)S[kl].R; cl1 = (double)S[kl].G; cl2 = (double)S[kl].B;
00174  cr0 = (double)S[kr].R; cr1 = (double)S[kr].G; cr2 = (double)S[kr].B;
00175  ct0 = (double)S[kt].R; ct1 = (double)S[kt].G; ct2 = (double)S[kt].B;
00176  cb0 = (double)S[kb].R; cb1 = (double)S[kb].G; cb2 = (double)S[kb].B;
00177  if(alpha > 0.5){
00178    alpha = alpha-0.5;
00179    if(beta > 0.5){
00180      beta = beta-0.5;
00181      *red   = (cr0-cc0)*alpha+(ct0-cc0)*beta+cc0;
00182      *green = (cr1-cc1)*alpha+(ct1-cc1)*beta+cc1;
00183      *blue  = (cr2-cc2)*alpha+(ct2-cc2)*beta+cc2;
00184    }
00185    else{
00186      beta = 0.5-beta;
00187      *red   = (cr0-cc0)*alpha+(cb0-cc0)*beta+cc0;
00188      *green = (cr1-cc1)*alpha+(cb1-cc1)*beta+cc1;
00189      *blue  = (cr2-cc2)*alpha+(cb2-cc2)*beta+cc2;
00190    }
00191  }
00192  else{
00193    alpha = 0.5-alpha;
00194    if(beta > 0.5){
00195      beta = beta-0.5;
00196      *red   = (cl0-cc0)*alpha+(ct0-cc0)*beta+cc0;
00197      *green = (cl1-cc1)*alpha+(ct1-cc1)*beta+cc1;
00198      *blue  = (cl2-cc2)*alpha+(ct2-cc2)*beta+cc2;
00199    }
00200    else{
00201      beta = 0.5-beta;
00202      *red   = (cl0-cc0)*alpha+(cb0-cc0)*beta+cc0;
00203      *green = (cl1-cc1)*alpha+(cb1-cc1)*beta+cc1;
00204      *blue  = (cl2-cc2)*alpha+(cb2-cc2)*beta+cc2;
00205    }
00206  }
00207  return 1;
00208 }
00209 
00210 static void scal(double t[4][4], double sx, double sy, double sz){
00211  long i,j;
00212  for(i=0;i<4;i++) for(j=0;j<4;j++) t[i][j]=0.0;
00213  t[0][0]=sx; t[1][1]=sy; t[2][2]=sz; t[3][3]=1;
00214  return;
00215 }
00216 
00217 static void rotz(double tr[4][4], double ang){
00218  short i,j;
00219  for(i=0;i<4;i++)
00220  for(j=0;j<4;j++)
00221  {
00222   tr[i][j]=0.0;
00223   if(i == j)tr[i][j]=1.0;
00224  }
00225   tr[0][0]= cos(ang);
00226   tr[0][1]=-sin(ang);
00227   tr[1][0]= sin(ang);
00228   tr[1][1]= cos(ang);
00229   return;
00230 }
00231 
00232 static void roty(double tr[4][4], double ang){
00233  short i,j;
00234  for(i=0;i<4;i++)
00235  for(j=0;j<4;j++)
00236  {
00237   tr[i][j]=0.0;
00238   if(i == j)tr[i][j]=1.0;
00239  }
00240   tr[0][0]= cos(ang);
00241   tr[0][2]=-sin(ang);
00242   tr[2][0]= sin(ang);
00243   tr[2][2]= cos(ang);
00244   return;
00245 }
00246 
00247 static void rotx(double tr[4][4], double ang){
00248  long i,j;
00249  for(i=0;i<4;i++)
00250  for(j=0;j<4;j++)
00251  {
00252   tr[i][j]=0.0;
00253   if(i == j)tr[i][j]=1.0;
00254  }
00255   tr[1][1]= cos(ang);
00256   tr[1][2]=-sin(ang);
00257   tr[2][1]= sin(ang);
00258   tr[2][2]= cos(ang);
00259   return;
00260 }
00261 
00262 static void tram(double t[4][4], double dx, double dy, double dz){
00263  short i,j;
00264  for(i=0;i<4;i++)
00265  for(j=0;j<4;j++)
00266  {
00267   t[i][j]=0.0;
00268   if(i == j)t[i][j]=1.0;
00269  }
00270  t[0][3]=dx;
00271  t[1][3]=dy;
00272  t[2][3]=dz;
00273  t[3][3]=1;
00274  return;
00275 }
00276 
00277 static void m4by4(double t1[4][4], double t2[4][4], double tr[4][4]){
00278  short n=4,i,j,k;
00279  for(i=0;i<4;i++)
00280  for(j=0;j<4;j++)
00281  {
00282   tr[i][j]=0.0;
00283   for(k=0;k<4;k++)tr[i][j]=tr[i][j]+t1[i][k]*t2[k][j];
00284  }
00285  return;
00286 }
00287 
00288 static void m4by1(double t4[4][4], vector v, vector vv){
00289  vv[0]=t4[0][0]*v[0]+t4[0][1]*v[1]+t4[0][2]*v[2]+t4[0][3];
00290  vv[1]=t4[1][0]*v[0]+t4[1][1]*v[1]+t4[1][2]*v[2]+t4[1][3];
00291  vv[2]=t4[2][0]*v[0]+t4[2][1]*v[1]+t4[2][2]*v[2]+t4[2][3];
00292  return;
00293 }
00294 
00295 static void c4to4(double tin[4][4], double tout[4][4]){
00296  short i,j;
00297  for(i=0;i<4;i++)
00298  for(j=0;j<4;j++)
00299  tout[i][j]=tin[i][j];
00300 }
00301 
00302 static void null_transform(double t[4][4]){
00303  short i,j;
00304  for(i=0;i<4;i++)
00305  for(j=0;j<4;j++)
00306  if(i == j)t[i][j]=1.0;
00307  else      t[i][j]=0.0;
00308 }
00309 
00310 static void rotate_round_vector(double angle, vector v, double t[4][4]){
00311   double dx,dy,dz,phi,theta,dxy;
00312   double t1[4][4],t2[4][4],t3[4][4];
00313   angle *= PI/180.0;
00314   dx=v[0]; dy=v[1]; dz=v[2];
00315   dxy=dx*dx+dy*dy;
00316   if(dxy < 0.00001){ /* vertical so just rotate about z  and return */
00317     if(dz > 0.0)rotz(t, angle);
00318     else        rotz(t,-angle);
00319     return;
00320   }
00321   dxy=sqrt(dxy);
00322   if     (dx == 0.0 && dy > 0.0)phi =  PI2;
00323   else if(dx == 0.0 && dy < 0.0)phi = -PI2;
00324   else phi = atan2(dy,dx);
00325   theta = atan2(dz,dxy);
00326   rotz(t3, -phi);
00327   roty(t2, -theta);
00328   m4by4(t2,t3,t1);
00329   rotx(t2, angle);
00330   m4by4(t2,t1,t3);
00331   roty(t2,theta);
00332   m4by4(t2,t3,t1);
00333   rotz(t2,phi);
00334   m4by4(t2,t1,t);
00335 }
00336 
00337 static BOOL normalise(vector v){
00338   double n,nn;
00339   n= (double)((v[0]*v[0]) + (v[1]*v[1]) + (v[2]*v[2]));
00340   if(n < 1.e-10)return FALSE;
00341   nn=sqrt(n);
00342   n = 1.0 / nn;
00343   VECSCALE(n,v,v)
00344   return TRUE;
00345 }
00346 
00347 fullscreenbuffer *ScaleImageMap(unsigned char *ImagePixels, int x, int y,
00348                              long X, long Y){
00349  vector p,Mn={0.0,0.0,1.0},Mx,My,Mp;
00350  double r,g,b;
00351  fullscreenbuffer *S,*s,*T,*t;
00352  unsigned char *px;
00353  long i,j;
00354  if((T=(fullscreenbuffer *)X__Malloc(x*y*sizeof(fullscreenbuffer))) == NULL){
00355    MessageBeep(MB_OK);
00356    return NULL;
00357  }
00358  px=ImagePixels; t=T;
00359  for(j=0;j<y;j++)for(i=0;i<x;i++){
00360    t->R = *px++;
00361    t->G = *px++;
00362    t->B = *px++;
00363    t++;
00364  }
00365  if((S=(fullscreenbuffer *)X__Malloc(X*Y*sizeof(fullscreenbuffer))) == NULL){
00366    MessageBeep(MB_OK);
00367    X__Free(T);
00368    return NULL;
00369  }
00370  memset(S,0,X*Y*sizeof(fullscreenbuffer));
00371  Mp[0]=Mp[1]=Mp[2]=Mx[2]=My[2]=p[2]=0.0;
00372  Mx[1]=0.0; My[0]=0.0;
00373  Mx[0]=(double)X;
00374  My[1]=(double)Y;
00375  s=S;
00376  for(i=0;i<Y;i++)for(j=0;j<X;j++){
00377    p[0]=(double)j;
00378    p[1]=(double)i;
00379    if(MapFrom(p,Mp,Mx,My,Mn,T,x,y,&r,&g,&b) > 0){
00380      s->R=(unsigned char)r;
00381      s->G=(unsigned char)g;
00382      s->B=(unsigned char)b;
00383    }
00384    s++;
00385  }
00386  X__Free(T);
00387  return S;
00388 }
00389 
00390 static void GetAnimFileName(char *fsurf, char *tempstr, long frame,
00391                        long firstframe, long lastframe, long framestep){
00392  long thisframe;
00393  char *cp1,*cp2;
00394  cp1=strchr(fsurf,'0');
00395  cp2=strchr(fsurf,'.');
00396  if(cp1 == NULL || cp2 == NULL){
00397    sprintf(tempstr,"dummy.evi");
00398  }
00399  else{
00400    int ln;
00401    char *cp,extn[16],fmat[16];
00402    strcpy(extn,cp2);
00403    ln=0; cp=cp1; while(cp != cp2){ln++; cp++; if(ln > 8)break;}
00404    *cp1='\0';
00405    framestep=min(1,framestep);
00406    thisframe=firstframe-1+frame/framestep;
00407    while(thisframe > lastframe)thisframe -= lastframe;
00408    sprintf(fmat,"%%s%%0%ldld%%s",ln);
00409    sprintf(tempstr,fmat,fsurf,thisframe,extn);
00410  }
00411  strcpy(fsurf,tempstr);
00412 }
00413 
00414 BOOL Bilinear(double alpha, double beta,
00415              fullscreenbuffer *S,
00416              long Xmax, long Ymax,
00417              double *red,double *green,double *blue){
00418  long  ialpha,ibeta,i,j,k,kl,kr,kt,kb,pixel;
00419  double a,b,cc0,cc1,cc2,cl0,cl1,cl2,
00420         cr0,cr1,cr2,ct0,ct1,ct2,cb0,cb1,cb2;
00421  unsigned char *pP,*pR,*pG,*pB;
00422  j=Xmax;
00423  ialpha = (long)alpha; alpha -= (double)ialpha;
00424  ibeta  = (long)beta;  beta  -= (double)ibeta;
00425  if(ialpha < 0 || ialpha >= Xmax)return FALSE;
00426  if(ibeta  < 0 || ibeta  >= Ymax)return FALSE;
00427  k =ibeta*j+ialpha;
00428  if(ialpha <   1)kl=k; else kl=k-1;
00429  if(ialpha > j-2)kr=k; else kr=k+1;
00430  if(ibeta  <   1)kb=k; else kb=k-j;
00431  if(ibeta  > Ymax-2)kt=k; else kt=k+j;
00432  cc0 = (double)S[k].R;  cc1 = (double)S[k].G;  cc2 = (double)S[k].B;
00433  cl0 = (double)S[kl].R; cl1 = (double)S[kl].G; cl2 = (double)S[kl].B;
00434  cr0 = (double)S[kr].R; cr1 = (double)S[kr].G; cr2 = (double)S[kr].B;
00435  ct0 = (double)S[kt].R; ct1 = (double)S[kt].G; ct2 = (double)S[kt].B;
00436  cb0 = (double)S[kb].R; cb1 = (double)S[kb].G; cb2 = (double)S[kb].B;
00437  if(alpha > 0.5){
00438    alpha = alpha-0.5;
00439    if(beta > 0.5){
00440      beta = beta-0.5;
00441      *red   = (cr0-cc0)*alpha+(ct0-cc0)*beta+cc0;
00442      *green = (cr1-cc1)*alpha+(ct1-cc1)*beta+cc1;
00443      *blue  = (cr2-cc2)*alpha+(ct2-cc2)*beta+cc2;
00444    }
00445    else{
00446      beta = 0.5-beta;
00447      *red   = (cr0-cc0)*alpha+(cb0-cc0)*beta+cc0;
00448      *green = (cr1-cc1)*alpha+(cb1-cc1)*beta+cc1;
00449      *blue  = (cr2-cc2)*alpha+(cb2-cc2)*beta+cc2;
00450    }
00451  }
00452  else{
00453    alpha = 0.5-alpha;
00454    if(beta > 0.5){
00455      beta = beta-0.5;
00456      *red   = (cl0-cc0)*alpha+(ct0-cc0)*beta+cc0;
00457      *green = (cl1-cc1)*alpha+(ct1-cc1)*beta+cc1;
00458      *blue  = (cl2-cc2)*alpha+(ct2-cc2)*beta+cc2;
00459    }
00460    else{
00461      beta = 0.5-beta;
00462      *red   = (cl0-cc0)*alpha+(cb0-cc0)*beta+cc0;
00463      *green = (cl1-cc1)*alpha+(cb1-cc1)*beta+cc1;
00464      *blue  = (cl2-cc2)*alpha+(cb2-cc2)*beta+cc2;
00465    }
00466  }
00467  return TRUE;
00468 }
00469 

Generated on Sun Apr 27 14:20:12 2014 for OpenFX by  doxygen 1.5.6