MCPcopy Create free account
hub / github.com/GreycLab/CImg / main

Function main

examples/image_surface3d.cpp:52–140  ·  view source on GitHub ↗

Main procedure ----------------

Source from the content-addressed store, hash-verified

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";

Callers

nothing calls this directly

Calls 1

cimg_forXFunction · 0.70

Tested by

no test coverage detected