/*
 #
 #  File        : CImg_demo.cpp
 #                ( C++ source file )
 #
 #  Description : A multi-part demo demonstrating some of the CImg capabilities.
 #                This file is a part of the CImg Library project.
 #                ( http://cimg.sourceforge.net )
 #
 #  Copyright   : David Tschumperle
 #                ( http://www.greyc.ensicaen.fr/~dtschump/ )
 #
 #  License     : CeCILL v2.0
 #                ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
 #
 #  This software is governed by the CeCILL  license under French law and
 #  abiding by the rules of distribution of free software.  You can  use,
 #  modify and/ or redistribute the software under the terms of the CeCILL
 #  license as circulated by CEA, CNRS and INRIA at the following URL
 #  "http://www.cecill.info".
 #
 #  As a counterpart to the access to the source code and rights to copy,
 #  modify and redistribute granted by the license, users are provided only
 #  with a limited warranty  and the software's author,  the holder of the
 #  economic rights,  and the successive licensors  have only  limited
 #  liability.
 #
 #  In this respect, the user's attention is drawn to the risks associated
 #  with loading,  using,  modifying and/or developing or reproducing the
 #  software by the user in light of its specific status of free software,
 #  that may mean  that it is complicated to manipulate,  and  that  also
 #  therefore means  that it is reserved for developers  and  experienced
 #  professionals having in-depth computer knowledge. Users are therefore
 #  encouraged to load and test the software's suitability as regards their
 #  requirements in conditions enabling the security of their systems and/or
 #  data to be ensured and,  more generally, to use and operate it in the
 #  same conditions as regards security.
 #
 #  The fact that you are presently reading this means that you have had
 #  knowledge of the CeCILL license and that you accept its terms.
 #
*/

// Include static image data, so that the exe does not depend on external image files.
#include "img/CImg_demo.h"

// Include CImg library header.
#include "CImg.h"
using namespace cimg_library;

// The lines below are necessary when using a non-standard compiler such as visualcpp6.
#ifdef cimg_use_visualcpp6
#define std
#endif
#ifdef min
#undef min
#undef max
#endif

// Item : Blurring Gradient
//----------------------------
void* item_blurring_gradient() {

  // Create color image 'milla'.
  const CImg<> src(data_milla,211,242,1,3);

  // Compute 2D gradient (spatial derivatives).
  CImgList<> grad = src.get_gradient();

  // Create visualization list with three images, the second one being
  // a normalized version of the gradient norm.
  CImgList<unsigned char> visu = src<<(grad[0].pow(2) + grad[1].pow(2)).sqrt().normalize(0,255)<<src;

  // Create and Pop-up display window.
  CImgDisplay disp(visu,"[#1] - Color Image, Gradient Norm and Blurring Gradient",0);

  // Start animation loop (until ESC or Q key is pressed, or display window closed).
  for (double sigma = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; sigma+=0.05) {

    // Compute blurred version of the gradient norm.
    visu[2] = visu[1].get_blur((float)cimg::abs(30*std::cos(sigma))).normalize(0,255);

    // Refresh display window.
    disp.resize(false).display(visu).wait(20);
  }
  return 0;
}

// Item : Rotozoom
//-----------------
void* item_rotozoom() {

  // Create color image 'milla' and resize it to 400x300.
  CImg<unsigned char> src = CImg<unsigned char>(data_milla,211,242,1,3,false).resize(400,300,1,3,3), img(src);

  // Create display window.
  CImgDisplay disp(img.dimx(),img.dimy(),"[#2] - Rotozoom",0);

  float alpha = 0, t = 0, angle = 0, zoom0 = -0.9f;
  const unsigned char color[] = { 16,32,64 };

  // Start animation loop.
  while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {

    // Add weird color effect on the image.
    cimg_forYV(src,y,k) {
      const int xc = 4*src.dimx() + (int)(60*std::sin((float)y*3/src.dimy()+10*t));
      cimg_forX(src,x) {
        const float val = (float)(src((xc+x)%src.dimx(),y,0,k)*
                                  (1.3f+0.20*std::sin(alpha+k*k*((float)src.dimx()/2-x)*
                                                      ((float)src.dimy()/2-y)*std::cos(t)/300.0)));
        img(x,y,0,k) = (unsigned char)(val>255.0f?255:val);
      }
    }

    // Rotate/zoom the resulting image, and display it.
    const float zoom = (float)(zoom0 + 0.3f*(1+std::cos(3*t)));
    img.get_rotate(angle,0.5f*img.dimx(),0.5f*img.dimy(),1+zoom,2,0).
      draw_text(3,3,"Mouse buttons\nto zoom in/out",color,0,0.8f,24).display(disp.resize(false).wait(20));

    // Smoothly move angle and zoom parameters
    alpha+=0.7f; t+=0.01f; angle+=0.8f;
    zoom0+=disp.button&1?0.1f:(disp.button&2?-0.1f:0);
    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(400,400,false).toggle_fullscreen(false);
  }
  return 0;
}

// Item : Anisotropic Smoothing (Total variation PDE, explicit scheme)
//--------------------------------------------------------------------
void* item_anisotropic_smoothing() {

  // Create color image 'milla' and noise it quite heavily with uniform noise.
  const CImg<> src = CImg<>(data_milla,211,242,1,3).noise(-30,1);

  // Create visualization list and corresponding display window.
  CImgList<> images(src,src);
  CImgDisplay disp(images,"[#3] - Anisotropic smoothing");
  const float white[] = { 255,255,255 }, black[] = { 0,0,0 };

  // Start PDE iterations
  for (unsigned int iter = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; ++iter) {

    // Compute PDE velocity field.
    CImg_3x3(I,float);
    CImg<> veloc(src);
    cimg_forV(src,k) cimg_for3x3(images[1],x,y,0,k,I) {
      const float
        ix = (Inc - Ipc)/2,
        iy = (Icn - Icp)/2,
        ng = (float)std::sqrt(1e-10f + ix*ix + iy*iy),
        ixx = Inc + Ipc - 2*Icc,
        iyy = Icn + Icp - 2*Icc,
        ixy = 0.25f*(Inn + Ipp - Ipn - Inp),
        iee = (ix*ix*iyy + iy*iy*ixx - 2*ix*iy*ixy)/(ng*ng);
      veloc(x,y,k) = iee/(0.1f+ng);
    }

    // Find adaptive time step and update current image.
    float m = 0, M = veloc.maxmin(m);
    veloc*=40.0f/cimg::max(cimg::abs(m),cimg::abs(M));
    images[1]+=veloc;
    images[0].draw_text(0,0,"iter %u",white,black,1,11,iter);

    // Refresh display window (and resize it if necessary).
    disp.resize(false).display(images);
  }
  return 0;
}

// Item : Fractal Animation
//--------------------------
void* item_fractal_animation() {

  // Create black 400x400 color image, and small 'noise' sprite.
  CImg<unsigned char> img(400,400,1,3,0), noise(3,2,1,3);

  // Create display window.
  CImgDisplay disp(img,"[#4] - Fractal Animation");

  // Start animation loop.
  float zoom = 0;
  for (unsigned int iter = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; ++iter, zoom+=0.2f) {

    // Put a noisy sprite on the center, then rotate and zoom the whole image, to make the 'fractal' effect.
    img.draw_image((img.dimx() - noise.dimx())/2,
                   (img.dimy() - noise.dimy())/2,
                   noise.fill(0).noise(255,1)).
      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).
      resize(disp.resize(false)).display(disp.wait(25));
    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(400,400,false).toggle_fullscreen(false);
  }
  return 0;
}

// Item : Gamma Correction and Histogram Visualization
//-----------------------------------------------------
void* item_gamma_correction() {

  // Create color image 'milla' and normalize it in [0,1].
  CImg<> img = CImg<>(data_milla,211,242,1,3).normalize(0,1);

  // Create visualization list (2 images).
  CImgList<unsigned char> visu(img*255.0, CImg<unsigned char>(512,300,1,3,0));

  // Define graph and text colors.
  const unsigned char
    yellow[] = { 255,255,0 }, blue[] = { 0,155,255 }, blue2[] = { 0,0,255 },
    blue3[] = { 0,0,155 }, white[] = { 255,255,255 }, green[] = { 100,255,100 };

  // Create display window.
  CImgDisplay disp(visu,"[#5] - Gamma Corrected Image and Histogram (Click to set Gamma)");

  // Enter event loop.
  for (double gamma = 1; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; ) {

    // Compute gamma-corrected version of the original image.
    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);

    // Compute corresponding image histogram.
    const CImg<> hist = visu[0].get_histogram(50,0,255);

    // Draw image histogram as a bar graph in the visualization list (2nd image).
    visu[1].fill(0).draw_text(50,5,"Gamma = %.3g",white,0,1,24,gamma).
      draw_graph(hist,green,1,3,0,20000,0).draw_graph(hist,yellow,1,2,0,20000,0).
      draw_axis(0,256,20000,0,white,0.7f);
    const int xb = (int)(50+gamma*150);
    visu[1].draw_grid(20,20,0,0,false,false,white,0.3f,0xCCCCCCCC,0xCCCCCCCC).
      draw_rectangle(51,31,xb-1,39,blue2).draw_rectangle(50,30,xb,30,blue).draw_rectangle(xb,30,xb,40,blue).
      draw_rectangle(xb,40,50,39,blue3).draw_rectangle(50,30,51,40,blue3);

    // Check for button press from the user's mouse.
    if (disp.button && disp.mouse_x>=img.dimx()+50 && disp.mouse_x<=img.dimx()+450)
      gamma = (disp.mouse_x - img.dimx()-50)/150.0;

    // Refresh display window, and wait for a user event.
    disp.resize(disp,false).display(visu).wait();
  }
  return 0;
}

