📄 cimg_demo.cpp
字号:
} break; case 6: { // Mandelbrot explorer //---------------------- CImg<unsigned char> img(800,600,1,3,0); CImg<unsigned int> iterimg(img.dimx(),img.dimy()); CImgDisplay disp(img,"Mandelbrot Explorer"); unsigned char color[3] = { 255,255,255 }; for (bool endflag=false; !endflag;) { bool stopflag = false; double xmin=-2.25, xmax=1, ymin=-1.5, ymax=1.5; const unsigned int cr = (2+(int)(8*cimg::rand()))*4; const unsigned int cg = (2+(int)(8*cimg::rand()))*4; const unsigned int cb = (2+(int)(8*cimg::rand()))*4; unsigned int maxiter=64; while(!stopflag) { if (disp.is_resized) { disp.resize(); iterimg.resize(disp,0); img.resize(disp,0); } const double dx=(xmax-xmin)/img.dimx(); const double dy=(ymax-ymin)/img.dimy(); double y = ymin; cimg_forY(img,py) { double x = xmin; cimg_forX(img,px) { const unsigned int m = mandelbrot(x,y,maxiter); iterimg(px,py) = m; img(px,py,0)=(unsigned char)(cr*m); img(px,py,1)=(unsigned char)(cg*m); img(px,py,2)=(unsigned char)(cb*m); x+=dx; } y+=dy; } int selection[6]; img.feature_selection(selection,2,disp,NULL,color); if (selection[0]>=0 && selection[1]>=0 && selection[3]>=0 && selection[4]>=0) { if (selection[3]-selection[0]<5 || selection[4]-selection[1]<5) stopflag=true; xmax = xmin + selection[3]*dx; ymax = ymin + selection[4]*dy; xmin = xmin + selection[0]*dx; ymin = ymin + selection[1]*dy; } if (disp.is_typed(cimg::keyCTRLLEFT,cimg::keyF)) disp.resize(disp.is_fullscreen?800:CImgDisplay::screen_dimx(), disp.is_fullscreen?600:CImgDisplay::screen_dimy()). toggle_fullscreen().is_resized = true; CImgStats stats(iterimg,0); if (stats.mean>0.7*maxiter) maxiter+=64; if (maxiter>1024) maxiter=1024; if (disp.is_closed || disp.key==cimg::keyQ || disp.key==cimg::keyESC) stopflag=endflag=true; } } } break; case 7: { // Mini-Paint demo //----------------- int xo=-1,yo=-1,x=-1,y=-1; bool redraw = true; CImg<unsigned char> img(256,256+64,1,3,0); unsigned char color[3]={255,255,255}; cimg_forXY(img,xx,yy) if (yy>=256) { img(xx,yy,0) = (unsigned char)xx; img(xx,yy,1) = (unsigned char)((yy-256)*4); img(xx,yy,2) = (unsigned char)((3*xx)%256); } CImgDisplay disp(img.draw_text(" ",5,5,color,color),"Mini-Paint"); while (!disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC) { const unsigned int but = disp.button; redraw = false; xo = x; yo = y; x = disp.mouse_x; y = disp.mouse_y; if (xo>=0 && yo>=0 && x>=0 && y>=0) { if (but&1 || but&4) { if (y<253) { const float tmax = (float)cimg::max(cimg::abs(xo-x),cimg::abs(yo-y))+0.1f, radius = (but&1?3.0f:0.0f) + (but&4?6.0f:0.0f); for (float t=0; t<=tmax; t++) img.draw_circle((int)(x+t*(xo-x)/tmax),(int)(y+t*(yo-y)/tmax),radius,color); } if (y>=256) { color[0]=img(x,y,0); color[1]=img(x,y,1); color[2]=img(x,y,2); img.draw_text(" ",5,5,color,color); } redraw = true; } if (y>=253) y=252; if (disp.button&2) { img.draw_fill(x,y,color); redraw = true; } } if (redraw) disp.display(img); disp.resize(disp).wait(); if (disp.key) cimg_forV(img,k) { img.get_shared_lines(0,255,0,k).fill(0); img.display(disp); } } } break; case 8: { // Soccer Bobs Demo //------------------ const unsigned int nb_canvas=16; unsigned int curr_canvas=0; float zoom=0.2f; unsigned char color[3]={255,255,0}; CImg<unsigned char> foot = "img/foot.ppm", canvas0(640,480,1,3,0); cimg_forXY(canvas0,x,y) canvas0(x,y,1)=(unsigned char)(20+(y*215/canvas0.dimy())+cimg::crand()*19); canvas0.draw_text("Left/Right Mouse Button = Zoom In/Out\nMiddle Button = Reset Screen",1,1,color); CImgList<unsigned char> canvas(nb_canvas, canvas0); CImg<float> mask(foot.dimx(),foot.dimy()); { cimg_forXY(mask,x,y) mask(x,y) = (foot(x,y,0)==255 && !foot(x,y,1) && !foot(x,y,2))?0:0.5f; } CImgDisplay disp(canvas0,"Unlimited Soccer Bobs"); while (!disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC) { if (disp.mouse_x>=0 && disp.mouse_y>=0) canvas[curr_canvas].draw_image(foot.get_resize((int)(foot.dimx()*zoom),(int)(foot.dimy()*zoom)), mask.get_resize((int)(foot.dimx()*zoom),(int)(foot.dimy()*zoom)), (int)(disp.mouse_x-0.5*zoom*foot.dimx()),(int)(disp.mouse_y-0.5*zoom*foot.dimy()),0,0); if (disp.button&1) zoom+=0.03f; if (disp.button&2) zoom-=0.03f; if (disp.button&4) cimglist_for(canvas,l) canvas[l] = canvas0; if (disp.is_typed(cimg::keyCTRLLEFT,cimg::keyF)) disp.toggle_fullscreen(); if (zoom>1) zoom=1; if (zoom<0.1) zoom=0.1f; disp.display(canvas[curr_canvas++]).resize(disp,false).wait(20); curr_canvas%=nb_canvas; } } break; case 9: { // Bump Effect //------------- float t=0,color = 255; CImg<> logo = CImg<>(56,32,1,1,0).draw_text("I Love\n CImg!",9,5,&color).resize(-800,-800,1,1,3).blur(6).normalize(0,255); logo += CImg<>(logo.dimx(),logo.dimy(),1,1,0).noise(80,1).deriche(2,0,'y',0).deriche(10,0,'x',0); CImgList<> grad = logo.get_gradientXY(); cimglist_apply(grad,normalize)(-140,140); logo.normalize(0,255); CImg<> light(300+2*logo.dimx(),300+2*logo.dimy()); light.draw_gaussian(0.5f*light.dimx(),0.5f*light.dimy(),80,&color); CImg<unsigned char> img(logo.dimx(),logo.dimy(),1,3,0); CImgDisplay disp(img,"Bump Effect"); while(!disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC) { const int mouse_x = (disp.mouse_x>=0 && disp.button)?disp.mouse_x*img.dimx()/disp.dimx():(int)(img.dimx()/2 + img.dimx()*cos(1*t)/2), mouse_y = (disp.mouse_y>=0 && disp.button)?disp.mouse_y*img.dimy()/disp.dimy():(int)(img.dimy()/2 + img.dimy()*sin(3*(t+=0.03f))/2); cimg_forXY(img,x,y) { const int gx = (int)grad[0](x,y), gy = (int)grad[1](x,y); const float val = 40+(gx+gy)/2+light(light.dimx()/2+mouse_x-x+gx,light.dimy()/2+mouse_y-y+gy); img(x,y,0) = img(x,y,1) = img(x,y,2) = (unsigned char)(val>255?255:(val<0?0:val)); } disp.resize(false).display(img.draw_image(logo,0,0,0,1,0.1f)).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 10: { // Bouncing bubble //------------------ CImg<unsigned char> back(320,256,1,3,0),img; cimg_forXY(back,x,y) back(x,y,2) = (unsigned char)((y<2*back.dimy()/3)?30:(255-2*(y+back.dimy()/2))); CImgDisplay disp(back,"Bouncing bubble",0,1); const unsigned char col1[3]={40,100,10}, col2[3]={20,70,0}, col3[3]={40,150,10}, col4[3]={200,255,100}, white[3]={255,255,255}; double u = sqrt(2.0), cx = back.dimx()/2, t = 0, vt=0.05, vx = 2; while (!disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC) { img = back; int xm =(int)cx, ym = (int)(img.dimy()/2-70 + (img.dimy()/2+10)* (1-cimg::abs(cos((t+=vt))))); float r1 = 50, r2 = 50; vt=0.05; if (xm+r1>=img.dimx()) { const float delta = (xm+r1)-img.dimx(); r1-=delta; r2+=delta; } if (xm-r1<0) { const float delta = -(xm-r1); r1-=delta; r2+=delta; } if (ym+r2>=img.dimy()-40) { const float delta = (ym+r2)-img.dimy()+40; r2-=delta; r1+=delta; vt=0.05-0.0015*(50-r2); } if (ym-r2<0) { const float delta = -(ym-r2); r2-=delta; r1+=delta; } img.draw_ellipse(xm,ym,r1,r2,1,0,col1); img.draw_ellipse((int)(xm+0.03*r1*u),(int)(ym-0.03*r2*u),0.85f*r1,0.85f*r2,1,0,col2); img.draw_ellipse((int)(xm+0.1*r1*u),(int)(ym-0.1*r2*u),0.8f*r1,0.8f*r2,1,0,col1); img.draw_ellipse((int)(xm+0.2*r1*u),(int)(ym-0.2*r2*u),r1/2,r2/2,1,0,col3); img.draw_ellipse((int)(xm+0.3*r1*u),(int)(ym-0.3*r2*u),r1/4,r2/4,1,0,col4); img.draw_image(img.get_crop(0,img.dimy()-80,img.dimx()-1,img.dimy()-40).mirror('y'),0,img.dimy()-40,0,0,0.45f); img.draw_text(xm-60,(int)(ym-r2-25),white,0,17,1,"Bubble (%d,%d)",xm,ym); if ((cx+=20*vt*vx)>=img.dimx()-30 || cx<30) vx=-vx; disp.display(img).wait(20); if (disp.is_resized) { disp.resize(disp.window_dimx()>200?disp.window_dimx():200,disp.dimy(),false); back.resize(disp); cx = back.dimx()/2; } } } break; case 11: { // Virtual landscape //------------------- CImg<int> background(400,300,1,3,0), visu(background); cimg_forXY(background,x,y) { if (y>background.dimy()/2) { background(x,y,2)=255; background(x,y,0)=(y-background.dimy()/2)*512/background.dimy(); } else background(x,y,2)=y*512/background.dimy(); } const int white[3]={255,255,255}; CImgDisplay disp(visu.draw_text("Please wait, generating landscape....",10,10,white,0,19). normalize(0,255),"Virtual Landscape",0); const CImg<> map = 5*(CImg<>(700,700,1,1,300).noise(300).draw_plasma(0.2,300).normalize(-140,150).blur(5).cut(0,150)); CImg<> cmap(map.dimx(),map.dimy()); CImg_3x3(I,float); { cimg_for3x3(map,x,y,0,0,I) { const float nox=0.5f*(Inc-Ipc), noy=0.5f*(Icn-Icp); cmap(x,y)=cimg::max(0.0f,0.5f*nox+noy); }} cmap.normalize(0,255); for (float t=0; !disp.is_closed && disp.key!=cimg::keyESC && disp.key!=cimg::keyESC; t+=0.0025f) { visu = background; int xm = (int)(map.dimx()/2 + (map.dimx()/3)*cos(4.2*t)), ym = (int)(map.dimy()/2 + (map.dimy()/3)*sin(5.6*t)); const CImg<> smap = map.get_crop(xm,ym,xm+100,ym+90), scmap = cmap.get_crop(xm,ym,xm+100,ym+90); CImg<int> ymin(visu.dimx(),1,1,1,visu.dimy()), ymax(ymin.dimx(),1,1,1,0); cimg_forY(smap,z) { const int y0 = (int)(visu.dimy()-1-10*pow((double)z,0.63) + 80); cimg_forX(visu,x) { const int nz = smap.dimy()-z; float mx = x*(smap.dimx()-2.0f*nz*0.2f)/visu.dimx() + nz*0.2f; const int y = (int)(y0-smap.linear_pix1d(mx,z)/(1+0.02*z)); const float cc = (float)scmap.linear_pix1d(mx,z); if (y<visu.dimy() && y<ymin(x)) { const float cz = (smap.dimy()-(float)z)/smap.dimy(), czz = cz>0.25?1:4*cz; if (y!=y0) for (int l=y>0?y:0; l<ymin(x); l++) { visu(x,l,0) = (int)((1-czz)*visu(x,l,0)+4*cc*czz); visu(x,l,1) = (int)((1-czz)*visu(x,l,1)+3*cc*czz); visu(x,l,2) = (int)((1-czz)*visu(x,l,2)+ cc*czz); } else for (int l=y>0?y:0; l<ymin(x); l++) { int cl = l-visu.dimy()/2; visu(x,l,0) = 10; visu(x,l,1) = 200-cl; visu(x,l,2) = 255-cl; } } ymin(x)=cimg::min(ymin(x),y); ymax(x)=cimg::max(ymax(x),y); } } visu.draw_text(5,5,white,0,11,0.5f,"%u frames/s",(unsigned int)disp.frames_per_second()); disp.resize(false).display(visu.cut(0,255)).wait(25); if (disp.is_typed(cimg::keyCTRLLEFT,cimg::keyF)) disp.toggle_fullscreen(); } } break; case 12: { // Plasma effect with sinus scrolling. //------------------------------------ CImg<> plasma,camp(3),cfreq(3),namp(3),nfreq(3); CImgList<unsigned char> font = CImgList<unsigned char>::get_font(57); CImg<unsigned char> visu(400,300,1,3,0), letter, scroll(visu.dimx()+2*font['W'].dimx(),font['W'].dimy(),1,1,0); const char *text = " This is a nice plasma effect, isn't it ?"; CImgDisplay disp(visu,"Plasma Effect"); const unsigned char white[3]={ 255, 255, 255 }; unsigned int cplasma = 0, pos = 0, tpos = 0, lwidth = 0; float tx=0, ts=0, alpha=2.0f, beta=0; namp.fill(0).noise(visu.dimy()/4,0); nfreq.fill(0).noise(0.1); visu.draw_text("Please wait, the plasma is generating...",10,10,white).display(disp); const unsigned int nb_plasmas = 5; plasma = CImg<>(5*visu.dimx()/3,visu.dimy(),1,nb_plasmas,0); plasma.noise(100).draw_plasma(); cimg_forV(plasma,k) plasma.get_shared_channel(k).blur((float)(cimg::rand()*6)).normalize(0,255); while (!disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC) { if (alpha>1) { alpha-=1; cplasma=(cplasma+1)%plasma.dimv(); camp = namp; cfreq = nfreq; namp.fill(0).noise(100).normalize(0,visu.dimy()/4.0f); nfreq.fill(0).noise(0.2); } const unsigned int v0=cplasma, v1=(cplasma+1)%plasma.dimv(), v2=(cplasma+2)%plasma.dimv(), v3=(cplasma+3)%plasma.dimv(); const float umalpha = 1-alpha; unsigned char *pR = visu.ptr(0,0,0,0), *pG = visu.ptr(0,0,0,1), *pB = visu.ptr(0,0,0,2); cimg_forY(visu,y) { const float *pR1 = plasma.ptr((unsigned int)(camp(0)*(1+sin(tx+cfreq(0)*y))),y,v0), *pG1 = plasma.ptr((unsigned int)(camp(1)*(1+sin(tx+cfreq(1)*y))),y,v1), *pB1 = plasma.ptr((unsigned int)(camp(2)*(2+sin(tx+cfreq(2)*y))),y,v2), *pR2 = plasma.ptr((unsigned int)(namp(0)*(1+sin(tx+nfreq(0)*y))),y,v1), *pG2 = plasma.ptr((unsigned int)(namp(1)*(1+sin(tx+nfreq(1)*y))),y,v2), *pB2 = plasma.ptr((unsigned int)(namp(2)*(2+sin(tx+nfreq(2)*y))),y,v3); cimg_forX(visu,x) { *(pR++) = (unsigned char)(umalpha*(*(pR1++))+alpha*(*(pR2++))); *(pG++) = (unsigned char)(umalpha*(*(pG1++))+alpha*(*(pG2++))); *(pB++) = (unsigned char)(umalpha*(*(pB1++))+alpha*(*(pB2++))); } } if (!pos) { const CImg<unsigned char>& letter = font(text[tpos]); lwidth = (unsigned int)letter.dimx(); scroll.draw_image(letter,visu.dimx(),0); tpos++; tpos%=strlen(text); } scroll.translate(2); pos+=2; if (pos>lwidth+2) pos=0; cimg_forX(visu,x) { const int y0 = (int)(visu.dimy()/2+visu.dimy()/4*sin(ts+x/(70+30*cos(beta)))); cimg_forY(scroll,y) { if (scroll(x,y)) { const unsigned int y1 = y0+y+2; visu(x,y1,0)/=2; visu(x,y1,1)/=2; visu(x,y1,2)/=2; const unsigned int y2 = y1-6; visu(x,y2,0)=visu(x,y2,1)=visu(x,y2,2)=255; } } } alpha+=0.007f; beta+=0.04f; tx+=0.09f; ts+=0.04f; disp.resize(false).display(visu).wait(20); 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(visu,false); } } } break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -