00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <string.h>
00029 
00030 
00031 #include "rgbprocess_lib.h"
00032 
00033 #include "cppcode/rgbcubeIPOL.h"
00034 #include "cppcode/colorfilteringIPOL.h"
00035 #include "cppcode/miscIPOL.h"
00036 
00037 
00059 void input2RGB(unsigned char *input, 
00060                unsigned char **RR, unsigned char **GG, unsigned char **BB, 
00061                int size)
00062 {
00063   unsigned char *R, *G, *B;
00064   int n;
00065 
00066   R=new unsigned char[size];
00067   G=new unsigned char[size];
00068   B=new unsigned char[size];
00069   for (n=0; n < size; n++) {
00070     R[n]=input[n];
00071     G[n]=input[size+n];
00072     B[n]=input[2*size+n];
00073   }
00074 
00075   *RR=R;
00076   *GG=G;
00077   *BB=B;
00078 }
00079 
00090 void RGB2output(unsigned char *R, unsigned char *G, unsigned char *B, 
00091                 unsigned char *output, int size)
00092 {
00093   int n;
00094 
00095   for (n=0; n < size; n++) {
00096     output[n]=R[n];
00097     output[size+n]=G[n];
00098     output[2*size+n]=B[n];
00099   }
00100 }
00101 
00102 
00155 void rgbprocess(unsigned char *input1, 
00156                 unsigned char *input2,
00157                 unsigned char *input3,
00158                 int w, int h,
00159                 unsigned char *output1,
00160                 unsigned char *output2,
00161                 unsigned char *output3,
00162                 int wOut, int hOut,
00163                 const char *option)
00164 {
00165 
00166   unsigned char *R1, *G1, *B1, *R2, *G2, *B2, *R3, *G3, *B3;
00167   unsigned char *Rout1, *Gout1, *Bout1, *Rout2, *Gout2, *Bout2, 
00168                 *Rout3, *Gout3, *Bout3;
00169 
00170   R1=G1=B1=NULL;
00171   R2=G2=B2=NULL;
00172   R3=G3=B3=NULL;
00173   Rout1=Gout1=Bout1=NULL;
00174   Rout2=Gout2=Bout2=NULL;
00175   Rout3=Gout3=Bout3=NULL;
00176 
00177   if (!strcmp(option, "filter")) {
00178     int type=2; 
00179     int r=10;  
00180     float eitmax=0.5f; 
00181                        
00182     int rsim=(r/2 > 0)?(r/2):(1); 
00183     printf("rsim=%i\n", rsim);
00184 
00185     input2RGB(input1, &R1, &G1, &B1, w*h);
00186     Rout1=new unsigned char[w*h];
00187     Gout1=new unsigned char[w*h];
00188     Bout1=new unsigned char[w*h];
00189 
00190     filtercolor(R1, G1, B1, Rout1, Gout1, Bout1, w, h, type, eitmax, r, rsim);
00191 
00192     RGB2output(Rout1, Gout1, Bout1, output1, w*h);
00193   }
00194 
00195   if (!strcmp(option, "rmisolated")) {
00196     int r=5;
00197     int nmin=10;
00198     int nRGB, nisolatedRGB, nisolatedpixels;
00199 
00200     input2RGB(input1, &R1, &G1, &B1, w*h);
00201     Rout1=new unsigned char[w*h];
00202     Gout1=new unsigned char[w*h];
00203     Bout1=new unsigned char[w*h];
00204 
00205     removeisolatedRGB(R1, G1, B1, Rout1, Gout1, Bout1, w, h, r, nmin, nRGB, 
00206                       nisolatedRGB, nisolatedpixels);
00207 
00208     RGB2output(Rout1, Gout1, Bout1, output1, w*h);
00209   }
00210 
00211 
00212   if (!strcmp(option, "pcaviews")) {
00213     input2RGB(input1, &R1, &G1, &B1, w*h);
00214     input2RGB(input2, &R2, &G2, &B2, w*h);
00215     Rout1=new unsigned char[wOut*hOut];
00216     Gout1=new unsigned char[wOut*hOut];
00217     Bout1=new unsigned char[wOut*hOut];
00218     Rout2=new unsigned char[wOut*hOut];
00219     Gout2=new unsigned char[wOut*hOut];
00220     Bout2=new unsigned char[wOut*hOut];
00221     Rout3=new unsigned char[wOut*hOut];
00222     Gout3=new unsigned char[wOut*hOut];
00223     Bout3=new unsigned char[wOut*hOut];
00224 
00225     PCAviews(R1, G1, B1, w, h, R2, G2, B2, w, h, 
00226                  Rout1, Gout1, Bout1, Rout2, Gout2, Bout2, 
00227              Rout3, Gout3, Bout3, wOut, hOut);
00228 
00229     RGB2output(Rout1, Gout1, Bout1, output1, wOut*hOut);
00230     RGB2output(Rout2, Gout2, Bout2, output2, wOut*hOut);
00231     RGB2output(Rout3, Gout3, Bout3, output3, wOut*hOut);
00232 
00233   }
00234 
00235   if (!strcmp(option, "pcaviewsB")) {
00236     input2RGB(input1, &R1, &G1, &B1, w*h);
00237     input2RGB(input2, &R2, &G2, &B2, w*h);
00238     Rout1=new unsigned char[3*wOut*hOut];
00239     Gout1=new unsigned char[3*wOut*hOut];
00240     Bout1=new unsigned char[3*wOut*hOut];
00241 
00242     PCAviewsB(R1, G1, B1, w, h, R2, G2, B2, w, h, 
00243               Rout1, Gout1, Bout1, wOut, hOut);
00244 
00245     RGB2output(Rout1, Gout1, Bout1, output1, 3*wOut*hOut);
00246 
00247   }
00248 
00249   if (!strcmp(option, "densityImage")) {
00250     int rdst=5;
00251     unsigned char logscale=1;
00252 
00253     input2RGB(input1, &R1, &G1, &B1, w*h);
00254 
00255     densityImage(R1, G1, B1, w, h, output1, rdst, logscale);
00256 
00257   }
00258 
00259   if (!strcmp(option, "densityviews")) {
00260     int rdst=5;
00261     unsigned char cumdsty=0;
00262 
00263     input2RGB(input1, &R1, &G1, &B1, w*h);
00264     input2RGB(input2, &R2, &G2, &B2, w*h);
00265         unsigned char *Idsty=input3;
00266     Rout1=new unsigned char[3*wOut*hOut];
00267     Gout1=new unsigned char[3*wOut*hOut];
00268     Bout1=new unsigned char[3*wOut*hOut];
00269 
00270     PCAviews_densityB(R1, G1, B1, w, h, R2, G2, B2, 
00271                       Rout1, Gout1, Bout1, wOut, hOut, rdst, cumdsty, Idsty);
00272 
00273     RGB2output(Rout1, Gout1, Bout1, output1, 3*wOut*hOut);
00274 
00275   }
00276 
00277 
00278   if (!strcmp(option, "mergeimages")) {
00279     input2RGB(input1, &R1, &G1, &B1, w*h);
00280     input2RGB(input2, &R2, &G2, &B2, w*h);
00281     Rout1=new unsigned char[w*h];
00282     Gout1=new unsigned char[w*h];
00283     Bout1=new unsigned char[w*h];
00284 
00285     mergeimages(R1, G1, B1, R2, G2, B2, Rout1, Gout1, Bout1, w, h);
00286 
00287     RGB2output(Rout1, Gout1, Bout1, output1, w*h);
00288 
00289   }
00290 
00291   if (!strcmp(option, "combineviews")) {
00292     
00293 
00294     int wout=3*w;
00295     int hout=h;
00296   
00297     input2RGB(input1, &R1, &G1, &B1, w*h);
00298     input2RGB(input2, &R2, &G2, &B2, w*h);
00299     input2RGB(input3, &R3, &G3, &B3, w*h);
00300     Rout1=new unsigned char[wout*hout];
00301     Gout1=new unsigned char[wout*hout];
00302     Bout1=new unsigned char[wout*hout];
00303 
00304     for (int y=0; y < h; y++) {
00305       memcpy(Rout1+y*wout, R1+y*w, w);
00306       memcpy(Rout1+w+y*wout, R2+y*w, w);
00307       memcpy(Rout1+2*w+y*wout, R3+y*w, w);
00308       memcpy(Gout1+y*wout, G1+y*w, w);
00309       memcpy(Gout1+w+y*wout, G2+y*w, w);
00310       memcpy(Gout1+2*w+y*wout, G3+y*w, w);
00311       memcpy(Bout1+y*wout, B1+y*w, w);
00312       memcpy(Bout1+w+y*wout, B2+y*w, w);
00313       memcpy(Bout1+2*w+y*wout, B3+y*w, w);
00314     }
00315 
00316     RGB2output(Rout1, Gout1, Bout1, output1, wout*hout);
00317   }
00318 
00319   if (!strcmp(option, "combineviewsB")) {
00320     
00321     
00322     int wout=2*w;
00323     int hout=2*h;
00324   
00325     input2RGB(input1, &R1, &G1, &B1, w*h);
00326     input2RGB(input2, &R2, &G2, &B2, w*h);
00327     input2RGB(input3, &R3, &G3, &B3, w*h);
00328     Rout1=new unsigned char[wout*hout];
00329     Gout1=new unsigned char[wout*hout];
00330     Bout1=new unsigned char[wout*hout];
00331     memset(Rout1, 255, wout*hout);
00332     memset(Gout1, 255, wout*hout);
00333     memset(Bout1, 255, wout*hout);
00334 
00335     for (int y=0; y < h; y++) {
00336       memcpy(Rout1+y*wout, R1+y*w, w);
00337       memcpy(Gout1+y*wout, G1+y*w, w);
00338       memcpy(Bout1+y*wout, B1+y*w, w);
00339       memcpy(Rout1+w+y*wout, R2+y*w, w);
00340       memcpy(Gout1+w+y*wout, G2+y*w, w);
00341       memcpy(Bout1+w+y*wout, B2+y*w, w);
00342       memcpy(Rout1+w/2+(h+y)*wout, R3+y*w, w);
00343       memcpy(Gout1+w/2+(h+y)*wout, G3+y*w, w);
00344       memcpy(Bout1+w/2+(h+y)*wout, B3+y*w, w);
00345     }
00346 
00347     RGB2output(Rout1, Gout1, Bout1, output1, wout*hout);
00348   }
00349 
00350   
00351   if (R1) delete[] R1;
00352   if (G1) delete[] G1;
00353   if (B1) delete[] B1;
00354   if (R2) delete[] R2;
00355   if (G2) delete[] G2;
00356   if (B2) delete[] B2;
00357   if (R3) delete[] R3;
00358   if (G3) delete[] G3;
00359   if (B3) delete[] B3;
00360   if (Rout1) delete[] Rout1;
00361   if (Gout1) delete[] Gout1;
00362   if (Bout1) delete[] Bout1;
00363   if (Rout2) delete[] Rout2;
00364   if (Gout2) delete[] Gout2;
00365   if (Bout2) delete[] Bout2;
00366   if (Rout3) delete[] Rout3;
00367   if (Gout3) delete[] Gout3;
00368   if (Bout3) delete[] Bout3;
00369 
00370   return;
00371 }
00372 
00373 
00384 void RGBviewsparams(const char *nameparams)
00385 {
00386   int nviews;
00387   nviews=RGBviews_sequence_params(nameparams);
00388 }
00389 
00390 
00407 void getRGBview(unsigned char *input, 
00408                 int w, int h, unsigned char *I,
00409                 unsigned char *output,
00410                 int wview, int hview,
00411                 float P[3], float u[3], float v[3])
00412 {
00413   unsigned char *R, *G, *B, *Rout, *Gout, *Bout;
00414   
00415   input2RGB(input, &R, &G, &B, w*h);
00416   Rout=new unsigned char[wview*hview];
00417   Gout=new unsigned char[wview*hview];
00418   Bout=new unsigned char[wview*hview];
00419 
00420   projectRGBcubeB(R, G, B, w*h, I, Rout, Gout, Bout, wview, hview, P, u, v);
00421 
00422   RGB2output(Rout, Gout, Bout, output, wview*hview);
00423 
00424   
00425   if (R) delete[] R;
00426   if (G) delete[] G;
00427   if (B) delete[] B;
00428   if (Rout) delete[] Rout;
00429   if (Gout) delete[] Gout;
00430   if (Bout) delete[] Bout;
00431 }
00432 
00433 
00445 float computeRMSE(unsigned char *input1, unsigned char *input2,
00446                   int w, int h, float &dmean)
00447 {
00448   float rmse;
00449   unsigned char use_voxel;
00450   unsigned char *R1, *G1, *B1, *R2, *G2, *B2;
00451   
00452   input2RGB(input1, &R1, &G1, &B1, w*h);
00453   input2RGB(input2, &R2, &G2, &B2, w*h);
00454 
00455   use_voxel=1; 
00456                
00457   rmse=diffRMSE(R1, G1, B1, R2, G2, B2, w, h, 1, dmean);
00458 
00459   
00460   if (R1) delete[] R1;
00461   if (G1) delete[] G1;
00462   if (B1) delete[] B1;
00463   if (R2) delete[] R2;
00464   if (G2) delete[] G2;
00465   if (B2) delete[] B2;
00466 
00467   return rmse;
00468 }
00469 
00470 
00480 int countcolors(unsigned char *input, int w, int h)
00481 {
00482   int nRGB;
00483   unsigned char *R, *G, *B;
00484   
00485   input2RGB(input, &R, &G, &B, w*h);
00486 
00487   nRGB=count_RGBcolors(R, G, B, w, h);
00488 
00489   
00490   if (R) delete[] R;
00491   if (G) delete[] G;
00492   if (B) delete[] B;
00493 
00494   return nRGB;
00495 }
00496 
00497 
00498 
00511 void RGB2VRML2(unsigned char *input, int w, int h, unsigned char *Idsty, 
00512                const char *nameout)
00513 {
00514   unsigned char *R, *G, *B;
00515   
00516   input2RGB(input, &R, &G, &B, w*h);
00517 
00518   RGBcube2VRML2(R, G, B, w*h, Idsty, nameout);
00519 
00520   
00521   if (R) delete[] R;
00522   if (G) delete[] G;
00523   if (B) delete[] B;
00524 }
00525