// Item : Filled Triangles
//-------------------------
void* item_filled_triangles() {

  // Create a colored 640x480 background image which consists of different color shades.
  CImg<> background(640,480,1,3);
  cimg_forXY(background,x,y) background.fillV(x,y,0,
                                              x*std::cos(6.0*y/background.dimy())+y*std::sin(9.0*x/background.dimx()),
                                              x*std::sin(8.0*y/background.dimy())-y*std::cos(11.0*x/background.dimx()),
                                              x*std::cos(13.0*y/background.dimy())-y*std::sin(8.0*x/background.dimx()));
  background.normalize(0,180);

  // Init images and create display window.
  CImg<unsigned char> img0(background), img;
  unsigned char white[] = { 255,255,255 }, color[100][3];
  CImgDisplay disp(img0,"[#6] - Filled Triangles (Click to shrink)");

  // Define random properties (pos, size, colors, ..) for all triangles that will be displayed.
  float posx[100], posy[100], rayon[100], angle[100], veloc[100], opacity[100];
  int num = 1;
  std::srand((unsigned int)time(NULL));
  for (int k = 0; k<100; ++k) {
    posx[k]  = (float)(cimg::rand()*img0.dimx());
    posy[k]  = (float)(cimg::rand()*img0.dimy());
    rayon[k] = (float)(10+cimg::rand()*50);
    angle[k] = (float)(cimg::rand()*360);
    veloc[k] = (float)(cimg::rand()*20-10);
    color[k][0] = (unsigned char)(cimg::rand()*255);
    color[k][1] = (unsigned char)(cimg::rand()*255);
    color[k][2] = (unsigned char)(cimg::rand()*255);
    opacity[k] = (float)(0.3+1.5*cimg::rand());
  }

  // Start animation loop.
  while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
    img = img0;

    // Draw each triangle on the background image.
    for (int k = 0; k<num; ++k) {
      const int
        x0 = (int)(posx[k] + rayon[k]*std::cos(angle[k]*cimg::valuePI/180)),
        y0 = (int)(posy[k] + rayon[k]*std::sin(angle[k]*cimg::valuePI/180)),
        x1 = (int)(posx[k] + rayon[k]*std::cos((angle[k]+120)*cimg::valuePI/180)),
        y1 = (int)(posy[k] + rayon[k]*std::sin((angle[k]+120)*cimg::valuePI/180)),
        x2 = (int)(posx[k] + rayon[k]*std::cos((angle[k]+240)*cimg::valuePI/180)),
        y2 = (int)(posy[k] + rayon[k]*std::sin((angle[k]+240)*cimg::valuePI/180));
      if (k%10) img.draw_triangle(x0,y0,x1,y1,x2,y2,color[k],opacity[k]);
      else img.draw_triangle(x0,y0,x1,y1,x2,y2,img0,0,0,img0.dimx()-1,0,0,img.dimy()-1,opacity[k]);
      img.draw_triangle(x0,y0,x1,y1,x2,y2,white,opacity[k],~0U);

      // Make the triangles rotate, and check for mouse click event.
      // (to make triangles collapse or join).
      angle[k]+=veloc[k];
      if (disp.mouse_x>0 && disp.mouse_y>0) {
        float u = disp.mouse_x - posx[k], v = disp.mouse_y - posy[k];
        if (disp.button) { u=-u; v=-v; }
        posx[k]-=0.03f*u, posy[k]-=0.03f*v;
        if (posx[k]<0 || posx[k]>=img.dimx()) posx[k]=(float)(cimg::rand()*img.dimx());
        if (posy[k]<0 || posy[k]>=img.dimy()) posy[k]=(float)(cimg::rand()*img.dimy());
      }
    }

    // Display current animation framerate, and refresh display window.
    img.draw_text(5,5,"%u frames/s",white,0,0.5f,11,(unsigned int)disp.frames_per_second());
    img0.resize(disp.display(img).resize(false).wait(20));
    if (++num>100) num = 100;

    // Allow the user to toggle fullscreen mode, by pressing CTRL+F.
    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(640,480,false).toggle_fullscreen(false);
  }
  return 0;
}

// Item : Mandelbrot/Julia Explorer
//----------------------------------
void* item_mandelbrot_explorer() {

  // Define image canvas and corresponding display window.
  CImg<unsigned char> img(800,600,1,3,0);
  CImgDisplay disp(img);

  // Start main explorer loop.
  double juliar = 0, juliai = 0;
  for (bool endflag = false, fractal_type = false, smooth = false, show_help = true; !endflag;) {
    bool stopflag = false;
    double xmin, xmax, ymin, ymax;

    // Init default upper-left/lower-right coordinates of the fractal set.
    if (fractal_type) { xmin = -1.5; xmax = 1.5; ymin = -1.5; ymax = 1.5; juliar = 0.317; juliai = 0.029; }
    else { xmin = -2.25; xmax = 1.0; ymin = -1.5; ymax = 1.5; juliar = juliai = 0; }

    // Create random palette for displaying the fractal set.
    const CImg<unsigned char> palette =
      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);
    unsigned int maxiter = 64;

    // Enter event loop for the current fractal set.
    while (!stopflag) {

      // Draw Mandelbrot or Julia fractal set on the image.
      img.resize(disp.resize().set_title("[#7] - %s Set : (%g,%g)-(%g,%g), %s = (%g,%g) (%u iter.)",
                                         fractal_type?"Julia":"Mandelbrot",xmin,ymin,xmax,ymax,
                                         fractal_type?"c":"z0",juliar,juliai,maxiter)).
        fill(0).draw_mandelbrot(palette,1,xmin,ymin,xmax,ymax,maxiter,smooth,fractal_type,juliar,juliai);

      // Display help if necessary.
      if (show_help) {
        const unsigned char white[] = { 255,255,255 };
        static CImg<unsigned char>
          help = CImg<unsigned char>().draw_text(0,0,"\n"
                                                 " Use mouse to zoom on desired region. \n"
                                                 " H             Show/Hide help \n"
                                                 " PAD 1...9       Fractal navigation \n"
                                                 " PAD +/-       Zoom/Unzoom \n"
                                                 " SPACE         Set/Disable color smoothing \n"
                                                 " ENTER         Switch Mandelbrot/Julia sets \n"
                                                 " Arrows        Change set parameterization \n"
                                                 " Page UP/DOWN  Add/Reduce iteration numbers \n\n",
                                                 white);
        help.draw_rectangle(2,2,help.dimx()-3,help.dimy()-3,white,1,~0U);
        img.draw_image(img.dimx()-help.dimx(),help,0.7f);
      }

      // Get rectangular shape from the user to define the zoomed region.
      const CImg<int> selection = img.get_select(disp,2,0);
      const int xs0 = selection[0], ys0 = selection[1], xs1 = selection[3], ys1 = selection[4];

      // If the user has selected a region with the mouse, then zoom-in !
      if (xs0>=0 && ys0>=0 && xs1>=0 && ys1>=0) {
        const double dx =(xmax-xmin)/img.dimx(), dy =(ymax-ymin)/img.dimy();
        const int dsmax = (ys1-ys0)/2, xs = (xs0+xs1)/2, ys = (ys0+ys1)/2;

        // If the region is too small (point) then reset the fractal set position and zoom.
        if (dsmax<5) stopflag = true;
        xmin += (xs-dsmax*dy/dx)*dx;
        ymin += (ys-dsmax)*dy;
        xmax -= (img.dimx()-xs-dsmax*dy/dx)*dx;
        ymax -= (img.dimy()-ys-dsmax)*dy;
      }

      // Also, test if a key has been pressed.
      // (moving in the fractal set can be done, using keyboard).
      switch (disp.key) {

        // Show/hide help.
      case cimg::keyH: show_help = !show_help; break;

        // Switch between Julia/Mandelbrot sets.
      case cimg::keyENTER: fractal_type = !fractal_type; stopflag = true; break;

        // Enable/disable smoothed colors.
      case cimg::keySPACE: smooth = !smooth; break;

        // Change fractal set parameters.
      case cimg::keyARROWLEFT: juliar-=fractal_type?0.001f:0.05f; break;
      case cimg::keyARROWRIGHT: juliar+=fractal_type?0.001f:0.05f; break;
      case cimg::keyARROWUP: juliai+=fractal_type?0.001f:0.05f; break;
      case cimg::keyARROWDOWN: juliai-=fractal_type?0.001f:0.05f; break;

        // Add/remove iterations.
      case cimg::keyPAGEDOWN: maxiter-=32; break;
      case cimg::keyPAGEUP: maxiter+=16; break;

        // Move left, right, up and down in the fractal set.
      case cimg::keyPAD4: { const double delta = (xmax-xmin)/10; xmin-=delta; xmax-=delta; } break;
      case cimg::keyPAD6: { const double delta = (xmax-xmin)/10; xmin+=delta; xmax+=delta; } break;
      case cimg::keyPAD8: { const double delta = (ymax-ymin)/10; ymin-=delta; ymax-=delta; } break;
      case cimg::keyPAD2: { const double delta = (ymax-ymin)/10; ymin+=delta; ymax+=delta; } break;

        // Allow to zoom in/out in the fractal set.
      case cimg::keyPADADD: {
        const double xc = 0.5*(xmin+xmax), yc = 0.5*(ymin+ymax), dx = (xmax-xmin)*0.85/2, dy = (ymax-ymin)*0.85/2;
        xmin = xc - dx; ymin = yc - dy; xmax = xc + dx; ymax = yc + dy;
      } break;
      case cimg::keyPADSUB:
        const double xc = 0.5*(xmin+xmax), yc = 0.5*(ymin+ymax), dx = (xmax-xmin)*1.15/2, dy = (ymax-ymin)*1.15/2;
        xmin = xc - dx; ymin = yc - dy; xmax = xc + dx; ymax = yc + dy;
        break;
      }

      // Do a simple test to check if more/less iterations are necessary for the next step.
      const float value = img.get_pointwise_norm().get_histogram(256,0,255)(0)*3;
      if (value>img.size()/6.0f) maxiter+=16;
      if (maxiter>1024) maxiter = 1024;
      if (value<img.size()/10.0f) maxiter-=4;
      if (maxiter<32) maxiter = 32;

      // Check if the user want to quit the explorer.
      if (disp.is_closed || disp.key==cimg::keyQ || disp.key==cimg::keyESC) stopflag = endflag = true;
    }
  }
  return 0;
}

// Item : Mini-Paint
//------------------
void* item_mini_paint() {
  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[] = { 255,255,255 };
  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);
  CImgDisplay disp(img.draw_text(5,5,"   ",color,color),"[#8] - Mini-Paint");
  while (!disp.is_closed && !disp.is_keyQ && !disp.is_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;
          const int radius = (but&1?3:0) + (but&4?6:0);
          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); }
  }
  return 0;
}

