Mon, 03 Aug 2009 23:41:04 +0100
added dep/*.d and obj/*.o to hgignore
1 /*
2 #
3 # File : image_surface.cpp
4 # ( C++ source file )
5 #
6 # Description : This tool allows to show an image as a 3D surface.
7 # This file is a part of the CImg Library project.
8 # ( http://cimg.sourceforge.net )
9 #
10 # Copyright : David Tschumperle
11 # ( http://www.greyc.ensicaen.fr/~dtschump/ )
12 #
13 # License : CeCILL v2.0
14 # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
15 #
16 # This software is governed by the CeCILL license under French law and
17 # abiding by the rules of distribution of free software. You can use,
18 # modify and/ or redistribute the software under the terms of the CeCILL
19 # license as circulated by CEA, CNRS and INRIA at the following URL
20 # "http://www.cecill.info".
21 #
22 # As a counterpart to the access to the source code and rights to copy,
23 # modify and redistribute granted by the license, users are provided only
24 # with a limited warranty and the software's author, the holder of the
25 # economic rights, and the successive licensors have only limited
26 # liability.
27 #
28 # In this respect, the user's attention is drawn to the risks associated
29 # with loading, using, modifying and/or developing or reproducing the
30 # software by the user in light of its specific status of free software,
31 # that may mean that it is complicated to manipulate, and that also
32 # therefore means that it is reserved for developers and experienced
33 # professionals having in-depth computer knowledge. Users are therefore
34 # encouraged to load and test the software's suitability as regards their
35 # requirements in conditions enabling the security of their systems and/or
36 # data to be ensured and, more generally, to use and operate it in the
37 # same conditions as regards security.
38 #
39 # The fact that you are presently reading this means that you have had
40 # knowledge of the CeCILL license and that you accept its terms.
41 #
42 */
44 #include "CImg.h"
45 using namespace cimg_library;
47 // The lines below are necessary when using a non-standard compiler as visualcpp6.
48 #ifdef cimg_use_visualcpp6
49 #define std
50 #endif
51 #ifdef min
52 #undef min
53 #undef max
54 #endif
56 #ifndef cimg_imagepath
57 #define cimg_imagepath "img/"
58 #endif
60 // Main procedure
61 //----------------
62 int main(int argc,char **argv) {
64 // Read command line arguments.
65 cimg_usage("Render an image as a surface");
66 const char *file_i = cimg_option("-i",cimg_imagepath "logo.bmp","Input image");
67 const char *file_o = cimg_option("-o",(char*)0,"Output 3D object");
68 const float sigma = cimg_option("-smooth",1.0f,"Amount of image smoothing");
69 const float ratioz = cimg_option("-z",0.25f,"Aspect ratio along z-axis");
70 const unsigned int di = cimg_option("-di",10,"Step for isophote skipping");
72 // Load 2D image file.
73 std::fprintf(stderr,"\n- Load file '%s'",cimg::basename(file_i)); std::fflush(stderr);
74 const CImg<unsigned char>
75 img = CImg<>(file_i).blur(sigma).resize(-100,-100,1,3),
76 norm = img.get_pointwise_norm().normalize(0,255);
78 // Compute surface with triangles.
79 std::fprintf(stderr,"\n- Create image surface"); std::fflush(stderr);
80 CImgList<unsigned int> primitives;
81 CImgList<unsigned char> colors;
82 const CImg<> points = img.get_elevation3d(primitives,colors,norm*-ratioz);
84 // Compute image isophotes.
85 std::fprintf(stderr,"\n- Compute image isophotes"); std::fflush(stderr);
86 CImgList<unsigned int> isoprimitives;
87 CImgList<unsigned char> isocolors;
88 CImg<> isopoints;
89 for (unsigned int i = 0; i<255; i+=di) {
90 CImgList<> prims;
91 const CImg<> pts = norm.get_isovalue3d(prims,(float)i);
92 isopoints.append_object3d(isoprimitives,pts,prims);
93 }
94 cimglist_for(isoprimitives,l) {
95 const unsigned int i0 = isoprimitives(l,0);
96 const float x0 = isopoints(i0,0), y0 = isopoints(i0,1);
97 const unsigned char
98 r = (unsigned char)img.linear_atXY(x0,y0,0),
99 g = (unsigned char)img.linear_atXY(x0,y0,1),
100 b = (unsigned char)img.linear_atXY(x0,y0,2);
101 isocolors.insert(CImg<unsigned char>::vector(r,g,b));
102 }
103 cimg_forX(isopoints,ll) isopoints(ll,2) = -ratioz*norm.linear_atXY(isopoints(ll,0),isopoints(ll,1));
105 // Save object if necessary
106 if (file_o) {
107 std::fprintf(stderr,"\n- Save 3d object as '%s'",cimg::basename(file_o)); std::fflush(stderr);
108 points.save_off(file_o,primitives,colors);
109 }
111 // Enter event loop
112 std::fprintf(stderr,
113 "\n- Enter interactive loop.\n\n"
114 "Reminder : \n"
115 " + Use mouse to rotate and zoom object\n"
116 " + key 'F' : Toggle fullscreen\n"
117 " + key 'Q' or 'ESC' : Quit\n"
118 " + Any other key : Change rendering type\n\n"); std::fflush(stderr);
119 const char *const title = "Image viewed as a surface";
120 CImgDisplay disp(800,600,title,0);
121 unsigned int rtype = 2;
122 CImg<float> pose = CImg<float>::identity_matrix(4);
124 while (!disp.is_closed) {
125 const unsigned char white[3]={ 255, 255, 255 };
126 CImg<unsigned char> visu(disp.dimx(), disp.dimy(),1,3,0);
127 visu.draw_text(10,10,"Render : %s",white,0,1,19,
128 rtype==0?"Points":(rtype==1?"Lines":(rtype==2?"Faces":(rtype==3?"Flat-shaded faces":
129 (rtype==4?"Gouraud-shaded faces":(rtype==5?"Phong-shaded faces":"Isophotes"))))));
130 if (rtype==6) visu.display_object3d(disp,isopoints,isoprimitives,isocolors,true,1,-1,true,500.0f,0.0f,0.0f,true,pose.ptr());
131 else visu.display_object3d(disp,points,primitives,colors,true,rtype,-1,true,500.0f,0.0f,0.0f,true,pose.ptr());
132 switch (disp.key) {
133 case 0: break;
134 case cimg::keyBACKSPACE: rtype = (7+rtype-1)%7; break;
135 case cimg::keyQ:
136 case cimg::keyESC: disp.close(); break;
137 case cimg::keyF:
138 if (disp.is_fullscreen) disp.resize(800,600); else disp.resize(disp.screen_dimx(),disp.screen_dimy());
139 disp.toggle_fullscreen();
140 break;
141 default: rtype = (rtype+1)%7; break;
142 }
143 }
145 return 0;
146 }