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