Allink  v0.1
DrOpenGL.cpp
00001 #include "DrEffect.h"
00002 #ifdef __glut_h__
00003 DrEffect::DrEffect(){
00004   Mat = new Matematica();
00005   NChar = 256;
00006   Hist = (double **)calloc(NLevel,sizeof(double));
00007   for(int l=0;l<NLevel;l++){
00008     *(Hist + l) = (double *)calloc(NChar,sizeof(double));
00009     Quart1[l] =  (unsigned char ) 0;
00010     Median[l] =  (unsigned char ) 0;
00011     Quart3[l] =  (unsigned char ) 0;
00012   }
00013   Initialize();
00014 };
00015 void DrEffect::Run(){
00016   int NStep = 1000;
00017   int Acc = -1;
00018   int Vel = 100;
00019   int Pos = 0.;
00020   Matrice *ImIn[3];
00021   for(int d=0;d<3;d++){
00022     ImIn[d] = new Matrice(ImHeight,ImWidth);
00023   }
00024   Matrice *ImOut = new Matrice(ImHeight,ImWidth);
00025   for(int l=0;l<NLevel-1;l++){
00026     for(int r=0;r<ImHeight;r++)
00027       for(int c=0;c<ImWidth;c++)
00028    ImIn[l]->Set(r,c,(double)pixel[(r*ImWidth+c)*NLevel+l]);
00029   }
00030   for(Step=NStep;Step>0;Step--){
00031     for(int l=0;l<NLevel-1;l++){
00032   //     for(int r=0;r<ImHeight;r++){
00033   //  int r1 = r + Pos;
00034   //  if(r1 >= In->pNRow()) r1 -= ImHeight;
00035   //  if(r1 < 0) r1 += ImHeight;
00036   //  for(int c=0;c<Imwidth;c++){
00037      
00038   //    Out->Set(r1,c,In->Val(r,c));
00039   //  }
00040   // }
00041       Mat->BackFold(ImIn[l],ImOut,Pos);
00042       for(int r=0;r<ImHeight;r++)
00043    for(int c=0;c<ImWidth;c++)
00044      pixel[(r*ImWidth+c)*NLevel+l] = (unsigned char)ImOut->Val(r,c);
00045     }
00046     Vel += Acc;
00047     Pos += Vel;
00048     //WritePixel();
00049     WritePngwriter();
00050   }
00051   delete ImOut;
00052   delete ImIn;
00053 }
00054 void DrEffect::EffectIncrease(){
00055   int Times = 2;
00056   int SubDiv = Times*Times;
00057   unsigned char *raster = (unsigned char *)malloc(SubDiv*NLevel*ImWidth*ImHeight*sizeof(char));
00058   //IncreaseResolution(pixel,raster,ImWidth,ImHeight,Times);
00059   free(pixel);
00060   ImWidth *= Times;
00061   ImHeight *= Times;
00062   pixel = (unsigned char *)malloc(NLevel*ImWidth*ImHeight*sizeof(char));
00063   memcpy(pixel,raster,NLevel*ImWidth*ImHeight);
00064   free(raster);
00065   //SubWindow1 = glutCreateSubWindow(MainWindow, 0, 0,width/2,height/2);
00066   return;
00067 }
00068 void DrEffect::EffectMC(){
00069   unsigned char *raster = (unsigned char *)malloc(NLevel*ImWidth*ImHeight*sizeof(char));
00070   int NGridw = 32;
00071   int NGridh = 32;//24;
00072   int NStep = NGridw*NGridh/2+30;
00073   //int NBlock = 4;
00074   int NPartition = 1;//(int)(NGridw*NGridh/(double)NStep);
00075   int *Sequence = (int *)calloc(NGridw*NGridh,sizeof(int));
00076   PERMUTE *Perm = (PERMUTE *)calloc(NGridw*NGridh,sizeof(PERMUTE));
00077   Mat->PermuteRandomAll(Perm,NGridw*NGridh);
00078   Mat->PermuteRandomAll(Sequence,NGridw*NGridh/2);
00079   //for(Step=NStep;Step>0;Step--)
00080     {
00081       SwapBlocks(pixel,raster,ImWidth,ImHeight,Perm,NGridw,NGridh,Sequence,NPartition);
00082       Sequence += NPartition;
00083       memcpy(pixel,raster,NLevel*ImWidth*ImHeight);
00084       glutPostRedisplay();
00085       printf("%d/%d %lf\n",Step,NStep,Step/(double)NStep);
00086       //WritePng();
00087       WritePixel();
00088     }
00089   free(Perm);
00090   free(raster);
00091   //free(Sequence);
00092   return;
00093 }
00094 void DrEffect::EffectCoarseGrain(int Grana){
00095   unsigned char *raster = (unsigned char *)malloc(4*ImWidth*ImHeight*sizeof(char));
00096   int NStep = 8;//NGridw*NGridh/2;
00097   int *Sequence = (int *)calloc(NStep*NStep,sizeof(int));
00098   int Discrx = 1;
00099   int Discry = 1;
00100   int TtWidth = (int)(ImWidth/(double)NStep);
00101   int TtHeight = (int)(ImHeight/(double)NStep);
00102   int TWidth = 0;
00103   int THeight= 0;
00104   for(int m=Grana;m>=0;m--){
00105     Discrx *= 2;
00106     Discry *= 2;
00107   }
00108   int m = 0;
00109     {
00110     Mat->PermuteRandomAll(Sequence,NStep*NStep);
00111     THeight = 0;
00112     TWidth = 0;
00113     int n=0;
00114     for(Step=NStep*NStep*(m+1);Step>NStep*NStep*m;Step--,n++)
00115       {
00116       THeight = (int)(((Sequence[n])/(double)NStep))*TtHeight;
00117       TWidth = ((Sequence[n]%NStep))*TtWidth;
00118       memcpy(raster,pixel,4*ImWidth*ImHeight);
00119       int nPos = ((THeight)*ImWidth + (TWidth))*NLevel;
00120       //int nPos = ((THeight-TtHeight)*ImWidth + (TWidth-TtWidth))*NLevel;
00121       //printf("(%d %d) (%d %d) %d %d %d %d %d\n",TWidth,THeight,ImWidth,ImHeight,Discrx,Discry,Sequence[n],nPos,(ImHeight*ImWidth + (ImWidth))*NLevel);
00122       Discretize(pixel+nPos,raster+nPos,ImWidth,ImHeight,TtWidth,TtHeight,Discrx,Discry);
00123       if((n+1)%NStep==0){
00124    THeight+= TtHeight;
00125    TWidth = 0;
00126       }
00127       TWidth += TtWidth;
00128       memcpy(pixel,raster,4*ImWidth*ImHeight);
00129       glutPostRedisplay();
00130       //printf("%d/%d %lf\n",Step,NStep,Step/(double)NStep);
00131       //WritePng();
00132       //WritePixel();
00133     }
00134   }
00135   free(raster);
00136   //free(Sequence);
00137   return;
00138 }
00139 void DrEffect::NablaPhi(){
00140   if(Median[0] == 0){printf("Create an histogram before\n");return;}
00141   SPLINE Weight;
00142   Weight.a0 = 0.;
00143   Weight.a1 = 1.; Weight.a2 = 0.;
00144   Weight.a3 = 0.; Weight.a4 = 0.;
00145   Matrice *Mask = new Matrice(Weight,3);
00146   Mask->Print();
00147   Matrice *ImIn = new Matrice(ImHeight,ImWidth);
00148   Phi = (double **)calloc(2,sizeof(double));
00149   for(int i=0;i<2;i++)
00150     *(Phi+i) = (double *)calloc(ImWidth*ImHeight,sizeof(double));
00151   for(int r=0;r<ImHeight;r++)
00152     for(int c=0;c<ImWidth;c++){
00153       int i = r*ImWidth+c;
00154       double Val = (pixel[i*NLevel]==0?1.:(double)pixel[i*NLevel])/(double)Median[0];
00155       Phi[0][i] = log10(Val);
00156     ImIn->Set(r,c,Phi[0][i]);
00157     }
00158   Mat->ApplyFilter(ImIn,Mask);
00159   Mask->Transpose();
00160   Mat->ApplyFilter(ImIn,Mask);
00161   for(int r=0;r<ImHeight;r++)
00162     for(int c=0;c<ImWidth;c++)
00163       Phi[1][r*ImWidth+c] = ImIn->Val(r,c);
00164   free(Mask);
00165   free(ImIn);
00166   printf("Fatto\n");
00167 }
00168 void DrEffect::EffectFilter(){
00169   //Mat->Binary(pixel,width,height,100);return;
00170   //Mat->Contrast(pixel,width,height);return;
00171   SPLINE Weight;
00172   Weight.a0 = 0.;
00173   Weight.a1 = 1.; Weight.a2 = 0.;
00174   Weight.a3 = 0.; Weight.a4 = 0.;
00175   Matrice *Mask = new Matrice(Weight,3);
00176   double Sigma = .5;
00177   double CutOff = 3.;
00178   //  Mask->FillGaussian(Sigma,CutOff);
00179   Mask->Print();
00180   Matrice *ImIn = new Matrice(ImHeight,ImWidth);
00181   //int l=1;
00182   for(int l=0;l<NLevel-1;l++){
00183     for(int r=0;r<ImHeight;r++)
00184       for(int c=0;c<ImWidth;c++)
00185    ImIn->Set(r,c,(double)pixel[(r*ImWidth+c)*NLevel+l]);
00186     Mat->ApplyFilter(ImIn,Mask);
00187     // Mask->Transpose();
00188     // Mat->ApplyFilter(ImIn,Mask);
00189     for(int r=0;r<ImHeight;r++)
00190       for(int c=0;c<ImWidth;c++)
00191    pixel[(r*ImWidth+c)*NLevel+l] = (unsigned char)ImIn->Val(r,c);
00192   }
00193   delete Mask;
00194   delete ImIn;
00195   return;
00196 }
00197 void DrEffect::EffectMotion(){
00198   Matrice *ImIn = new Matrice(ImHeight,ImWidth);
00199   Matrice *ImOut = new Matrice(ImHeight,ImWidth);
00200   for(int l=0;l<NLevel-1;l++){
00201     for(int r=0;r<ImHeight;r++)
00202       for(int c=0;c<ImWidth;c++)
00203    ImIn->Set(r,c,(double)pixel[(r*ImWidth+c)*NLevel+l]);
00204     Mat->BackFold(ImIn,ImOut,120);
00205     for(int r=0;r<ImHeight;r++)
00206       for(int c=0;c<ImWidth;c++)
00207    pixel[(r*ImWidth+c)*NLevel+l] = (unsigned char)ImOut->Val(r,c);
00208   }
00209   delete ImOut;
00210   delete ImIn;
00211   return;
00212 }  
00213 void DrEffect::Histo(){
00214   double Area[4] = {0.,0.,0.,0.};
00215   for(int l=0;l<NLevel;l++){
00216     for(int i=0;i<NChar;i++){
00217       Hist[l][i] = 0.000001;
00218     }
00219   }
00220   for(int l=0;l<3;l++){
00221     for(int h=0;h<ImHeight; h++) {
00222       for(int w=0;w<ImWidth; w++) {
00223    int Intens = (int)pixel[(h*ImWidth+w)*NLevel +l];
00224    //if( Intens <= 0 || Intens >= NChar -1){ continue;}
00225    Hist[l][Intens] += 1.;
00226    Area[l] += 1.;
00227       }
00228     }
00229   }
00230   double Max[4] = {0.,0.,0.,0.};
00231   for(int l=0;l<NLevel;l++)
00232     for(int i=0;i<NChar;i++){
00233       if(Max[l] < Hist[l][i])
00234    Max[l] =  Hist[l][i] ;
00235     }
00236   for(int l=0;l<NLevel;l++){
00237     double dMedian = 0.;
00238     int Fatto[3] = {0,0,0};
00239     for(int i=0;i<NChar;i++){
00240       //      Hist[l][i] /= Max[l] > 0. ? Max[l] : 1.;
00241       Hist[l][i] /= Area[l] > 0. ? Area[l] : 1.;
00242       dMedian += Hist[l][i];
00243       if(dMedian > .25 && !Fatto[0]){
00244    Quart1[l] = i;
00245    Fatto[0] = 1;
00246       }
00247       if(dMedian > .5 && !Fatto[1]){
00248    Median[l] = i;
00249    Fatto[1] = 1;
00250       }
00251       if(dMedian > .75 && !Fatto[2]){
00252    Quart3[l] = i;
00253    Fatto[2] = 1;
00254       }
00255       //printf("%d %d %lf Quartiles %d %d %d\n",l,i,Hist[l][i],Quart1[l],Median[l],Quart3[l]);
00256     }
00257   }
00258   //PrintIntensity();
00259 }
00260 void DrEffect::PrintIntensity(){
00261   FILE *Ciccia = fopen("Intensity.dat","w");
00262   for(int l=0;l<3;l++)
00263     for(int h=0;h<ImHeight; h++) 
00264       for(int w=0;w<ImWidth; w++) 
00265    fprintf(Ciccia,"%d %lf %lf %lf",(h*ImWidth+w),(double)pixel[(h*ImWidth+w)*NLevel +0],(double)pixel[(h*ImWidth+w)*NLevel +1],(double)pixel[(h*ImWidth+w)*NLevel +2]);
00266   fclose(Ciccia);
00267 }
00268 void DrEffect::BlackWhite(){
00269   for(int h=0;h<ImHeight; h++) {
00270     for(int w=0;w<ImWidth; w++) {
00271       double Temp = 0.;
00272       for(int l=0;l<3;l++)
00273    Temp += (double)pixel[(h*ImWidth+w)*NLevel +l];
00274       for(int l=0;l<3;l++)
00275    pixel[(h*ImWidth+w)*4 +l] = (GLubyte)(Temp*.333);
00276     }
00277   }
00278 }
00279 int DrEffect::Binary(int Mode){
00280   Histo();
00281   for(int l=0;l<3;l++){
00282     for(int i=0;i<ImWidth*ImHeight;i++){
00283       if(pixel[i*NLevel+l] < Median[l])
00284    pixel[i*NLevel+l] = (unsigned char)0;
00285       else 
00286    pixel[i*NLevel+l] = (unsigned char)255;
00287     }
00288   }
00289   return 1;
00290 }
00291 int DrEffect::Contrast(){
00292   Histo();
00293   double Factor[NLevel];
00294   double Min[NLevel];
00295   for(int l=0;l<NLevel;l++){
00296     Factor[l]  = .1*(double)NChar/(double)((Quart3[l] - Quart1[l]));
00297     Min[l] = (double) Quart1[l];
00298   }
00299   for(int l=0;l<NLevel;l++){
00300     for(int i=0;i<ImWidth*ImHeight;i++){
00301       double dNew = (double)pixel[i*NLevel+l]*Factor[l];// - Min[l];
00302       unsigned char New = dNew > 0. ? (unsigned char) dNew : 0;
00303       pixel[i*NLevel+l] = New;
00304     }
00305   }
00306   return 1;
00307 }
00308 int DrEffect::SwapBlocks(unsigned char *Picture,unsigned char *OutPicture,int width,int height,PERMUTE *Perm,int NGridw,int NGridh,int *Sequence,int NPartition){
00309   int Stepw = (int)(width/NGridw);
00310   int Steph = (int)(height/NGridh);
00311   memcpy(OutPicture,Picture,4*width*height);
00312   for(int p=0;p<NPartition;p++){
00313     int PSwap = Sequence[p];
00314     int vw1 = (int)(PSwap/(double)NGridw);
00315     int vh1 = PSwap%NGridh;
00316     int w1 = vw1*Stepw;
00317     int h1 = vh1*Steph;
00318     int vw2 = (int)(Perm[PSwap].m/(double)NGridw);//(int)(Sequence[PSwap]/(double)NGridw);
00319     int vh2 = Perm[PSwap].m%NGridh;//Sequence[PSwap]%NGridh;
00320     int w2 = vw2*Stepw;
00321     int h2 = vh2*Steph;
00322     for(int vw=0;vw<Stepw;vw++){
00323       for(int vh=0;vh<Steph;vh++){
00324    int Pos1 = (vw+w1) + (vh + h1)*width;
00325    int Pos2 = (vw+w2) + (vh + h2)*width;
00326    if(Pos1 < 0 || Pos1 >= width*height) {printf("Width out of range 0<=%d<%d\n",Pos1,width);continue;}
00327    if(Pos2 < 0 || Pos2 >= width*height) {printf("Height out of range 0<=%d<%d\n",Pos2,height);continue;}
00328    for(int l=0;l<NLevel;l++){
00329      OutPicture[Pos1*NLevel+l] = Picture[Pos2*NLevel+l];
00330      OutPicture[Pos2*NLevel+l] = Picture[Pos1*NLevel+l];
00331    }
00332       }
00333     }
00334   }
00335   return 1;
00336 }
00337 int DrEffect::ShiftBlocks(unsigned char *Picture,unsigned char *OutPicture,int width,int height,int *Sequence,int NGridw,int NGridh,int *Sequence1,int NPartition){
00338   int Stepw = (int)(width/NGridw);
00339   int Steph = (int)(height/NGridh);
00340   //memcpy(OutPicture,Picture,4*width*height);
00341   for(int wi=0;wi<width;wi++){
00342     int vw = (int) (wi/(double)width*NGridw);
00343     for(int hi=0;hi<height;hi++){
00344       int vh = (int) (hi/(double)height*NGridh);
00345       int PosIn = vw*NGridh + vh;
00346       int PosOut = Sequence[PosIn];
00347       int vvw = (int)(PosOut/(double)NGridw);
00348       int vvh = PosOut%NGridh;
00349       int wo = wi - (vw - vvw)*Stepw;
00350       int ho = hi - (vh - vvh)*Steph;
00351       //printf("%d %d -> %d %d / %d %d, %d %d -> %d %d / %d %d\n",wi,hi,wo,ho,width,height,vw,vh,vvw,vvh,Stepw,Steph);
00352       if(wo < 0 || wo >= width) {printf("Width out of range 0<=%d<%d\n",wo,width);continue;}
00353       if(ho < 0 || ho >= height) {printf("Height out of range 0<=%d<%d\n",ho,height);continue;}
00354       for(int l=0;l<NLevel;l++){
00355    OutPicture[(ho*width+wo)*NLevel+l] = Picture[(hi*width+wi)*NLevel+l];
00356       }
00357     }
00358   }
00359   free(Sequence);
00360   return 1;
00361 }
00362 int DrEffect::Discretize(unsigned char *Picture,unsigned char *OutPicture,int ImWidth,int ImHeight,int WWidth,int WHeight,int Blockw,int Blockh){
00363   for(int wo=0;wo<WWidth;wo+=Blockw){
00364     for(int ho=0;ho<WHeight;ho+=Blockh){
00365       for(int l=0;l<NLevel;l++){
00366    double ColAverage=0.;
00367    for(int bwi=0;bwi<Blockw;bwi++){
00368      for(int bhi=0;bhi<Blockh;bhi++){
00369        int wi = wo + bwi;
00370        int hi = ho + bhi;
00371        ColAverage += (double)Picture[(hi*ImWidth+wi)*NLevel+l];
00372      }
00373    }
00374    ColAverage /= (double)(Blockw*Blockh);
00375    for(int bwi=0;bwi<Blockw;bwi++){
00376      for(int bhi=0;bhi<Blockh;bhi++){
00377        int wi = wo + bwi;
00378        int hi = ho + bhi;
00379        OutPicture[(hi*ImWidth+wi)*NLevel+l] = (unsigned char) ColAverage;
00380      }
00381    }
00382       }
00383     }
00384   }
00385   return 1;
00386 }
00387 int DrEffect::IncreaseResolution(unsigned char *Picture,unsigned char *OutPicture,int ImWidth,int ImHeight,int Times){
00388   int OutWidth = ImWidth*Times;
00389   int OutHeight = ImHeight*Times;
00390   double DeltaIn=1./(double)(ImWidth-1);
00391   double DeltaOut=1./(double)(OutWidth-1);
00392   int NOrder = 3+1;
00393   double *dArray = (double *)calloc(ImWidth+NOrder+1,sizeof(double));
00394   for(int p=0;p<=ImWidth+NOrder;p++){
00395     if(p<NOrder){
00396       dArray[p] = 0;
00397     }
00398     else if( (NOrder<=p) && (p<=ImWidth) ){
00399       dArray[p] = (p-NOrder+1)*DeltaIn;//Pm[p-NOrder].Pos[CLat1];//
00400     }
00401     else if( p>ImWidth){
00402       dArray[p] = (p-NOrder+2)*DeltaIn;
00403     }
00404   }
00405 //   for(int vo=0;vo<ImWidth;vo++){
00406 //    for(int vvo=0;vvo<ImHeight;vvo++){
00407 //      PlOut[vo][vvo] = 0.;
00408 //      double x = DeltaOut*vo;
00409 //      //for(int vi=0;vi<NIn;vi++){
00410 //      for(int vi=vo-1;vi<vo+NOrder+1;vi++){
00411 //        if(vi < 0 || vi >= ImWidth) continue;
00412 //        double Blendx = Blend(dArray,x,vi,NOrder);
00413 //        double y = DeltaOut*vvo;
00414 //        //for(int vvi=0;vvi<NIn;vvi++){
00415 //        for(int vvi=vvo-1;vvi<vvo+NOrder+1;vvi++){
00416 //     if(vvi < 0 || vvi >= ImWidth) continue;
00417 //     double Blendy = Mat->Blend(dArray,y,vvi,NOrder);
00418 //     PlOut[vo][vvo] += Blendx*Blendy * PlIn[vi][vvi];
00419 //        }
00420 //      }
00421 //    }
00422 //   }
00423    for(int l=0;l<NLevel;l++){
00424     for(int w=0;w<ImWidth;w++){
00425       for(int h=0;h<ImHeight;h++){
00426    int ho = h*Times;
00427    int wo = w*Times;
00428    if(w>0 && h>0)
00429      OutPicture[((ho+0)*OutWidth+(wo+0))*NLevel+l] = 
00430        .5*(Picture[( (h+0)*ImWidth+(w-1) )*NLevel+l] + 
00431       Picture[( (h-1)*ImWidth+(w+0) )*NLevel+l]);
00432    if(h>0 && w<ImWidth-1)
00433      OutPicture[( (ho+0)*OutWidth+(wo+1))*NLevel+l] = 
00434        .5*(Picture[( (h+0)*ImWidth+(w+1) )*NLevel+l] + 
00435       Picture[( (h-1)*ImWidth+(w+0) )*NLevel+l]);
00436    if(h<ImHeight-1 && w<ImWidth-1)  
00437      OutPicture[( (ho+1)*OutWidth+(wo+1))*NLevel+l] = 
00438        .5*(Picture[( (h+0)*ImWidth+(w+1) )*NLevel+l] + 
00439       Picture[( (h+1)*ImWidth+(w+0) )*NLevel+l]);
00440    if(h<ImHeight-1 && w>0)
00441      OutPicture[( (ho+1)*OutWidth+(wo+0))*NLevel+l] = 
00442        .5*(Picture[( (h+0)*ImWidth+(w-1) )*NLevel+l] + 
00443       Picture[( (h+1)*ImWidth+(w+0) )*NLevel+l]);
00444      }
00445     }
00446   }
00447   return 1;
00448 }
00449 int DrEffect::Noise(unsigned char *Picture,unsigned char *OutPicture,int width,int height){
00450   int NMaskh = 3;
00451   int NMaskw = 3;
00452   double Mask[3][3];
00453   //unsigned char * raster = (unsigned char *) malloc(sizeof(unsigned char) * width * height * 4);
00454   double Det = 1./9.;
00455   Mask[0][0] = 1; Mask[0][1] = 1; Mask[0][2] = 1;
00456   Mask[1][0] = 1; Mask[1][1] = 1; Mask[1][2] = 1;
00457   Mask[2][0] = 1; Mask[2][1] = 1; Mask[2][2] = 1;
00458   Det = 1./10.;
00459   Mask[0][0] = 1; Mask[0][1] = 1; Mask[0][2] = 1;
00460   Mask[1][0] = 1; Mask[1][1] = 2; Mask[1][2] = 1;
00461   Mask[2][0] = 1; Mask[2][1] = 1; Mask[2][2] = 1;
00462   Det = 1./16.;
00463   Mask[0][0] = 1; Mask[0][1] = 2; Mask[0][2] = 1;
00464   Mask[1][0] = 2; Mask[1][1] = 4; Mask[1][2] = 2;
00465   Mask[2][0] = 1; Mask[2][1] = 2; Mask[2][2] = 1;
00466   //Gaussian   
00467   // Det = 1./249.;
00468   //   3 6  8   6 3
00469   //   6 14 19 14 6 
00470   //   8 19 25 19 8                
00471   //   6 14 19 14 6 
00472   //   3 6  8   6 3
00473   for(int l=0;l<NLevel;l++){
00474     for(int h=0;h<height-NMaskh; h++) {
00475       for(int w=0;w<width-NMaskw; w++) {
00476    OutPicture[(h*width+w)*NLevel +l] = 0;
00477    for(int lh=0;lh<NMaskh;lh++){
00478      for(int lw=0;lw<NMaskw;lw++){
00479        OutPicture[(h*width+w)*NLevel +l] += (unsigned char)(
00480                          Mask[lh][lw]*Picture[((h+lh)*width+(w+lw))*NLevel +l]*Det);
00481      }
00482    }
00483       }
00484     }
00485   }
00486   return 1;
00487 }
00488       
00489 int DrEffect::Edges(unsigned char *Picture,unsigned char *OutPicture,int width,int height){
00490   int NMaskh = 3;
00491   int NMaskw = 3;
00492   //unsigned char *raster = (unsigned char *)malloc(width*height*NLevel*sizeof(unsigned char));
00493   double Mask[3][3];
00494   double Det = 1./4.;
00495   Mask[0][0] = 1; Mask[0][1] = 0; Mask[0][2] = -1;
00496   Mask[1][0] = 2; Mask[1][1] = 0; Mask[1][2] = -2;
00497   Mask[2][0] = 1; Mask[2][1] = 0; Mask[2][2] = -1;
00498   //Gradiente per riga
00499   for(int l=0;l<NLevel;l++)
00500     {
00501       //int l=2;
00502       for(int h=0;h<height-NMaskh; h++) {
00503    for(int w=0;w<width-NMaskw; w++) {
00504      OutPicture[(h*width+w)*NLevel +l] = 0;
00505      double Temp=0.;
00506      for(int lh=0;lh<NMaskh;lh++){
00507        for(int lw=0;lw<NMaskw;lw++){
00508          if(h+lh-1 <0 ) continue;
00509          if(w+lw-1<0 ) continue;
00510          if(h+lh-1 > height-1) continue;
00511          if(w+lw-1 > width-1) continue;
00512          Temp +=  ASS((Mask[lh][lw]*Picture[((h+lh-1)*width+(w+lw-1))*NLevel +l]*Det));
00513          //OutPicture[(h*width+w)*NLevel +l] += (unsigned char)(ASS((Mask[lh][lw]*Picture[((h+lh-1)*width+(w+lw-1))*NLevel +l]*Det)));
00514        }
00515      }
00516      OutPicture[(h*width+w)*NLevel +l] += (unsigned char) Temp;
00517    }
00518       }
00519     }
00520   //Gradiente per colonna
00521   for(int l=0;l<NLevel;l++)
00522     {
00523       //int l=2;
00524       for(int h=0;h<height-NMaskh; h++) {
00525    for(int w=0;w<width-NMaskw; w++) {
00526      double Temp=0.;
00527      for(int lh=NMaskh;lh>0;lh--){
00528        for(int lw=0;lw<NMaskw;lw++){
00529          if(h+lh-1 <0 ) continue;
00530          if(w+lw-1<0 ) continue;
00531          if(h+lh-1 > height-1) continue;
00532          if(w+lw-1 > width-1) continue;
00533          //OutPicture[(h*width+w)*NLevel +l] += (unsigned char)(ASS((Mask[lw][lh]*Picture[((h+lh-1)*width+(w+lw-1))*NLevel +l]*Det)));
00534          Temp +=  ASS((Mask[lw][lh]*Picture[((h+lh-1)*width+(w+lw-1))*NLevel +l]*Det));
00535        }
00536      }
00537      OutPicture[(h*width+w)*NLevel +l] += (unsigned char) Temp;
00538    }
00539       }
00540     }
00541   return 1;
00542 }
00543 void DrEffect::DrEkeyboard(unsigned char key){
00544   switch (key){
00545   case 'a':
00546     glutPostRedisplay();
00547     break;
00548   case 'b':
00549     glutPostRedisplay();
00550     break;
00551   case 'c':
00552     glutPostRedisplay();
00553     break;
00554   case 'd':
00555     glutPostRedisplay();
00556     break;
00557   case 'e':
00558     glutPostRedisplay();
00559     break;
00560   case 27:
00561     exit(0);
00562     break;
00563   case 40:
00564     break;
00565   default:
00566     break;
00567   }
00568   keyboardDraw(key);
00569 }
00570 
00571 #endif// __glut_h__