PTdecode/CImg-1.3.0/examples/CImg_demo.cpp

Wed, 05 Aug 2009 17:10:56 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 05 Aug 2009 17:10:56 +0100
changeset 17
cf9d239ac1c9
parent 5
1204ebf9340d
permissions
-rwxr-xr-x

add README

     1 /*
     2  #
     3  #  File        : CImg_demo.cpp
     4  #                ( C++ source file )
     5  #
     6  #  Description : A multi-part demo demonstrating some of the CImg capabilities.
     7  #                This file is a part of the CImg Library project.
     8  #                ( http://cimg.sourceforge.net )
     9  #
    10  #  Copyright   : David Tschumperle
    11  #                ( http://www.greyc.ensicaen.fr/~dtschump/ )
    12  #
    13  #  License     : CeCILL v2.0
    14  #                ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
    15  #
    16  #  This software is governed by the CeCILL  license under French law and
    17  #  abiding by the rules of distribution of free software.  You can  use,
    18  #  modify and/ or redistribute the software under the terms of the CeCILL
    19  #  license as circulated by CEA, CNRS and INRIA at the following URL
    20  #  "http://www.cecill.info".
    21  #
    22  #  As a counterpart to the access to the source code and rights to copy,
    23  #  modify and redistribute granted by the license, users are provided only
    24  #  with a limited warranty  and the software's author,  the holder of the
    25  #  economic rights,  and the successive licensors  have only  limited
    26  #  liability.
    27  #
    28  #  In this respect, the user's attention is drawn to the risks associated
    29  #  with loading,  using,  modifying and/or developing or reproducing the
    30  #  software by the user in light of its specific status of free software,
    31  #  that may mean  that it is complicated to manipulate,  and  that  also
    32  #  therefore means  that it is reserved for developers  and  experienced
    33  #  professionals having in-depth computer knowledge. Users are therefore
    34  #  encouraged to load and test the software's suitability as regards their
    35  #  requirements in conditions enabling the security of their systems and/or
    36  #  data to be ensured and,  more generally, to use and operate it in the
    37  #  same conditions as regards security.
    38  #
    39  #  The fact that you are presently reading this means that you have had
    40  #  knowledge of the CeCILL license and that you accept its terms.
    41  #
    42 */
    44 // Include static image data, so that the exe does not depend on external image files.
    45 #include "img/CImg_demo.h"
    47 // Include CImg library header.
    48 #include "CImg.h"
    49 using namespace cimg_library;
    51 // The lines below are necessary when using a non-standard compiler such as visualcpp6.
    52 #ifdef cimg_use_visualcpp6
    53 #define std
    54 #endif
    55 #ifdef min
    56 #undef min
    57 #undef max
    58 #endif
    60 // Item : Blurring Gradient
    61 //----------------------------
    62 void* item_blurring_gradient() {
    64   // Create color image 'milla'.
    65   const CImg<> src(data_milla,211,242,1,3);
    67   // Compute 2D gradient (spatial derivatives).
    68   CImgList<> grad = src.get_gradient();
    70   // Create visualization list with three images, the second one being
    71   // a normalized version of the gradient norm.
    72   CImgList<unsigned char> visu = src<<(grad[0].pow(2) + grad[1].pow(2)).sqrt().normalize(0,255)<<src;
    74   // Create and Pop-up display window.
    75   CImgDisplay disp(visu,"[#1] - Color Image, Gradient Norm and Blurring Gradient",0);
    77   // Start animation loop (until ESC or Q key is pressed, or display window closed).
    78   for (double sigma = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; sigma+=0.05) {
    80     // Compute blurred version of the gradient norm.
    81     visu[2] = visu[1].get_blur((float)cimg::abs(30*std::cos(sigma))).normalize(0,255);
    83     // Refresh display window.
    84     disp.resize(false).display(visu).wait(20);
    85   }
    86   return 0;
    87 }
    89 // Item : Rotozoom
    90 //-----------------
    91 void* item_rotozoom() {
    93   // Create color image 'milla' and resize it to 400x300.
    94   CImg<unsigned char> src = CImg<unsigned char>(data_milla,211,242,1,3,false).resize(400,300,1,3,3), img(src);
    96   // Create display window.
    97   CImgDisplay disp(img.dimx(),img.dimy(),"[#2] - Rotozoom",0);
    99   float alpha = 0, t = 0, angle = 0, zoom0 = -0.9f;
   100   const unsigned char color[] = { 16,32,64 };
   102   // Start animation loop.
   103   while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
   105     // Add weird color effect on the image.
   106     cimg_forYV(src,y,k) {
   107       const int xc = 4*src.dimx() + (int)(60*std::sin((float)y*3/src.dimy()+10*t));
   108       cimg_forX(src,x) {
   109         const float val = (float)(src((xc+x)%src.dimx(),y,0,k)*
   110                                   (1.3f+0.20*std::sin(alpha+k*k*((float)src.dimx()/2-x)*
   111                                                       ((float)src.dimy()/2-y)*std::cos(t)/300.0)));
   112         img(x,y,0,k) = (unsigned char)(val>255.0f?255:val);
   113       }
   114     }
   116     // Rotate/zoom the resulting image, and display it.
   117     const float zoom = (float)(zoom0 + 0.3f*(1+std::cos(3*t)));
   118     img.get_rotate(angle,0.5f*img.dimx(),0.5f*img.dimy(),1+zoom,2,0).
   119       draw_text(3,3,"Mouse buttons\nto zoom in/out",color,0,0.8f,24).display(disp.resize(false).wait(20));
   121     // Smoothly move angle and zoom parameters
   122     alpha+=0.7f; t+=0.01f; angle+=0.8f;
   123     zoom0+=disp.button&1?0.1f:(disp.button&2?-0.1f:0);
   124     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(400,400,false).toggle_fullscreen(false);
   125   }
   126   return 0;
   127 }
   129 // Item : Anisotropic Smoothing (Total variation PDE, explicit scheme)
   130 //--------------------------------------------------------------------
   131 void* item_anisotropic_smoothing() {
   133   // Create color image 'milla' and noise it quite heavily with uniform noise.
   134   const CImg<> src = CImg<>(data_milla,211,242,1,3).noise(-30,1);
   136   // Create visualization list and corresponding display window.
   137   CImgList<> images(src,src);
   138   CImgDisplay disp(images,"[#3] - Anisotropic smoothing");
   139   const float white[] = { 255,255,255 }, black[] = { 0,0,0 };
   141   // Start PDE iterations
   142   for (unsigned int iter = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; ++iter) {
   144     // Compute PDE velocity field.
   145     CImg_3x3(I,float);
   146     CImg<> veloc(src);
   147     cimg_forV(src,k) cimg_for3x3(images[1],x,y,0,k,I) {
   148       const float
   149         ix = (Inc - Ipc)/2,
   150         iy = (Icn - Icp)/2,
   151         ng = (float)std::sqrt(1e-10f + ix*ix + iy*iy),
   152         ixx = Inc + Ipc - 2*Icc,
   153         iyy = Icn + Icp - 2*Icc,
   154         ixy = 0.25f*(Inn + Ipp - Ipn - Inp),
   155         iee = (ix*ix*iyy + iy*iy*ixx - 2*ix*iy*ixy)/(ng*ng);
   156       veloc(x,y,k) = iee/(0.1f+ng);
   157     }
   159     // Find adaptive time step and update current image.
   160     float m = 0, M = veloc.maxmin(m);
   161     veloc*=40.0f/cimg::max(cimg::abs(m),cimg::abs(M));
   162     images[1]+=veloc;
   163     images[0].draw_text(0,0,"iter %u",white,black,1,11,iter);
   165     // Refresh display window (and resize it if necessary).
   166     disp.resize(false).display(images);
   167   }
   168   return 0;
   169 }
   171 // Item : Fractal Animation
   172 //--------------------------
   173 void* item_fractal_animation() {
   175   // Create black 400x400 color image, and small 'noise' sprite.
   176   CImg<unsigned char> img(400,400,1,3,0), noise(3,2,1,3);
   178   // Create display window.
   179   CImgDisplay disp(img,"[#4] - Fractal Animation");
   181   // Start animation loop.
   182   float zoom = 0;
   183   for (unsigned int iter = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; ++iter, zoom+=0.2f) {
   185     // Put a noisy sprite on the center, then rotate and zoom the whole image, to make the 'fractal' effect.
   186     img.draw_image((img.dimx() - noise.dimx())/2,
   187                    (img.dimy() - noise.dimy())/2,
   188                    noise.fill(0).noise(255,1)).
   189       rotate((float)(10*std::sin(iter/25.0)),0.5f*img.dimx(),0.5f*img.dimy(),(float)(1.04+0.02*std::sin(zoom/10)),0,0).
   190       resize(disp.resize(false)).display(disp.wait(25));
   191     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(400,400,false).toggle_fullscreen(false);
   192   }
   193   return 0;
   194 }
   196 // Item : Gamma Correction and Histogram Visualization
   197 //-----------------------------------------------------
   198 void* item_gamma_correction() {
   200   // Create color image 'milla' and normalize it in [0,1].
   201   CImg<> img = CImg<>(data_milla,211,242,1,3).normalize(0,1);
   203   // Create visualization list (2 images).
   204   CImgList<unsigned char> visu(img*255.0, CImg<unsigned char>(512,300,1,3,0));
   206   // Define graph and text colors.
   207   const unsigned char
   208     yellow[] = { 255,255,0 }, blue[] = { 0,155,255 }, blue2[] = { 0,0,255 },
   209     blue3[] = { 0,0,155 }, white[] = { 255,255,255 }, green[] = { 100,255,100 };
   211   // Create display window.
   212   CImgDisplay disp(visu,"[#5] - Gamma Corrected Image and Histogram (Click to set Gamma)");
   214   // Enter event loop.
   215   for (double gamma = 1; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; ) {
   217     // Compute gamma-corrected version of the original image.
   218     cimg_forXYZV(visu[0],x,y,z,k) visu[0](x,y,z,k) = (unsigned char)(std::pow((double)img(x,y,z,k),1.0/gamma)*256);
   220     // Compute corresponding image histogram.
   221     const CImg<> hist = visu[0].get_histogram(50,0,255);
   223     // Draw image histogram as a bar graph in the visualization list (2nd image).
   224     visu[1].fill(0).draw_text(50,5,"Gamma = %.3g",white,0,1,24,gamma).
   225       draw_graph(hist,green,1,3,0,20000,0).draw_graph(hist,yellow,1,2,0,20000,0).
   226       draw_axis(0,256,20000,0,white,0.7f);
   227     const int xb = (int)(50+gamma*150);
   228     visu[1].draw_grid(20,20,0,0,false,false,white,0.3f,0xCCCCCCCC,0xCCCCCCCC).
   229       draw_rectangle(51,31,xb-1,39,blue2).draw_rectangle(50,30,xb,30,blue).draw_rectangle(xb,30,xb,40,blue).
   230       draw_rectangle(xb,40,50,39,blue3).draw_rectangle(50,30,51,40,blue3);
   232     // Check for button press from the user's mouse.
   233     if (disp.button && disp.mouse_x>=img.dimx()+50 && disp.mouse_x<=img.dimx()+450)
   234       gamma = (disp.mouse_x - img.dimx()-50)/150.0;
   236     // Refresh display window, and wait for a user event.
   237     disp.resize(disp,false).display(visu).wait();
   238   }
   239   return 0;
   240 }
   242 // Item : Filled Triangles
   243 //-------------------------
   244 void* item_filled_triangles() {
   246   // Create a colored 640x480 background image which consists of different color shades.
   247   CImg<> background(640,480,1,3);
   248   cimg_forXY(background,x,y) background.fillV(x,y,0,
   249                                               x*std::cos(6.0*y/background.dimy())+y*std::sin(9.0*x/background.dimx()),
   250                                               x*std::sin(8.0*y/background.dimy())-y*std::cos(11.0*x/background.dimx()),
   251                                               x*std::cos(13.0*y/background.dimy())-y*std::sin(8.0*x/background.dimx()));
   252   background.normalize(0,180);
   254   // Init images and create display window.
   255   CImg<unsigned char> img0(background), img;
   256   unsigned char white[] = { 255,255,255 }, color[100][3];
   257   CImgDisplay disp(img0,"[#6] - Filled Triangles (Click to shrink)");
   259   // Define random properties (pos, size, colors, ..) for all triangles that will be displayed.
   260   float posx[100], posy[100], rayon[100], angle[100], veloc[100], opacity[100];
   261   int num = 1;
   262   std::srand((unsigned int)time(NULL));
   263   for (int k = 0; k<100; ++k) {
   264     posx[k]  = (float)(cimg::rand()*img0.dimx());
   265     posy[k]  = (float)(cimg::rand()*img0.dimy());
   266     rayon[k] = (float)(10+cimg::rand()*50);
   267     angle[k] = (float)(cimg::rand()*360);
   268     veloc[k] = (float)(cimg::rand()*20-10);
   269     color[k][0] = (unsigned char)(cimg::rand()*255);
   270     color[k][1] = (unsigned char)(cimg::rand()*255);
   271     color[k][2] = (unsigned char)(cimg::rand()*255);
   272     opacity[k] = (float)(0.3+1.5*cimg::rand());
   273   }
   275   // Start animation loop.
   276   while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
   277     img = img0;
   279     // Draw each triangle on the background image.
   280     for (int k = 0; k<num; ++k) {
   281       const int
   282         x0 = (int)(posx[k] + rayon[k]*std::cos(angle[k]*cimg::valuePI/180)),
   283         y0 = (int)(posy[k] + rayon[k]*std::sin(angle[k]*cimg::valuePI/180)),
   284         x1 = (int)(posx[k] + rayon[k]*std::cos((angle[k]+120)*cimg::valuePI/180)),
   285         y1 = (int)(posy[k] + rayon[k]*std::sin((angle[k]+120)*cimg::valuePI/180)),
   286         x2 = (int)(posx[k] + rayon[k]*std::cos((angle[k]+240)*cimg::valuePI/180)),
   287         y2 = (int)(posy[k] + rayon[k]*std::sin((angle[k]+240)*cimg::valuePI/180));
   288       if (k%10) img.draw_triangle(x0,y0,x1,y1,x2,y2,color[k],opacity[k]);
   289       else img.draw_triangle(x0,y0,x1,y1,x2,y2,img0,0,0,img0.dimx()-1,0,0,img.dimy()-1,opacity[k]);
   290       img.draw_triangle(x0,y0,x1,y1,x2,y2,white,opacity[k],~0U);
   292       // Make the triangles rotate, and check for mouse click event.
   293       // (to make triangles collapse or join).
   294       angle[k]+=veloc[k];
   295       if (disp.mouse_x>0 && disp.mouse_y>0) {
   296         float u = disp.mouse_x - posx[k], v = disp.mouse_y - posy[k];
   297         if (disp.button) { u=-u; v=-v; }
   298         posx[k]-=0.03f*u, posy[k]-=0.03f*v;
   299         if (posx[k]<0 || posx[k]>=img.dimx()) posx[k]=(float)(cimg::rand()*img.dimx());
   300         if (posy[k]<0 || posy[k]>=img.dimy()) posy[k]=(float)(cimg::rand()*img.dimy());
   301       }
   302     }
   304     // Display current animation framerate, and refresh display window.
   305     img.draw_text(5,5,"%u frames/s",white,0,0.5f,11,(unsigned int)disp.frames_per_second());
   306     img0.resize(disp.display(img).resize(false).wait(20));
   307     if (++num>100) num = 100;
   309     // Allow the user to toggle fullscreen mode, by pressing CTRL+F.
   310     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(640,480,false).toggle_fullscreen(false);
   311   }
   312   return 0;
   313 }
   315 // Item : Mandelbrot/Julia Explorer
   316 //----------------------------------
   317 void* item_mandelbrot_explorer() {
   319   // Define image canvas and corresponding display window.
   320   CImg<unsigned char> img(800,600,1,3,0);
   321   CImgDisplay disp(img);
   323   // Start main explorer loop.
   324   double juliar = 0, juliai = 0;
   325   for (bool endflag = false, fractal_type = false, smooth = false, show_help = true; !endflag;) {
   326     bool stopflag = false;
   327     double xmin, xmax, ymin, ymax;
   329     // Init default upper-left/lower-right coordinates of the fractal set.
   330     if (fractal_type) { xmin = -1.5; xmax = 1.5; ymin = -1.5; ymax = 1.5; juliar = 0.317; juliai = 0.029; }
   331     else { xmin = -2.25; xmax = 1.0; ymin = -1.5; ymax = 1.5; juliar = juliai = 0; }
   333     // Create random palette for displaying the fractal set.
   334     const CImg<unsigned char> palette =
   335       CImg<unsigned char>(256,1,1,3,16+120).noise(119,1).resize(1024,1,1,3,3).fillV(0,0,0,0,0,0);
   336     unsigned int maxiter = 64;
   338     // Enter event loop for the current fractal set.
   339     while (!stopflag) {
   341       // Draw Mandelbrot or Julia fractal set on the image.
   342       img.resize(disp.resize().set_title("[#7] - %s Set : (%g,%g)-(%g,%g), %s = (%g,%g) (%u iter.)",
   343                                          fractal_type?"Julia":"Mandelbrot",xmin,ymin,xmax,ymax,
   344                                          fractal_type?"c":"z0",juliar,juliai,maxiter)).
   345         fill(0).draw_mandelbrot(palette,1,xmin,ymin,xmax,ymax,maxiter,smooth,fractal_type,juliar,juliai);
   347       // Display help if necessary.
   348       if (show_help) {
   349         const unsigned char white[] = { 255,255,255 };
   350         static CImg<unsigned char>
   351           help = CImg<unsigned char>().draw_text(0,0,"\n"
   352                                                  " Use mouse to zoom on desired region. \n"
   353                                                  " H             Show/Hide help \n"
   354                                                  " PAD 1...9       Fractal navigation \n"
   355                                                  " PAD +/-       Zoom/Unzoom \n"
   356                                                  " SPACE         Set/Disable color smoothing \n"
   357                                                  " ENTER         Switch Mandelbrot/Julia sets \n"
   358                                                  " Arrows        Change set parameterization \n"
   359                                                  " Page UP/DOWN  Add/Reduce iteration numbers \n\n",
   360                                                  white);
   361         help.draw_rectangle(2,2,help.dimx()-3,help.dimy()-3,white,1,~0U);
   362         img.draw_image(img.dimx()-help.dimx(),help,0.7f);
   363       }
   365       // Get rectangular shape from the user to define the zoomed region.
   366       const CImg<int> selection = img.get_select(disp,2,0);
   367       const int xs0 = selection[0], ys0 = selection[1], xs1 = selection[3], ys1 = selection[4];
   369       // If the user has selected a region with the mouse, then zoom-in !
   370       if (xs0>=0 && ys0>=0 && xs1>=0 && ys1>=0) {
   371         const double dx =(xmax-xmin)/img.dimx(), dy =(ymax-ymin)/img.dimy();
   372         const int dsmax = (ys1-ys0)/2, xs = (xs0+xs1)/2, ys = (ys0+ys1)/2;
   374         // If the region is too small (point) then reset the fractal set position and zoom.
   375         if (dsmax<5) stopflag = true;
   376         xmin += (xs-dsmax*dy/dx)*dx;
   377         ymin += (ys-dsmax)*dy;
   378         xmax -= (img.dimx()-xs-dsmax*dy/dx)*dx;
   379         ymax -= (img.dimy()-ys-dsmax)*dy;
   380       }
   382       // Also, test if a key has been pressed.
   383       // (moving in the fractal set can be done, using keyboard).
   384       switch (disp.key) {
   386         // Show/hide help.
   387       case cimg::keyH: show_help = !show_help; break;
   389         // Switch between Julia/Mandelbrot sets.
   390       case cimg::keyENTER: fractal_type = !fractal_type; stopflag = true; break;
   392         // Enable/disable smoothed colors.
   393       case cimg::keySPACE: smooth = !smooth; break;
   395         // Change fractal set parameters.
   396       case cimg::keyARROWLEFT: juliar-=fractal_type?0.001f:0.05f; break;
   397       case cimg::keyARROWRIGHT: juliar+=fractal_type?0.001f:0.05f; break;
   398       case cimg::keyARROWUP: juliai+=fractal_type?0.001f:0.05f; break;
   399       case cimg::keyARROWDOWN: juliai-=fractal_type?0.001f:0.05f; break;
   401         // Add/remove iterations.
   402       case cimg::keyPAGEDOWN: maxiter-=32; break;
   403       case cimg::keyPAGEUP: maxiter+=16; break;
   405         // Move left, right, up and down in the fractal set.
   406       case cimg::keyPAD4: { const double delta = (xmax-xmin)/10; xmin-=delta; xmax-=delta; } break;
   407       case cimg::keyPAD6: { const double delta = (xmax-xmin)/10; xmin+=delta; xmax+=delta; } break;
   408       case cimg::keyPAD8: { const double delta = (ymax-ymin)/10; ymin-=delta; ymax-=delta; } break;
   409       case cimg::keyPAD2: { const double delta = (ymax-ymin)/10; ymin+=delta; ymax+=delta; } break;
   411         // Allow to zoom in/out in the fractal set.
   412       case cimg::keyPADADD: {
   413         const double xc = 0.5*(xmin+xmax), yc = 0.5*(ymin+ymax), dx = (xmax-xmin)*0.85/2, dy = (ymax-ymin)*0.85/2;
   414         xmin = xc - dx; ymin = yc - dy; xmax = xc + dx; ymax = yc + dy;
   415       } break;
   416       case cimg::keyPADSUB:
   417         const double xc = 0.5*(xmin+xmax), yc = 0.5*(ymin+ymax), dx = (xmax-xmin)*1.15/2, dy = (ymax-ymin)*1.15/2;
   418         xmin = xc - dx; ymin = yc - dy; xmax = xc + dx; ymax = yc + dy;
   419         break;
   420       }
   422       // Do a simple test to check if more/less iterations are necessary for the next step.
   423       const float value = img.get_pointwise_norm().get_histogram(256,0,255)(0)*3;
   424       if (value>img.size()/6.0f) maxiter+=16;
   425       if (maxiter>1024) maxiter = 1024;
   426       if (value<img.size()/10.0f) maxiter-=4;
   427       if (maxiter<32) maxiter = 32;
   429       // Check if the user want to quit the explorer.
   430       if (disp.is_closed || disp.key==cimg::keyQ || disp.key==cimg::keyESC) stopflag = endflag = true;
   431     }
   432   }
   433   return 0;
   434 }
   436 // Item : Mini-Paint
   437 //------------------
   438 void* item_mini_paint() {
   439   int xo = -1, yo = -1, x = -1, y = -1;
   440   bool redraw = true;
   441   CImg<unsigned char> img(256,256+64,1,3,0);
   442   unsigned char color[] = { 255,255,255 };
   443   cimg_for_inY(img,256,img.dimy()-1,yy) cimg_forX(img,xx) img.fillV(xx,yy,0,xx,(yy-256)*4,(3*xx)%256);
   444   CImgDisplay disp(img.draw_text(5,5,"   ",color,color),"[#8] - Mini-Paint");
   445   while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
   446     const unsigned int but = disp.button;
   447     redraw = false;
   448     xo = x; yo = y; x = disp.mouse_x; y = disp.mouse_y;
   449     if (xo>=0 && yo>=0 && x>=0 && y>=0) {
   450       if (but&1 || but&4) {
   451         if (y<253) {
   452           const float tmax = (float)cimg::max(cimg::abs(xo-x),cimg::abs(yo-y)) + 0.1f;
   453           const int radius = (but&1?3:0) + (but&4?6:0);
   454           for (float t=0; t<=tmax; ++t) img.draw_circle((int)(x+t*(xo-x)/tmax),(int)(y+t*(yo-y)/tmax),radius,color);
   455         }
   456         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); }
   457         redraw = true;
   458       }
   459       if (y>=253) y = 252;
   460       if (disp.button&2) { img.draw_fill(x,y,color); redraw = true; }
   461     }
   462     if (redraw) disp.display(img);
   463     disp.resize(disp).wait();
   464     if (disp.key) cimg_forV(img,k) { img.get_shared_lines(0,255,0,k).fill(0); img.display(disp); }
   465   }
   466   return 0;
   467 }
   469 // Item : Soccer Bobs
   470 //-------------------
   471 void* item_soccer_bobs() {
   472   CImg<unsigned char> foot(data_foot,200,200,1,3,false), canvas0(640,480,1,3,0);
   473   const unsigned char color[] = { 255,255,0 };
   474   float zoom = 0.2f;
   475   cimg_forXY(canvas0,x,y) canvas0(x,y,1) = (unsigned char)(20+(y*215/canvas0.dimy()) + cimg::crand()*19);
   476   canvas0.draw_text(5,5,"Left/Right Mouse Button = Zoom In/Out\nMiddle Button = Reset Screen",color);
   477   CImgList<unsigned char> canvas(16, canvas0);
   478   CImg<float> mask(foot.dimx(),foot.dimy());
   479   { cimg_forXY(mask,x,y) mask(x,y) = (foot(x,y,0)==255 && !foot(x,y,1) && !foot(x,y,2))?0:0.8f; }
   480   CImgDisplay disp(canvas0,"[#9] - Unlimited Soccer Bobs");
   481   for (unsigned int curr_canvas=0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; (++curr_canvas)%=16) {
   482     if (disp.mouse_x>=0 && disp.mouse_y>=0)
   483       canvas[curr_canvas].draw_image((int)(disp.mouse_x - zoom*foot.dimx()/2),
   484                                      (int)(disp.mouse_y - zoom*foot.dimy()/2),
   485                                      foot.get_resize((int)(foot.dimx()*zoom),(int)(foot.dimy()*zoom)),
   486                                      mask.get_resize((int)(foot.dimx()*zoom),(int)(foot.dimy()*zoom)));
   487     zoom+=disp.button&1?0.03f:(disp.button&2?-0.03f:0);
   488     zoom = zoom<0.1f?0.1f:(zoom>1?1.0f:zoom);
   489     if (disp.button&4) cimglist_for(canvas,l) canvas[l] = canvas0;
   490     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.toggle_fullscreen(false);
   491     disp.display(canvas[curr_canvas]).resize(disp,false).wait(20);
   492   }
   493   return 0;
   494 }
   496 // Item : Bump Effect
   497 //--------------------
   498 void* item_bump() {
   499   CImg<> logo = CImg<>(56,32,1,1,0).draw_text(9,5,"I Love\n CImg!",CImg<>::vector(255).ptr()).resize(-800,-800,1,1,3).blur(6).normalize(0,255);
   500   logo += CImg<>(logo.dimx(),logo.dimy(),1,1,0).noise(80,1).deriche(2,0,'y',false).deriche(10,0,'x',false);
   501   CImgList<> grad = logo.get_gradient();
   502   cimglist_apply(grad,normalize)(-140,140);
   503   logo.normalize(0,255);
   504   CImg<> light = CImg<>(300+2*logo.dimx(),300+2*logo.dimy());
   505   light.draw_gaussian(0.5f*light.dimx(),0.5f*light.dimy(),80,CImg<>::vector(255));
   506   CImg<unsigned char> img(logo.dimx(),logo.dimy(),1,3,0);
   507   CImgDisplay disp(img,"[#10] - Bump Effect (Move lightsource with mouse)");
   508   for (float t = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; t+=0.03f) {
   509     const int
   510       mouse_x = (disp.mouse_x>=0 && disp.button)?disp.mouse_x*img.dimx()/disp.dimx():(int)(img.dimx()/2 + img.dimx()*std::cos(1*t)/2),
   511       mouse_y = (disp.mouse_y>=0 && disp.button)?disp.mouse_y*img.dimy()/disp.dimy():(int)(img.dimy()/2 + img.dimy()*std::sin(3*t)/2);
   512     cimg_forXY(img,x,y) {
   513       const int gx = (int)grad[0](x,y), gy = (int)grad[1](x,y);
   514       const float val = 40+(gx+gy)/2+light(light.dimx()/2+mouse_x-x+gx,light.dimy()/2+mouse_y-y+gy);
   515       img(x,y,0) = img(x,y,1) = img(x,y,2) = (unsigned char)(val>255?255:(val<0?0:val));
   516     }
   517     disp.resize(false).display(img.draw_image(0,0,0,1,logo,0.1f)).wait(25);
   518     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(640,480,false).toggle_fullscreen(false);
   519   }
   520   return 0;
   521 }
   523 // Item : Bouncing Bubble
   524 //------------------------
   525 void* item_bouncing_bubble() {
   526   CImg<unsigned char> back(420,256,1,3,0), img;
   527   cimg_forXY(back,x,y) back(x,y,2) = (unsigned char)((y<2*back.dimy()/3)?30:(255-2*(y+back.dimy()/2)));
   528   CImgDisplay disp(back,"[#11] - Bouncing bubble");
   529   const unsigned char col1[] = { 40,100,10 }, col2[] = { 20,70,0 }, col3[] = { 40,150,10 },
   530                       col4[] = { 200,255,100 }, white[] = { 255,255,255 };
   531   float u = (float)std::sqrt(2.0f),  cx = back.dimx()/2.0f, t = 0, vt = 0.05f, vx = 2;
   532   while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
   533     img = back;
   534     int xm = (int)cx, ym = (int)(img.dimy()/2-70 + (img.dimy()/2+10)* (1-cimg::abs(std::cos((t+=vt)))));
   535     float r1 = 50, r2 = 50;
   536     vt = 0.05f;
   537     if (xm+r1>=img.dimx())    { const float delta = (xm+r1)-img.dimx(); r1-=delta; r2+=delta; }
   538     if (xm-r1<0)              { const float delta = -(xm-r1); r1-=delta; r2+=delta; }
   539     if (ym+r2>=img.dimy()-40) { const float delta = (ym+r2)-img.dimy()+40; r2-=delta; r1+=delta; vt=0.05f - 0.0015f*(50-r2); }
   540     if (ym-r2<0)              { const float delta = -(ym-r2); r2-=delta; r1+=delta; }
   541     img.draw_ellipse(xm,ym,r1,r2,1,0,col1).
   542       draw_ellipse((int)(xm+0.03*r1*u),(int)(ym-0.03*r2*u),0.85f*r1,0.85f*r2,1,0,col2).
   543       draw_ellipse((int)(xm+0.1*r1*u),(int)(ym-0.1*r2*u),0.8f*r1,0.8f*r2,1,0,col1).
   544       draw_ellipse((int)(xm+0.2*r1*u),(int)(ym-0.2*r2*u),r1/2,r2/2,1,0,col3).
   545       draw_ellipse((int)(xm+0.3*r1*u),(int)(ym-0.3*r2*u),r1/4,r2/4,1,0,col4).
   546       draw_image(0,img.dimy()-40,img.get_crop(0,img.dimy()-80,img.dimx()-1,img.dimy()-40).mirror('y'),0.45f).
   547       draw_text(xm-60,(int)(ym-r2-25),"Bubble (%d,%d)",white,0,1,17,xm,ym);
   548     if ((cx+=20*vt*vx)>=img.dimx()-30 || cx<30) vx = -vx;
   549     disp.display(img).wait(20);
   550     if (disp.is_resized) {
   551       back.resize(disp.resize(disp.window_dimx()>200?disp.window_dimx():200,disp.dimy(),false));
   552       cx = back.dimx()/2.0f;
   553     }
   554   }
   555   return 0;
   556 }
   558 // Item : Virtual Landscape
   559 //--------------------------
   560 void* item_virtual_landscape() {
   561   CImg<int> background(400,300,1,3,0), visu(background);
   562   cimg_forXY(background,x,y) {
   563     if (y>background.dimy()/2) { background(x,y,2) = 255; background(x,y,0) = (y-background.dimy()/2)*512/background.dimy(); }
   564     else background(x,y,2) = y*512/background.dimy();
   565   }
   566   const int white[] = { 255,255,255 };
   567   CImgDisplay disp(visu.draw_text(10,10,"Please wait, generating landscape...",white).
   568                    normalize(0,255),"[#12] - Virtual Landscape",0);
   569   CImg<> map = 5.0*(CImg<>(700,700,1,1,300).noise(300).draw_plasma(0.2f,300).normalize(-140,150).blur(5).cut(0,150)), cmap(map.dimx(),map.dimy());
   570   CImg_3x3(I,float); Ipp = Inp = Icc = Ipn = Inn = 0;
   571   { 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); }}
   572   cmap.normalize(0,255);
   574   for (float t = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; t+=0.0025f) {
   575     visu = background;
   576     const int
   577       xm = (int)(map.dimx()/2 + (map.dimx()/3)*std::cos(4.2f*t)),
   578       ym = (int)(map.dimy()/2 + (map.dimy()/3)*std::sin(5.6f*t));
   579     const CImg<>
   580       smap = map.get_crop(xm,ym,xm+100,ym+90),
   581       scmap = cmap.get_crop(xm,ym,xm+100,ym+90);
   582     CImg<int> ymin(visu.dimx(),1,1,1,visu.dimy()), ymax(ymin.dimx(),1,1,1,0);
   583     cimg_forY(smap,z) {
   584       const int y0 = (int)(visu.dimy()-1-10*std::pow((double)z,0.63) + 80);
   585       cimg_forX(visu,x) {
   586         const int nz = smap.dimy()-z;
   587         float mx = x*(smap.dimx()-2.0f*nz*0.2f)/visu.dimx() + nz*0.2f;
   588         const int y = (int)(y0-smap.linear_atX(mx,z)/(1+0.02*z));
   589         const float cc = (float)scmap.linear_atX(mx,z);
   590         if (y<visu.dimy() && y<ymin(x)) {
   591           const float cz = (smap.dimy()-(float)z)/smap.dimy(), czz = cz>0.25?1:4*cz;
   592           if (y!=y0) for (int l=y>0?y:0; l<ymin(x); ++l) {
   593             visu(x,l,0) = (int)((1-czz)*visu(x,l,0)+4*cc*czz);
   594             visu(x,l,1) = (int)((1-czz)*visu(x,l,1)+3*cc*czz);
   595             visu(x,l,2) = (int)((1-czz)*visu(x,l,2)+  cc*czz);
   596           } else for (int l=y>0?y:0; l<ymin(x); ++l) { int cl = l-visu.dimy()/2;
   597           visu(x,l,0) = 10; visu(x,l,1) = 200-cl; visu(x,l,2) = 255-cl;
   598           }
   599         }
   600         ymin(x) = cimg::min(ymin(x),y); ymax(x) = cimg::max(ymax(x),y);
   601       }
   602     }
   603     visu.draw_text(5,5,"%u frames/s",white,0,0.5f,11,(unsigned int)disp.frames_per_second());
   604     disp.resize(false).display(visu.cut(0,255)).wait(25);
   605     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(400,300,false).toggle_fullscreen(false);
   606   }
   607   return 0;
   608 }
   610 // Item : Plasma Effect with Sinus Scrolling.
   611 //-------------------------------------------
   612 void* item_plasma() {
   613   CImg<> plasma, camp(3), cfreq(3), namp(3), nfreq(3);
   614   CImgList<unsigned char> font = CImgList<unsigned char>::font(57);
   615   CImg<unsigned char> visu(400,300,1,3,0), letter, scroll(visu.dimx()+2*font['W'].dimx(),font['W'].dimy(),1,1,0);
   616   const char *text = "   * The CImg Library : C++ Template Image Processing Toolkit *";
   617   CImgDisplay disp(visu,"[#13] - Plasma Effect");
   618   const unsigned char white[] = { 255, 255, 255 };
   619   unsigned int cplasma = 0, pos = 0, tpos = 0, lwidth = 0;
   620   float tx = 0, ts = 0, alpha = 2, beta = 0;
   621   namp.fill(0).noise(visu.dimy()/4,0);
   622   nfreq.fill(0).noise(0.1);
   624   visu.draw_text(10,10,"Please wait, generating plasma...",white).display(disp);
   625   const unsigned int nb_plasmas = 5;
   626   plasma.assign(5*visu.dimx()/3,visu.dimy(),1,nb_plasmas,0).noise(100).draw_plasma();
   627   cimg_forV(plasma,k) plasma.get_shared_channel(k).blur((float)(cimg::rand()*6)).normalize(0,255);
   629   while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
   630     if (alpha>1) {
   631       alpha-=1;
   632       cplasma = (cplasma+1)%plasma.dimv();
   633       camp = namp;
   634       cfreq = nfreq;
   635       namp.fill(0).noise(100).normalize(0,visu.dimy()/4.0f);
   636       nfreq.fill(0).noise(0.2);
   637     }
   638     const unsigned int
   639       v0 = cplasma, v1 = (cplasma+1)%plasma.dimv(),
   640       v2 = (cplasma+2)%plasma.dimv(), v3 = (cplasma+3)%plasma.dimv();
   641     const float umalpha = 1-alpha;
   642     unsigned char *pR = visu.ptr(0,0,0,0), *pG = visu.ptr(0,0,0,1), *pB = visu.ptr(0,0,0,2);
   643     cimg_forY(visu,y) {
   644       const float
   645         *pR1 = plasma.ptr((unsigned int)(camp(0)*(1+std::sin(tx+cfreq(0)*y))),y,v0),
   646         *pG1 = plasma.ptr((unsigned int)(camp(1)*(1+std::sin(tx+cfreq(1)*y))),y,v1),
   647         *pB1 = plasma.ptr((unsigned int)(camp(2)*(2+std::sin(tx+cfreq(2)*y))),y,v2),
   648         *pR2 = plasma.ptr((unsigned int)(namp(0)*(1+std::sin(tx+nfreq(0)*y))),y,v1),
   649         *pG2 = plasma.ptr((unsigned int)(namp(1)*(1+std::sin(tx+nfreq(1)*y))),y,v2),
   650         *pB2 = plasma.ptr((unsigned int)(namp(2)*(2+std::sin(tx+nfreq(2)*y))),y,v3);
   651       cimg_forX(visu,x) {
   652         *(pR++) = (unsigned char)(umalpha*(*(pR1++))+alpha*(*(pR2++)));
   653         *(pG++) = (unsigned char)(umalpha*(*(pG1++))+alpha*(*(pG2++)));
   654         *(pB++) = (unsigned char)(umalpha*(*(pB1++))+alpha*(*(pB2++)));
   655       }
   656     }
   657     if (!pos) {
   658       const CImg<unsigned char>& letter = font(text[tpos]);
   659       lwidth = (unsigned int)letter.dimx();
   660       scroll.draw_image(visu.dimx(),letter);
   661       (++tpos)%=strlen(text);
   662     }
   663     scroll.translate(2);
   664     pos+=2; if (pos>lwidth+2) pos = 0;
   665     cimg_forX(visu,x) {
   666       const int y0 = (int)(visu.dimy()/2+visu.dimy()/4*std::sin(ts+x/(70+30*std::cos(beta))));
   667       cimg_forY(scroll,y) {
   668         if (scroll(x,y)) {
   669           const unsigned int y1 = y0+y+2; visu(x,y1,0)/=2; visu(x,y1,1)/=2; visu(x,y1,2)/=2;
   670           const unsigned int y2 = y1-6;   visu(x,y2,0)=visu(x,y2,1)=visu(x,y2,2)=255;
   671         }
   672       }
   673     }
   674     alpha+=0.007f; beta+=0.04f; tx+=0.09f; ts+=0.04f;
   675     disp.resize(false).display(visu).wait(20);
   676     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(640,480,false).toggle_fullscreen(false);
   677   }
   678   return 0;
   679 }
   681 // Item : Oriented Convolutions
   682 //------------------------------
   683 void* item_oriented_convolutions() {
   684   const CImg<unsigned char> img = CImg<unsigned char>(data_lena,256,256,1,1,false).noise(50,2);
   685   CImgList<unsigned char> visu = img<<img<<img;
   686   CImg<float> mask(16,16);
   687   const float value = 255;
   688   CImgDisplay disp(visu,"[#14] - Original image, Oriented kernel and Convolved image");
   689   for (float angle = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; angle+=0.1f) {
   690     const float ca = (float)std::cos(angle), sa = (float)std::sin(angle);
   691     const CImg<> u = CImg<>::vector(ca,sa), v = CImg<>::vector(-sa,ca),
   692       tensor = 30.0*u*u.get_transpose() + 2.0*v*v.get_transpose();
   693     mask.draw_gaussian(0.5f*mask.dimx(),0.5f*mask.dimy(),tensor,&value);
   694     mask/=mask.sum();
   695     visu[1] = mask.get_resize(img).normalize(0,255).
   696       draw_text(2,2,"Angle = %d deg",&value,0,1,11,cimg::mod((int)(angle*180/cimg::valuePI),360));
   697     visu[2] = img.get_convolve(mask);
   698     disp.resize(disp.window_dimx(),(int)(disp.dimy()*disp.window_dimx()/disp.dimx()),false).
   699       display(visu).wait(25);
   700   }
   701   return 0;
   702 }
   704 // Item : Shade Bobs
   705 //-------------------
   706 void* item_shade_bobs() {
   707   float t = 100, rx = 0, ry = 0, rz = 0, rt = 0, rcx = 0;
   708   CImg<unsigned char> img(512,512,1,1,0), palette;
   709   CImgDisplay disp(img,"[#15] - Shade Bobs");
   710   const unsigned char one = 1;
   711   int nbbobs = 0, rybobs = 0;
   712   while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
   713     if ((t+=0.015f)>4*cimg::valuePI) {
   714       img.fill(0);
   715       rx = (float)(cimg::crand());
   716       ry = (float)(cimg::crand());
   717       rz = (float)(cimg::crand());
   718       rt = (float)(cimg::crand());
   719       rcx = 0.6f*(float)(cimg::crand());
   720       t = 0;
   721       palette = CImg<unsigned char>(3,4+(int)(12*cimg::rand()),1,1,0).noise(255,2).resize(3,256,1,1,3);
   722       palette(0) = palette(1) = palette(2) = 0;
   723       nbbobs = 20+(int)(cimg::rand()*80);
   724       rybobs = (10+(int)(cimg::rand()*50))*cimg::min(img.dimx(),img.dimy())/300;
   725       disp.key = disp.button = 0;
   726     }
   727     for (int i = 0; i<nbbobs; ++i) {
   728       const float
   729         r = (float)(ry + rx*std::cos(6*rz*t) + (1-rx)*std::sin(6*rt*t)),
   730         a = (float)((360*std::sin(rz*t)+30*ry*i)*cimg::valuePI/180),
   731         ax = (float)(i*2*cimg::valuePI/nbbobs+t);
   732       const int
   733         cx = (int)((1+rcx*std::cos(ax)+r*std::cos(a))*img.dimx()/2),
   734         cy = (int)((1+rcx*std::sin(ax)+r*std::sin(a))*img.dimy()/2);
   735       img.draw_circle(cx,cy,rybobs,&one,-1.0f);
   736     }
   737     CImg_3x3(I,unsigned char); Ipp = Inp = Ipn = Inn = 0;
   738     CImg<unsigned char> tmp(img);
   739     cimg_for3x3(tmp,x,y,0,0,I) img(x,y) = (Inc+Ipc+Icn+Icp+(Icc<<2))>>3;
   740     CImg<unsigned char> visu(img.dimx(),img.dimy(),1,3);
   741     cimg_forXY(visu,xx,yy) {
   742       const unsigned char *col = palette.ptr(0,img(xx,yy));
   743       visu(xx,yy,0) = *(col++);
   744       visu(xx,yy,1) = *(col++);
   745       visu(xx,yy,2) = *(col++);
   746     }
   747     disp.display(visu).wait(25);
   748     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(640,480,false).toggle_fullscreen(false);
   749     if (disp.is_resized) img.resize(disp.resize(false),3);
   750     if ((disp.key && disp.key!=cimg::keyCTRLLEFT) || disp.button) t=70;
   751   }
   752   return 0;
   753 }
   755 // Item : Fourier Filtering
   756 //-------------------------
   757 void* item_fourier_filtering() {
   758   const CImg<unsigned char> img = CImg<unsigned char>(data_lena,256,256,1,1,false).resize(256,256);
   759   CImgList<> F = img.get_FFT();
   760   cimglist_apply(F,translate)(img.dimx()/2,img.dimy()/2,0,0,2);
   761   const CImg<unsigned char> mag = ((F[0].get_pow(2) + F[1].get_pow(2)).sqrt()+1.0f).log().normalize(0,255);
   762   CImgList<unsigned char> visu(img,mag);
   763   CImgDisplay disp(visu,"[#16] - Fourier Filtering (Click to set filter)");
   764   CImg<unsigned char> mask(img.dimx(),img.dimy(),1,1,1);
   765   unsigned char one[] = { 1 }, zero[] = { 0 }, white[] = { 255 };
   766   int rmin = 0, rmax = 256;
   767   while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
   768     disp.wait();
   769     const int
   770       xm = disp.mouse_x*2*img.dimx()/disp.dimx()-img.dimx(),
   771       ym = disp.mouse_y*img.dimy()/disp.dimy(),
   772       x = xm-img.dimx()/2,
   773       y = ym-img.dimy()/2;
   774     if (disp.button && xm>=0 && ym>=0) {
   775       const int r = (int)cimg::max(0.0f,(float)std::sqrt((float)x*x+y*y)-3.0f);
   776       if (disp.button&1) rmax = r;
   777       if (disp.button&2) rmin = r;
   778       if (rmin>=rmax) rmin = cimg::max(rmax-1,0);
   779       mask.fill(0).draw_circle(mag.dimx()/2,mag.dimy()/2,rmax,one).
   780         draw_circle(mag.dimx()/2,mag.dimy()/2,rmin,zero);
   781       CImgList<> nF(F);
   782       cimglist_for(F,l) nF[l].mul(mask).translate(-img.dimx()/2,-img.dimy()/2,0,0,2);
   783       visu[0] = nF.FFT(true)[0].normalize(0,255);
   784     }
   785     if (disp.is_resized) disp.resize(disp.window_dimx(),disp.window_dimx()/2).display(visu);
   786     visu[1] = mag.get_mul(mask).draw_text(5,5,"Freq Min/Max = %d / %d",white,zero,0.6f,11,(int)rmin,(int)rmax);
   787     visu.display(disp);
   788   }
   789   return 0;
   790 }
   792 // Item : Image Zoomer
   793 //---------------------
   794 void* item_image_zoomer() {
   795   const CImg<unsigned char> img = CImg<unsigned char>(data_logo,555,103,1,3,false);
   796   CImgDisplay disp(img,"[#17] - Original Image"), dispz(500,500,"[#17] - Zoomed Image",0);
   797   disp.move((CImgDisplay::screen_dimx()-dispz.dimx())/2,(CImgDisplay::screen_dimy()-dispz.dimy()-disp.dimy())/2);
   798   dispz.move(disp.window_posx(),disp.window_posy() + disp.window_dimy() + 40);
   799   int factor = 20, x = 0, y = 0;
   800   bool grid = false, redraw = false;
   801   while (!disp.is_closed && !dispz.is_closed && !disp.is_keyQ && !dispz.is_keyQ && !disp.is_keyESC && !dispz.is_keyESC ) {
   802     if (disp.mouse_x>=0) { x = disp.mouse_x; y = disp.mouse_y; redraw = true; }
   803     if (redraw) {
   804       const int
   805         x0 = x-factor, y0 = y-factor,
   806         x1 = x+factor, y1 = y+factor;
   807       const unsigned char red[] = { 255,0,0 }, black[] = { 0,0,0 }, white[] = { 255,255,255 };
   808       (+img).draw_rectangle(x0,y0,x1,y1,red,1.0f,~0U).display(disp);
   809       CImg<unsigned char> visu = img.get_crop(x0,y0,x1,y1).draw_point(x-x0,y-y0,red,0.2f).resize(dispz);
   810       if (grid) {
   811         const int bfac = 2*factor+1;
   812         for (int i = 0; i<bfac; ++i) {
   813           const int X = i*dispz.dimx()/bfac, Y = i*dispz.dimy()/bfac;
   814           visu.draw_line(X,0,X,dispz.dimy()-1,black).draw_line(0,Y,dispz.dimx()-1,Y,black);
   815         }
   816       }
   817       visu.draw_text(2,2,"Coords (%d,%d)",white,0,1,11,x,y).display(dispz);
   818     }
   819     if (disp.button&1) { factor=(int)(factor/1.5f); if (factor<3) factor = 3; disp.button=0; redraw = true; }
   820     if (disp.button&2) { factor=(int)(factor*1.5f); if (factor>100) factor = 100; disp.button=0; redraw = true; }
   821     if (disp.button&4 || dispz.button) { grid = !grid; disp.button = dispz.button = 0; redraw = true; }
   822     if (disp.is_resized) disp.resize(disp);
   823     if (dispz.is_resized) { dispz.resize(); redraw = true; }
   824     CImgDisplay::wait(disp,dispz);
   825   }
   826   return 0;
   827 }
   829 // Item : Blobs Editor
   830 //--------------------
   831 void* item_blobs_editor() {
   832   CImg<unsigned int> img(300,300,1,3);
   833   CImgList<unsigned int> colors;
   834   CImgList<> blobs;
   835   CImgDisplay disp(img,"[#18] - Blobs Editor",0);
   836   bool moving = false;
   837   unsigned int white[] = { 255,255,255 };
   839   for (float alpha = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; alpha+=0.1f) {
   840     const int xm = disp.mouse_x*img.dimx()/disp.dimx(), ym = disp.mouse_y*img.dimy()/disp.dimy();
   841     int selected = -1;
   842     img.fill(0);
   844     if (blobs) {
   845       float dist = 0, dist_min = (float)img.dimx()*img.dimx() + img.dimy()*img.dimy();
   846       cimglist_for(blobs,l) {
   847         const CImg<>& blob = blobs[l];
   848         const float
   849           xb = blob[0], yb = blob[1], rb = blob[2],
   850           sigma = (float)(rb*(1+0.05f*std::cos(blob[3]*alpha))),
   851           sigma2 = 2*sigma*sigma, precision = 4.5f*sigma2;
   852         const int tx0 = (int)(xb-3*sigma), ty0 = (int)(yb-3*sigma), tx1 = (int)(xb+3*sigma), ty1 = (int)(yb+3*sigma);
   853         const unsigned int
   854           col1 = colors[l](0), col2 = colors[l](1), col3 = colors[l](2), wh = img.dimx()*img.dimy(),
   855           x0 = tx0<0?0:tx0, y0 = ty0<0?0:ty0,
   856           x1 = tx1>=img.dimx()?(img.dimx()-1):tx1, y1 = ty1>=img.dimy()?(img.dimy()-1):ty1;
   857         float dy = y0-yb;
   858         unsigned int *ptr = img.ptr(x0,y0);
   859         for (unsigned int y = y0; y<=y1; ++y) {
   860           float dx = x0-xb;
   861           for (unsigned int x = x0; x<=x1; ++x) {
   862             float dist = dx*dx + dy*dy;
   863             if (dist<precision) {
   864               const float val = (float)exp(-dist/sigma2);
   865               *ptr += (unsigned int)(val*col1);
   866               *(ptr+wh) += (unsigned int)(val*col2);
   867               *(ptr+2*wh) += (unsigned int)(val*col3);
   868             }
   869             ++dx; ++ptr;
   870           }
   871           ptr+=img.dimx()-(x1-x0)-1;
   872           ++dy;
   873         }
   874         if ((dist=(xb-xm)*(xb-xm)+(yb-ym)*(yb-ym))<dist_min) { dist_min = dist; selected = l; }
   875       }
   877       for (unsigned int *ptr1 = img.ptr(0,0,0,1), *ptr2 = img.ptr(0,0,0,2), *ptr3 = img.ptr(img.size()-1)+1,
   878              off = 0, wh = img.dimx()*img.dimy(); ptr1>img.data; ++off) {
   879         unsigned int val1 = *(--ptr1), val2 = *(--ptr2), val3 = *(--ptr3);
   880         const unsigned int pot = val1*val1 + val2*val2 + val3*val3;
   881         if (pot<128*128) { *ptr1=*ptr3=255*off/wh; *ptr2=180*off/wh; }
   882         else {
   883           if (pot<140*140) { *ptr1>>=1; *ptr2>>=1; *ptr3>>=1; }
   884           else {
   885             *ptr1 = val1<255?val1:255;
   886             *ptr2 = val2<255?val2:255;
   887             *ptr3 = val3<255?val3:255;
   888           }
   889         }
   890       }
   891       cimglist_for(blobs,ll) {
   892         const CImg<>& blob = blobs[ll];
   893         const int rb = (int)(blob[2]*(1+0.05f*std::cos(blob[3]*alpha))),
   894           xb = (int)(blob[0]+rb/2.5f), yb = (int)(blob[1]-rb/2.5f);
   895         img.draw_circle(xb,yb,rb>>1,white,0.2f).draw_circle(xb,yb,rb/3,white,0.2f).
   896           draw_circle(xb,yb,rb/5,white,0.2f);
   897       }
   898     } else {
   899       CImg<unsigned int> text;
   900       text.draw_text(0,0,
   901                      "CImg Blobs Editor\n"
   902                      "-----------------\n\n"
   903                      "* Left mouse button :\n   Create and Move Blob.\n\n"
   904                      "* Right mouse button :\n  Remove nearest Blob.\n\n"
   905                      "* Colors and size of Appearing Blobs\n"
   906                      "  are randomly chosen.\n\n\n"
   907                      " >> Press mouse button to start ! <<",
   908                      white);
   909       img.fill(100).draw_image((img.dimx()-text.dimx())/2,
   910                                (img.dimy()-text.dimy())/2,
   911                                text,text,1,255U);
   912     }
   914     if (disp.mouse_x>=0 && disp.mouse_y>=0) {
   915       if (disp.button&1) {
   916         float dist_selected = 0;
   917         if (selected>=0) {
   918           const float a = xm-blobs[selected](0), b = ym-blobs[selected](1), c = blobs[selected](2);
   919           dist_selected = a*a+b*b-c*c;
   920         }
   921         if (moving || dist_selected<0) { blobs[selected](0) = (float)xm; blobs[selected](1) = (float)ym; }
   922         else {
   923           blobs.insert(CImg<>::vector((float)xm,(float)ym,(float)(10+30*cimg::rand()),(float)(3*cimg::rand())));
   924           colors.insert(CImg<>(3).fill(0).noise(255,1).normalize(0,255));
   925         }
   926         moving = true;
   927       } else moving = false;
   928       if (selected>=0 && disp.button&2) { blobs.remove(selected); colors.remove(selected); disp.button = 0; }
   929     }
   931     img.display(disp.wait(25));
   932     if (disp.is_resized) {
   933       img.resize(disp.resize(false));
   934       cimglist_for(blobs,l) if (blobs[l](0)>=img.dimx() || blobs[l](1)>=img.dimy()) { blobs.remove(l); colors.remove(l--); }
   935     }
   936   }
   937   return 0;
   938 }
   940 // Item : Double Torus
   941 //---------------------
   942 void* item_double_torus() {
   943   CImg<unsigned char> visu(300,256,1,3,0);
   944   CImgDisplay disp(300,256,"[#19] - Double 3D Torus");
   945   CImgList<unsigned int> primitives;
   946   CImg<> points = CImg<>::torus3d(primitives,60,20), points2 = CImg<>::rotation_matrix(1,0,0,(float)cimg::valuePI/2.0f)*points;
   947   CImgList<> opacities = CImgList<>(primitives.size,1,1,1,1,1).insert(CImgList<>(primitives.size,1,1,1,1,0.4f));
   948   CImgList<unsigned char> colors(2*primitives.size,CImg<unsigned char>(1,3,1,1,255,255,0));
   949   cimglist_for(primitives,ll) colors[ll++].fill(100,255,100);
   950   cimglist_for(primitives,l) if (l%2) colors[primitives.size+l].fill(255,200,255); else colors[primitives.size+l].fill(200,150,255);
   951   points.translate_object3d(-30,0,0).append_object3d(primitives,points2.translate_object3d(30,0,0),primitives);
   952   float alpha = 0, beta = 0, gamma = 0, theta = 0;
   953   while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
   954     visu.get_shared_channels(1,2).fill(0);
   955     visu.get_shared_line(visu.dimy()-1,0,0).noise(200,1);
   956     CImg_3x3(I,unsigned char); Ipp = Icp = Inp = Ipc = Inc = 0;
   957     cimg_for3x3(visu,x,y,0,0,I) visu(x,y,0) = (Icc+Ipn+Icn+Inn)>>2;
   958     { for (unsigned int y = 0; y<100; ++y) std::memset(visu.ptr(0,y,0,2),255-y*255/100,visu.dimx()); }
   959     const CImg<>
   960       rpoints = CImg<>::rotation_matrix(1,1,0,(alpha+=0.01f))*CImg<>::rotation_matrix(1,0,1,(beta-=0.02f))*
   961       CImg<>::rotation_matrix(0,1,1,(gamma+=0.03f))*points;
   962     if (disp.is_resized) disp.resize(false);
   963     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(300,256,false).toggle_fullscreen(false);
   964     visu.draw_object3d(visu.dimx()/2.0f,visu.dimy()/2.0f,0,
   965                        rpoints,primitives,colors,opacities,4,
   966                        true,500.0f,(float)(std::cos(theta+=0.01f)+1)*visu.dimx()/2.0f,
   967                        (float)visu.dimy(),-100.0f,0.1f,1.5f).
   968       display(disp.wait(25));
   969   }
   970   return 0;
   971 }
   973 // Item : 3D Metaballs
   974 //---------------------
   975 struct metaballs3d {
   976   float cx1, cy1, cz1, cx2, cy2, cz2, cx3, cy3, cz3;
   977   inline float operator()(const float x, const float y, const float z) const {
   978     const float
   979       x1 = x - cx1, y1 = y - cy1, z1 = z - cz1,
   980       x2 = x - cx2, y2 = y - cy2, z2 = z - cz2,
   981       x3 = x - cx3, y3 = y - cy3, z3 = z - cz3,
   982       r1 = 0.3f*(x1*x1 + y1*y1 + z1*z1),
   983       r2 = 0.4f*(x2*x2 + y2*y2 + z2*z2),
   984       r3 = 0.5f*(x3*x3 + y3*y3 + z3*z3);
   985     float potential = 0;
   986     if (r1<1.3f) potential+= 1.0f - r1*(r1*(4*r1+17)-22)/9;
   987     if (r2<1.3f) potential+= 1.0f - r2*(r2*(4*r2+17)-22)/9;
   988     if (r3<1.3f) potential+= 1.0f - r3*(r3*(4*r3+17)-22)/9;
   989     return potential;
   990   }
   991 };
   993 void* item_3d_metaballs() {
   994   CImg<unsigned char> img = CImg<unsigned char>(100,100,1,3,0).noise(100,2).draw_plasma(0,0,99,99).resize(512,320,1,3).blur(4);
   995   img.get_shared_channel(2)/=4; img.get_shared_channel(1)/=2;
   996   metaballs3d met;
   997   CImgList<unsigned int> primitives;
   998   CImgList<unsigned char> colors(8000,3,1,1,1,255);
   999   unsigned char white[] = { 255,255,255 };
  1001   float alpha = 0, beta = 0, delta = 0, theta = 0, gamma = 0;
  1002   CImgDisplay disp(img,"[#20] - 3D Metaballs");
  1003   while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
  1004     met.cx2 = 1.5f*(float)std::cos(theta); met.cy2 = 2.5f*(float)std::sin(3*(theta+=0.017f)); met.cz2 = 0;
  1005     met.cx1 = 0; met.cy1 = 2.0f*(float)std::sin(4*gamma); met.cz1 = 1.2f*(float)std::cos(2*(gamma-=0.0083f));
  1006     met.cx3 = 2.5f*(float)std::cos(2.5*delta); met.cy3 = 0; met.cz3 = 1.5f*(float)std::sin(2*(delta+=0.0125f));
  1007     const CImg<>
  1008       points = CImg<>::marching_cubes(primitives,met,0.8f,-4.5f,-4.5f,-3.5f,4.5f,4.5f,3.5f,0.29f,0.29f,0.29f,true),
  1009       rot = 50.0*CImg<>::rotation_matrix(0,0,1,(alpha+=0.02f))*CImg<>::rotation_matrix(1,1,0,(beta+=0.076f)),
  1010       rpoints = rot*points;
  1011     cimglist_for(primitives,ll) {
  1012       colors(ll,0) = -60+191+64*ll/primitives.size;
  1013       colors(ll,1) = -30+191+64*ll/primitives.size;
  1014       colors(ll,2) = 255*ll/primitives.size;
  1016     if (primitives.size) {
  1017       (+img).draw_object3d(img.dimx()/2.0f,img.dimy()/2.0f,0.0f,
  1018                            rpoints,primitives,
  1019                            colors.get_crop(0,primitives.size-1,true),
  1020                            4,false,500, 0,0,-500, 0.1f,1.5f).
  1021         draw_text(5,5,"%u frames/s",white,0,0.5f,11,(unsigned int)disp.frames_per_second()).display(disp.wait(20));
  1023     if (disp.is_resized) disp.resize(false);
  1024     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(512,320,false).toggle_fullscreen(false);
  1026   return 0;
  1029 // Item : Fireworks
  1030 //------------------
  1031 void* item_fireworks() {
  1032   CImg<unsigned char> img(640,480,1,3,0);
  1033   CImgDisplay disp(img,"[#21] - Fireworks (Click to add/explode rockets)");
  1034   CImgList<unsigned char> colors;
  1035   unsigned char white[] = { 255,255,255 }, black[] = { 128,0,0 };
  1036   CImgList<> particles;
  1037   float time = 0, speed = 100.0f;
  1039   while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
  1041     if (disp.button&1 || !particles.size || (--time)<0) {
  1042       particles.insert(CImg<>::vector((float)cimg::rand()*img.dimx(),(float)img.dimy(),
  1043                                       (float)cimg::crand()*4,-6-(float)cimg::rand()*3,
  1044                                       30+60*(float)cimg::rand(),3));
  1045       colors.insert(CImg<unsigned char>::vector(255,255,255));
  1046       time = (float)(cimg::rand()*speed);
  1048     img*=0.92f;
  1050     cimglist_for(particles,l) {
  1051       bool remove_particle = false;
  1052       float &x = particles(l,0), &y = particles(l,1), &vx = particles(l,2), &vy = particles(l,3),
  1053             &t = particles(l,4), &r = particles(l,5);
  1054       const float n = (float)std::sqrt(1e-5f+vx*vx+vy*vy), nvx = vx/n, nvy = vy/n,
  1055         r2 = (t>0 || t<-42)?r/3:r*(1-2*(-(t+2)/40.0f)/3);
  1056       img.draw_ellipse((int)x,(int)y,r,r2,nvx,nvy,colors[l],0.6f);
  1057       x+=vx; y+=vy; vy+=0.09f; t--;
  1058       if (y>img.dimy()+10 || x<0 || x>=img.dimx()+10) remove_particle = true;
  1060       if (t<0 && t>=-1) {
  1061         if ((speed*=0.9f)<10) speed=10.0f;
  1062         const unsigned char
  1063           r = cimg::min(50+3*(unsigned char)(100*cimg::rand()), 255),
  1064           g = cimg::min(50+3*(unsigned char)(100*cimg::rand()), 255),
  1065           b = cimg::min(50+3*(unsigned char)(100*cimg::rand()), 255);
  1066         const float di = 10+(float)cimg::rand()*60, nr = (float)cimg::rand()*30;
  1067         for (float i=0; i<360; i+=di) {
  1068           const float rad = i*(float)cimg::valuePI/180, c = (float)std::cos(rad), s = (float)std::sin(rad);
  1069           particles.insert(CImg<>::vector(x,y,2*c+vx/1.5f,2*s+vy/1.5f,-2.0f,nr));
  1070           colors.insert(CImg<unsigned char>::vector(r,g,b));
  1072         remove_particle = true;
  1073       } else if (t<-1) { r*=0.95f; if (r<0.5f) remove_particle=true; }
  1074       if (remove_particle) { particles.remove(l); colors.remove(l); l--; }
  1076     if (disp.button&2) cimglist_for(particles,l) if (particles(l,4)>0) particles(l,4)=0.5f;
  1077     img.draw_text(5,5,"%u frames/s",white,black,0.5f,11,(unsigned int)disp.frames_per_second());
  1078     disp.display(img).wait(25);
  1079     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(640,480,false).toggle_fullscreen(false);
  1080     if (disp.is_resized) disp.resize(disp,false);
  1082   return 0;
  1085 // Item : Rubber Logo
  1086 //--------------------
  1087 void* item_rubber_logo() {
  1088   const unsigned char white[] = { 255,255,255 };
  1089   CImg<unsigned char> background = CImg<unsigned char>(300,300).noise(100,2);
  1090   background(0,0) = background(299,0) = background(299,299) = background(0,299) = 0;
  1091   background.draw_plasma(0,0,299,299).blur(1.0f,14.0f,0.0f,0).resize(-100,-100,1,3);
  1092   CImgDisplay disp(CImg<unsigned char>(background).
  1093                    draw_text(10,10,"Please wait, generating rubber object...",white),"[#22] - 3D Rubber Logo");
  1095   CImg<unsigned char> vol = CImg<unsigned char>().draw_text(30,30,"CImg",white,0,1,48).resize(-100,-100,15,1);
  1096   for (unsigned int k = 0; k<5; ++k) { vol.get_shared_plane(k).fill(0); vol.get_shared_plane(vol.dimz()-1-k).fill(0); }
  1097   vol.resize(vol.dimx()+30,vol.dimy()+30,-100,1,0).blur(2).resize(-50,-50);
  1098   CImgList<unsigned int> faces;
  1099   CImg<> points = vol.get_isovalue3d(faces,45,1,1,1,true);
  1100   CImgList<unsigned char> colors;
  1101   colors.insert(faces.size,CImg<unsigned char>::vector(100,100,255));
  1102   cimglist_for(colors,l) {
  1103     const float x = (points(faces(l,0),0) + points(faces(l,1),0) + points(faces(l,2),0))/3;
  1104     if (x<27) colors[l] = CImg<unsigned char>::vector(255,100,100);
  1105     else { if (x<38) colors[l] = CImg<unsigned char>::vector(200,155,100);
  1106     else { if (x<53) colors[l] = CImg<unsigned char>::vector(100,255,155);
  1107     }}}
  1108   { cimg_forX(points,l) { points(l,0)-=vol.dimx()/2; points(l,1)-=vol.dimy()/2; points(l,2)-=vol.dimz()/2; }}
  1109   points*=5.5;
  1111   CImgList<unsigned char> frames(100,background);
  1112   bool ok_visu = false;
  1113   unsigned int nb_frame = 0;
  1114   float alpha = 0, beta = 0, gamma = 0;
  1116   while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
  1117     CImg<unsigned char>& frame = frames[nb_frame++];
  1118     if (nb_frame>=frames.size) { ok_visu = true; nb_frame = 0; }
  1119     const CImg<>
  1120       rot = CImg<>::rotation_matrix(0,1,0.2f,alpha+=0.011f)*
  1121       CImg<>::rotation_matrix(1,0.4f,1,beta+=0.015f)*
  1122       (1+0.1f*std::cos((double)(gamma+=0.1f)));
  1123     (frame=background).draw_object3d(frame.dimx()/2.0f,frame.dimy()/2.0f,frame.dimz()/2.0f,rot*points,faces,colors,5,
  1124                                      false,500,0,0,-5000,0.1f,1.0f);
  1126     if (ok_visu) {
  1127       CImg<unsigned char> visu(frame);
  1128       cimglist_for(frames,l) {
  1129         const unsigned int
  1130           y0 = l*visu.dimy()/frames.size,
  1131           y1 = (l+1)*visu.dimy()/frames.size-1;
  1132         cimg_forV(visu,k) visu.get_shared_lines(y0,y1,0,k) = frames[(nb_frame+l)%frames.size].get_shared_lines(y0,y1,0,k);
  1134       visu.get_resize(disp,1).draw_text(5,5,"%u frames/s",white,0,0.5f,11,(unsigned int)disp.frames_per_second()).display(disp.wait(20));
  1137     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(300,300,false).toggle_fullscreen(false);
  1138     if (disp.is_resized) disp.resize();
  1140   return 0;
  1143 // Item : Image Waves
  1144 //--------------------
  1145 void* item_image_waves() {
  1146   const CImg<unsigned char> img = CImg<unsigned char>(data_milla,211,242,1,3,false).get_resize(128,128,1,3);
  1147   const unsigned int w = img.dimx()+1, h = img.dimy()+1;
  1148   CImgList<> points0;
  1149   CImgList<unsigned int> faces0;
  1150   CImgList<unsigned char> colors0;
  1151   { for (unsigned int y = 0; y<h; ++y) for (unsigned int x=0; x<w; ++x)
  1152     points0.insert(CImg<>::vector(3*(x-w/2.0f),3*(y-w/2.0f),0)); }
  1153   cimg_forXY(img,x,y) {
  1154     faces0.insert(CImg<unsigned int>::vector(x+y*w,x+(y+1)*w,x+1+(y+1)*w,x+1+y*w));
  1155     colors0.insert(CImg<unsigned char>::vector(img(x,y,0),img(x,y,1),img(x,y,2)));
  1157   CImgList<> opacities0(faces0.size,CImg<>::vector(1.0f));
  1159   CImg<unsigned char>
  1160     back = CImg<unsigned char>(400,300,1,3).sequence(0,130),
  1161     ball = CImg<unsigned char>(12,12,1,3,0).draw_circle(6,6,5,CImg<unsigned char>::vector(0,128,64));
  1162   const CImg<> mball = CImg<>(12,12,1,1,0).draw_circle(6,6,5,CImg<>::vector(1.0f));
  1163   ball.draw_circle(7,5,4,CImg<unsigned char>::vector(16,96,52)).
  1164     draw_circle(8,4,2,CImg<unsigned char>::vector(0,128,64)).
  1165     draw_circle(8,4,1,CImg<unsigned char>::vector(64,196,128));
  1167   CImg<> uc(img.dimx()/2,img.dimy()/2,1,1,0), up(uc), upp(uc);
  1168   CImgDisplay disp(back,"[#23] - Image Waves (Try mouse buttons!)");
  1169   CImgList<int> particles;
  1171   for (float alpha = 0.0f, count=10.0f; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; ) {
  1172     if ((disp.button&1 && disp.mouse_x>=0) || --count<0) {
  1173       particles.insert(CImg<int>::vector((int)(cimg::rand()*(img.dimx()-1)),(int)(cimg::rand()*(img.dimy()-1)),-200,0));
  1174       count = (float)(cimg::rand()*15);
  1176     alpha = (disp.mouse_x>=0 && disp.button&2)?(float)(disp.mouse_x*2*cimg::valuePI/disp.dimx()):(alpha+0.04f);
  1177     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(400,300,false).toggle_fullscreen(false);
  1179     cimglist_for(particles,l) {     // Handle particles
  1180       float& z = up(particles(l,0)>>1,particles(l,1)>>1);
  1181       if ((particles(l,2)+=(particles(l,3)++))>z-10) { z = 250.0f; particles.remove(l--); }
  1184     CImg_3x3(U,float); Upp = Unp = Ucc = Upn = Unn = 0; // Apply wave effect
  1185     cimg_for3x3(up,x,y,0,0,U) uc(x,y) = (Unc+Upc+Ucn+Ucp)/2 - upp(x,y);
  1186     (uc-=(float)(uc.blur(0.7f).mean())).swap(upp).swap(up);
  1188     CImgList<> points(points0);
  1189     CImgList<unsigned int> faces(faces0);
  1190     CImgList<unsigned char> colors(colors0);
  1191     CImgList<> opacities(opacities0);
  1192     cimglist_for(points,p) points(p,2) = cimg::min(30 + uc.linear_atXY((p%w)/2.0f,(p/w)/2.0f),70.0f);
  1193     { cimglist_for(particles,l) {
  1194       points.insert(CImg<>::vector(3*(particles(l,0)-w/2.0f),3*(particles(l,1)-h/2.0f),30.0f+particles(l,2)));
  1195       faces.insert(CImg<unsigned int>::vector(points.size-1));
  1196       colors.insert(ball);
  1197       opacities.insert(mball);
  1198     }}
  1199     const CImg<> rot = CImg<>::rotation_matrix(1.0f,0,0,(float)(cimg::valuePI/3.0f))*CImg<>::rotation_matrix(0,0,1.0f,alpha);
  1200     (+back).draw_object3d(back.dimx()/2.0f,back.dimy()/2.0f,0,rot*points,faces,colors,opacities,4,false,500.0f,0,0,0,1,1).
  1201       display(disp.resize(false).wait(20));
  1203   return 0;
  1206 // Item : Breakout
  1207 //-----------------
  1208 void* item_breakout() {
  1210   // Init graphics
  1211   CImg<unsigned char>
  1212     board(8,10,1,1,0),
  1213     background = CImg<unsigned char>(board.dimx()*32,board.dimy()*16+200,1,3,0).noise(20,1).draw_plasma().blur(1,8,0),
  1214     visu0(background/2.0), visu(visu0), brick(16,16,1,1,200), racket(64,8,1,3,0), ball(8,8,1,3,0);
  1215   const unsigned char white[] = { 255,255,255 }, green1[] = { 60,150,30 }, green2[] = { 130,255,130 };
  1216   { cimg_for_borderXY(brick,x,y,1) brick(x,y) = x>y?255:128; }
  1217   { cimg_for_insideXY(brick,x,y,1) brick(x,y) = cimg::min(255,64+8*(x+y)); }
  1218   brick.resize(31,15,1,1,1).resize(32,16,1,1,0);
  1219   ball.draw_circle(4,4,2,white); ball-=ball.get_erode(3)/1.5;
  1220   racket.draw_circle(4,3,4,green1).draw_circle(3,2,2,green2);
  1221   { cimg_forY(racket,y) racket.draw_rectangle(4,y,racket.dimx()-7,y,CImg<unsigned char>::vector(y*4,255-y*32,255-y*25)); }
  1222   racket.draw_image(racket.dimx()/2,racket.get_crop(0,0,racket.dimx()/2-1,racket.dimy()-1).mirror('x'));
  1223   const int
  1224     w = visu.dimx(), h = visu.dimy(), w2 = w/2, h2 = h/2,
  1225     bw = ball.dimx(), bh = ball.dimy(), bw2 = bw/2, bh2 = bh/2,
  1226     rw = racket.dimx(), rh = racket.dimy(), rw2 = rw/2;
  1227   float xr = (float)(w-rw2), oxr = (float)xr, xb = 0, yb = 0, oxb = 0, oyb = 0, vxb = 0, vyb = 0;
  1229   // Begin game loop
  1230   CImgDisplay disp(visu,"[#24] - Breakout");
  1231   disp.move((CImgDisplay::screen_dimx()-w)/2,(CImgDisplay::screen_dimy()-h)/2);
  1232   for (unsigned int N = 0, N0 = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; ) {
  1233     if (N0) {
  1234       int X = (int)xr;
  1235       if (disp.mouse_x>=0) X = (int)(w2+((disp.mouse_x<0?w2:disp.mouse_x)-w2)*2);
  1236       else disp.set_mouse(xr>w2?w-81:80,h2);
  1237       if (X<rw2) { X = rw2; disp.set_mouse(80,h2); }
  1238       if (X>=w-rw2) { X = w-rw2-1; disp.set_mouse(w-81,h2); }
  1239       oxr = xr; xr = (float)X; oxb = xb; oyb = yb; xb+=vxb; yb+=vyb;
  1240       if ((xb>=w-bw2) || (xb<bw2)) { xb-=vxb; yb-=vyb; vxb=-vxb; }
  1241       if (yb<bh2) { yb = (float)bh2; vyb=-vyb; }
  1242       if (yb>=h-rh-8-bh2 && yb<h-8-bh2 && xr-rw2<=xb && xr+rw2>=xb) {
  1243         xb = oxb; yb = h-rh-8.0f-bh2; vyb=-vyb; vxb+=(xr-oxr)/4;
  1244         if (cimg::abs(vxb)>8) vxb*=8/cimg::abs(vxb);
  1246       if (yb<board.dimy()*16) {
  1247         const int X = (int)xb/32, Y = (int)yb/16;
  1248         if (board(X,Y)) {
  1249           board(X,Y) = 0;
  1250           ++N;
  1251           const unsigned int x0 = X*brick.dimx(), y0 = Y*brick.dimy(), x1 = (X+1)*brick.dimx()-1, y1 = (Y+1)*brick.dimy()-1;
  1252           visu0.draw_image(x0,y0,background.get_crop(x0,y0,x1,y1));
  1253           if (oxb<(X<<5) || oxb>=((X+1)<<5)) vxb=-vxb;
  1254           else if (oyb<(Y<<4) || oyb>=((Y+1)<<4)) vyb=-vyb;
  1257       disp.set_title("[#24] - Breakout : %u/%u",N,N0);
  1259     if (yb>h || N==N0) {
  1260       disp.show_mouse();
  1261       while (!disp.is_closed && !disp.key && !disp.button) {
  1262         ((visu=visu0)/=2).draw_text(50,visu.dimy()/2-10,N0?"Game Over !":"Get Ready ?",white,0,1,25).
  1263           display(disp);
  1264         disp.wait();
  1265         if (disp.is_resized) disp.resize(disp);
  1267       board.fill(0); visu0 = background;
  1268       cimg_forXY(board,x,y) if (0.2f+cimg::crand()>=0) {
  1269         CImg<> cbrick = CImg<double>::vector(100+cimg::rand()*155,100+cimg::rand()*155,100+cimg::rand()*155).
  1270           unroll('v').resize(brick.dimx(),brick.dimy());
  1271         cimg_forV(cbrick,k) (cbrick.get_shared_channel(k).mul(brick))/=255;
  1272         visu0.draw_image(x*32,y*16,cbrick);
  1273         board(x,y) = 1;
  1275       N0 = (int)board.sum(); N = 0;
  1276       oxb = xb = (float)w2; oyb = yb = board.dimy()*16.0f+bh; vxb = 2.0f; vyb = 3.0f;
  1277       disp.hide_mouse();
  1278     } else disp.display((visu=visu0).draw_image((int)(xr-rw2),h-rh-8,racket).draw_image((int)(xb-bw2),(int)(yb-bh2),ball));
  1279     if (disp.is_resized) disp.resize(disp);
  1280     disp.wait(20);
  1282   return 0;
  1285 // Item : 3D Reflection
  1286 //----------------------
  1287 void* item_3d_reflection() {
  1289   // Init images and display
  1290   CImgDisplay disp(512,512,"[#25] - 3D Reflection",0);
  1291   CImg<unsigned char> back(400,400,1,3,0);
  1292   cimg_forV(back,k) { back(399,0,k) = back(0,0,k) = 50*((k+1)%2); back(399,399,k) = back(0,399,k) = 20*k; }
  1293   back.draw_plasma().blur(6,1,0).translate(200,0,0,0,2).blur(6,1,0);
  1294   CImg<unsigned char> light0 = back.get_resize(-50,-50,1,1), visu(back), reflet(back.dimx(),back.dimy(),1,1), light(light0);
  1295   back.get_shared_channel(0)/=3; back.get_shared_channel(2)/=2;
  1297   // Create 3D objects
  1298   CImgList<unsigned int> back_faces, main_faces;
  1299   CImgList<float> back_pts0, main_pts;
  1300   CImgList<unsigned char> main_colors, back_colors, light_colors, light_colors2;
  1302   main_pts = CImg<>::torus3d(main_faces,30,12,24,12).get_split('x');
  1303   cimglist_for(main_faces,l)
  1304     if (l%2) main_colors.insert(CImg<unsigned char>::vector(255,120,16));
  1305     else main_colors.insert(CImg<unsigned char>::vector(255,100,16));
  1307   const unsigned int res1 = 32, res2 = 32;
  1308   for (unsigned int v = 1; v<res2; ++v) for (unsigned int u = 0; u<res1; ++u) {
  1309     const float
  1310       alpha = (float)(u*2*cimg::valuePI/res1), beta = (float)(-cimg::valuePI/2 + v*cimg::valuePI/res2),
  1311       x = (float)(std::cos(beta)*std::cos(alpha)),
  1312       y = (float)(std::cos(beta)*std::sin(alpha)),
  1313       z = (float)(std::sin(beta));
  1314     back_pts0.insert(CImg<>::vector(x,y,z));
  1316   const unsigned int N = back_pts0.size;
  1317   back_pts0.insert(CImg<>::vector(0,0,-140)).insert(CImg<>::vector(0,0,140));
  1318   CImg<float> back_pts = back_pts0.get_append('x');
  1319   for (unsigned int vv = 0; vv<res2-2; ++vv) for (unsigned int uu = 0; uu<res1; ++uu) {
  1320     const int nv = (vv+1)%(res2-1), nu = (uu+1)%res1;
  1321     back_faces.insert(CImg<unsigned int>::vector(res1*vv+nu,res1*nv+uu,res1*vv+uu));
  1322     back_faces.insert(CImg<unsigned int>::vector(res1*vv+nu,res1*nv+nu,res1*nv+uu));
  1323     back_colors.insert(CImg<unsigned char>::vector(128,255,255));
  1324     back_colors.insert(CImg<unsigned char>::vector(64,240,196));
  1326   for (unsigned int uu = 0; uu<res1; ++uu) {
  1327     const int nu = (uu+1)%res1;
  1328     back_faces.insert(CImg<unsigned int>::vector(nu,uu,N));
  1329     back_faces.insert(CImg<unsigned int>::vector(res1*(res2-2)+nu, N+1,res1*(res2-2)+uu));
  1330     if (uu%2) back_colors.insert(2,CImg<unsigned char>::vector(128,255,255));
  1331     else back_colors.insert(2,CImg<unsigned char>::vector(64,240,196));
  1334   light_colors.assign(back_faces.size,CImg<unsigned char>::vector(255));
  1335   light_colors2.assign(light_colors).insert(light,light_colors.size,true);
  1337   // Start 3D animation
  1338   for (float main_x = -1.5f*visu.dimx(),
  1339          back_alpha = 0, back_beta = 0, back_theta = -3.0f,
  1340          main_alpha = 0, main_beta = 0, main_theta = 0;
  1341        !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC;
  1342        main_alpha+=0.041f, main_beta+=0.063f, main_theta+=0.02f,
  1343          back_alpha+=0.0031f, back_beta+=0.0043f, back_theta+=0.01f) {
  1344     const int
  1345       main_X = (int)(visu.dimx()/2 + main_x + 100*std::cos(2.1*main_theta)),
  1346       main_Y = (int)(visu.dimy()/2 + 120*std::sin(1.8*main_theta));
  1347     CImgList<> rmain_pts = (CImg<>::rotation_matrix(-1,1,0,main_alpha)*CImg<>::rotation_matrix(1,0,1,main_beta))*main_pts;
  1348     const CImg<> rback_pts = (CImg<>::rotation_matrix(1,1,0,back_alpha)*CImg<>::rotation_matrix(0.5,0,1,back_beta))*back_pts;
  1349     (light=light0).draw_object3d(main_X/2.0f,main_Y/2.0f,0,rmain_pts,main_faces,light_colors,3,false,500,0,0,-5000,0.2f,0.1f);
  1350     reflet.fill(0).draw_object3d(2*visu.dimx()/3.0f,visu.dimy()/2.0f,0,rback_pts,back_faces,light_colors2,5,false,500,0,0,-5000,0.2f,0.1f);
  1351     rmain_pts*=2;
  1352     (visu=back).draw_object3d(2*visu.dimx()/3.0f,visu.dimy()/2.0f,0,rback_pts,back_faces,back_colors,3,false,500,0,0,-5000,0.2f,0.1f);
  1353     unsigned char *ptrs = reflet.ptr(), *ptrr = visu.ptr(0,0,0,0), *ptrg = visu.ptr(0,0,0,1), *ptrb = visu.ptr(0,0,0,2);
  1354     cimg_forXY(visu,x,y) {
  1355       const unsigned char v = *(ptrs++);
  1356       if (v) { *ptrr = (*ptrr+v)>>1; *ptrg = (3**ptrr+v)>>2; *ptrb = (*ptrb+v)>>1; }
  1357       ++ptrr; ++ptrg; ++ptrb;
  1359     visu.draw_object3d((float)main_X,(float)main_Y,0,rmain_pts,main_faces,main_colors,4,
  1360                        false,500,0,0,-5000,0.1f,1.4f);
  1362     if (disp.is_resized) {
  1363       const int s = cimg::min(disp.window_dimx(),disp.window_dimy());
  1364       disp.resize(s,s,false);
  1366     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(512,512,false).toggle_fullscreen(false);
  1367     disp.display(visu).wait(20);
  1368     back.translate(-4,0,0,0,2);
  1369     light0.translate(2,0,0,0,2);
  1370     if (main_x<0) main_x +=2;
  1371     const float H = back_theta<0?0.0f:(float)(0.3f-0.3f*std::cos(back_theta));
  1372     for (unsigned int p = 0, v = 1; v<res2; ++v) for (unsigned int u = 0; u<res1; ++u) {
  1373       const float
  1374         alpha = (float)(u*2*cimg::valuePI/res1), beta = (float)(-cimg::valuePI/2 + v*cimg::valuePI/res2),
  1375         x = back_pts0(p,0), y = back_pts0(p,1), z = back_pts0(p,2),
  1376         altitude = 140*(float)cimg::abs(1+H*std::sin(3*alpha)*std::cos(5*beta));
  1377       back_pts(p,0) = altitude*x; back_pts(p,1) = altitude*y; back_pts(p,2) = altitude*z;
  1378       ++p;
  1381   return 0;
  1384 // Item : Fish-Eye Magnification
  1385 //------------------------------
  1386 void* item_fisheye_magnification() {
  1387   const unsigned char purple[] = { 255,0,255 }, white[] = { 255,255,255 }, black[] = { 0,0,0 };
  1388   const CImg<unsigned char> img0 = CImg<unsigned char>(data_logo,555,103,1,3,true).get_resize(-144,-144,1,3,5);
  1389   CImgDisplay disp(img0,"[#26] - Fish-Eye Magnification");
  1390   int rm = 80, xc = 0, yc = 0, rc = 0;
  1391   CImg<unsigned char> img, res;
  1392   for (float alpha = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; alpha+=0.02f) {
  1393     if (!img) img = img0.get_resize(disp,3);
  1394     if (disp.mouse_x>=0) { xc = disp.mouse_x; yc = disp.mouse_y; rc = rm; }
  1395     else {
  1396       xc = (int)(img.dimx()*(1 + 0.9f*std::cos(1.2f*alpha))/2);
  1397       yc = (int)(img.dimy()*(1 + 0.8f*std::sin(3.4f*alpha))/2);
  1398       rc = (int)(90 + 60*std::sin(alpha));
  1400     const int x0 = xc - rc, y0 = yc - rc, x1 = xc + rc, y1 = yc + rc;
  1401     res = img;
  1402     cimg_for_inXY(res,x0,y0,x1,y1,x,y) {
  1403       const float X = (float)x - xc, Y = (float)y - yc, r2 = X*X + Y*Y, rrc = (float)std::sqrt(r2)/rc;
  1404       if (rrc<1) {
  1405         const int xi = (int)(xc + rrc*X), yi = (int)(yc + rrc*Y);
  1406         res(x,y,0) = img(xi,yi,0); res(x,y,1) = img(xi,yi,1); res(x,y,2) = img(xi,yi,2);
  1409     const int xf = xc+3*rc/8, yf = yc-3*rc/8;
  1410     res.draw_circle(xc,yc,rc,purple,0.2f).draw_circle(xf,yf,rc/3,white,0.2f).draw_circle(xf,yf,rc/5,white,0.2f).
  1411       draw_circle(xf,yf,rc/10,white,0.2f).draw_circle(xc,yc,rc,black,0.7f,~0U);
  1412     disp.display(res).wait(20);
  1413     rm+=(disp.button&1?8:(disp.button&2?-8:0));
  1414     rm = rm<30?30:(rm>200?200:rm);
  1415     if (disp.is_resized) { disp.resize(false); img.assign(); }
  1417   return 0;
  1420 // Item : Word puzzle
  1421 //------------------------------
  1422 void* item_word_puzzle() {
  1424   // Create B&W and color letters
  1425   CImg<unsigned char> model(60,60,1,3,0), color(3), background, canvas, elaps;
  1426   CImgList<unsigned char> letters('Z'-'A'+1), cletters(letters);
  1427   const unsigned char white[] = { 255,255,255 }, gray[] = { 128,128,128 }, black[] = { 0,0,0 };
  1428   char tmptxt[] = { 'A',0 };
  1429   model.fill(255).draw_rectangle(5,5,54,54,gray).blur(3,0).threshold(140).normalize(0,255);
  1430   cimglist_for(letters,l)
  1431     (letters[l].draw_text(5,2,&(tmptxt[0]='A'+l),white,0,1,64).resize(60,60,1,1,0,0,true).
  1432      resize(-100,-100,1,3)|=model).blur(0.5);
  1433   { cimglist_for(cletters,l) {
  1434     CImg<int> tmp = letters[l];
  1435     color.rand(100,255);
  1436     cimg_forV(tmp,k) (tmp.get_shared_channel(k)*=color[k])/=255;
  1437     cletters[l] = tmp;
  1438   }}
  1440   CImgDisplay disp(500,400,"[#27] - Word Puzzle",0);
  1441   while (!disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC) {
  1443     // Create background, word data and display.
  1444     background.assign(40,40,1,2,0).noise(30,2).distance(255).normalize(0,255).resize(500,400,1,3,3);
  1445     CImg<int> current(14,6,1,1,0), solution(14,4,1,1,0);
  1446     current.get_shared_line(0).fill('T','H','E','C','I','M','G','L','I','B','R','A','R','Y');
  1447     current.get_shared_line(1).rand(-30,background.dimx()-30);
  1448     current.get_shared_line(2).rand(-30,background.dimy()-30);
  1449     solution.get_shared_line(0) = current.get_shared_line(0);
  1450     solution.get_shared_line(1).fill(20,80,140,100,180,260,340,40,100,160,220,280,340,400);
  1451     solution.get_shared_line(2).fill(20,20,20,120,150,180,210,310,310,310,310,310,310,310);
  1452     { cimg_forX(solution,l) background.draw_image(solution(l,1),solution(l,2),letters(solution(l)-'A'),0.3f); }
  1453     const int last = current.dimx()-1;
  1455     // Start user interaction
  1456     int timer = 0, completed = 0;
  1457     for (bool selected = false, refresh_canvas = true, stopflag = false;
  1458          !stopflag && !disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC; disp.resize(disp).wait(20)) {
  1459       if (refresh_canvas) {
  1460         canvas = background;
  1461         cimg_forX(current,l) if (!current(l,5)) {
  1462           int &x = current(l,1), &y = current(l,2);
  1463           if (x<-30) x = -30; else if (x>canvas.dimx()-30) x = canvas.dimx()-30;
  1464           if (y<-30) y = -30; else if (y>canvas.dimy()-30) y = canvas.dimy()-30;
  1465           canvas.draw_rectangle(x+8,y+8,x+67,y+67,black,0.3f).draw_image(x,y,cletters(current(l)-'A'));
  1467         refresh_canvas = false;
  1469       (+canvas).draw_text(360,3,"Elapsed Time : %d",white,0,1,16,timer++).display(disp);
  1471       if (disp.button&1) {
  1472         const int mx = disp.mouse_x, my = disp.mouse_y;
  1473         if (mx>=0 && my>=0) {
  1474           if (!selected) {
  1475             int ind = -1;
  1476             cimg_forX(current,l) if (!current(l,5)) {
  1477               const int x = current(l,1), y = current(l,2), dx = mx - x, dy = my - y;
  1478               if (dx>=0 && dx<60 && dy>=0 && dy<60) { selected = true; ind = l; current(l,3) = dx; current(l,4) = dy; }
  1480             if (ind>=0 && ind<last) {
  1481               const CImg<int> vec = current.get_column(ind);
  1482               current.draw_image(ind,current.get_crop(ind+1,last)).draw_image(last,vec);
  1484           } else { current(last,1) = mx - current(last,3); current(last,2) = my - current(last,4); refresh_canvas = true; }
  1486       } else {
  1487         bool win = true;
  1488         cimg_forX(solution,j) if (!solution(j,3)) {
  1489           win = false;
  1490           const int x = solution(j,1), y = solution(j,2);
  1491           cimg_forX(current,i) if (!current(i,5) && solution(j)==current(i)) {
  1492             const int xc = current(i,1), yc = current(i,2), dx = cimg::abs(x-xc), dy = cimg::abs(y-yc);
  1493             if (dx<=12 && dy<=12) {
  1494               cimg_forV(background,k) cimg_forY(letters[0],y)
  1495                 background.get_shared_line(solution(j,2)+y,0,k).
  1496                 draw_image(solution(j,1),0,
  1497                            (CImg<float>(cletters(solution(j)-'A').get_shared_line(y,0,k))*=2.0*std::cos((y-30.0f)/18)).
  1498                            cut(0,255),0.8f);
  1499               current(i,5) = solution(j,3) = 1; refresh_canvas = true;
  1503         selected = false;
  1504         if (win) { stopflag = true; completed = 1; }
  1508     // Display final score
  1509     const char
  1510       *const mention0 = "Need more training !", *const mention1 = "Still amateur, hu ?",
  1511       *const mention2 = "Not so bad !", *const mention3 = "  Good !", *const mention4 = "Very good !",
  1512       *const mention5 = " Expert !",
  1513       *mention = completed?(timer<700?mention5:timer<800?mention4:timer<900?mention3:timer<1000?mention2:timer<1200?mention1:mention0):mention0;
  1514     canvas.assign().draw_text(0,0,"Final time : %d\n\n%s",white,0,1,32,timer,mention);
  1515     ((background/=2)&CImg<unsigned char>(2,2).fill(0,255,255,0).resize(background,0,2)).
  1516       draw_image((background.dimx()-canvas.dimx())/2,(background.dimy()-canvas.dimy())/2,
  1517                  canvas,canvas.get_dilate(3).dilate(3).dilate(3),1,255).display(disp.flush());
  1518     while (!disp.is_closed && !disp.key && !disp.button) disp.resize(disp).wait();
  1520   return 0;
  1523 // Run a selected effect
  1524 //-----------------------
  1525 void start_item(const unsigned int demo_number) {
  1526   switch (demo_number) {
  1527   case 1: item_blurring_gradient(); break;
  1528   case 2: item_rotozoom(); break;
  1529   case 3: item_anisotropic_smoothing(); break;
  1530   case 4: item_fractal_animation(); break;
  1531   case 5: item_gamma_correction(); break;
  1532   case 6: item_filled_triangles(); break;
  1533   case 7: item_mandelbrot_explorer(); break;
  1534   case 8: item_mini_paint(); break;
  1535   case 9: item_soccer_bobs(); break;
  1536   case 10: item_bump(); break;
  1537   case 11: item_bouncing_bubble(); break;
  1538   case 12: item_virtual_landscape(); break;
  1539   case 13: item_plasma(); break;
  1540   case 14: item_oriented_convolutions(); break;
  1541   case 15: item_shade_bobs(); break;
  1542   case 16: item_fourier_filtering(); break;
  1543   case 17: item_image_zoomer(); break;
  1544   case 18: item_blobs_editor(); break;
  1545   case 19: item_double_torus(); break;
  1546   case 20: item_3d_metaballs(); break;
  1547   case 21: item_fireworks(); break;
  1548   case 22: item_rubber_logo(); break;
  1549   case 23: item_image_waves(); break;
  1550   case 24: item_breakout(); break;
  1551   case 25: item_3d_reflection(); break;
  1552   case 26: item_fisheye_magnification(); break;
  1553   case 27: item_word_puzzle(); break;
  1554   default: break;
  1558 /*---------------------------
  1560   Main procedure
  1562   --------------------------*/
  1563 int main(int argc, char **argv) {
  1565   // Display info about the CImg Library configuration
  1566   //--------------------------------------------------
  1567   unsigned int demo_number = cimg_option("-run",0,0);
  1568   if (demo_number) start_item(demo_number);
  1569   else {
  1570     cimg::info();
  1572     // Demo selection menu
  1573     //---------------------
  1574     const unsigned char
  1575       white[]  = { 255,255,255 }, black[] = { 0,0,0 },     red[] = { 120,50,80 },
  1576       yellow[] = { 200,155,0 },   green[] = { 30,200,70 }, purple[] = { 175,32,186 },
  1577       blue[]   = { 55,140,185 },  grey[] = { 127,127,127 };
  1578     float
  1579       rx = 0, ry = 0, t = 0, gamma = 0, vgamma = 0, T = 0.9f,
  1580       nrx = (float)(2*cimg::crand()),
  1581       nry = (float)(2*cimg::crand());
  1582     int y0 = 2*13;
  1583     CImg<unsigned char> back(1,2,1,3,10), fore, text, img;
  1584     back.fillV(0,1,0,10,10,235).resize(320,420,1,3,3).get_shared_channel(2).noise(10,1).draw_plasma();
  1585     back.draw_rectangle(0,y0-7,back.dimx()-1,y0+20,red);
  1586     fore.assign(back.dimx(),50,1,1,0).draw_text(20,y0-5,"** CImg %u.%u.%u Samples **",grey,0,1,22,
  1587                                                 cimg_version/100,(cimg_version/10)%10,cimg_version%10);
  1588     (fore+=fore.get_dilate(3).dilate(3)).resize(-100,-100,1,3);
  1589     cimg_forXY(fore,x,y)
  1590       if (fore(x,y)==127) fore(x,y,0) = fore(x,y,1) = fore(x,y,2) = 1;
  1591       else if (fore(x,y)) {
  1592         const float val = cimg::min(255.0f,7.0f*(y-3));
  1593         fore(x,y,0) = (unsigned char)(val/1.5f);
  1594         fore(x,y,1) = (unsigned char)val;
  1595         fore(x,y,2) = (unsigned char)(val/1.1f);
  1597     text.draw_text(1,1,
  1598                    "1- Blurring Gradient\n"
  1599                    "2- Rotozoom\n"
  1600                    "3- Anisotropic Smoothing\n"
  1601                    "4- Fractal Animation\n"
  1602                    "5- Gamma Correction\n"
  1603                    "6- Filled Triangles\n"
  1604                    "7- Mandelbrot explorer\n"
  1605                    "8- Mini-Paint\n"
  1606                    "9- Soccer Bobs\n"
  1607                    "10- Bump Effect\n"
  1608                    "11- Bouncing Bubble\n"
  1609                    "12- Virtual Landscape\n"
  1610                    "13- Plasma & Sinus Scroll\n"
  1611                    "14- Oriented Convolutions\n"
  1612                    "15- Shade Bobs\n"
  1613                    "16- Fourier Filtering\n"
  1614                    "17- Image Zoomer\n"
  1615                    "18- Blobs Editor\n"
  1616                    "19- Double Torus\n"
  1617                    "20- 3D Metaballs\n"
  1618                    "21- Fireworks\n"
  1619                    "22- Rubber Logo\n"
  1620                    "23- Image Waves\n"
  1621                    "24- Breakout\n"
  1622                    "25- 3D Reflection\n"
  1623                    "26- Fish-Eye Magnification\n"
  1624                    "27- Word Puzzle\n",
  1625                    white,0,1,13);
  1626     fore.resize(back,0).draw_image(20,y0+2*13,text|=text.get_dilate(3)>>4);
  1628     CImgDisplay disp(back,"CImg Library Samples",0,false,true);
  1629     disp.move((disp.screen_dimx()-disp.window_dimx())/2,(disp.screen_dimy()-disp.window_dimy())/2);
  1630     img = back; back*=0.15f;
  1631     for (y0+=2*13; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; demo_number = 0) {
  1632       while (!demo_number && !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
  1633         img*=0.85f; img+=back;
  1634         for (int i = 0; i<60; ++i) {
  1635           const float
  1636             mx = (float)(img.dimx()/2+(img.dimx()/2-30)*((1-gamma)*std::cos(3*t+rx*i*18.0f*cimg::valuePI/180) +
  1637                                                          gamma*std::cos(3*t+nrx*i*18.0f*cimg::valuePI/180))),
  1638             my = (float)(img.dimy()/2+(img.dimy()/2-30)*((1-gamma)*std::sin(4*t+ry*i*18.0f*cimg::valuePI/180) +
  1639                                                          gamma*std::sin(4*t+nry*i*18.0f*cimg::valuePI/180))),
  1640             mz = (float)(1.3f + 1.2f*((1-gamma)*std::sin(2*t+(rx+ry)*i*20*cimg::valuePI/180) +
  1641                                       gamma*std::sin(2*t+(nrx+nry)*i*20*cimg::valuePI/180)));
  1642           const int j = i%5;
  1643           img.draw_circle((int)mx,(int)my,(int)(10*mz),j!=0?(j!=1?(j!=2?(j!=3?green:red):yellow):purple):blue,0.2f).
  1644             draw_circle((int)(mx+4*mz),(int)(my-4),(int)(3*mz),white,0.1f).
  1645             draw_circle((int)mx,(int)my,(int)(10*mz),black,0.2f,~0U);
  1647         const unsigned char *ptrs = fore.end();
  1648         cimg_for(img,ptrd,unsigned char) { const unsigned char val = *(--ptrs); if (val) *ptrd = val; }
  1649         int y = disp.mouse_y;
  1650         if (y>=y0 && y<y0+27*13) {
  1651           y = (y/13)*13+7;
  1652           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)));
  1653           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);
  1655         gamma+=vgamma; if (gamma>1) { gamma = vgamma = 0; rx = nrx; ry = nry; nrx=(float)(2*cimg::crand()); nry=(float)(2*cimg::crand()); }
  1656         t+=0.006f; T+=0.005f; if (T>1) { T-=(float)(1+cimg::crand()); vgamma = 0.03f; }
  1657         if (disp.button) { disp.button = 0; demo_number = 1+(disp.mouse_y-y0)/13; }
  1658         disp.resize(disp,false).display(img).wait(25);
  1660       start_item(demo_number);
  1664   // Exit demo
  1665   //-----------
  1666   std::exit(0);
  1667   return 0;