PTdecode/CImg-1.3.0/examples/image_surface.cpp

Mon, 03 Aug 2009 14:09:20 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 03 Aug 2009 14:09:20 +0100
changeset 5
1204ebf9340d
permissions
-rwxr-xr-x

added P-touch decoder source

philpem@5 1 /*
philpem@5 2 #
philpem@5 3 # File : image_surface.cpp
philpem@5 4 # ( C++ source file )
philpem@5 5 #
philpem@5 6 # Description : This tool allows to show an image as a 3D surface.
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 "CImg.h"
philpem@5 45 using namespace cimg_library;
philpem@5 46
philpem@5 47 // The lines below are necessary when using a non-standard compiler as visualcpp6.
philpem@5 48 #ifdef cimg_use_visualcpp6
philpem@5 49 #define std
philpem@5 50 #endif
philpem@5 51 #ifdef min
philpem@5 52 #undef min
philpem@5 53 #undef max
philpem@5 54 #endif
philpem@5 55
philpem@5 56 #ifndef cimg_imagepath
philpem@5 57 #define cimg_imagepath "img/"
philpem@5 58 #endif
philpem@5 59
philpem@5 60 // Main procedure
philpem@5 61 //----------------
philpem@5 62 int main(int argc,char **argv) {
philpem@5 63
philpem@5 64 // Read command line arguments.
philpem@5 65 cimg_usage("Render an image as a surface");
philpem@5 66 const char *file_i = cimg_option("-i",cimg_imagepath "logo.bmp","Input image");
philpem@5 67 const char *file_o = cimg_option("-o",(char*)0,"Output 3D object");
philpem@5 68 const float sigma = cimg_option("-smooth",1.0f,"Amount of image smoothing");
philpem@5 69 const float ratioz = cimg_option("-z",0.25f,"Aspect ratio along z-axis");
philpem@5 70 const unsigned int di = cimg_option("-di",10,"Step for isophote skipping");
philpem@5 71
philpem@5 72 // Load 2D image file.
philpem@5 73 std::fprintf(stderr,"\n- Load file '%s'",cimg::basename(file_i)); std::fflush(stderr);
philpem@5 74 const CImg<unsigned char>
philpem@5 75 img = CImg<>(file_i).blur(sigma).resize(-100,-100,1,3),
philpem@5 76 norm = img.get_pointwise_norm().normalize(0,255);
philpem@5 77
philpem@5 78 // Compute surface with triangles.
philpem@5 79 std::fprintf(stderr,"\n- Create image surface"); std::fflush(stderr);
philpem@5 80 CImgList<unsigned int> primitives;
philpem@5 81 CImgList<unsigned char> colors;
philpem@5 82 const CImg<> points = img.get_elevation3d(primitives,colors,norm*-ratioz);
philpem@5 83
philpem@5 84 // Compute image isophotes.
philpem@5 85 std::fprintf(stderr,"\n- Compute image isophotes"); std::fflush(stderr);
philpem@5 86 CImgList<unsigned int> isoprimitives;
philpem@5 87 CImgList<unsigned char> isocolors;
philpem@5 88 CImg<> isopoints;
philpem@5 89 for (unsigned int i = 0; i<255; i+=di) {
philpem@5 90 CImgList<> prims;
philpem@5 91 const CImg<> pts = norm.get_isovalue3d(prims,(float)i);
philpem@5 92 isopoints.append_object3d(isoprimitives,pts,prims);
philpem@5 93 }
philpem@5 94 cimglist_for(isoprimitives,l) {
philpem@5 95 const unsigned int i0 = isoprimitives(l,0);
philpem@5 96 const float x0 = isopoints(i0,0), y0 = isopoints(i0,1);
philpem@5 97 const unsigned char
philpem@5 98 r = (unsigned char)img.linear_atXY(x0,y0,0),
philpem@5 99 g = (unsigned char)img.linear_atXY(x0,y0,1),
philpem@5 100 b = (unsigned char)img.linear_atXY(x0,y0,2);
philpem@5 101 isocolors.insert(CImg<unsigned char>::vector(r,g,b));
philpem@5 102 }
philpem@5 103 cimg_forX(isopoints,ll) isopoints(ll,2) = -ratioz*norm.linear_atXY(isopoints(ll,0),isopoints(ll,1));
philpem@5 104
philpem@5 105 // Save object if necessary
philpem@5 106 if (file_o) {
philpem@5 107 std::fprintf(stderr,"\n- Save 3d object as '%s'",cimg::basename(file_o)); std::fflush(stderr);
philpem@5 108 points.save_off(file_o,primitives,colors);
philpem@5 109 }
philpem@5 110
philpem@5 111 // Enter event loop
philpem@5 112 std::fprintf(stderr,
philpem@5 113 "\n- Enter interactive loop.\n\n"
philpem@5 114 "Reminder : \n"
philpem@5 115 " + Use mouse to rotate and zoom object\n"
philpem@5 116 " + key 'F' : Toggle fullscreen\n"
philpem@5 117 " + key 'Q' or 'ESC' : Quit\n"
philpem@5 118 " + Any other key : Change rendering type\n\n"); std::fflush(stderr);
philpem@5 119 const char *const title = "Image viewed as a surface";
philpem@5 120 CImgDisplay disp(800,600,title,0);
philpem@5 121 unsigned int rtype = 2;
philpem@5 122 CImg<float> pose = CImg<float>::identity_matrix(4);
philpem@5 123
philpem@5 124 while (!disp.is_closed) {
philpem@5 125 const unsigned char white[3]={ 255, 255, 255 };
philpem@5 126 CImg<unsigned char> visu(disp.dimx(), disp.dimy(),1,3,0);
philpem@5 127 visu.draw_text(10,10,"Render : %s",white,0,1,19,
philpem@5 128 rtype==0?"Points":(rtype==1?"Lines":(rtype==2?"Faces":(rtype==3?"Flat-shaded faces":
philpem@5 129 (rtype==4?"Gouraud-shaded faces":(rtype==5?"Phong-shaded faces":"Isophotes"))))));
philpem@5 130 if (rtype==6) visu.display_object3d(disp,isopoints,isoprimitives,isocolors,true,1,-1,true,500.0f,0.0f,0.0f,true,pose.ptr());
philpem@5 131 else visu.display_object3d(disp,points,primitives,colors,true,rtype,-1,true,500.0f,0.0f,0.0f,true,pose.ptr());
philpem@5 132 switch (disp.key) {
philpem@5 133 case 0: break;
philpem@5 134 case cimg::keyBACKSPACE: rtype = (7+rtype-1)%7; break;
philpem@5 135 case cimg::keyQ:
philpem@5 136 case cimg::keyESC: disp.close(); break;
philpem@5 137 case cimg::keyF:
philpem@5 138 if (disp.is_fullscreen) disp.resize(800,600); else disp.resize(disp.screen_dimx(),disp.screen_dimy());
philpem@5 139 disp.toggle_fullscreen();
philpem@5 140 break;
philpem@5 141 default: rtype = (rtype+1)%7; break;
philpem@5 142 }
philpem@5 143 }
philpem@5 144
philpem@5 145 return 0;
philpem@5 146 }