?? cimg_demo.cpp
字號:
/*------------------------------------------------------------------------------- File : CImg_demo.cpp Description : A multi-part demo demonstrating some of the CImg capabilities Copyright : David Tschumperle - http://www.greyc.ensicaen.fr/~dtschump/ This software is governed by the CeCILL-C license under French law and abiding by the rules of distribution of free software. You can use, modify and/ or redistribute the software under the terms of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info". As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license, users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability. In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software, that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge. Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured and, more generally, to use and operate it in the same conditions as regards security. The fact that you are presently reading this means that you have had knowledge of the CeCILL-C license and that you accept its terms. ------------------------------------------------------------------------------*/#include "../CImg.h"using namespace cimg_library;// Routine used to compute point depth in Mandelbrot fractalsinline int mandelbrot(const double x, const double y, const int maxiter) { static double old_x, fx, fy; static int m; fx = fy = m = 0; do { old_x = fx; fx = fx*fx-fy*fy+x; fy = 2*old_x*fy+y; m++; } while (((fx*fx+fy*fy)<4) && (m<maxiter)); return m;}// Class used by the metaballs demostruct metaballs3d { float cx1,cy1,cz1,cx2,cy2,cz2,cx3,cy3,cz3; inline float operator()(const float x,const float y,const float z) const { const float x1 = x-cx1, y1 = y-cy1, z1 = z-cz1, x2 = x-cx2, y2 = y-cy2, z2 = z-cz2, x3 = x-cx3, y3 = y-cy3, z3 = z-cz3, r1 = 0.3f*(x1*x1 + y1*y1 + z1*z1), r2 = 0.4f*(x2*x2 + y2*y2 + z2*z2), r3 = 0.5f*(x3*x3 + y3*y3 + z3*z3); float potential = 0; if (r1<1.3) potential+= 1.0f - r1*(r1*(4*r1+17)-22)/9; if (r2<1.3) potential+= 1.0f - r2*(r2*(4*r2+17)-22)/9; if (r3<1.3) potential+= 1.0f - r3*(r3*(4*r3+17)-22)/9; return potential; }};/*--------------------------- Main procedure --------------------------*/int main() { // Display info about the CImg Library configuration //-------------------------------------------------- cimg::info(); // Demo selection menu //--------------------- const unsigned char white[3] = {255,255,255}, red[3] = {120,50,80}, yellow[3] = {200,155,0}, green[3] = {30,200,70}; float t=0, rx=(float)(2*cimg::crand()), ry=(float)(2*cimg::crand()); int demo_number,y0=2*13; bool stopflag = false; CImg<unsigned char> back(1,2,1,3,10), img; back(0,1,2) = 235; back.resize(320,390,1,3,3).get_shared_channel(2).noise(10,1).draw_plasma(); back.draw_rectangle(0,y0,back.dimx()-1,y0+12,red). draw_text(20,y0,white,0,13,1, "=> CImg %g Demos : \n\n" "- Blurring Gradient\n" "- Rotozoom\n" "- Image Denoising\n" "- Fractal Animation\n" "- Gamma Correction and Histograms\n" "- Filled Triangles\n" "- Mandelbrot explorer\n" "- Mini-Paint\n" "- Soccer Bobs\n" "- Bump Effect\n" "- Bouncing Bubble\n" "- Virtual Landscape\n" "- Plasma & Sinus Scroll\n" "- Oriented Convolutions\n" "- Shade Bobs\n" "- Fourier Filtering\n" "- Image Zoomer\n" "- Blobs Editor\n" "- Double Torus\n" "- 3D Metaballs\n" "- Fireworks\n" "- Rubber Logo\n" "- Image Waves\n" "- Breakout\n", cimg_version); CImgDisplay disp(back,"CImg Demo Menu",0,3,false,true); disp.move((CImgDisplay::screen_dimx()-disp.window_width)/2, (CImgDisplay::screen_dimy()-disp.window_height)/2); img=back; back*=0.1; for(y0+=2*13; !stopflag; ) { while(!(disp.button&1) && !stopflag) { if (disp.is_closed || disp.key == cimg::keyQ || disp.key==cimg::keyESC) stopflag=true; img*=0.9; img+=back; int y = disp.mouse_y; if (y>=y0 && y<y0+24*13) { y = (y/13)*13+7; for (int yy=y-7; yy<=y+6; yy++) img.draw_rectangle(0,yy,0,1,img.dimx()-1,yy,0,1,(unsigned char)(130-15*cimg::abs(yy-y))); img.draw_triangle(2,y-4,2,y+4,8,y,yellow).draw_triangle(img.dimx()-2,y-4,img.dimx()-2,y+4,img.dimx()-8,y,yellow); } for (int i=0; i<20; i++) { const int mx = (int)(img.dimx()/2+(img.dimx()/2-30)*cos(3*t+rx*i*36*cimg::PI/180)), my = (int)(img.dimy()/2+(img.dimy()/2-30)*sin(4*t+ry*i*36*cimg::PI/180)); const float mz = (float)(1.0f+0.4f*sin(2*t+(rx+ry)*i*20*cimg::PI/180)); img.draw_circle(mx,my,10*mz,i%3?(i%2?green:red):yellow,0L,0.2f).draw_circle((int)(mx+3*mz),my-3,3*mz,white,0L,0.1f); } if ((t+=0.007f)>1) { rx=(float)(2*cimg::crand()); ry=(float)(2*cimg::crand()); t=0; } disp.resize(disp,false).display(img).wait(25); } disp.button=0; if (!stopflag) demo_number = (disp.mouse_y-y0)/13; else break; switch(demo_number) { case 0: { // Blurring gradient demo //------------------------ const CImg<> src("img/milla.ppm"); CImgList<> grad = src.get_gradientXY(3); CImgList<unsigned char> visu = src<<((grad[0].pow(2))+(grad[1].pow(2))).sqrt().normalize(0,255)<<src; CImgDisplay disp(visu,"Image, Gradient Norm and Blurring Gradient",0,1); for (double sigma=0; !disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC; sigma+=0.05) { visu[2] = visu[1].get_blur((float)cimg::abs(30*cos(sigma))).normalize(0,255); disp.resize(false).display(visu).wait(20); } } break; case 1: { // Rotozoom demo //--------------- CImg<unsigned char> src = CImg<unsigned char>("img/milla.ppm").resize(400,300,1,3,3), img(src); CImgDisplay disp(img,"Rotozoom Demo"); float alpha=0, t=0, angle=0, zoom0=-0.5f; unsigned char color[3]={0,0,0}; while(!disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC) { cimg_forYV(src,y,k) { const int xc = 4*src.dimx()+(int)(60*sin((float)y*3/src.dimy()+10*t)); cimg_forX(src,x) { const float val = (float)(src((xc+x)%src.dimx(),y,0,k)* (1.3f+0.20*sin(alpha+k*k*((float)src.dimx()/2-x)* ((float)src.dimy()/2-y)*cos(t)/300.0))); img(x,y,0,k) = (unsigned char)(val>255.0f?255:val); } } const float zoom=(float)(zoom0+0.3*(1+cos(3*t))); img.get_rotate(angle,0.5f*img.dimx(),0.5f*img.dimy(),1+zoom,1). draw_text("Mouse buttons\nto zoom in/out",3,3,color,0,24,1.0f).display(disp); alpha+=0.7f; t+=0.01f; angle+=0.8f; if (disp.button&1) zoom0+=0.10f; if (disp.button&2) zoom0-=0.10f; if (disp.is_typed(cimg::keyCTRLLEFT,cimg::keyF)) { disp.toggle_fullscreen(); if (disp.is_fullscreen) disp.resize(disp.screen_dimx(), disp.screen_dimy(), false); else disp.resize(img,false); } disp.resize(false).wait(20); } } break; case 2: { // Image denoising (Total variation PDE) //--------------------------------------- const CImg<> src = CImg<>("img/milla.ppm").noise(-3,2).noise(-10,1); CImgList<> images(src,src); CImgDisplay disp(images,"Image denoising",1,1); float white[3]={255,255,255}, black[3]={0,0,0}; CImg_3x3(I,float); for (unsigned int iter=0; !disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC; iter++) { CImg<> veloc(src); cimg_forV(src,k) cimg_for3x3(images[1],x,y,0,k,I) { const float ix = (Inc-Ipc)/2, iy = (Icn-Icp)/2, ng = ix*ix + iy*iy; veloc(x,y,k) = (ng>1e-14)?( ix*ix * ( Icn + Icp - 2*Icc ) + iy*iy * ( Inc + Ipc - 2*Icc ) - ix*iy * ( Ipp + Inn - Ipn - Inp )/2 )/ng:0; } images[1]+=veloc*0.02; images[0].draw_text(0,0,white,black,11,1,"iter %u",iter++); disp.resize(false).display(images); } } break; case 3: { // Fractal-like animation //------------------------ CImg<unsigned char> img(256,256,1,3,0),noise(3,2,1,3); CImgDisplay disp(img,"Super Zoomator",0,3); double zoom=0; for (unsigned int iter=0; !disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC; iter++,zoom+=0.2) { img.draw_image(noise.fill(0).noise(255,1),(img.dimx()-noise.dimx())/2,(img.dimy()-noise.dimy())/2); img.rotate((float)(10*sin(iter/25.0)),0.5f*img.dimx(),0.5f*img.dimy(),(float)(1.04+0.02*sin(zoom/10)),0); disp.resize(img.resize(disp.window_dimx(),disp.window_dimy()),false).display(img).wait(25); if (disp.is_typed(cimg::keyCTRLLEFT,cimg::keyF)) { disp.toggle_fullscreen(); if (disp.is_fullscreen) disp.resize(disp.screen_dimx(),disp.screen_dimy(),false); else disp.resize(img,false); } } } break; case 4: { // Gamma correction and histogram visualization //---------------------------------------------- CImg<> img = CImg<>("img/milla.ppm").normalize(0,1); CImgList<unsigned char> visu(img*255, CImg<unsigned char>(512,300,1,3,0)); const unsigned char yellow[3] = {255,255,0}, blue[3]={0,155,255}, blue2[3]={0,0,255}, blue3[3]={0,0,155}, white[3]={255,255,255}; CImgDisplay disp(visu,"Image and Histogram (Mouse click to set the Gamma correction)"); for (double gamma=1;!disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC; ) { cimg_forXYZV(visu[0],x,y,z,k) visu[0](x,y,z,k) = (unsigned char)(pow((double)img(x,y,z,k),1.0/gamma)*256); const CImg<> hist = visu[0].get_histogram(50,0,255); visu[1].fill(0).draw_text(50,5,white,0,24,1,"Gamma = %g",gamma). draw_graph(hist,yellow,1,20000,0).draw_graph(hist,white,2,20000,0). draw_axis(0,256,20000,0,white,-60,-60,0,0,0.7f); const int xb = (int)(50+gamma*150); visu[1].draw_grid(20,20,0,0,white,0xCCCCCCCC,0xCCCCCCCC,false,false,0.3f); visu[1].draw_rectangle(51,31,xb-1,39,blue2).draw_rectangle(50,30,xb,30,blue).draw_rectangle(xb,30,xb,40,blue); visu[1].draw_rectangle(xb,40,50,39,blue3).draw_rectangle(50,30,51,40,blue3); if (disp.button && disp.mouse_x>=img.dimx()+50 && disp.mouse_x<=img.dimx()+450) gamma = (disp.mouse_x-img.dimx()-50)/150.0; disp.resize(disp,false).display(visu).wait(); } } break; case 5: { // Filled triangle demos //----------------------- CImg<> background(640,480,1,3); cimg_forXY(background,x,y) { background(x,y,0) = (float)(x*cos(6.0*y/background.dimy())+y*sin(9.0*x/background.dimx())); background(x,y,1) = (float)(x*sin(8.0*y/background.dimy())-y*cos(11.0*x/background.dimx())); background(x,y,2) = (float)(x*cos(13.0*y/background.dimy())-y*sin(8.0*x/background.dimx())); } background.normalize(0,180); CImg<unsigned char> img0(background), img; unsigned char white[3] = { 255,255,255 }, color[100][3]; CImgDisplay disp(img0,"Filled Triangle Demo",0,3); float posx[100],posy[100],rayon[100],angle[100],veloc[100],opacity[100]; int num=1; srand((unsigned int)time(NULL)); for (int k=0; k<100; k++) { posx[k] = (float)(cimg::rand()*img0.dimx()); posy[k] = (float)(cimg::rand()*img0.dimy()); rayon[k] = (float)(10+cimg::rand()*50); angle[k] = (float)(cimg::rand()*360); veloc[k] = (float)(cimg::rand()*20-10); color[k][0] = (unsigned char)(cimg::rand()*255); color[k][1] = (unsigned char)(cimg::rand()*255); color[k][2] = (unsigned char)(cimg::rand()*255); opacity[k] = (float)(0.3+1.5*cimg::rand()); } while(!disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC) { img = img0; for (int k=0; k<num; k++) { const int x0 = (int)(posx[k]+rayon[k]*cos(angle[k]*cimg::PI/180)), y0 = (int)(posy[k]+rayon[k]*sin(angle[k]*cimg::PI/180)), x1 = (int)(posx[k]+rayon[k]*cos((angle[k]+120)*cimg::PI/180)), y1 = (int)(posy[k]+rayon[k]*sin((angle[k]+120)*cimg::PI/180)), x2 = (int)(posx[k]+rayon[k]*cos((angle[k]+240)*cimg::PI/180)), y2 = (int)(posy[k]+rayon[k]*sin((angle[k]+240)*cimg::PI/180)); if (k%10) img.draw_triangle(x0,y0,x1,y1,x2,y2,color[k],opacity[k]); else img.draw_triangle(x0,y0,x1,y1,x2,y2,img0,0,0,img0.dimx()-1,0,0,img.dimy()-1,opacity[k]); img.draw_line(x0,y0,x1,y1,white,(unsigned long)~0,opacity[k]). draw_line(x1,y1,x2,y2,white,(unsigned long)~0,opacity[k]). draw_line(x2,y2,x0,y0,white,(unsigned long)~0,opacity[k]); angle[k]+=veloc[k]; if (disp.button && disp.mouse_x>0 && disp.mouse_y>0) { float u = disp.mouse_x-posx[k], v = disp.mouse_y-posy[k]; if (disp.button&2) { u=-u; v=-v; } posx[k]-=0.1f*u, posy[k]-=0.1f*v; if (posx[k]<0 || posx[k]>=img.dimx()) posx[k]=(float)(cimg::rand()*img.dimx()); if (posy[k]<0 || posy[k]>=img.dimy()) posy[k]=(float)(cimg::rand()*img.dimy()); } } img.draw_text(5,5,white,0,11,0.5f,"%u frames/s",(unsigned int)disp.frames_per_second()); disp.display(img).resize(false).wait(20); img0.resize(disp); num++; if (num>100) num=100; if (disp.is_typed(cimg::keyCTRLLEFT,cimg::keyF)) { disp.toggle_fullscreen(); if (disp.is_fullscreen) disp.resize(disp.screen_dimx(),disp.screen_dimy(),false); else disp.resize(img,false); } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -