Allink
v0.1
|
00001 #include "DrImage.h" 00002 00003 #ifndef USE_PNG 00004 int main(int agrc,char **argv){ 00005 printf("No pngwriter libs used.\nExit.\n"); 00006 } 00007 #else 00008 int main(int argc, char **argv){ 00009 DrImage *DrI = new DrImage(argc,argv); 00010 for(int i=1;i<argc;i++){ 00011 if(!strncmp(argv[i],"-ga",3)){ 00012 DrI->Gauss(); 00013 } 00014 else if(!strncmp(argv[i],"-c",2)){ 00015 DrI->Cut(); 00016 } 00017 else if(!strncmp(argv[i],"-g",2)){ 00018 DrI->Gravity(); 00019 } 00020 else if(!strncmp(argv[i],"-f",2)){ 00021 DrI->Fourier(); 00022 } 00023 else if(!strncmp(argv[i],"-i",2)){ 00024 DrI->Ising(); 00025 } 00026 else if(!strncmp(argv[i],"-l",2)){ 00027 DrI->LennardJones(); 00028 } 00029 else if(!strncmp(argv[i],"-m",2)){ 00030 DrI->ConvMatrix(); 00031 } 00032 else if(!strncmp(argv[i],"-s",2)){ 00033 DrI->Shear(); 00034 } 00035 else if(!strncmp(argv[i],"-r",2)){ 00036 DrI->Rotor(); 00037 } 00038 else if(!strncmp(argv[i],"-d",2)){ 00039 DrI->Difference(); 00040 } 00041 else if(!strncmp(argv[i],"-sl",2)){ 00042 DrI->SlitScan(); 00043 } 00044 else if(!strncmp(argv[i],"-y",2)){ 00045 } 00046 } 00047 return 0; 00048 } 00049 DrImage::DrImage(int argc, char *argv){ 00050 NFile = 0; 00051 NWidth = 0; 00052 NHeight = 0; 00053 for(int c=0;c<argc;c++){ 00054 if(!strncmp(argv[i],"-",1)){ 00055 continue; 00056 } 00057 00058 NFile++; 00059 } 00060 FileList = (char **)calloc(NFile,sizeof(char)); 00061 for(int c=0,f=0;c<argc;c++){ 00062 if(!strncmp(argv[i],"-",1)){ 00063 continue; 00064 } 00065 FileList[f] = (char *)calloc(120,sizeof(char)); 00066 sprintf(FileList[f],argv[c]); 00067 f++; 00068 } 00069 Load(FileList[0]); 00070 Load2(FileList[NFile-1]); 00071 } 00072 DrImage::~DrImage(){ 00073 for(int l=0;l<3;l++){ 00074 free(data[l]); 00075 free(data1[l]); 00076 free(data); 00077 free(data1); 00078 for(int f=0;f<NFile;f++){ 00079 free(FileList[f]); 00080 } 00081 free(FileList); 00082 } 00083 void DrImage::Load(char *FName){ 00084 printf("Load %s\n",FName); 00085 ImageIn = new pngwriter(0,0,1.0,FName); 00086 ImageIn->readfromfile(FName); 00087 NWidth = ImageIn->getwidth(); 00088 NHeight = ImageIn->getheight(); 00089 if(NWidth*NHeight == 0){ 00090 printf("The image is empty\n"); 00091 exit(0); 00092 } 00093 data = (double **)calloc(3,sizeof(double)); 00094 for(int l=0;l<3;l++){ 00095 data[l] = (double *)calloc(NWidth*NHeight,sizeof(double)); 00096 } 00097 for(int l=0;l<3;l++){ 00098 for(int w=0;w<NWidth;w++){ 00099 for(int h=0;h<NHeight;h++){ 00100 data[l][h*NWidth + w] = ImageIn->dread(w,h,l+1); 00101 } 00102 } 00103 } 00104 //ImageIn.close(); 00105 } 00106 void DrImage::ReLoad(char *FName){ 00107 ImageIn->readfromfile(FName); 00108 for(int l=0;l<3;l++){ 00109 for(int w=0;w<NWidth;w++){ 00110 for(int h=0;h<NHeight;h++){ 00111 data[l][h*NWidth + w] = ImageIn->dread(w,h,l+1); 00112 } 00113 } 00114 } 00115 } 00116 void DrImage::Load2(char *FName){ 00117 printf("Load %s\n",FName); 00118 ImageIn->readfromfile(FName); 00119 data1 = (double **)calloc(3,sizeof(double)); 00120 for(int l=0;l<3;l++){ 00121 data1[l] = (double *)calloc(NWidth*NHeight,sizeof(double)); 00122 } 00123 for(int l=0;l<3;l++){ 00124 for(int w=0;w<NWidth;w++){ 00125 for(int h=0;h<NHeight;h++){ 00126 data1[l][h*NWidth + w] = ImageIn->dread(w,h,l+1); 00127 } 00128 } 00129 } 00130 //ImageIn.close(); 00131 } 00132 void DrImage::Gravity(){ 00133 int NStep = (int)(25.*60.*.2); 00134 double InvNStep = 1./(double)NStep; 00135 double InvNHei = 1./(double)NHeight; 00136 double Acc = -1.; 00137 double Vel = fabs(Acc)*NStep*.75; 00138 int Pos = 0.; 00139 for(int s=0;s<NStep;s++){ 00140 fprintf(stderr,"Elaborating %lf %%\r",s*InvNStep*100.); 00141 char cImage[160]; 00142 sprintf(cImage,"Image%05u.png",s); 00143 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00144 for(int h=0;h<NHeight;h++){ 00145 int h1 = h + Pos; 00146 if(h1 >= NHeight) h1 -= NHeight; 00147 if(h1 < 0) h1 += NHeight; 00148 for(int w=0;w<NWidth;w++){ 00149 ImageOut.plot(w,h1,data[0][h*NWidth + w],data[1][h*NWidth + w],data[2][h*NWidth + w]); 00150 } 00151 } 00152 Vel += Acc; 00153 if(Vel > NHeight) Vel = NHeight; 00154 Pos += (int)Vel; 00155 Pos -= (int)(floor(Pos*InvNHei)*NHeight); 00156 ImageOut.close(); 00157 } 00158 printf("\n"); 00159 } 00160 void DrImage::Ising(){ 00161 int NStill = 50; 00162 int NStep = (int)(25.*60.*.2) - NStill; 00163 int NChange = 60; 00164 int NSquare = 4; 00165 double InvNStep = 1./(double)NStep; 00166 Matematica *Mate = new Matematica(); 00167 for(int s=NStill+NStep;s>=NStep;s--){ 00168 char cImage[160]; 00169 sprintf(cImage,"Image%05u.png",s); 00170 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00171 for(int h=0;h<NHeight;h++){ 00172 for(int w=0;w<NWidth;w++){ 00173 ImageOut.plot(w,h,data[0][h*NWidth+w],data[1][h*NWidth+w],data[2][h*NWidth+w]); 00174 } 00175 } 00176 ImageOut.close(); 00177 } 00178 for(int s=NStep;s>=0;s--){ 00179 fprintf(stderr,"Elaborating %lf %%\r",s*InvNStep*100.); 00180 char cImage[160]; 00181 sprintf(cImage,"Image%05u.png",s); 00182 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00183 for(int c=0;c<NChange;c++){ 00184 int w = (int)(NWidth*Mate->Casuale()); 00185 int h = (int)(NHeight*Mate->Casuale()); 00186 w = w - w%NSquare; 00187 h = h - h%NSquare; 00188 if(Mate->Casuale() < .5){ 00189 for(int ws=0;ws<NSquare;ws++){ 00190 if(ws+w > NWidth) continue; 00191 for(int hs=0;hs<NSquare;hs++){ 00192 if(hs+h > NHeight) continue; 00193 for(int l=0;l<3;l++){ 00194 data[l][(h+hs)*NWidth+(w+ws)] = 0.; 00195 } 00196 } 00197 } 00198 } 00199 else { 00200 for(int ws=0;ws<NSquare;ws++){ 00201 if(ws+w > NWidth) continue; 00202 for(int hs=0;hs<NSquare;hs++){ 00203 if(hs+h > NHeight) continue; 00204 for(int l=0;l<3;l++){ 00205 data[l][(h+hs)*NWidth+(w+ws)] = 1.; 00206 } 00207 } 00208 } 00209 } 00210 } 00211 for(int h=0;h<NHeight;h++){ 00212 for(int w=0;w<NWidth;w++){ 00213 ImageOut.plot(w,h,data[0][h*NWidth+w],data[1][h*NWidth+w],data[2][h*NWidth+w]); 00214 } 00215 } 00216 ImageOut.close(); 00217 } 00218 printf("\n"); 00219 } 00220 void DrImage::ConvMatrix(){ 00221 double Average = 0.; 00222 double Count = 0.; 00223 Matrice ImIn(NWidth,NHeight); 00224 for(int h=0;h<NHeight;h++){ 00225 for(int w=0;w<NWidth;w++){ 00226 double Sum = 1.- .3333*(data[0][h*NWidth+w]+data[1][h*NWidth+w]+data[2][h*NWidth+w]); 00227 //double Sum = 1. - (data[0][h*NWidth+w]+data[1][h*NWidth+w]+data[2][h*NWidth+w]); 00228 ImIn.Set(w,h,Sum); 00229 Average += Sum; 00230 Count += 1.; 00231 } 00232 } 00233 Average /= Count; 00234 //---------Canny--------------------- 00235 Matematica *Mat = new Matematica; 00236 Matrice Canny(5,5); 00237 Canny.FillCanny(); 00238 Canny.Print(); 00239 // Mat->ApplyFilter(&ImIn,&Canny); 00240 // Mat->ApplyFilter(&ImIn,&Canny); 00241 //-----------Edge------------------- 00242 SPLINE Weight; 00243 Weight.a0 = 0.; 00244 Weight.a1 = 1.; Weight.a2 = 0.; 00245 Weight.a3 = 0.; Weight.a4 = 0.; 00246 Matrice *Mask = new Matrice(Weight,3); 00247 Mask->Print(); 00248 //Mat->ApplyFilter(&ImIn,Mask); 00249 // Mask->Transpose(); 00250 // Mat->ApplyFilter(ImIn,Mask); 00251 //-------------Smooth---------------- 00252 const int NMatSize = 5; 00253 Matrice GaussEdge(NMatSize,NMatSize); 00254 GaussEdge.FillGaussian(.5,1.); 00255 //double LatPos[5] = {.125,-.25,0.,.25,-.125}; 00256 // double LatPos[3] = {-1.,0.,1.}; 00257 // for(int w=0;w<NMatSize;w++){ 00258 // for(int h=0;h<NMatSize;h++){ 00259 // GaussEdge.Set(w,h,GaussEdge.Val(w,h)*LatPos[w]); 00260 // } 00261 // } 00262 GaussEdge.Print(); 00263 //Mat->ApplyFilter(ImIn,&GaussEdge); 00264 Matrice GaussSmooth(5,5); 00265 GaussSmooth.FillGaussian(.5,3.); 00266 // Mat->ApplyFilter(ImIn,&GaussSmooth); 00267 //------------PixDev------------------ 00268 for(int h=0;h<NHeight;h++){ 00269 for(int w=0;w<NWidth;w++){ 00270 ImIn.Set(w,h,Average-ImIn.Val(w,h)); 00271 } 00272 } 00273 int PixelDev = 5; 00274 double ValStep[3] = {0.,0.,0.}; 00275 int ValNStep[3] = {0,0,0}; 00276 for(int h=0;h<NHeight;h++){ 00277 for(int w=0;w<NWidth;w++){ 00278 ValNStep[0] = w - PixelDev; 00279 if(ValNStep[0] < 0 ) continue; 00280 if(ValNStep[0] >= NWidth) continue; 00281 ValNStep[1] = w; 00282 ValNStep[2] = w + PixelDev; 00283 if(ValNStep[2] < 0 ) continue; 00284 if(ValNStep[2] >= NWidth) continue; 00285 for(int d=0;d<3;d++){ 00286 ValStep[d] = ImIn.Val(ValNStep[d],h); 00287 if(d == 1){ 00288 ValStep[d] = ValStep[d] > 0. ? ValStep[d] : 0.; 00289 continue; 00290 } 00291 ValStep[d] = ValStep[d] < 0. ? -ValStep[d] : 0.; 00292 } 00293 double Resp = ValStep[0]*ValStep[1]*ValStep[2]; 00294 //double Resp = ValStep[1]; 00295 //ImIn.Set(w,h,Resp); 00296 } 00297 } 00298 char cImage[160]; 00299 sprintf(cImage,"Canny.png"); 00300 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00301 FILE *Ciccia = fopen("Pos3d.dat","w"); 00302 double NormH = 1./(double)NHeight; 00303 double NormW = 1./(double)NWidth; 00304 for(int h=0;h<NHeight;h++){ 00305 for(int w=0;w<NWidth;w++){ 00306 fprintf(Ciccia,"%lf %lf %lf\n",w*NormH,h*NormH,ImIn.Val(w,h)); 00307 ImageOut.plot(w,h,ImIn.Val(w,h),ImIn.Val(w,h),ImIn.Val(w,h)); 00308 } 00309 } 00310 fclose(Ciccia); 00311 ImageOut.close(); 00312 } 00313 void DrImage::Cut(){ 00314 char cImage[160]; 00315 sprintf(cImage,"Small.png"); 00316 double pixel[4]; 00317 double xBound[2] = {.5,.75}; 00318 double yBound[2] = {.2,.45}; 00319 int xNBound[2]; 00320 int yNBound[2]; 00321 for(int d=0;d<2;d++){ 00322 xNBound[d] = (int)(xBound[d]*NWidth); 00323 yNBound[d] = (int)(yBound[d]*NHeight); 00324 printf("%d %d %d %d\n",xNBound[d],NWidth,yNBound[d],NHeight); 00325 } 00326 int Dx = xNBound[1]-xNBound[0]; 00327 int Dy = yNBound[1]-yNBound[0]; 00328 double *Plot = new double[Dx*Dy]; 00329 Matematica *Mat = new Matematica; 00330 //VarData *Var = new VarData; 00331 Matrice *ImIn = new Matrice(Dx,Dy); 00332 pngwriter ImageOut(xNBound[1]-xNBound[0],yNBound[1]-yNBound[0],1.0,cImage); 00333 double Average = 0.; 00334 double Count = 0.; 00335 for(int h=yNBound[0];h<yNBound[1];h++){ 00336 for(int w=xNBound[0];w<xNBound[1];w++){ 00337 int hh = h -yNBound[0]; 00338 int ww = w -xNBound[0]; 00339 double Sum = data[0][h*NWidth+w]+data[1][h*NWidth+w]+data[2][h*NWidth+w]; 00340 //Sum *= .5; 00341 // ImageOut.plot(ww,hh,data[0][h*NWidth+w],data[1][h*NWidth+w],data[2][h*NWidth+w]); 00342 ImIn->Set(ww,hh,Sum); 00343 Plot[hh*Dx+ww] = Sum; 00344 } 00345 } 00346 00347 00348 for(int h=0;h<Dy;h++){ 00349 for(int w=0;w<Dx;w++){ 00350 //if(ImIn->Val(w,h) >= 0.) 00351 ImageOut.plot(w,h,10.*ImIn->Val(w,h),ImIn->Val(w,h),ImIn->Val(w,h)); 00352 } 00353 } 00354 ImageOut.close(); 00355 delete [] Plot; 00356 } 00357 void DrImage::Difference(){ 00358 char cImage[160]; 00359 sprintf(cImage,"Difference.png"); 00360 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00361 double pixel[4]; 00362 for(int h=0;h<NHeight;h++){ 00363 for(int w=0;w<NWidth;w++){ 00364 for(int l=0;l<3;l++){ 00365 pixel[l] = data[l][h*NWidth+w] - data1[l][h*NWidth+w]; 00366 } 00367 ImageOut.plot(w,h,pixel[0],pixel[1],pixel[2]); 00368 } 00369 } 00370 ImageOut.close(); 00371 printf("\n"); 00372 return; 00373 printf("Difference\n"); 00374 //char cImage[60]; 00375 double **data2 = (double **)calloc(3,sizeof(double)); 00376 for(int l=0;l<3;l++){ 00377 data2[l] = (double *)calloc(NHeight*NWidth,sizeof(double)); 00378 } 00379 for(int h=0;h<NHeight;h++){ 00380 for(int w=0;w<NWidth;w++){ 00381 for(int l=0;l<4;l++){ 00382 data2[l][h*NWidth+w] = data[l][h*NWidth+w];// - data1[l][h*NWidth+w]; 00383 } 00384 } 00385 } 00386 sprintf(cImage,"Difference.png"); 00387 //pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00388 for(int h=0;h<NHeight;h++){ 00389 for(int w=0;w<NWidth;w++){ 00390 ImageOut.plot(w,h,data2[0][h*NWidth+w],data2[1][h*NWidth+w],data2[2][h*NWidth+w]); 00391 } 00392 } 00393 ImageOut.close(); 00394 } 00395 void DrImage::Shear(){ 00396 int NStill = 50; 00397 int NStep = (int)(25.*60.*.2) - NStill; 00398 double InvNStep = 1./(double)NStep; 00399 double InvNHei = 1./(double)NHeight; 00400 int NStrip = 50; 00401 double InvNStrip = 1./(double)NStrip; 00402 double *Acc = (double *)calloc(NStrip,sizeof(double)); 00403 double *Vel = (double *)calloc(NStrip,sizeof(double)); 00404 int *Pos = (int *)calloc(NStrip,sizeof(int)); 00405 double NotRational = .1*exp(1)*sqrt(M_PI)/(double)(NStrip); 00406 for(int s=0;s<NStrip;s++){ 00407 Acc[s] = (s*NotRational); 00408 Vel[s] = 0.; 00409 Pos[s] = 0; 00410 } 00411 for(int s=NStill+NStep;s>=NStep;s--){ 00412 char cImage[160]; 00413 sprintf(cImage,"Image%05u.png",s); 00414 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00415 for(int h=0;h<NHeight;h++){ 00416 for(int w=0;w<NWidth;w++){ 00417 ImageOut.plot(w,h,data[0][h*NWidth+w],data[1][h*NWidth+w],data[2][h*NWidth+w]); 00418 } 00419 } 00420 ImageOut.close(); 00421 } 00422 for(int s=NStep;s>=0;s--){ 00423 //for(int s=0;s<NStep;s++){ 00424 fprintf(stderr,"Elaborating %lf %%\r",s*InvNStep*100.); 00425 char cImage[160]; 00426 sprintf(cImage,"Image%05u.png",s); 00427 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00428 for(int h=0;h<NHeight;h++){ 00429 int st = (int)(h*InvNHei*NStrip); 00430 for(int w=0;w<NWidth;w++){ 00431 int w1 = w + Pos[st]; 00432 if(w1 >= NWidth) w1 -= NWidth; 00433 if(w1 < 0) w1 += NWidth; 00434 ImageOut.plot(w1,h,data[0][h*NWidth+w],data[1][h*NWidth+w],data[2][h*NWidth+w]); 00435 } 00436 } 00437 for(int st=0;st<NStrip;st++){ 00438 Vel[st] += Acc[st]; 00439 //if(Vel[st] > NHeight) Vel[st] = NHeight; 00440 Pos[st] += (int)Vel[st]; 00441 Pos[st] -= (int)(floor(Pos[st]*InvNHei)*NHeight); 00442 } 00443 ImageOut.close(); 00444 } 00445 printf("\n"); 00446 } 00447 void DrImage::EffectOnDataR(double *data2,int l,int w,int h,int ws,int hs,int NSquare){ 00448 data[l][(h+hs)*NWidth+(w+ws)] = data2[(NSquare-1-ws)*NSquare+hs]; 00449 } 00450 void DrImage::EffectOnDataT(double *data2,int l,int w,int h,int ws,int hs,int NSquare){ 00451 data[l][(h+hs)*NWidth+(w+ws)] = data2[ws*NSquare+hs]; 00452 } 00453 void DrImage::EffectOnDataM(double *data2,int l,int w,int h,int ws,int hs,int NSquare){ 00454 data[l][(h+hs)*NWidth+(w+ws)] = data2[(NSquare-1-hs)*NSquare+ws]; 00455 } 00456 void DrImage::Rotor(){ 00457 int NStill = 50;//50; 00458 int NStep = (int)(25.*60.*.2) - NStill; 00459 const int NSquare = 6; 00460 int NDecr = (int)((NStep)/(double)NSquare); 00461 int NChange = 4; 00462 int NSq[7] = {64,49,36,25,16,9,4}; 00463 int NIncrement = 2; 00464 int NSkip = 15; 00465 double *data2 = (double *)calloc(NSq[0]*NSq[0],sizeof(double)); 00466 double InvNStep = 1./(double)NStep; 00467 Matematica *Mate = new Matematica(); 00468 for(int s=NStill+NStep;s>=NStep;s--){ 00469 char cImage[160]; 00470 sprintf(cImage,"Image%05u.png",s); 00471 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00472 for(int h=0;h<NHeight;h++){ 00473 for(int w=0;w<NWidth;w++){ 00474 ImageOut.plot(w,h,data[0][h*NWidth+w],data[1][h*NWidth+w],data[2][h*NWidth+w]); 00475 } 00476 } 00477 ImageOut.close(); 00478 } 00479 for(int s=NStep,sq=0;s>=0;s--){ 00480 // if( (s%NDecr) == 0 ) sq++; 00481 // if(sq < 0) sq = 0; 00482 // if(sq > NSquare) sq = NSquare-1; 00483 fprintf(stderr,"Elaborating %lf %%\r",s*InvNStep*100.); 00484 char cImage[160]; 00485 sprintf(cImage,"Image%05u.png",s); 00486 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00487 if((s%NSkip) == 0){ 00488 for(int c=0;c<NChange;c++){ 00489 int w = (int)(NWidth*Mate->Casuale()); 00490 int h = (int)(NHeight*Mate->Casuale()); 00491 for(int l=0;l<3;l++){ 00492 for(int ws=0;ws<NSq[sq];ws++){ 00493 if(ws+w > NWidth) continue; 00494 for(int hs=0;hs<NSq[sq];hs++){ 00495 if(hs+h > NHeight) continue; 00496 data2[hs*NSq[sq]+ws] = data[l][(h+hs)*NWidth+(w+ws)]; 00497 } 00498 } 00499 for(int ws=0;ws<NSq[sq];ws++){ 00500 if(ws+w > NWidth) continue; 00501 for(int hs=0;hs<NSq[sq];hs++){ 00502 if(hs+h > NHeight) continue; 00503 EffectOnDataR(data2,l,w,h,ws,hs,NSq[sq]); 00504 //data[l][(h+hs)*NWidth+(w+ws)] = data2[(NMin-hs)*NSq[sq]+ws]; 00505 } 00506 } 00507 } 00508 } 00509 } 00510 for(int h=0;h<NHeight;h++){ 00511 for(int w=0;w<NWidth;w++){ 00512 ImageOut.plot(w,h,data[0][h*NWidth+w],data[1][h*NWidth+w],data[2][h*NWidth+w]); 00513 } 00514 } 00515 ImageOut.close(); 00516 } 00517 free(data2); 00518 printf("\n"); 00519 } 00520 void DrImage::LennardJones(){ 00521 //---------------------alloc---------------------- 00522 int NStill = 50; 00523 int NStep = (int)(25.*60.*.2) - NStill; 00524 int NRow = 20; 00525 int NCol = 20; 00526 double NPRow = 1.;//(double)NRow; 00527 double NPCol = 1.;//(double)NCol; 00528 double NStepPRow = NRow/(double)NStep; 00529 double NStepPCol = NCol/(double)NStep; 00530 double Eps = 0.1; 00531 double Sigma = 3.0; 00532 double Dt = 2.;//.1; 00533 double InvNStep = 1./(double)NStep; 00534 double InvWid = 1./(double)NWidth; 00535 double InvHei = 1./(double)NHeight; 00536 double InvRow = 1./(double)NRow; 00537 double InvCol = 1./(double)NCol; 00538 //int HeiSize = (int)(NHeight*InvRow); 00539 //int WidSize = (int)(NWidth*InvCol); 00540 double **data2 = (double **)calloc(3,sizeof(double)); 00541 for(int l=0;l<3;l++){ 00542 data2[l] = (double *)calloc(NHeight*NWidth,sizeof(double)); 00543 } 00544 double *PPos = (double *)calloc(2*NRow*NCol,sizeof(double)); 00545 int *PBound = (int *)calloc(4*NRow*NCol,sizeof(int)); 00546 double *PVel = (double *)calloc(2*NRow*NCol,sizeof(double)); 00547 double *PFor = (double *)calloc(2*NRow*NCol,sizeof(double)); 00548 double Edge[2] = {NWidth,NHeight}; 00549 double InvEdge[2] = {1./(double)NWidth,1./(double)NHeight}; 00550 Matematica *Mate = new Matematica(); 00551 PERMUTE *Perm = (PERMUTE *)calloc(NRow*NCol,sizeof(PERMUTE)); 00552 //-------------------------initializing----------------------- 00553 for(int l=0;l<3;l++){ 00554 for(int h=0;h<NHeight;h++){ 00555 for(int w=0;w<NWidth;w++){ 00556 data2[l][h*NWidth+w] = data[l][h*NWidth+w]; 00557 } 00558 } 00559 } 00560 for(int r=0;r<NRow;r++){ 00561 for(int c=0;c<NCol;c++){ 00562 double x = (c)*InvCol*NWidth; 00563 double y = (r)*InvRow*NHeight; 00564 int p1 = (r*NCol+c)*2; 00565 PPos[p1 ] = x; 00566 PPos[p1+1] = y; 00567 PVel[p1 ] = (2.*Mate->Casuale()-1.); 00568 PVel[p1+1] = (2.*Mate->Casuale()-1.); 00569 PBound[(r*NCol+c)*4 ] = (int)(c*InvCol*NWidth); 00570 PBound[(r*NCol+c)*4+1] = (int)((c+1)*InvCol*NWidth); 00571 PBound[(r*NCol+c)*4+2] = (int)(r*InvRow*NHeight); 00572 PBound[(r*NCol+c)*4+3] = (int)((r+1)*InvRow*NHeight); 00573 Perm[r*NCol+c].n = r*NCol+c; 00574 Perm[r*NCol+c].m = r*NCol+c; 00575 } 00576 } 00577 //squares of different sizes 00578 if(1==1){ 00579 for(int c=0;c<NCol-1;c++){ 00580 int NBorder = (int)((2.*Mate->Casuale()-1.)*10.); 00581 for(int r=0;r<NRow;r++){ 00582 int p1 = (r*NCol+c)*4; 00583 int p2 = (r*NCol+(c+1))*4; 00584 PBound[p1+1] += NBorder; 00585 PBound[p2 ] += NBorder; 00586 } 00587 } 00588 for(int r=0;r<NRow-1;r++){ 00589 int NBorder = (int)((2.*Mate->Casuale()-1.)*10.); 00590 for(int c=0;c<NCol;c++){ 00591 int p1 = (r*NCol+c)*4; 00592 int p2 = ((r+1)*NCol+c)*4; 00593 PBound[p1+3] += NBorder; 00594 PBound[p2+2] += NBorder; 00595 } 00596 } 00597 for(int r=0;r<NRow;r++){ 00598 for(int c=0;c<NCol;c++){ 00599 int p1 = (r*NCol+c)*4; 00600 double x = PBound[p1 ]; 00601 double y = PBound[p1+2]; 00602 int p2 = (r*NCol+c)*2; 00603 PPos[p2 ] = x; 00604 PPos[p2+1] = y; 00605 PPos[p2 ] -= floor(PPos[p2 ]*InvEdge[0])*Edge[0]; 00606 PPos[p2+1] -= floor(PPos[p2+1]*InvEdge[1])*Edge[1]; 00607 } 00608 } 00609 } 00610 // for(int c=0;c<NCol;c++){ 00611 // int p1 = (0*NCol+c)*4; 00612 // printf("x %d) %d %d %d\n",c,PBound[p1],PBound[p1+1],PBound[p1+1]-PBound[p1],NWidth); 00613 // } 00614 // for(int r=0;r<NRow;r++){ 00615 // int p1 = (r*NCol+0)*4; 00616 // printf("y %d) %d %d %d\n",r,PBound[p1+2],PBound[p1+3],PBound[p1+3]-PBound[p1+2],NHeight); 00617 // } 00618 Mate->PermuteRandomAll(Perm,NRow*NCol); 00619 //------------------------loop---------------------------- 00620 for(int s=NStill+NStep;s>=NStep;s--){ 00621 char cImage[160]; 00622 sprintf(cImage,"Image%05u.png",s); 00623 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00624 for(int h=0;h<NHeight;h++){ 00625 for(int w=0;w<NWidth;w++){ 00626 ImageOut.plot(w,h,data[0][h*NWidth+w],data[1][h*NWidth+w],data[2][h*NWidth+w]); 00627 } 00628 } 00629 ImageOut.close(); 00630 } 00631 for(int s=NStep;s>=0;s--){ 00632 NPCol += NStepPCol; 00633 if(NPCol > NCol) NPCol = NCol; 00634 NPRow += NStepPRow; 00635 if(NPRow > NRow) NPRow = NRow; 00636 fprintf(stderr,"Elaborating %lf %%\r",s*InvNStep*100.); 00637 for(int r=0;r<NRow;r++){ 00638 for(int c=0;c<NCol;c++){ 00639 int p1 = (r*NCol+c)*2; 00640 PFor[p1 ] = 0.; 00641 PFor[p1+1] = 0.; 00642 } 00643 } 00644 for(int r=0;r<NRow;r++){ 00645 for(int c=0;c<NCol;c++){ 00646 if(c > (int)NPCol) continue; 00647 if(r > (int)NPRow) continue; 00648 int p1 = Perm[r*NCol+c].m*2; 00649 if(p1 < 0 || p1 >= NRow*NCol*2){ 00650 continue; 00651 } 00652 for(int r1=r+1;r1<NRow;r1++){ 00653 for(int c1=c+1;c1<NCol;c1++){ 00654 //int p2 = (r1*NCol+c1)*2; 00655 int p2 = Perm[r1*NCol+c1].m*2; 00656 if(p2 < 0 || p2 >= NRow*NCol) continue; 00657 double Dist[3]; 00658 for(int d=0;d<2;d++){ 00659 Dist[d] = PPos[p1+d] - PPos[p2+d]; 00660 Dist[d] -= floor(Dist[d]/Edge[d] + .5)*Edge[d]; 00661 } 00662 double Dist2 = SQR(Dist[0]) + SQR(Dist[1]); 00663 if(Dist2 > SQR(3.*Sigma))continue; 00664 Dist[2] = sqrt(Dist2); 00665 double a = (Sigma/sqrt(Dist2)); 00666 //double Force = Eps*12.*pow(a,13.) - Eps*6.*pow(a,7.); 00667 // if(Force > 20.) Force = 20.; 00668 double Force = -Eps/Dist2; 00669 for(int d=0;d<2;d++){ 00670 PFor[p1+d] -= Force*Dist[d]/Dist[2]; 00671 PFor[p2+d] += Force*Dist[d]/Dist[2]; 00672 } 00673 } 00674 } 00675 } 00676 } 00677 //-------------integration-------------- 00678 for(int r=0;r<NRow;r++){ 00679 for(int c=0;c<NCol;c++){ 00680 if(c > (int)NPCol) continue; 00681 if(r > (int)NPRow) continue; 00682 int p1 = Perm[r*NCol+c].m*2; 00683 if(p1 < 0 || p1 >= NRow*NCol*2){ 00684 continue; 00685 } 00686 for(int d=0;d<2;d++){ 00687 PVel[p1+d] += PFor[p1+d]*Dt; 00688 PPos[p1+d] += PVel[p1+d]*Dt; 00689 PPos[p1+d] -= floor(PPos[p1+d]*InvEdge[d])*Edge[d]; 00690 } 00691 // printf("%d %lf %lf\n",r*NCol+c,PPos[p1],PPos[p1+1]); 00692 } 00693 } 00694 for(int l=0;l<3;l++){ 00695 for(int h=0;h<NHeight;h++){ 00696 for(int w=0;w<NWidth;w++){ 00697 data[l][h*NWidth+w] = 0.; 00698 } 00699 } 00700 } 00701 //---------------updating position------------- 00702 for(int r=0;r<NRow;r++){ 00703 for(int c=0;c<NCol;c++){ 00704 int p1 = (r*NCol+c)*2; 00705 int xn = (int)(PPos[p1 ]); 00706 int yn = (int)(PPos[p1+1]); 00707 int xo = PBound[(r*NCol+c)*4 ]; 00708 int yo = PBound[(r*NCol+c)*4+2]; 00709 int WidSize = PBound[(r*NCol+c)*4+1]-PBound[(r*NCol+c)*4 ]; 00710 int HeiSize = PBound[(r*NCol+c)*4+3]-PBound[(r*NCol+c)*4+2]; 00711 //printf("%d %d) %d %d -> %d %d |%d %d| %f %f\n",s,r*NCol+c,xo,yo,xn,yn,WidSize,HeiSize,PPos[p1],PPos[p1+1]); 00712 for(int l=0;l<3;l++){ 00713 for(int ws=0;ws<WidSize;ws++){ 00714 int wo = xo+ws; 00715 int wn = xn+ws; 00716 if(wo >= NWidth){ 00717 printf("x out of bound %d > %d | %d\n",wo,NWidth,WidSize); 00718 continue; 00719 } 00720 if(wn >= NWidth) wn -= NWidth; 00721 for(int hs=0;hs<HeiSize;hs++){ 00722 int ho = yo+hs; 00723 int hn = yn+hs; 00724 if(ho >= NHeight){ 00725 printf("y out of bound %d+%d = %d > %d |%d\n",yo,hs,ho,NHeight,HeiSize); 00726 continue; 00727 } 00728 if(hn > NHeight) hn -= NHeight; 00729 //printf("%d= %d %d) %d->%d (%d %d) %d->%d (%d %d)\n",p1,r,c,xo,xn,WidSize,NWidth,yo,yn,HeiSize,NHeight); 00730 if(hn*NWidth+wn < 0 || hn*NWidth+wn >= NWidth*NHeight){ 00731 printf("input out of border hn %d wn %d > %d %d\n",hn,wn,yn,xn,hn*NWidth+wn,NWidth,NHeight); 00732 continue; 00733 } 00734 if(ho*NWidth+wo < 0 || ho*NWidth+wo >= NWidth*NHeight){ 00735 printf("output out of border %d %d %d > %d %d\n",hn,wn,hn*NWidth+wn,NWidth,NHeight); 00736 continue; 00737 } 00738 data[l][hn*NWidth+wn] = data2[l][ho*NWidth+wo]; 00739 } 00740 } 00741 } 00742 } 00743 } 00744 //-------------saving images 00745 char cImage[160]; 00746 sprintf(cImage,"Image%05u.png",s); 00747 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00748 for(int h=0;h<NHeight;h++){ 00749 for(int w=0;w<NWidth;w++){ 00750 ImageOut.plot(w,h,data[0][h*NWidth+w],data[1][h*NWidth+w],data[2][h*NWidth+w]); 00751 } 00752 } 00753 ImageOut.close(); 00754 } 00755 printf("\n"); 00756 for(int l=0;l<3;l++){ 00757 free(data2[l]); 00758 } 00759 free(data2); 00760 free(PPos); 00761 free(PFor); 00762 free(PVel); 00763 free(PBound); 00764 } 00765 #include <fftw3.h> 00766 void DrImage::Fourier(){ 00767 int NStill = 0;//50; 00768 int NStep = 10;//(int)(25.*60.*.2) - NStill; 00769 int NChange = 60; 00770 int NSquare = 2; 00771 double Norm = 1./(double)(NWidth*NHeight); 00772 double InvNStep = 1./(double)NStep; 00773 Matematica *Mate = new Matematica(); 00774 fftw_complex *out = (fftw_complex *)fftw_malloc(NWidth*NHeight*sizeof(fftw_complex)); 00775 fftw_complex *in = (fftw_complex *)fftw_malloc(NWidth*NHeight*sizeof(fftw_complex)); 00776 fftw_plan direct = fftw_plan_dft_2d(NWidth,NHeight, 00777 in,out,FFTW_FORWARD,FFTW_PATIENT); 00778 fftw_plan reverse = fftw_plan_dft_2d(NWidth,NHeight, 00779 out,in,FFTW_BACKWARD,FFTW_PATIENT); 00780 int NhInit = NHeight; 00781 int NwInit = NWidth; 00782 double **data2 = (double **)calloc(3,sizeof(double)); 00783 for(int l=0;l<3;l++){ 00784 data2[l] = (double *)calloc(NHeight*NWidth,sizeof(double)); 00785 } 00786 for(int l=0;l<3;l++){ 00787 for(int h=0;h<NHeight;h++){ 00788 for(int w=0;w<NWidth;w++){ 00789 data2[l][h*NWidth+w] = data[l][h*NWidth+w]; 00790 } 00791 } 00792 } 00793 // for(int s=NStill+NStep;s>=NStep;s--){ 00794 // char cImage[160]; 00795 // sprintf(cImage,"Image%05u.png",s); 00796 // pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00797 // for(int h=0;h<NHeight;h++){ 00798 // for(int w=0;w<NWidth;w++){ 00799 // ImageOut.plot(w,h,data[0][h*NWidth+w],data[1][h*NWidth+w],data[2][h*NWidth+w]); 00800 // } 00801 // } 00802 // ImageOut.close(); 00803 // } 00804 for(int s=NStep;s>0;s--){ 00805 // NhInit--; 00806 // if(NhInit < 0) NhInit = 0; 00807 // NwInit--; 00808 // if(NwInit < 0) NwInit = 0; 00809 fprintf(stderr,"Elaborating %lf %%\r",s*InvNStep*100.); 00810 for(int l=0;l<3;l++){ 00811 for(int h=0;h<NHeight;h++){ 00812 for(int w=0;w<NWidth;w++){ 00813 in[h*NWidth+w][0] = data2[l][h*NWidth+w]; 00814 out[h*NWidth+w][0] = 0.; 00815 out[h*NWidth+w][1] = 0.; 00816 } 00817 } 00818 fftw_execute(direct); 00819 // for(int h=NhInit;h<NHeight;h++){ 00820 // for(int w=NwInit;w<NWidth;w++){ 00821 // printf("%d %d\n",h,w); 00822 // out[h*NWidth+w][0] = 0.; 00823 // out[h*NWidth+w][1] = 0.; 00824 // } 00825 // } 00826 fftw_execute(reverse); 00827 for(int h=0;h<NHeight;h++){ 00828 for(int w=0;w<NWidth;w++){ 00829 data[l][h*NWidth+w] = in[h*NWidth+w][0]*Norm; 00830 } 00831 } 00832 } 00833 char cImage[160]; 00834 sprintf(cImage,"Image%05u.png",s); 00835 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00836 for(int h=0;h<NHeight;h++){ 00837 for(int w=0;w<NWidth;w++){ 00838 ImageOut.plot(w,h,data[0][h*NWidth+w],data[1][h*NWidth+w],data[2][h*NWidth+w]); 00839 } 00840 } 00841 ImageOut.close(); 00842 // FILE *FOut = fopen("Result1.dat","w"); 00843 // for(int h=0;h<NHeight;h++){ 00844 // for(int w=0;w<NWidth;w++){ 00845 // double x = w/(double)NWidth; 00846 // double y = h/(double)NHeight; 00847 // fprintf(FOut,"%lf %lf %lf\n",x,y,data[2][h*NWidth+w]); 00848 // } 00849 // } 00850 // fclose(FOut); 00851 // FOut = fopen("Result2.dat","w"); 00852 // for(int h=0;h<NHeight;h++){ 00853 // for(int w=0;w<NWidth;w++){ 00854 // double x = w/(double)NWidth; 00855 // double y = h/(double)NHeight; 00856 // fprintf(FOut,"%lf %lf %lf\n",x,y,in[h*NWidth+w][0]); 00857 // } 00858 // } 00859 // fclose(FOut); 00860 } 00861 fftw_destroy_plan(direct); 00862 fftw_destroy_plan(reverse); 00863 fftw_free(out); 00864 fftw_free(in); 00865 printf("\n"); 00866 } 00867 void DrImage::Gauss(){ 00868 int NStill = 0;//50; 00869 int NStep = (int)(25.*60.*.2) - NStill; 00870 int NSkip = 15; 00871 double *Plot2 = (double *)calloc(NWidth*NHeight,sizeof(double)); 00872 double InvNStep = 1./(double)NStep; 00873 Matrice Mask(3,3); 00874 Mask.FillGaussian(.5,3.); 00875 Mask.Print(); 00876 int NMat2 = (int)Mask.pNCol()/2; 00877 for(int s=NStill+NStep;s>=NStep;s--){ 00878 char cImage[160]; 00879 sprintf(cImage,"Image%05u.png",s); 00880 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00881 for(int h=0;h<NHeight;h++){ 00882 for(int w=0;w<NWidth;w++){ 00883 ImageOut.plot(w,h,data[0][h*NWidth+w],data[1][h*NWidth+w],data[2][h*NWidth+w]); 00884 } 00885 } 00886 ImageOut.close(); 00887 } 00888 for(int s=NStep,sq=0;s>=0;s--){ 00889 // if( (s%NDecr) == 0 ) sq++; 00890 // if(sq < 0) sq = 0; 00891 // if(sq > NSquare) sq = NSquare-1; 00892 fprintf(stderr,"Elaborating %lf %%\r",s*InvNStep*100.); 00893 char cImage[160]; 00894 sprintf(cImage,"Image%05u.png",s); 00895 pngwriter ImageOut(NWidth,NHeight,1.0,cImage); 00896 //if((s%NSkip) == 0) 00897 { 00898 //convolute filter 00899 for(int l=0;l<3;l++){ 00900 for(int gx=0;gx<NWidth;gx++){ 00901 for(int gy=0;gy<NHeight;gy++){ 00902 Plot2[gy*NWidth+gx] = 0.; 00903 for(int mx=0;mx<Mask.pNRow();mx++){ 00904 int g1x = gx + mx - NMat2; 00905 if(g1x >= NWidth) continue;//g1x -= NGrid; 00906 if(g1x < 0) continue;//g1x + NGrid; 00907 for(int my=0;my<Mask.pNCol();my++){ 00908 int g1y = gy + my - NMat2; 00909 if(g1y >= NHeight) continue;//g1y -= NGrid; 00910 if(g1y < 0) continue;//g1y + NGrid; 00911 Plot2[gy*NWidth+gx] += data[l][g1y*NWidth+g1x]*Mask.Val(mx,my); 00912 } 00913 } 00914 } 00915 } 00916 for(int gx=0;gx<NWidth;gx++){ 00917 for(int gy=0;gy<NHeight;gy++){ 00918 data[l][gy*NWidth+gx] = Plot2[gy*NWidth+gx]; 00919 } 00920 } 00921 } 00922 } 00923 for(int h=0;h<NHeight;h++){ 00924 for(int w=0;w<NWidth;w++){ 00925 ImageOut.plot(w,h,data[0][h*NWidth+w],data[1][h*NWidth+w],data[2][h*NWidth+w]); 00926 } 00927 } 00928 ImageOut.close(); 00929 } 00930 free(Plot2); 00931 printf("\n"); 00932 } 00933 void DrImage::SlitScan(){ 00934 int NStep = 1; 00935 pngwriter ImageOut[NStep]; 00936 for(int f=0;f<NStep;f++){ 00937 char cImage[160]; 00938 sprintf(cImage,"Image%05u.png",f); 00939 ImageOut[f] = new pngwriter(NWidth,NHeight,1.0,cImage); 00940 } 00941 for(int f=0;f<NFile;f++){ 00942 fprintf(stderr,"Elaborating %lf %%\r",f/(double)NFile*100.); 00943 ReLoad(FileList[f]); 00944 int wInit = f; 00945 if(wInit >= NWidth) break;//wInit -= NWidth; 00946 for(int w=wInit;w<NWidth;w++){ 00947 int f1 = 0; 00948 for(int h=0;h<NHeight;h++){ 00949 //int set = f + h*NFile; 00950 ImageOut[f1].plot(w,h,data[0][h*NWidth + w],data[1][h*NWidth + w],data[2][h*NWidth + w]); 00951 } 00952 } 00953 } 00954 for(int s=0;s<NStep;s++){ 00955 ImageOut[s].close(); 00956 } 00957 printf("\n"); 00958 } 00959 #endif //USE_PNG