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