1.1 diff -r 5edfbd3e7a46 -r 1204ebf9340d PTdecode/CImg-1.3.0/examples/curve_editor.cpp 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/PTdecode/CImg-1.3.0/examples/curve_editor.cpp Mon Aug 03 14:09:20 2009 +0100 1.4 @@ -0,0 +1,344 @@ 1.5 +/* 1.6 + # 1.7 + # File : curve_editor.cpp 1.8 + # ( C++ source file ) 1.9 + # 1.10 + # Description : A simple user interface to construct 2D spline curves. 1.11 + # This file is a part of the CImg Library project. 1.12 + # ( http://cimg.sourceforge.net ) 1.13 + # 1.14 + # Copyright : David Tschumperle 1.15 + # ( http://www.greyc.ensicaen.fr/~dtschump/ ) 1.16 + # 1.17 + # License : CeCILL v2.0 1.18 + # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) 1.19 + # 1.20 + # This software is governed by the CeCILL license under French law and 1.21 + # abiding by the rules of distribution of free software. You can use, 1.22 + # modify and/ or redistribute the software under the terms of the CeCILL 1.23 + # license as circulated by CEA, CNRS and INRIA at the following URL 1.24 + # "http://www.cecill.info". 1.25 + # 1.26 + # As a counterpart to the access to the source code and rights to copy, 1.27 + # modify and redistribute granted by the license, users are provided only 1.28 + # with a limited warranty and the software's author, the holder of the 1.29 + # economic rights, and the successive licensors have only limited 1.30 + # liability. 1.31 + # 1.32 + # In this respect, the user's attention is drawn to the risks associated 1.33 + # with loading, using, modifying and/or developing or reproducing the 1.34 + # software by the user in light of its specific status of free software, 1.35 + # that may mean that it is complicated to manipulate, and that also 1.36 + # therefore means that it is reserved for developers and experienced 1.37 + # professionals having in-depth computer knowledge. Users are therefore 1.38 + # encouraged to load and test the software's suitability as regards their 1.39 + # requirements in conditions enabling the security of their systems and/or 1.40 + # data to be ensured and, more generally, to use and operate it in the 1.41 + # same conditions as regards security. 1.42 + # 1.43 + # The fact that you are presently reading this means that you have had 1.44 + # knowledge of the CeCILL license and that you accept its terms. 1.45 + # 1.46 +*/ 1.47 + 1.48 +#include "CImg.h" 1.49 +using namespace cimg_library; 1.50 + 1.51 +// The lines below are necessary when using a non-standard compiler as visualcpp6. 1.52 +#ifdef cimg_use_visualcpp6 1.53 +#define std 1.54 +#endif 1.55 +#ifdef min 1.56 +#undef min 1.57 +#undef max 1.58 +#endif 1.59 + 1.60 +//--------------- 1.61 +// Main procedure 1.62 +//--------------- 1.63 +int main(int argc, char **argv) { 1.64 + 1.65 + // Read command line parameters 1.66 + //----------------------------- 1.67 + cimg_usage("2D Spline Curve Editor"); 1.68 + const char *file_i = cimg_option("-i",(char*)0,"Input image"); 1.69 + const float contrast = cimg_option("-contrast",0.6f,"Image contrast"); 1.70 + const char *file_ip = cimg_option("-ip",(char*)0,"Input control points"); 1.71 + const char *file_oc = cimg_option("-oc",(char*)0,"Output curve points"); 1.72 + const char *file_op = cimg_option("-op",(char*)0,"Output control points"); 1.73 + const char *file_od = cimg_option("-od",(char*)0,"Output distance function"); 1.74 + bool interp = cimg_option("-poly",true,"Use polynomial interpolation"); 1.75 + bool closed = cimg_option("-closed",true,"Closed curve"); 1.76 + bool show_tangents = cimg_option("-tangents",false,"Show tangents"); 1.77 + bool show_points = cimg_option("-points",true,"Show control points"); 1.78 + bool show_outline = cimg_option("-outline",true,"Show polygon outline"); 1.79 + bool show_indices = cimg_option("-indices",true,"Show points indices"); 1.80 + bool show_coordinates = cimg_option("-coords",false,"Show points coordinates"); 1.81 + const float precision = cimg_option("-prec",0.05f,"Precision of curve discretization"); 1.82 + 1.83 + // Init image data 1.84 + //----------------- 1.85 + const unsigned char yellow[] = { 255,255,0 }, white[] = { 255,255,255 }, green[] = { 0,255,0 }, 1.86 + red[] = { 255,0,50 }, purple[] = { 255,100,255 }, black[] = { 0,0,0 }; 1.87 + CImg<unsigned char> img0, img, help_img; 1.88 + if (file_i) { 1.89 + std::fprintf(stderr,"\n - Load input image '%s' : ",cimg::basename(file_i)); 1.90 + img0 = CImg<>(file_i).normalize(0,255.0f*contrast); 1.91 + std::fprintf(stderr,"Size = %dx%dx%dx%d \n",img0.dimx(),img0.dimy(),img0.dimz(),img0.dimv()); 1.92 + img0.resize(-100,-100,1,3); 1.93 + } 1.94 + else { 1.95 + std::fprintf(stderr,"\n - No input image specified, use default 512x512 image.\n"); 1.96 + img0.assign(512,512,1,3,0).draw_grid(32,32,0,0,false,false,green,0.4f,0xCCCCCCCC,0xCCCCCCCC); 1.97 + } 1.98 + 1.99 + help_img.assign(270,160,1,3,0). 1.100 + draw_text(5,5, 1.101 + "------------------------------------------\n" 1.102 + "2D Curve Editor\n" 1.103 + "------------------------------------------\n" 1.104 + "Left button : Create or move control point\n" 1.105 + "Right button : Delete control point\n" 1.106 + "Spacebar : Switch interpolation\n" 1.107 + "Key 'C' : Switch open/closed mode\n" 1.108 + "Key 'T' : Show/hide tangents\n" 1.109 + "Key 'P' : Show/hide control points\n" 1.110 + "Key 'O' : Show/hide polygon outline\n" 1.111 + "Key 'N' : Show/hide points indices\n" 1.112 + "Key 'X' : Show/hide points coordinates\n" 1.113 + "Key 'H' : Show/hide this help\n" 1.114 + "Key 'S' : Save control points\n" 1.115 + "Key 'R' : Reset curve\n", 1.116 + green); 1.117 + CImgDisplay disp(img0,"2D Curve Editor",0); 1.118 + CImgList<float> points, curve; 1.119 + bool moving = false; 1.120 + bool help = !file_i; 1.121 + 1.122 + if (file_ip) { 1.123 + std::fprintf(stderr," - Load input control points '%s' : ",cimg::basename(file_ip)); 1.124 + points = CImg<>(file_ip).transpose().get_split('x'); 1.125 + std::fprintf(stderr," %u points\n",points.size); 1.126 + } 1.127 + 1.128 + // Enter user loop 1.129 + //----------------- 1.130 + while (!disp.is_closed && !disp.is_keyESC && !disp.is_keyQ) { 1.131 + 1.132 + // Handle mouse manipulation 1.133 + //--------------------------- 1.134 + const float 1.135 + x = disp.mouse_x*(float)img0.dimx()/disp.dimx(), 1.136 + y = disp.mouse_y*(float)img0.dimy()/disp.dimy(); 1.137 + const unsigned int 1.138 + button = disp.button; 1.139 + 1.140 + if (points && button && x>=0 && y>=0) { 1.141 + 1.142 + // Find nearest point and nearest segment 1.143 + float dmin_pt = 1e10f, dmin_seg = dmin_pt; 1.144 + unsigned int p_pt = 0, p_seg = 0; 1.145 + cimglist_for(points,p) { 1.146 + const unsigned int 1.147 + pnext = closed?(p+1)%points.size:(p+1<points.size?p+1:p); 1.148 + const float 1.149 + xp = points(p,0), 1.150 + yp = points(p,1), 1.151 + xm = 0.5f*(xp + points(pnext,0)), 1.152 + ym = 0.5f*(yp + points(pnext,1)); 1.153 + const float 1.154 + d_pt = (xp-x)*(xp-x) + (yp-y)*(yp-y), 1.155 + d_seg = (xm-x)*(xm-x) + (ym-y)*(ym-y); 1.156 + if (d_pt<dmin_pt) { dmin_pt = d_pt; p_pt = p; } 1.157 + if (d_seg<dmin_seg) { dmin_seg = d_seg; p_seg = p; } 1.158 + } 1.159 + 1.160 + // Handle button 1.161 + if (button&1) { 1.162 + if (dmin_pt<100 || moving) { points(p_pt,0) = x; points(p_pt,1) = y; } 1.163 + else points.insert(CImg<>::vector(x,y),p_seg+1); 1.164 + moving = true; 1.165 + } 1.166 + if (button&2 && dmin_pt<100) { 1.167 + if (points.size>3) points.remove(p_pt); 1.168 + else points.assign(); 1.169 + disp.button=0; 1.170 + } 1.171 + } 1.172 + if (!button) moving = false; 1.173 + 1.174 + if (disp.key) { 1.175 + switch (disp.key) { 1.176 + case cimg::keySPACE: interp = !interp; break; 1.177 + case cimg::keyC: closed = !closed; break; 1.178 + case cimg::keyT: show_tangents = !show_tangents; break; 1.179 + case cimg::keyP: show_points = !show_points; break; 1.180 + case cimg::keyO: show_outline = !show_outline; break; 1.181 + case cimg::keyN: show_indices = !show_indices; break; 1.182 + case cimg::keyX: show_coordinates = !show_coordinates; break; 1.183 + case cimg::keyR: points.assign(); break; 1.184 + case cimg::keyH: help = !help; break; 1.185 + case cimg::keyS: { 1.186 + const char *filename = file_op?file_op:"curve_points.dlm"; 1.187 + std::fprintf(stderr," - Save control points in '%s'\n",filename); 1.188 + points.get_append('x').transpose().save(filename); 1.189 + } break; 1.190 + } 1.191 + disp.key = 0; 1.192 + } 1.193 + 1.194 + // Init list of points if empty 1.195 + //------------------------------ 1.196 + if (!points) { 1.197 + const float 1.198 + x0 = img0.dimx()/4.0f, 1.199 + y0 = img0.dimy()/4.0f, 1.200 + x1 = img0.dimx()-x0, 1.201 + y1 = img0.dimy()-y0; 1.202 + points.insert(CImg<>::vector(x0,y0)). 1.203 + insert(CImg<>::vector(x1,y0)). 1.204 + insert(CImg<>::vector(x1,y1)). 1.205 + insert(CImg<>::vector(x0,y1)); 1.206 + } 1.207 + 1.208 + // Estimate curve tangents 1.209 + //------------------------- 1.210 + CImg<> tangents(points.size,2); 1.211 + { cimglist_for(points,p) { 1.212 + const unsigned int 1.213 + p0 = closed?(p+points.size-1)%points.size:(p?p-1:0), 1.214 + p1 = closed?(p+1)%points.size:(p+1<points.size?p+1:p); 1.215 + const float 1.216 + x = points(p,0), 1.217 + y = points(p,1), 1.218 + x0 = points(p0,0), 1.219 + y0 = points(p0,1), 1.220 + x1 = points(p1,0), 1.221 + y1 = points(p1,1), 1.222 + u0 = x-x0, 1.223 + v0 = y-y0, 1.224 + n0 = 1e-8f + (float)std::sqrt(u0*u0+v0*v0), 1.225 + u1 = x1-x, 1.226 + v1 = y1-y, 1.227 + n1 = 1e-8f + (float)std::sqrt(u1*u1+v1*v1), 1.228 + u = u0/n0 + u1/n1, 1.229 + v = v0/n0 + v1/n1, 1.230 + n = 1e-8f + (float)std::sqrt(u*u+v*v), 1.231 + fact = 0.5f*(n0+n1); 1.232 + tangents(p,0) = fact*u/n; 1.233 + tangents(p,1) = fact*v/n; 1.234 + }} 1.235 + 1.236 + // Estimate 3th-order polynomial interpolation 1.237 + //--------------------------------------------- 1.238 + curve.assign(); 1.239 + const unsigned int pmax = points.size-(closed?0:1); 1.240 + for (unsigned int p0=0; p0<pmax; p0++) { 1.241 + const unsigned int 1.242 + p1 = closed?(p0+1)%points.size:(p0+1<points.size?p0+1:p0); 1.243 + const float 1.244 + x0 = points(p0,0), 1.245 + y0 = points(p0,1), 1.246 + x1 = points(p1,0), 1.247 + y1 = points(p1,1); 1.248 + float ax=0, bx=0, cx=0, dx=0, ay=0, by=0, cy=0, dy=0; 1.249 + if (interp) { 1.250 + const float 1.251 + u0 = tangents(p0,0), 1.252 + v0 = tangents(p0,1), 1.253 + u1 = tangents(p1,0), 1.254 + v1 = tangents(p1,1); 1.255 + ax = 2*(x0-x1)+u0+u1; 1.256 + bx = 3*(x1-x0)-2*u0-u1; 1.257 + cx = u0; 1.258 + dx = x0; 1.259 + ay = 2*(y0-y1)+v0+v1; 1.260 + by = 3*(y1-y0)-2*v0-v1; 1.261 + cy = v0; 1.262 + dy = y0; 1.263 + } else { 1.264 + ax = ay = bx = by = 0; 1.265 + dx = x0; 1.266 + dy = y0; 1.267 + cx = (x1-x0); 1.268 + cy = (y1-y0); 1.269 + } 1.270 + const float tmax = 1+precision; 1.271 + for (float t=0; t<tmax; t+=precision) { 1.272 + const float 1.273 + xt = ax*t*t*t + bx*t*t + cx*t + dx, 1.274 + yt = ay*t*t*t + by*t*t + cy*t + dy; 1.275 + curve.insert(CImg<>::vector(xt,yt)); 1.276 + } 1.277 + } 1.278 + 1.279 + // Draw curve and display image 1.280 + //------------------------------- 1.281 + const float 1.282 + factx = (float)disp.dimx()/img0.dimx(), 1.283 + facty = (float)disp.dimy()/img0.dimy(); 1.284 + img = img0.get_resize(disp.dimx(),disp.dimy()); 1.285 + if (help) img.draw_image(help_img,0.6f); 1.286 + if (interp && show_outline) { 1.287 + CImg<> npoints = points.get_append('x'); 1.288 + npoints.get_shared_line(0)*=factx; 1.289 + npoints.get_shared_line(1)*=facty; 1.290 + img.draw_polygon(npoints,red,0.4f); 1.291 + if (closed) img.draw_polygon(npoints,yellow,0.8f,0x11111111); 1.292 + else img.draw_line(npoints,yellow,0.8f,0x11111111); 1.293 + } 1.294 + CImg<> ncurve = curve.get_append('x'); 1.295 + ncurve.get_shared_line(0)*=factx; 1.296 + ncurve.get_shared_line(1)*=facty; 1.297 + if (closed) img.draw_polygon(ncurve,white,1.0f,~0U); 1.298 + else img.draw_line(ncurve,white); 1.299 + 1.300 + if (show_points) cimglist_for(points,p) { 1.301 + const float 1.302 + x = points(p,0)*factx, 1.303 + y = points(p,1)*facty; 1.304 + if (show_tangents) { 1.305 + const float 1.306 + u = tangents(p,0), 1.307 + v = tangents(p,1), 1.308 + n = 1e-8f + (float)std::sqrt(u*u+v*v), 1.309 + nu = u/n, 1.310 + nv = v/n; 1.311 + img.draw_arrow((int)(x-15*nu),(int)(y-15*nv),(int)(x+15*nu),(int)(y+15*nv),green); 1.312 + } 1.313 + if (show_indices) img.draw_text((int)x,(int)(y-16),"%d",purple,black,1,6,p); 1.314 + if (show_coordinates) img.draw_text((int)(x-24),(int)(y+8),"(%d,%d)",yellow,black,0.5f,6,(int)points(p,0),(int)points(p,1)); 1.315 + img.draw_circle((int)x,(int)y,3,red,0.7f); 1.316 + } 1.317 + 1.318 + img.display(disp); 1.319 + disp.wait(); 1.320 + 1.321 + if (disp.is_resized) disp.resize(false); 1.322 + } 1.323 + 1.324 + // Save output result and exit 1.325 + //----------------------------- 1.326 + if (file_op) { 1.327 + std::fprintf(stderr," - Save control points in '%s'\n",cimg::basename(file_op)); 1.328 + points.get_append('x').transpose().save(file_op); 1.329 + } 1.330 + if (file_oc) { 1.331 + std::fprintf(stderr," - Save curve points in '%s'\n",cimg::basename(file_oc)); 1.332 + curve.get_append('x').transpose().save(file_oc); 1.333 + } 1.334 + if (file_od) { 1.335 + std::fprintf(stderr," - Computing distance function, please wait...."); std::fflush(stderr); 1.336 + CImg<> ncurve = (closed?(+curve).insert(curve[0]):curve).get_append('x'); 1.337 + const float zero = 0.0f, one = 1.0f; 1.338 + CImg<> distance = 1.339 + CImg<>(img0.dimx(),img0.dimy(),1,1,-1.0f).draw_line(ncurve,&zero).draw_fill(0,0,&one). 1.340 + distance_hamilton(200); 1.341 + std::fprintf(stderr,"\n - Save distance function in '%s'\n",cimg::basename(file_od)); 1.342 + distance.save(file_od); 1.343 + } 1.344 + 1.345 + std::fprintf(stderr," - Exit.\n"); 1.346 + std::exit(0); 1.347 + return 0; 1.348 +}