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