// Item : Soccer Bobs
//-------------------
void* item_soccer_bobs() {
  CImg<unsigned char> foot(data_foot,200,200,1,3,false), canvas0(640,480,1,3,0);
  const unsigned char color[] = { 255,255,0 };
  float zoom = 0.2f;
  cimg_forXY(canvas0,x,y) canvas0(x,y,1) = (unsigned char)(20+(y*215/canvas0.dimy()) + cimg::crand()*19);
  canvas0.draw_text(5,5,"Left/Right Mouse Button = Zoom In/Out\nMiddle Button = Reset Screen",color);
  CImgList<unsigned char> canvas(16, 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.8f; }
  CImgDisplay disp(canvas0,"[#9] - Unlimited Soccer Bobs");
  for (unsigned int curr_canvas=0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; (++curr_canvas)%=16) {
    if (disp.mouse_x>=0 && disp.mouse_y>=0)
      canvas[curr_canvas].draw_image((int)(disp.mouse_x - zoom*foot.dimx()/2),
                                     (int)(disp.mouse_y - zoom*foot.dimy()/2),
                                     foot.get_resize((int)(foot.dimx()*zoom),(int)(foot.dimy()*zoom)),
                                     mask.get_resize((int)(foot.dimx()*zoom),(int)(foot.dimy()*zoom)));
    zoom+=disp.button&1?0.03f:(disp.button&2?-0.03f:0);
    zoom = zoom<0.1f?0.1f:(zoom>1?1.0f:zoom);
    if (disp.button&4) cimglist_for(canvas,l) canvas[l] = canvas0;
    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.toggle_fullscreen(false);
    disp.display(canvas[curr_canvas]).resize(disp,false).wait(20);
  }
  return 0;
}

// Item : Bump Effect
//--------------------
void* item_bump() {
  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);
  logo += CImg<>(logo.dimx(),logo.dimy(),1,1,0).noise(80,1).deriche(2,0,'y',false).deriche(10,0,'x',false);
  CImgList<> grad = logo.get_gradient();
  cimglist_apply(grad,normalize)(-140,140);
  logo.normalize(0,255);
  CImg<> light = CImg<>(300+2*logo.dimx(),300+2*logo.dimy());
  light.draw_gaussian(0.5f*light.dimx(),0.5f*light.dimy(),80,CImg<>::vector(255));
  CImg<unsigned char> img(logo.dimx(),logo.dimy(),1,3,0);
  CImgDisplay disp(img,"[#10] - Bump Effect (Move lightsource with mouse)");
  for (float t = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; t+=0.03f) {
    const int
      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),
      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);
    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(0,0,0,1,logo,0.1f)).wait(25);
    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(640,480,false).toggle_fullscreen(false);
  }
  return 0;
}

// Item : Bouncing Bubble
//------------------------
void* item_bouncing_bubble() {
  CImg<unsigned char> back(420,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,"[#11] - Bouncing bubble");
  const unsigned char col1[] = { 40,100,10 }, col2[] = { 20,70,0 }, col3[] = { 40,150,10 },
                      col4[] = { 200,255,100 }, white[] = { 255,255,255 };
  float u = (float)std::sqrt(2.0f),  cx = back.dimx()/2.0f, t = 0, vt = 0.05f, vx = 2;
  while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
    img = back;
    int xm = (int)cx, ym = (int)(img.dimy()/2-70 + (img.dimy()/2+10)* (1-cimg::abs(std::cos((t+=vt)))));
    float r1 = 50, r2 = 50;
    vt = 0.05f;
    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.05f - 0.0015f*(50-r2); }
    if (ym-r2<0)              { const float delta = -(ym-r2); r2-=delta; r1+=delta; }
    img.draw_ellipse(xm,ym,r1,r2,0,col1).
      draw_ellipse((int)(xm+0.03*r1*u),(int)(ym-0.03*r2*u),0.85f*r1,0.85f*r2,0,col2).
      draw_ellipse((int)(xm+0.1*r1*u),(int)(ym-0.1*r2*u),0.8f*r1,0.8f*r2,0,col1).
      draw_ellipse((int)(xm+0.2*r1*u),(int)(ym-0.2*r2*u),r1/2,r2/2,0,col3).
      draw_ellipse((int)(xm+0.3*r1*u),(int)(ym-0.3*r2*u),r1/4,r2/4,0,col4).
      draw_image(0,img.dimy()-40,img.get_crop(0,img.dimy()-80,img.dimx()-1,img.dimy()-40).mirror('y'),0.45f).
      draw_text(xm-60,(int)(ym-r2-25),"Bubble (%d,%d)",white,0,1,17,xm,ym);
    if ((cx+=20*vt*vx)>=img.dimx()-30 || cx<30) vx = -vx;
    disp.display(img).wait(20);
    if (disp.is_resized) {
      back.resize(disp.resize(disp.window_dimx()>200?disp.window_dimx():200,disp.dimy(),false));
      cx = back.dimx()/2.0f;
    }
  }
  return 0;
}

// Item : Virtual Landscape
//--------------------------
void* item_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[] = { 255,255,255 };
  CImgDisplay disp(visu.draw_text(10,10,"Please wait, generating landscape...",white).
                   normalize(0,255),"[#12] - Virtual Landscape",0);
  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());
  CImg_3x3(I,float); Ipp = Inp = Icc = Ipn = Inn = 0;
  { 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.is_keyQ && !disp.is_keyESC; t+=0.0025f) {
    visu = background;
    const int
      xm = (int)(map.dimx()/2 + (map.dimx()/3)*std::cos(4.2f*t)),
      ym = (int)(map.dimy()/2 + (map.dimy()/3)*std::sin(5.6f*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*std::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_atX(mx,z)/(1+0.02*z));
        const float cc = (float)scmap.linear_atX(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,"%u frames/s",white,0,0.5f,11,(unsigned int)disp.frames_per_second());
    disp.resize(false).display(visu.cut(0,255)).wait(25);
    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(400,300,false).toggle_fullscreen(false);
  }
  return 0;
}

// Item : Plasma Effect with Sinus Scrolling.
//-------------------------------------------
void* item_plasma() {
  CImg<> plasma, camp(3), cfreq(3), namp(3), nfreq(3);
  CImgList<unsigned char> font = CImgList<unsigned char>::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 = "   * The CImg Library : C++ Template Image Processing Toolkit *";
  CImgDisplay disp(visu,"[#13] - Plasma Effect");
  const unsigned char white[] = { 255, 255, 255 };
  unsigned int cplasma = 0, pos = 0, tpos = 0, lwidth = 0;
  float tx = 0, ts = 0, alpha = 2, beta = 0;
  namp.fill(0).noise(visu.dimy()/4,0);
  nfreq.fill(0).noise(0.1);

  visu.draw_text(10,10,"Please wait, generating plasma...",white).display(disp);
  const unsigned int nb_plasmas = 5;
  plasma.assign(5*visu.dimx()/3,visu.dimy(),1,nb_plasmas,0).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.is_keyQ && !disp.is_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+std::sin(tx+cfreq(0)*y))),y,v0),
        *pG1 = plasma.ptr((unsigned int)(camp(1)*(1+std::sin(tx+cfreq(1)*y))),y,v1),
        *pB1 = plasma.ptr((unsigned int)(camp(2)*(2+std::sin(tx+cfreq(2)*y))),y,v2),
        *pR2 = plasma.ptr((unsigned int)(namp(0)*(1+std::sin(tx+nfreq(0)*y))),y,v1),
        *pG2 = plasma.ptr((unsigned int)(namp(1)*(1+std::sin(tx+nfreq(1)*y))),y,v2),
        *pB2 = plasma.ptr((unsigned int)(namp(2)*(2+std::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(visu.dimx(),letter);
      (++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*std::sin(ts+x/(70+30*std::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_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(640,480,false).toggle_fullscreen(false);
  }
  return 0;
}

// Item : Oriented Convolutions
//------------------------------
void* item_oriented_convolutions() {
  const CImg<unsigned char> img = CImg<unsigned char>(data_lena,256,256,1,1,false).noise(50,2);
  CImgList<unsigned char> visu = img<<img<<img;
  CImg<float> mask(16,16);
  const float value = 255;
  CImgDisplay disp(visu,"[#14] - Original image, Oriented kernel and Convolved image");
  for (float angle = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; angle+=0.1f) {
    const float ca = (float)std::cos(angle), sa = (float)std::sin(angle);
    const CImg<> u = CImg<>::vector(ca,sa), v = CImg<>::vector(-sa,ca),
      tensor = 30.0*u*u.get_transpose() + 2.0*v*v.get_transpose();
    mask.draw_gaussian(0.5f*mask.dimx(),0.5f*mask.dimy(),tensor,&value);
    mask/=mask.sum();
    visu[1] = mask.get_resize(img).normalize(0,255).
      draw_text(2,2,"Angle = %d deg",&value,0,1,11,cimg::mod((int)(angle*180/cimg::valuePI),360));
    visu[2] = img.get_convolve(mask);
    disp.resize(disp.window_dimx(),(int)(disp.dimy()*disp.window_dimx()/disp.dimx()),false).
      display(visu).wait(25);
  }
  return 0;
}

// Item : Shade Bobs
//-------------------
void* item_shade_bobs() {
  float t = 100, rx = 0, ry = 0, rz = 0, rt = 0, rcx = 0;
  CImg<unsigned char> img(512,512,1,1,0), palette;
  CImgDisplay disp(img,"[#15] - Shade Bobs");
  const unsigned char one = 1;
  int nbbobs = 0, rybobs = 0;
  while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
    if ((t+=0.015f)>4*cimg::valuePI) {
      img.fill(0);
      rx = (float)(cimg::crand());
      ry = (float)(cimg::crand());
      rz = (float)(cimg::crand());
      rt = (float)(cimg::crand());
      rcx = 0.6f*(float)(cimg::crand());
      t = 0;
      palette = CImg<unsigned char>(3,4+(int)(12*cimg::rand()),1,1,0).noise(255,2).resize(3,256,1,1,3);
      palette(0) = palette(1) = palette(2) = 0;
      nbbobs = 20+(int)(cimg::rand()*80);
      rybobs = (10+(int)(cimg::rand()*50))*cimg::min(img.dimx(),img.dimy())/300;
      disp.key = disp.button = 0;
    }
    for (int i = 0; i<nbbobs; ++i) {
      const float
        r = (float)(ry + rx*std::cos(6*rz*t) + (1-rx)*std::sin(6*rt*t)),
        a = (float)((360*std::sin(rz*t)+30*ry*i)*cimg::valuePI/180),
        ax = (float)(i*2*cimg::valuePI/nbbobs+t);
      const int
        cx = (int)((1+rcx*std::cos(ax)+r*std::cos(a))*img.dimx()/2),
        cy = (int)((1+rcx*std::sin(ax)+r*std::sin(a))*img.dimy()/2);
      img.draw_circle(cx,cy,rybobs,&one,-1.0f);
    }
    CImg_3x3(I,unsigned char); Ipp = Inp = Ipn = Inn = 0;
    CImg<unsigned char> tmp(img);
    cimg_for3x3(tmp,x,y,0,0,I) img(x,y) = (Inc+Ipc+Icn+Icp+(Icc<<2))>>3;
    CImg<unsigned char> visu(img.dimx(),img.dimy(),1,3);
    cimg_forXY(visu,xx,yy) {
      const unsigned char *col = palette.ptr(0,img(xx,yy));
      visu(xx,yy,0) = *(col++);
      visu(xx,yy,1) = *(col++);
      visu(xx,yy,2) = *(col++);
    }
    disp.display(visu).wait(25);
    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(640,480,false).toggle_fullscreen(false);
    if (disp.is_resized) img.resize(disp.resize(false),3);
    if ((disp.key && disp.key!=cimg::keyCTRLLEFT) || disp.button) t=70;
  }
  return 0;
}

// Item : Fourier Filtering
//-------------------------
void* item_fourier_filtering() {
  const CImg<unsigned char> img = CImg<unsigned char>(data_lena,256,256,1,1,false).resize(256,256);
  CImgList<> F = img.get_FFT();
  cimglist_apply(F,translate)(img.dimx()/2,img.dimy()/2,0,0,2);
  const CImg<unsigned char> mag = ((F[0].get_pow(2) + F[1].get_pow(2)).sqrt()+1.0f).log().normalize(0,255);
  CImgList<unsigned char> visu(img,mag);
  CImgDisplay disp(visu,"[#16] - Fourier Filtering (Click to set filter)");
  CImg<unsigned char> mask(img.dimx(),img.dimy(),1,1,1);
  unsigned char one[] = { 1 }, zero[] = { 0 }, white[] = { 255 };
  int rmin = 0, rmax = 256;
  while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
    disp.wait();
    const int
      xm = disp.mouse_x*2*img.dimx()/disp.dimx()-img.dimx(),
      ym = disp.mouse_y*img.dimy()/disp.dimy(),
      x = xm-img.dimx()/2,
      y = ym-img.dimy()/2;
    if (disp.button && xm>=0 && ym>=0) {
      const int r = (int)cimg::max(0.0f,(float)std::sqrt((float)x*x+y*y)-3.0f);
      if (disp.button&1) rmax = r;
      if (disp.button&2) rmin = r;
      if (rmin>=rmax) rmin = cimg::max(rmax-1,0);
      mask.fill(0).draw_circle(mag.dimx()/2,mag.dimy()/2,rmax,one).
        draw_circle(mag.dimx()/2,mag.dimy()/2,rmin,zero);
      CImgList<> nF(F);
      cimglist_for(F,l) nF[l].mul(mask).translate(-img.dimx()/2,-img.dimy()/2,0,0,2);
      visu[0] = nF.FFT(true)[0].normalize(0,255);
    }
    if (disp.is_resized) disp.resize(disp.window_dimx(),disp.window_dimx()/2).display(visu);
    visu[1] = mag.get_mul(mask).draw_text(5,5,"Freq Min/Max = %d / %d",white,zero,0.6f,11,(int)rmin,(int)rmax);
    visu.display(disp);
  }
  return 0;
}

// Item : Image Zoomer
//---------------------
void* item_image_zoomer() {
  const CImg<unsigned char> img = CImg<unsigned char>(data_logo,555,103,1,3,false);
  CImgDisplay disp(img,"[#17] - Original Image"), dispz(500,500,"[#17] - Zoomed Image",0);
  disp.move((CImgDisplay::screen_dimx()-dispz.dimx())/2,(CImgDisplay::screen_dimy()-dispz.dimy()-disp.dimy())/2);
  dispz.move(disp.window_posx(),disp.window_posy() + disp.window_dimy() + 40);
  int factor = 20, x = 0, y = 0;
  bool grid = false, redraw = false;
  while (!disp.is_closed && !dispz.is_closed && !disp.is_keyQ && !dispz.is_keyQ && !disp.is_keyESC && !dispz.is_keyESC ) {
    if (disp.mouse_x>=0) { x = disp.mouse_x; y = disp.mouse_y; redraw = true; }
    if (redraw) {
      const int
        x0 = x-factor, y0 = y-factor,
        x1 = x+factor, y1 = y+factor;
      const unsigned char red[] = { 255,0,0 }, black[] = { 0,0,0 }, white[] = { 255,255,255 };
      (+img).draw_rectangle(x0,y0,x1,y1,red,1.0f,~0U).display(disp);
      CImg<unsigned char> visu = img.get_crop(x0,y0,x1,y1).draw_point(x-x0,y-y0,red,0.2f).resize(dispz);
      if (grid) {
        const int bfac = 2*factor+1;
        for (int i = 0; i<bfac; ++i) {
          const int X = i*dispz.dimx()/bfac, Y = i*dispz.dimy()/bfac;
          visu.draw_line(X,0,X,dispz.dimy()-1,black).draw_line(0,Y,dispz.dimx()-1,Y,black);
        }
      }
      visu.draw_text(2,2,"Coords (%d,%d)",white,0,1,11,x,y).display(dispz);
    }
    if (disp.button&1) { factor=(int)(factor/1.5f); if (factor<3) factor = 3; disp.button=0; redraw = true; }
    if (disp.button&2) { factor=(int)(factor*1.5f); if (factor>100) factor = 100; disp.button=0; redraw = true; }
    if (disp.button&4 || dispz.button) { grid = !grid; disp.button = dispz.button = 0; redraw = true; }
    if (disp.is_resized) disp.resize(disp);
    if (dispz.is_resized) { dispz.resize(); redraw = true; }
    CImgDisplay::wait(disp,dispz);
  }
  return 0;
}

// Item : Blobs Editor
//--------------------
void* item_blobs_editor() {
  CImg<unsigned int> img(300,300,1,3);
  CImgList<unsigned int> colors;
  CImgList<> blobs;
  CImgDisplay disp(img,"[#18] - Blobs Editor",0);
  bool moving = false;
  unsigned int white[] = { 255,255,255 };

  for (float alpha = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; alpha+=0.1f) {
    const int xm = disp.mouse_x*img.dimx()/disp.dimx(), ym = disp.mouse_y*img.dimy()/disp.dimy();
    int selected = -1;
    img.fill(0);

    if (blobs) {
      float dist = 0, dist_min = (float)img.dimx()*img.dimx() + img.dimy()*img.dimy();
      cimglist_for(blobs,l) {
        const CImg<>& blob = blobs[l];
        const float
          xb = blob[0], yb = blob[1], rb = blob[2],
          sigma = (float)(rb*(1+0.05f*std::cos(blob[3]*alpha))),
          sigma2 = 2*sigma*sigma, precision = 4.5f*sigma2;
        const int tx0 = (int)(xb-3*sigma), ty0 = (int)(yb-3*sigma), tx1 = (int)(xb+3*sigma), ty1 = (int)(yb+3*sigma);
        const unsigned int
          col1 = colors[l](0), col2 = colors[l](1), col3 = colors[l](2), wh = img.dimx()*img.dimy(),
          x0 = tx0<0?0:tx0, y0 = ty0<0?0:ty0,
          x1 = tx1>=img.dimx()?(img.dimx()-1):tx1, y1 = ty1>=img.dimy()?(img.dimy()-1):ty1;
        float dy = y0-yb;
        unsigned int *ptr = img.ptr(x0,y0);
        for (unsigned int y = y0; y<=y1; ++y) {
          float dx = x0-xb;
          for (unsigned int x = x0; x<=x1; ++x) {
            float dist = dx*dx + dy*dy;
            if (dist<precision) {
              const float val = (float)exp(-dist/sigma2);
              *ptr += (unsigned int)(val*col1);
              *(ptr+wh) += (unsigned int)(val*col2);
              *(ptr+2*wh) += (unsigned int)(val*col3);
            }
            ++dx; ++ptr;
          }
          ptr+=img.dimx()-(x1-x0)-1;
          ++dy;
        }
        if ((dist=(xb-xm)*(xb-xm)+(yb-ym)*(yb-ym))<dist_min) { dist_min = dist; selected = l; }
      }

      for (unsigned int *ptr1 = img.ptr(0,0,0,1), *ptr2 = img.ptr(0,0,0,2), *ptr3 = img.ptr(img.size()-1)+1,
             off = 0, wh = img.dimx()*img.dimy(); ptr1>img.data; ++off) {
        unsigned int val1 = *(--ptr1), val2 = *(--ptr2), val3 = *(--ptr3);
        const unsigned int pot = val1*val1 + val2*val2 + val3*val3;
        if (pot<128*128) { *ptr1=*ptr3=255*off/wh; *ptr2=180*off/wh; }
        else {
          if (pot<140*140) { *ptr1>>=1; *ptr2>>=1; *ptr3>>=1; }
          else {
            *ptr1 = val1<255?val1:255;
            *ptr2 = val2<255?val2:255;
            *ptr3 = val3<255?val3:255;
          }
        }
      }
      cimglist_for(blobs,ll) {
        const CImg<>& blob = blobs[ll];
        const int rb = (int)(blob[2]*(1+0.05f*std::cos(blob[3]*alpha))),
          xb = (int)(blob[0]+rb/2.5f), yb = (int)(blob[1]-rb/2.5f);
        img.draw_circle(xb,yb,rb>>1,white,0.2f).draw_circle(xb,yb,rb/3,white,0.2f).
          draw_circle(xb,yb,rb/5,white,0.2f);
      }
    } else {
      CImg<unsigned int> text;
      text.draw_text(0,0,
                     "CImg Blobs Editor\n"
                     "-----------------\n\n"
                     "* Left mouse button :\n   Create and Move Blob.\n\n"
                     "* Right mouse button :\n  Remove nearest Blob.\n\n"
                     "* Colors and size of Appearing Blobs\n"
                     "  are randomly chosen.\n\n\n"
                     " >> Press mouse button to start ! <<",
                     white);
      img.fill(100).draw_image((img.dimx()-text.dimx())/2,
                               (img.dimy()-text.dimy())/2,
                               text,text,1,255U);
    }

    if (disp.mouse_x>=0 && disp.mouse_y>=0) {
      if (disp.button&1) {
        float dist_selected = 0;
        if (selected>=0) {
          const float a = xm-blobs[selected](0), b = ym-blobs[selected](1), c = blobs[selected](2);
          dist_selected = a*a+b*b-c*c;
        }
        if (moving || dist_selected<0) { blobs[selected](0) = (float)xm; blobs[selected](1) = (float)ym; }
        else {
          blobs.insert(CImg<>::vector((float)xm,(float)ym,(float)(10+30*cimg::rand()),(float)(3*cimg::rand())));
          colors.insert(CImg<>(3).fill(0).noise(255,1).normalize(0,255));
        }
        moving = true;
      } else moving = false;
      if (selected>=0 && disp.button&2) { blobs.remove(selected); colors.remove(selected); disp.button = 0; }
    }

    img.display(disp.wait(25));
    if (disp.is_resized) {
      img.resize(disp.resize(false));
      cimglist_for(blobs,l) if (blobs[l](0)>=img.dimx() || blobs[l](1)>=img.dimy()) { blobs.remove(l); colors.remove(l--); }
    }
  }
  return 0;
}

// Item : Double Torus
//---------------------
void* item_double_torus() {
  CImg<unsigned char> visu(300,256,1,3,0);
  CImgDisplay disp(300,256,"[#19] - Double 3D Torus");
  CImgList<unsigned int> primitives;
  CImg<> points = CImg<>::torus3d(primitives,60,20), points2 = CImg<>::rotation_matrix(1,0,0,(float)cimg::valuePI/2.0f)*points;
  CImgList<> opacities = CImgList<>(primitives.size,1,1,1,1,1).insert(CImgList<>(primitives.size,1,1,1,1,0.4f));
  CImgList<unsigned char> colors(2*primitives.size,CImg<unsigned char>(1,3,1,1,255,255,0));
  cimglist_for(primitives,ll) colors[ll++].fill(100,255,100);
  cimglist_for(primitives,l) if (l%2) colors[primitives.size+l].fill(255,200,255); else colors[primitives.size+l].fill(200,150,255);
  points.translate_object3d(-30,0,0).append_object3d(primitives,points2.translate_object3d(30,0,0),primitives);
  float alpha = 0, beta = 0, gamma = 0, theta = 0;
  while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
    visu.get_shared_channels(1,2).fill(0);
    visu.get_shared_line(visu.dimy()-1,0,0).noise(200,1);
    CImg_3x3(I,unsigned char); Ipp = Icp = Inp = Ipc = Inc = 0;
    cimg_for3x3(visu,x,y,0,0,I) visu(x,y,0) = (Icc+Ipn+Icn+Inn)>>2;
    { for (unsigned int y = 0; y<100; ++y) std::memset(visu.ptr(0,y,0,2),255-y*255/100,visu.dimx()); }
    const CImg<>
      rpoints = CImg<>::rotation_matrix(1,1,0,(alpha+=0.01f))*CImg<>::rotation_matrix(1,0,1,(beta-=0.02f))*
      CImg<>::rotation_matrix(0,1,1,(gamma+=0.03f))*points;
    if (disp.is_resized) disp.resize(false);
    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(300,256,false).toggle_fullscreen(false);
    visu.draw_object3d(visu.dimx()/2.0f,visu.dimy()/2.0f,0,
                       rpoints,primitives,colors,opacities,4,
                       true,500.0f,(float)(std::cos(theta+=0.01f)+1)*visu.dimx()/2.0f,
                       (float)visu.dimy(),-100.0f,0.1f,1.5f).
      display(disp.wait(25));
  }
  return 0;
}

// Item : 3D Metaballs
//---------------------
struct metaballs3d {
  float cx1, cy1, cz1, cx2, cy2, cz2, cx3, cy3, cz3;
  inline float operator()(const float x, const float y, const float z) const {
    const float
      x1 = x - cx1, y1 = y - cy1, z1 = z - cz1,
      x2 = x - cx2, y2 = y - cy2, z2 = z - cz2,
      x3 = x - cx3, y3 = y - cy3, z3 = z - cz3,
      r1 = 0.3f*(x1*x1 + y1*y1 + z1*z1),
      r2 = 0.4f*(x2*x2 + y2*y2 + z2*z2),
      r3 = 0.5f*(x3*x3 + y3*y3 + z3*z3);
    float potential = 0;
    if (r1<1.3f) potential+= 1.0f - r1*(r1*(4*r1+17)-22)/9;
    if (r2<1.3f) potential+= 1.0f - r2*(r2*(4*r2+17)-22)/9;
    if (r3<1.3f) potential+= 1.0f - r3*(r3*(4*r3+17)-22)/9;
    return potential;
  }
};

void* item_3d_metaballs() {
  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);
  img.get_shared_channel(2)/=4; img.get_shared_channel(1)/=2;
  metaballs3d met;
  CImgList<unsigned int> primitives;
  CImgList<unsigned char> colors(8000,3,1,1,1,255);
  unsigned char white[] = { 255,255,255 };

  float alpha = 0, beta = 0, delta = 0, theta = 0, gamma = 0;
  CImgDisplay disp(img,"[#20] - 3D Metaballs");
  while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
    met.cx2 = 1.5f*(float)std::cos(theta); met.cy2 = 2.5f*(float)std::sin(3*(theta+=0.017f)); met.cz2 = 0;
    met.cx1 = 0; met.cy1 = 2.0f*(float)std::sin(4*gamma); met.cz1 = 1.2f*(float)std::cos(2*(gamma-=0.0083f));
    met.cx3 = 2.5f*(float)std::cos(2.5*delta); met.cy3 = 0; met.cz3 = 1.5f*(float)std::sin(2*(delta+=0.0125f));
    const CImg<>
      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),
      rot = 50.0*CImg<>::rotation_matrix(0,0,1,(alpha+=0.02f))*CImg<>::rotation_matrix(1,1,0,(beta+=0.076f)),
      rpoints = rot*points;
    cimglist_for(primitives,ll) {
      colors(ll,0) = -60+191+64*ll/primitives.size;
      colors(ll,1) = -30+191+64*ll/primitives.size;
      colors(ll,2) = 255*ll/primitives.size;
    }
    if (primitives.size) {
      (+img).draw_object3d(img.dimx()/2.0f,img.dimy()/2.0f,0.0f,
                           rpoints,primitives,
                           colors.get_crop(0,primitives.size-1,true),
                           4,false,500, 0,0,-500, 0.1f,1.5f).
        draw_text(5,5,"%u frames/s",white,0,0.5f,11,(unsigned int)disp.frames_per_second()).display(disp.wait(20));
    }
    if (disp.is_resized) disp.resize(false);
    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(512,320,false).toggle_fullscreen(false);
  }
  return 0;
}

// Item : Fireworks
//------------------
void* item_fireworks() {
  CImg<unsigned char> img(640,480,1,3,0);
  CImgDisplay disp(img,"[#21] - Fireworks (Click to add/explode rockets)");
  CImgList<unsigned char> colors;
  unsigned char white[] = { 255,255,255 }, black[] = { 128,0,0 };
  CImgList<> particles;
  float time = 0, speed = 100.0f;

  while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {

    if (disp.button&1 || !particles.size || (--time)<0) {
      particles.insert(CImg<>::vector((float)cimg::rand()*img.dimx(),(float)img.dimy(),
                                      (float)cimg::crand()*4,-6-(float)cimg::rand()*3,
                                      30+60*(float)cimg::rand(),3));
      colors.insert(CImg<unsigned char>::vector(255,255,255));
      time = (float)(cimg::rand()*speed);
    }
    img*=0.92f;

    cimglist_for(particles,l) {
      bool remove_particle = false;
      float &x = particles(l,0), &y = particles(l,1), &vx = particles(l,2), &vy = particles(l,3),
            &t = particles(l,4), &r = particles(l,5);
      const float r2 = (t>0 || t<-42)?r/3:r*(1-2*(-(t+2)/40.0f)/3);
      img.draw_ellipse((int)x,(int)y,r,r2,(float)(std::atan2(vy,vx)*180/cimg::valuePI),colors[l],0.6f);
      x+=vx; y+=vy; vy+=0.09f; t--;
      if (y>img.dimy()+10 || x<0 || x>=img.dimx()+10) remove_particle = true;

      if (t<0 && t>=-1) {
        if ((speed*=0.9f)<10) speed=10.0f;
        const unsigned char
          r = cimg::min(50+3*(unsigned char)(100*cimg::rand()), 255),
          g = cimg::min(50+3*(unsigned char)(100*cimg::rand()), 255),
          b = cimg::min(50+3*(unsigned char)(100*cimg::rand()), 255);
        const float di = 10+(float)cimg::rand()*60, nr = (float)cimg::rand()*30;
        for (float i=0; i<360; i+=di) {
          const float rad = i*(float)cimg::valuePI/180, c = (float)std::cos(rad), s = (float)std::sin(rad);
          particles.insert(CImg<>::vector(x,y,2*c+vx/1.5f,2*s+vy/1.5f,-2.0f,nr));
          colors.insert(CImg<unsigned char>::vector(r,g,b));
        }
        remove_particle = true;
      } else if (t<-1) { r*=0.95f; if (r<0.5f) remove_particle=true; }
      if (remove_particle) { particles.remove(l); colors.remove(l); l--; }
    }
    if (disp.button&2) cimglist_for(particles,l) if (particles(l,4)>0) particles(l,4)=0.5f;
    img.draw_text(5,5,"%u frames/s",white,black,0.5f,11,(unsigned int)disp.frames_per_second());
    disp.display(img).wait(25);
    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(640,480,false).toggle_fullscreen(false);
    if (disp.is_resized) disp.resize(disp,false);
  }
  return 0;
}

// Item : Rubber Logo
//--------------------
void* item_rubber_logo() {
  const unsigned char white[] = { 255,255,255 };
  CImg<unsigned char> background = CImg<unsigned char>(300,300).noise(100,2);
  background(0,0) = background(299,0) = background(299,299) = background(0,299) = 0;
  background.draw_plasma(0,0,299,299).blur(1.0f,14.0f,0.0f,0).resize(-100,-100,1,3);
  CImgDisplay disp(CImg<unsigned char>(background).
                   draw_text(10,10,"Please wait, generating rubber object...",white),"[#22] - 3D Rubber Logo");

  CImg<unsigned char> vol = CImg<unsigned char>().draw_text(30,30,"CImg",white,0,1,48).resize(-100,-100,15,1);
  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); }
  vol.resize(vol.dimx()+30,vol.dimy()+30,-100,1,0).blur(2).resize(-50,-50);
  CImgList<unsigned int> faces;
  CImg<> points = vol.get_isovalue3d(faces,45,1,1,1,true);
  CImgList<unsigned char> colors;
  colors.insert(faces.size,CImg<unsigned char>::vector(100,100,255));
  cimglist_for(colors,l) {
    const float x = (points(faces(l,0),0) + points(faces(l,1),0) + points(faces(l,2),0))/3;
    if (x<27) colors[l] = CImg<unsigned char>::vector(255,100,100);
    else { if (x<38) colors[l] = CImg<unsigned char>::vector(200,155,100);
    else { if (x<53) colors[l] = CImg<unsigned char>::vector(100,255,155);
    }}}
  { cimg_forX(points,l) { points(l,0)-=vol.dimx()/2; points(l,1)-=vol.dimy()/2; points(l,2)-=vol.dimz()/2; }}
  points*=5.5;

  CImgList<unsigned char> frames(100,background);
  bool ok_visu = false;
  unsigned int nb_frame = 0;
  float alpha = 0, beta = 0, gamma = 0;

  while (!disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
    CImg<unsigned char>& frame = frames[nb_frame++];
    if (nb_frame>=frames.size) { ok_visu = true; nb_frame = 0; }
    const CImg<>
      rot = CImg<>::rotation_matrix(0,1,0.2f,alpha+=0.011f)*
      CImg<>::rotation_matrix(1,0.4f,1,beta+=0.015f)*
      (1+0.1f*std::cos((double)(gamma+=0.1f)));
    (frame=background).draw_object3d(frame.dimx()/2.0f,frame.dimy()/2.0f,frame.dimz()/2.0f,rot*points,faces,colors,5,
                                     false,500,0,0,-5000,0.1f,1.0f);

    if (ok_visu) {
      CImg<unsigned char> visu(frame);
      cimglist_for(frames,l) {
        const unsigned int
          y0 = l*visu.dimy()/frames.size,
          y1 = (l+1)*visu.dimy()/frames.size-1;
        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);
      }
      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));
    }

    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(300,300,false).toggle_fullscreen(false);
    if (disp.is_resized) disp.resize();
  }
  return 0;
}

// Item : Image Waves
//--------------------
void* item_image_waves() {
  const CImg<unsigned char> img = CImg<unsigned char>(data_milla,211,242,1,3,false).get_resize(128,128,1,3);
  const unsigned int w = img.dimx()+1, h = img.dimy()+1;
  CImgList<> points0;
  CImgList<unsigned int> faces0;
  CImgList<unsigned char> colors0;
  { for (unsigned int y = 0; y<h; ++y) for (unsigned int x=0; x<w; ++x)
    points0.insert(CImg<>::vector(3*(x-w/2.0f),3*(y-w/2.0f),0)); }
  cimg_forXY(img,x,y) {
    faces0.insert(CImg<unsigned int>::vector(x+y*w,x+(y+1)*w,x+1+(y+1)*w,x+1+y*w));
    colors0.insert(CImg<unsigned char>::vector(img(x,y,0),img(x,y,1),img(x,y,2)));
  }
  CImgList<> opacities0(faces0.size,CImg<>::vector(1.0f));

  CImg<unsigned char>
    back = CImg<unsigned char>(400,300,1,3).sequence(0,130),
    ball = CImg<unsigned char>(12,12,1,3,0).draw_circle(6,6,5,CImg<unsigned char>::vector(0,128,64));
  const CImg<> mball = CImg<>(12,12,1,1,0).draw_circle(6,6,5,CImg<>::vector(1.0f));
  ball.draw_circle(7,5,4,CImg<unsigned char>::vector(16,96,52)).
    draw_circle(8,4,2,CImg<unsigned char>::vector(0,128,64)).
    draw_circle(8,4,1,CImg<unsigned char>::vector(64,196,128));

  CImg<> uc(img.dimx()/2,img.dimy()/2,1,1,0), up(uc), upp(uc);
  CImgDisplay disp(back,"[#23] - Image Waves (Try mouse buttons!)");
  CImgList<int> particles;

  for (float alpha = 0.0f, count=10.0f; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; ) {
    if ((disp.button&1 && disp.mouse_x>=0) || --count<0) {
      particles.insert(CImg<int>::vector((int)(cimg::rand()*(img.dimx()-1)),(int)(cimg::rand()*(img.dimy()-1)),-200,0));
      count = (float)(cimg::rand()*15);
    }
    alpha = (disp.mouse_x>=0 && disp.button&2)?(float)(disp.mouse_x*2*cimg::valuePI/disp.dimx()):(alpha+0.04f);
    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(400,300,false).toggle_fullscreen(false);

    cimglist_for(particles,l) {     // Handle particles
      float& z = up(particles(l,0)>>1,particles(l,1)>>1);
      if ((particles(l,2)+=(particles(l,3)++))>z-10) { z = 250.0f; particles.remove(l--); }
    }

    CImg_3x3(U,float); Upp = Unp = Ucc = Upn = Unn = 0; // Apply wave effect
    cimg_for3x3(up,x,y,0,0,U) uc(x,y) = (Unc+Upc+Ucn+Ucp)/2 - upp(x,y);
    (uc-=(float)(uc.blur(0.7f).mean())).swap(upp).swap(up);

    CImgList<> points(points0);
    CImgList<unsigned int> faces(faces0);
    CImgList<unsigned char> colors(colors0);
    CImgList<> opacities(opacities0);
    cimglist_for(points,p) points(p,2) = cimg::min(30 + uc.linear_atXY((p%w)/2.0f,(p/w)/2.0f),70.0f);
    { cimglist_for(particles,l) {
      points.insert(CImg<>::vector(3*(particles(l,0)-w/2.0f),3*(particles(l,1)-h/2.0f),30.0f+particles(l,2)));
      faces.insert(CImg<unsigned int>::vector(points.size-1));
      colors.insert(ball);
      opacities.insert(mball);
    }}
    const CImg<> rot = CImg<>::rotation_matrix(1.0f,0,0,(float)(cimg::valuePI/3.0f))*CImg<>::rotation_matrix(0,0,1.0f,alpha);
    (+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).
      display(disp.resize(false).wait(20));
  }
  return 0;
}

// Item : Breakout
//-----------------
void* item_breakout() {

  // Init graphics
  CImg<unsigned char>
    board(8,10,1,1,0),
    background = CImg<unsigned char>(board.dimx()*32,board.dimy()*16+200,1,3,0).noise(20,1).draw_plasma().blur(1,8,0),
    visu0(background/2.0), visu(visu0), brick(16,16,1,1,200), racket(64,8,1,3,0), ball(8,8,1,3,0);
  const unsigned char white[] = { 255,255,255 }, green1[] = { 60,150,30 }, green2[] = { 130,255,130 };
  { cimg_for_borderXY(brick,x,y,1) brick(x,y) = x>y?255:128; }
  { cimg_for_insideXY(brick,x,y,1) brick(x,y) = cimg::min(255,64+8*(x+y)); }
  brick.resize(31,15,1,1,1).resize(32,16,1,1,0);
  ball.draw_circle(4,4,2,white); ball-=ball.get_erode(3)/1.5;
  racket.draw_circle(4,3,4,green1).draw_circle(3,2,2,green2);
  { 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)); }
  racket.draw_image(racket.dimx()/2,racket.get_crop(0,0,racket.dimx()/2-1,racket.dimy()-1).mirror('x'));
  const int
    w = visu.dimx(), h = visu.dimy(), w2 = w/2, h2 = h/2,
    bw = ball.dimx(), bh = ball.dimy(), bw2 = bw/2, bh2 = bh/2,
    rw = racket.dimx(), rh = racket.dimy(), rw2 = rw/2;
  float xr = (float)(w-rw2), oxr = (float)xr, xb = 0, yb = 0, oxb = 0, oyb = 0, vxb = 0, vyb = 0;

  // Begin game loop
  CImgDisplay disp(visu,"[#24] - Breakout");
  disp.move((CImgDisplay::screen_dimx()-w)/2,(CImgDisplay::screen_dimy()-h)/2);
  for (unsigned int N = 0, N0 = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; ) {
    if (N0) {
      int X = (int)xr;
      if (disp.mouse_x>=0) X = (int)(w2+((disp.mouse_x<0?w2:disp.mouse_x)-w2)*2);
      else disp.set_mouse(xr>w2?w-81:80,h2);
      if (X<rw2) { X = rw2; disp.set_mouse(80,h2); }
      if (X>=w-rw2) { X = w-rw2-1; disp.set_mouse(w-81,h2); }
      oxr = xr; xr = (float)X; oxb = xb; oyb = yb; xb+=vxb; yb+=vyb;
      if ((xb>=w-bw2) || (xb<bw2)) { xb-=vxb; yb-=vyb; vxb=-vxb; }
      if (yb<bh2) { yb = (float)bh2; vyb=-vyb; }
      if (yb>=h-rh-8-bh2 && yb<h-8-bh2 && xr-rw2<=xb && xr+rw2>=xb) {
        xb = oxb; yb = h-rh-8.0f-bh2; vyb=-vyb; vxb+=(xr-oxr)/4;
        if (cimg::abs(vxb)>8) vxb*=8/cimg::abs(vxb);
      }
      if (yb<board.dimy()*16) {
        const int X = (int)xb/32, Y = (int)yb/16;
        if (board(X,Y)) {
          board(X,Y) = 0;
          ++N;
          const unsigned int x0 = X*brick.dimx(), y0 = Y*brick.dimy(), x1 = (X+1)*brick.dimx()-1, y1 = (Y+1)*brick.dimy()-1;
          visu0.draw_image(x0,y0,background.get_crop(x0,y0,x1,y1));
          if (oxb<(X<<5) || oxb>=((X+1)<<5)) vxb=-vxb;
          else if (oyb<(Y<<4) || oyb>=((Y+1)<<4)) vyb=-vyb;
        }
      }
      disp.set_title("[#24] - Breakout : %u/%u",N,N0);
    }
    if (yb>h || N==N0) {
      disp.show_mouse();
      while (!disp.is_closed && !disp.key && !disp.button) {
        ((visu=visu0)/=2).draw_text(50,visu.dimy()/2-10,N0?"Game Over !":"Get Ready ?",white,0,1,25).
          display(disp);
        disp.wait();
        if (disp.is_resized) disp.resize(disp);
      }
      board.fill(0); visu0 = background;
      cimg_forXY(board,x,y) if (0.2f+cimg::crand()>=0) {
        CImg<> cbrick = CImg<double>::vector(100+cimg::rand()*155,100+cimg::rand()*155,100+cimg::rand()*155).
          unroll('v').resize(brick.dimx(),brick.dimy());
        cimg_forV(cbrick,k) (cbrick.get_shared_channel(k).mul(brick))/=255;
        visu0.draw_image(x*32,y*16,cbrick);
        board(x,y) = 1;
      }
      N0 = (int)board.sum(); N = 0;
      oxb = xb = (float)w2; oyb = yb = board.dimy()*16.0f+bh; vxb = 2.0f; vyb = 3.0f;
      disp.hide_mouse();
    } else disp.display((visu=visu0).draw_image((int)(xr-rw2),h-rh-8,racket).draw_image((int)(xb-bw2),(int)(yb-bh2),ball));
    if (disp.is_resized) disp.resize(disp);
    disp.wait(20);
  }
  return 0;
}

// Item : 3D Reflection
//----------------------
void* item_3d_reflection() {

  // Init images and display
  CImgDisplay disp(512,512,"[#25] - 3D Reflection",0);
  CImg<unsigned char> back(400,400,1,3,0);
  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; }
  back.draw_plasma().blur(6,1,0).translate(-200,0,0,0,2).blur(6,1,0);
  CImg<unsigned char> light0 = back.get_resize(-50,-50,1,1), visu(back), reflet(back.dimx(),back.dimy(),1,1), light(light0);
  back.get_shared_channel(0)/=3; back.get_shared_channel(2)/=2;

  // Create 3D objects
  CImgList<unsigned int> back_faces, main_faces;
  CImgList<float> back_pts0, main_pts;
  CImgList<unsigned char> main_colors, back_colors, light_colors, light_colors2;

  main_pts = CImg<>::torus3d(main_faces,30,12,24,12).get_split('x');
  cimglist_for(main_faces,l)
    if (l%2) main_colors.insert(CImg<unsigned char>::vector(255,120,16));
    else main_colors.insert(CImg<unsigned char>::vector(255,100,16));

  const unsigned int res1 = 32, res2 = 32;
  for (unsigned int v = 1; v<res2; ++v) for (unsigned int u = 0; u<res1; ++u) {
    const float
      alpha = (float)(u*2*cimg::valuePI/res1), beta = (float)(-cimg::valuePI/2 + v*cimg::valuePI/res2),
      x = (float)(std::cos(beta)*std::cos(alpha)),
      y = (float)(std::cos(beta)*std::sin(alpha)),
      z = (float)(std::sin(beta));
    back_pts0.insert(CImg<>::vector(x,y,z));
  }
  const unsigned int N = back_pts0.size;
  back_pts0.insert(CImg<>::vector(0,0,-140)).insert(CImg<>::vector(0,0,140));
  CImg<float> back_pts = back_pts0.get_append('x');
  for (unsigned int vv = 0; vv<res2-2; ++vv) for (unsigned int uu = 0; uu<res1; ++uu) {
    const int nv = (vv+1)%(res2-1), nu = (uu+1)%res1;
    back_faces.insert(CImg<unsigned int>::vector(res1*vv+nu,res1*nv+uu,res1*vv+uu));
    back_faces.insert(CImg<unsigned int>::vector(res1*vv+nu,res1*nv+nu,res1*nv+uu));
    back_colors.insert(CImg<unsigned char>::vector(128,255,255));
    back_colors.insert(CImg<unsigned char>::vector(64,240,196));
  }
  for (unsigned int uu = 0; uu<res1; ++uu) {
    const int nu = (uu+1)%res1;
    back_faces.insert(CImg<unsigned int>::vector(nu,uu,N));
    back_faces.insert(CImg<unsigned int>::vector(res1*(res2-2)+nu, N+1,res1*(res2-2)+uu));
    if (uu%2) back_colors.insert(2,CImg<unsigned char>::vector(128,255,255));
    else back_colors.insert(2,CImg<unsigned char>::vector(64,240,196));
  }

  light_colors.assign(back_faces.size,CImg<unsigned char>::vector(255));
  light_colors2.assign(light_colors).insert(light,light_colors.size,true);

  // Start 3D animation
  for (float main_x = -1.5f*visu.dimx(),
         back_alpha = 0, back_beta = 0, back_theta = -3.0f,
         main_alpha = 0, main_beta = 0, main_theta = 0;
       !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC;
       main_alpha+=0.041f, main_beta+=0.063f, main_theta+=0.02f,
         back_alpha+=0.0031f, back_beta+=0.0043f, back_theta+=0.01f) {
    const int
      main_X = (int)(visu.dimx()/2 + main_x + 100*std::cos(2.1*main_theta)),
      main_Y = (int)(visu.dimy()/2 + 120*std::sin(1.8*main_theta));
    CImgList<> rmain_pts = (CImg<>::rotation_matrix(-1,1,0,main_alpha)*CImg<>::rotation_matrix(1,0,1,main_beta))*main_pts;
    const CImg<> rback_pts = (CImg<>::rotation_matrix(1,1,0,back_alpha)*CImg<>::rotation_matrix(0.5,0,1,back_beta))*back_pts;
    (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);
    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);
    rmain_pts*=2;
    (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);
    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);
    cimg_forXY(visu,x,y) {
      const unsigned char v = *(ptrs++);
      if (v) { *ptrr = (*ptrr+v)>>1; *ptrg = (3**ptrr+v)>>2; *ptrb = (*ptrb+v)>>1; }
      ++ptrr; ++ptrg; ++ptrb;
    }
    visu.draw_object3d((float)main_X,(float)main_Y,0,rmain_pts,main_faces,main_colors,4,
                       false,500,0,0,-5000,0.1f,1.4f);

    if (disp.is_resized) {
      const int s = cimg::min(disp.window_dimx(),disp.window_dimy());
      disp.resize(s,s,false);
    }
    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.resize(512,512,false).toggle_fullscreen(false);
    disp.display(visu).wait(20);
    back.translate(4,0,0,0,2);
    light0.translate(-2,0,0,0,2);
    if (main_x<0) main_x +=2;
    const float H = back_theta<0?0.0f:(float)(0.3f-0.3f*std::cos(back_theta));
    for (unsigned int p = 0, v = 1; v<res2; ++v) for (unsigned int u = 0; u<res1; ++u) {
      const float
        alpha = (float)(u*2*cimg::valuePI/res1), beta = (float)(-cimg::valuePI/2 + v*cimg::valuePI/res2),
        x = back_pts0(p,0), y = back_pts0(p,1), z = back_pts0(p,2),
        altitude = 140*(float)cimg::abs(1+H*std::sin(3*alpha)*std::cos(5*beta));
      back_pts(p,0) = altitude*x; back_pts(p,1) = altitude*y; back_pts(p,2) = altitude*z;
      ++p;
      }
  }
  return 0;
}

// Item : Fish-Eye Magnification
//------------------------------
void* item_fisheye_magnification() {
  const unsigned char purple[] = { 255,0,255 }, white[] = { 255,255,255 }, black[] = { 0,0,0 };
  const CImg<unsigned char> img0 = CImg<unsigned char>(data_logo,555,103,1,3,true).get_resize(-144,-144,1,3,5);
  CImgDisplay disp(img0,"[#26] - Fish-Eye Magnification");
  int rm = 80, xc = 0, yc = 0, rc = 0;
  CImg<unsigned char> img, res;
  for (float alpha = 0; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; alpha+=0.02f) {
    if (!img) img = img0.get_resize(disp,3);
    if (disp.mouse_x>=0) { xc = disp.mouse_x; yc = disp.mouse_y; rc = rm; }
    else {
      xc = (int)(img.dimx()*(1 + 0.9f*std::cos(1.2f*alpha))/2);
      yc = (int)(img.dimy()*(1 + 0.8f*std::sin(3.4f*alpha))/2);
      rc = (int)(90 + 60*std::sin(alpha));
    }
    const int x0 = xc - rc, y0 = yc - rc, x1 = xc + rc, y1 = yc + rc;
    res = img;
    cimg_for_inXY(res,x0,y0,x1,y1,x,y) {
      const float X = (float)x - xc, Y = (float)y - yc, r2 = X*X + Y*Y, rrc = (float)std::sqrt(r2)/rc;
      if (rrc<1) {
        const int xi = (int)(xc + rrc*X), yi = (int)(yc + rrc*Y);
        res(x,y,0) = img(xi,yi,0); res(x,y,1) = img(xi,yi,1); res(x,y,2) = img(xi,yi,2);
      }
    }
    const int xf = xc+3*rc/8, yf = yc-3*rc/8;
    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).
      draw_circle(xf,yf,rc/10,white,0.2f).draw_circle(xc,yc,rc,black,0.7f,~0U);
    disp.display(res).wait(20);
    rm+=(disp.button&1?8:(disp.button&2?-8:0));
    rm = rm<30?30:(rm>200?200:rm);
    if (disp.is_resized) { disp.resize(false); img.assign(); }
  }
  return 0;
}

// Item : Word puzzle
//------------------------------
void* item_word_puzzle() {

  // Create B&W and color letters
  CImg<unsigned char> model(60,60,1,3,0), color(3), background, canvas, elaps;
  CImgList<unsigned char> letters('Z'-'A'+1), cletters(letters);
  const unsigned char white[] = { 255,255,255 }, gray[] = { 128,128,128 }, black[] = { 0,0,0 };
  char tmptxt[] = { 'A',0 };
  model.fill(255).draw_rectangle(5,5,54,54,gray).blur(3,0).threshold(140).normalize(0,255);
  cimglist_for(letters,l)
    (letters[l].draw_text(5,2,&(tmptxt[0]='A'+l),white,0,1,64).resize(60,60,1,1,0,0,true).
     resize(-100,-100,1,3)|=model).blur(0.5);
  { cimglist_for(cletters,l) {
    CImg<int> tmp = letters[l];
    color.rand(100,255);
    cimg_forV(tmp,k) (tmp.get_shared_channel(k)*=color[k])/=255;
    cletters[l] = tmp;
  }}

  CImgDisplay disp(500,400,"[#27] - Word Puzzle",0);
  while (!disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC) {

    // Create background, word data and display.
    background.assign(40,40,1,2,0).noise(30,2).distance(255).normalize(0,255).resize(500,400,1,3,3);
    CImg<int> current(14,6,1,1,0), solution(14,4,1,1,0);
    current.get_shared_line(0).fill('T','H','E','C','I','M','G','L','I','B','R','A','R','Y');
    current.get_shared_line(1).rand(-30,background.dimx()-30);
    current.get_shared_line(2).rand(-30,background.dimy()-30);
    solution.get_shared_line(0) = current.get_shared_line(0);
    solution.get_shared_line(1).fill(20,80,140,100,180,260,340,40,100,160,220,280,340,400);
    solution.get_shared_line(2).fill(20,20,20,120,150,180,210,310,310,310,310,310,310,310);
    { cimg_forX(solution,l) background.draw_image(solution(l,1),solution(l,2),letters(solution(l)-'A'),0.3f); }
    const int last = current.dimx()-1;

    // Start user interaction
    int timer = 0, completed = 0;
    for (bool selected = false, refresh_canvas = true, stopflag = false;
         !stopflag && !disp.is_closed && disp.key!=cimg::keyQ && disp.key!=cimg::keyESC; disp.resize(disp).wait(20)) {
      if (refresh_canvas) {
        canvas = background;
        cimg_forX(current,l) if (!current(l,5)) {
          int &x = current(l,1), &y = current(l,2);
          if (x<-30) x = -30; else if (x>canvas.dimx()-30) x = canvas.dimx()-30;
          if (y<-30) y = -30; else if (y>canvas.dimy()-30) y = canvas.dimy()-30;
          canvas.draw_rectangle(x+8,y+8,x+67,y+67,black,0.3f).draw_image(x,y,cletters(current(l)-'A'));
        }
        refresh_canvas = false;
      }
      (+canvas).draw_text(360,3,"Elapsed Time : %d",white,0,1,16,timer++).display(disp);

      if (disp.button&1) {
        const int mx = disp.mouse_x, my = disp.mouse_y;
        if (mx>=0 && my>=0) {
          if (!selected) {
            int ind = -1;
            cimg_forX(current,l) if (!current(l,5)) {
              const int x = current(l,1), y = current(l,2), dx = mx - x, dy = my - y;
              if (dx>=0 && dx<60 && dy>=0 && dy<60) { selected = true; ind = l; current(l,3) = dx; current(l,4) = dy; }
            }
            if (ind>=0 && ind<last) {
              const CImg<int> vec = current.get_column(ind);
              current.draw_image(ind,current.get_crop(ind+1,last)).draw_image(last,vec);
            }
          } else { current(last,1) = mx - current(last,3); current(last,2) = my - current(last,4); refresh_canvas = true; }
        }
      } else {
        bool win = true;
        cimg_forX(solution,j) if (!solution(j,3)) {
          win = false;
          const int x = solution(j,1), y = solution(j,2);
          cimg_forX(current,i) if (!current(i,5) && solution(j)==current(i)) {
            const int xc = current(i,1), yc = current(i,2), dx = cimg::abs(x-xc), dy = cimg::abs(y-yc);
            if (dx<=12 && dy<=12) {
              cimg_forV(background,k) cimg_forY(letters[0],y)
                background.get_shared_line(solution(j,2)+y,0,k).
                draw_image(solution(j,1),0,
                           (CImg<float>(cletters(solution(j)-'A').get_shared_line(y,0,k))*=2.0*std::cos((y-30.0f)/18)).
                           cut(0,255),0.8f);
              current(i,5) = solution(j,3) = 1; refresh_canvas = true;
            }
          }
        }
        selected = false;
        if (win) { stopflag = true; completed = 1; }
      }
    }

    // Display final score
    const char
      *const mention0 = "Need more training !", *const mention1 = "Still amateur, hu ?",
      *const mention2 = "Not so bad !", *const mention3 = "  Good !", *const mention4 = "Very good !",
      *const mention5 = " Expert !",
      *mention = completed?(timer<700?mention5:timer<800?mention4:timer<900?mention3:timer<1000?mention2:timer<1200?mention1:mention0):mention0;
    canvas.assign().draw_text(0,0,"Final time : %d\n\n%s",white,0,1,32,timer,mention);
    ((background/=2)&CImg<unsigned char>(2,2).fill(0,255,255,0).resize(background,0,2)).
      draw_image((background.dimx()-canvas.dimx())/2,(background.dimy()-canvas.dimy())/2,
                 canvas,canvas.get_dilate(3).dilate(3).dilate(3),1,255).display(disp.flush());
    while (!disp.is_closed && !disp.key && !disp.button) disp.resize(disp).wait();
  }
  return 0;
}

// Run a selected effect
//-----------------------
void start_item(const unsigned int demo_number) {
  switch (demo_number) {
  case 1: item_blurring_gradient(); break;
  case 2: item_rotozoom(); break;
  case 3: item_anisotropic_smoothing(); break;
  case 4: item_fractal_animation(); break;
  case 5: item_gamma_correction(); break;
  case 6: item_filled_triangles(); break;
  case 7: item_mandelbrot_explorer(); break;
  case 8: item_mini_paint(); break;
  case 9: item_soccer_bobs(); break;
  case 10: item_bump(); break;
  case 11: item_bouncing_bubble(); break;
  case 12: item_virtual_landscape(); break;
  case 13: item_plasma(); break;
  case 14: item_oriented_convolutions(); break;
  case 15: item_shade_bobs(); break;
  case 16: item_fourier_filtering(); break;
  case 17: item_image_zoomer(); break;
  case 18: item_blobs_editor(); break;
  case 19: item_double_torus(); break;
  case 20: item_3d_metaballs(); break;
  case 21: item_fireworks(); break;
  case 22: item_rubber_logo(); break;
  case 23: item_image_waves(); break;
  case 24: item_breakout(); break;
  case 25: item_3d_reflection(); break;
  case 26: item_fisheye_magnification(); break;
  case 27: item_word_puzzle(); break;
  default: break;
  }
}

/*---------------------------

  Main procedure

  --------------------------*/
int main(int argc, char **argv) {

  // Display info about the CImg Library configuration
  //--------------------------------------------------
  unsigned int demo_number = cimg_option("-run",0,0);
  if (demo_number) start_item(demo_number);
  else {
    cimg::info();

    // Demo selection menu
    //---------------------
    const unsigned char
      white[]  = { 255,255,255 }, black[] = { 0,0,0 },     red[] = { 120,50,80 },
      yellow[] = { 200,155,0 },   green[] = { 30,200,70 }, purple[] = { 175,32,186 },
      blue[]   = { 55,140,185 },  grey[] = { 127,127,127 };
    float
      rx = 0, ry = 0, t = 0, gamma = 0, vgamma = 0, T = 0.9f,
      nrx = (float)(2*cimg::crand()),
      nry = (float)(2*cimg::crand());
    int y0 = 2*13;
    CImg<unsigned char> back(1,2,1,3,10), fore, text, img;
    back.fillV(0,1,0,10,10,235).resize(320,420,1,3,3).get_shared_channel(2).noise(10,1).draw_plasma();
    back.draw_rectangle(0,y0-7,back.dimx()-1,y0+20,red);
    fore.assign(back.dimx(),50,1,1,0).draw_text(20,y0-5,"** CImg %u.%u.%u Samples **",grey,0,1,22,
                                                cimg_version/100,(cimg_version/10)%10,cimg_version%10);
    (fore+=fore.get_dilate(3).dilate(3)).resize(-100,-100,1,3);
    cimg_forXY(fore,x,y)
      if (fore(x,y)==127) fore(x,y,0) = fore(x,y,1) = fore(x,y,2) = 1;
      else if (fore(x,y)) {
        const float val = cimg::min(255.0f,7.0f*(y-3));
        fore(x,y,0) = (unsigned char)(val/1.5f);
        fore(x,y,1) = (unsigned char)val;
        fore(x,y,2) = (unsigned char)(val/1.1f);
      }
    text.draw_text(1,1,
                   "1- Blurring Gradient\n"
                   "2- Rotozoom\n"
                   "3- Anisotropic Smoothing\n"
                   "4- Fractal Animation\n"
                   "5- Gamma Correction\n"
                   "6- Filled Triangles\n"
                   "7- Mandelbrot explorer\n"
                   "8- Mini-Paint\n"
                   "9- Soccer Bobs\n"
                   "10- Bump Effect\n"
                   "11- Bouncing Bubble\n"
                   "12- Virtual Landscape\n"
                   "13- Plasma & Sinus Scroll\n"
                   "14- Oriented Convolutions\n"
                   "15- Shade Bobs\n"
                   "16- Fourier Filtering\n"
                   "17- Image Zoomer\n"
                   "18- Blobs Editor\n"
                   "19- Double Torus\n"
                   "20- 3D Metaballs\n"
                   "21- Fireworks\n"
                   "22- Rubber Logo\n"
                   "23- Image Waves\n"
                   "24- Breakout\n"
                   "25- 3D Reflection\n"
                   "26- Fish-Eye Magnification\n"
                   "27- Word Puzzle\n",
                   white,0,1,13);
    fore.resize(back,0).draw_image(20,y0+2*13,text|=text.get_dilate(3)>>4);

    CImgDisplay disp(back,"CImg Library Samples",0,false,true);
    disp.move((disp.screen_dimx()-disp.window_dimx())/2,(disp.screen_dimy()-disp.window_dimy())/2);
    img = back; back*=0.15f;
    for (y0+=2*13; !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC; demo_number = 0) {
      while (!demo_number && !disp.is_closed && !disp.is_keyQ && !disp.is_keyESC) {
        img*=0.85f; img+=back;
        for (int i = 0; i<60; ++i) {
          const float
            mx = (float)(img.dimx()/2+(img.dimx()/2-30)*((1-gamma)*std::cos(3*t+rx*i*18.0f*cimg::valuePI/180) +
                                                         gamma*std::cos(3*t+nrx*i*18.0f*cimg::valuePI/180))),
            my = (float)(img.dimy()/2+(img.dimy()/2-30)*((1-gamma)*std::sin(4*t+ry*i*18.0f*cimg::valuePI/180) +
                                                         gamma*std::sin(4*t+nry*i*18.0f*cimg::valuePI/180))),
            mz = (float)(1.3f + 1.2f*((1-gamma)*std::sin(2*t+(rx+ry)*i*20*cimg::valuePI/180) +
                                      gamma*std::sin(2*t+(nrx+nry)*i*20*cimg::valuePI/180)));
          const int j = i%5;
          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).
            draw_circle((int)(mx+4*mz),(int)(my-4),(int)(3*mz),white,0.1f).
            draw_circle((int)mx,(int)my,(int)(10*mz),black,0.2f,~0U);
        }
        const unsigned char *ptrs = fore.end();
        cimg_for(img,ptrd,unsigned char) { const unsigned char val = *(--ptrs); if (val) *ptrd = val; }
        int y = disp.mouse_y;
        if (y>=y0 && y<y0+27*13) {
          y = (y/13)*13+7;
          for (int yy = y-7; yy<=y+6; ++yy) img.draw_rectangle(0,yy,0,1,img.dimx()-1,yy,0,1,(unsigned char)(130-15*cimg::abs(yy-y)));
          img.draw_triangle(2,y-4,2,y+4,8,y,yellow).draw_triangle(img.dimx()-2,y-4,img.dimx()-2,y+4,img.dimx()-8,y,yellow);
        }
        gamma+=vgamma; if (gamma>1) { gamma = vgamma = 0; rx = nrx; ry = nry; nrx=(float)(2*cimg::crand()); nry=(float)(2*cimg::crand()); }
        t+=0.006f; T+=0.005f; if (T>1) { T-=(float)(1+cimg::crand()); vgamma = 0.03f; }
        if (disp.button) { disp.button = 0; demo_number = 1+(disp.mouse_y-y0)/13; }
        disp.resize(disp,false).display(img).wait(25);
      }
      start_item(demo_number);
    }
  }

  // Exit demo
  //-----------
  std::exit(0);
  return 0;
}
