PTdecode/CImg-1.3.0/examples/image_surface.cpp

changeset 5
1204ebf9340d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/PTdecode/CImg-1.3.0/examples/image_surface.cpp	Mon Aug 03 14:09:20 2009 +0100
     1.3 @@ -0,0 +1,146 @@
     1.4 +/*
     1.5 + #
     1.6 + #  File        : image_surface.cpp
     1.7 + #                ( C++ source file )
     1.8 + #
     1.9 + #  Description : This tool allows to show an image as a 3D surface.
    1.10 + #                This file is a part of the CImg Library project.
    1.11 + #                ( http://cimg.sourceforge.net )
    1.12 + #
    1.13 + #  Copyright   : David Tschumperle
    1.14 + #                ( http://www.greyc.ensicaen.fr/~dtschump/ )
    1.15 + #
    1.16 + #  License     : CeCILL v2.0
    1.17 + #                ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
    1.18 + #
    1.19 + #  This software is governed by the CeCILL  license under French law and
    1.20 + #  abiding by the rules of distribution of free software.  You can  use,
    1.21 + #  modify and/ or redistribute the software under the terms of the CeCILL
    1.22 + #  license as circulated by CEA, CNRS and INRIA at the following URL
    1.23 + #  "http://www.cecill.info".
    1.24 + #
    1.25 + #  As a counterpart to the access to the source code and  rights to copy,
    1.26 + #  modify and redistribute granted by the license, users are provided only
    1.27 + #  with a limited warranty  and the software's author,  the holder of the
    1.28 + #  economic rights,  and the successive licensors  have only  limited
    1.29 + #  liability.
    1.30 + #
    1.31 + #  In this respect, the user's attention is drawn to the risks associated
    1.32 + #  with loading,  using,  modifying and/or developing or reproducing the
    1.33 + #  software by the user in light of its specific status of free software,
    1.34 + #  that may mean  that it is complicated to manipulate,  and  that  also
    1.35 + #  therefore means  that it is reserved for developers  and  experienced
    1.36 + #  professionals having in-depth computer knowledge. Users are therefore
    1.37 + #  encouraged to load and test the software's suitability as regards their
    1.38 + #  requirements in conditions enabling the security of their systems and/or
    1.39 + #  data to be ensured and,  more generally, to use and operate it in the
    1.40 + #  same conditions as regards security.
    1.41 + #
    1.42 + #  The fact that you are presently reading this means that you have had
    1.43 + #  knowledge of the CeCILL license and that you accept its terms.
    1.44 + #
    1.45 +*/
    1.46 +
    1.47 +#include "CImg.h"
    1.48 +using namespace cimg_library;
    1.49 +
    1.50 +// The lines below are necessary when using a non-standard compiler as visualcpp6.
    1.51 +#ifdef cimg_use_visualcpp6
    1.52 +#define std
    1.53 +#endif
    1.54 +#ifdef min
    1.55 +#undef min
    1.56 +#undef max
    1.57 +#endif
    1.58 +
    1.59 +#ifndef cimg_imagepath
    1.60 +#define cimg_imagepath "img/"
    1.61 +#endif
    1.62 +
    1.63 +// Main procedure
    1.64 +//----------------
    1.65 +int main(int argc,char **argv) {
    1.66 +
    1.67 +  // Read command line arguments.
    1.68 +  cimg_usage("Render an image as a surface");
    1.69 +  const char *file_i    = cimg_option("-i",cimg_imagepath "logo.bmp","Input image");
    1.70 +  const char *file_o    = cimg_option("-o",(char*)0,"Output 3D object");
    1.71 +  const float sigma     = cimg_option("-smooth",1.0f,"Amount of image smoothing");
    1.72 +  const float ratioz    = cimg_option("-z",0.25f,"Aspect ratio along z-axis");
    1.73 +  const unsigned int di = cimg_option("-di",10,"Step for isophote skipping");
    1.74 +
    1.75 +  // Load 2D image file.
    1.76 +  std::fprintf(stderr,"\n- Load file '%s'",cimg::basename(file_i)); std::fflush(stderr);
    1.77 +  const CImg<unsigned char>
    1.78 +    img  = CImg<>(file_i).blur(sigma).resize(-100,-100,1,3),
    1.79 +    norm = img.get_pointwise_norm().normalize(0,255);
    1.80 +
    1.81 +  // Compute surface with triangles.
    1.82 +  std::fprintf(stderr,"\n- Create image surface"); std::fflush(stderr);
    1.83 +  CImgList<unsigned int> primitives;
    1.84 +  CImgList<unsigned char> colors;
    1.85 +  const CImg<> points = img.get_elevation3d(primitives,colors,norm*-ratioz);
    1.86 +
    1.87 +  // Compute image isophotes.
    1.88 +  std::fprintf(stderr,"\n- Compute image isophotes"); std::fflush(stderr);
    1.89 +  CImgList<unsigned int> isoprimitives;
    1.90 +  CImgList<unsigned char> isocolors;
    1.91 +  CImg<> isopoints;
    1.92 +  for (unsigned int i = 0; i<255; i+=di) {
    1.93 +    CImgList<> prims;
    1.94 +    const CImg<> pts = norm.get_isovalue3d(prims,(float)i);
    1.95 +    isopoints.append_object3d(isoprimitives,pts,prims);
    1.96 +  }
    1.97 +  cimglist_for(isoprimitives,l) {
    1.98 +    const unsigned int i0 = isoprimitives(l,0);
    1.99 +    const float x0 = isopoints(i0,0), y0 = isopoints(i0,1);
   1.100 +    const unsigned char
   1.101 +      r = (unsigned char)img.linear_atXY(x0,y0,0),
   1.102 +      g = (unsigned char)img.linear_atXY(x0,y0,1),
   1.103 +      b = (unsigned char)img.linear_atXY(x0,y0,2);
   1.104 +    isocolors.insert(CImg<unsigned char>::vector(r,g,b));
   1.105 +  }
   1.106 +  cimg_forX(isopoints,ll) isopoints(ll,2) = -ratioz*norm.linear_atXY(isopoints(ll,0),isopoints(ll,1));
   1.107 +
   1.108 +  // Save object if necessary
   1.109 +  if (file_o) {
   1.110 +    std::fprintf(stderr,"\n- Save 3d object as '%s'",cimg::basename(file_o)); std::fflush(stderr);
   1.111 +    points.save_off(file_o,primitives,colors);
   1.112 +  }
   1.113 +
   1.114 +  // Enter event loop
   1.115 +  std::fprintf(stderr,
   1.116 +               "\n- Enter interactive loop.\n\n"
   1.117 +               "Reminder : \n"
   1.118 +               " + Use mouse to rotate and zoom object\n"
   1.119 +               " + key 'F'          : Toggle fullscreen\n"
   1.120 +               " + key 'Q' or 'ESC' : Quit\n"
   1.121 +               " + Any other key    : Change rendering type\n\n"); std::fflush(stderr);
   1.122 +  const char *const title = "Image viewed as a surface";
   1.123 +  CImgDisplay disp(800,600,title,0);
   1.124 +  unsigned int rtype = 2;
   1.125 +  CImg<float> pose = CImg<float>::identity_matrix(4);
   1.126 +
   1.127 +  while (!disp.is_closed) {
   1.128 +    const unsigned char white[3]={ 255, 255, 255 };
   1.129 +    CImg<unsigned char> visu(disp.dimx(), disp.dimy(),1,3,0);
   1.130 +    visu.draw_text(10,10,"Render : %s",white,0,1,19,
   1.131 +                rtype==0?"Points":(rtype==1?"Lines":(rtype==2?"Faces":(rtype==3?"Flat-shaded faces":
   1.132 +               (rtype==4?"Gouraud-shaded faces":(rtype==5?"Phong-shaded faces":"Isophotes"))))));
   1.133 +    if (rtype==6) visu.display_object3d(disp,isopoints,isoprimitives,isocolors,true,1,-1,true,500.0f,0.0f,0.0f,true,pose.ptr());
   1.134 +    else visu.display_object3d(disp,points,primitives,colors,true,rtype,-1,true,500.0f,0.0f,0.0f,true,pose.ptr());
   1.135 +    switch (disp.key) {
   1.136 +    case 0: break;
   1.137 +    case cimg::keyBACKSPACE: rtype = (7+rtype-1)%7; break;
   1.138 +    case cimg::keyQ:
   1.139 +    case cimg::keyESC: disp.close(); break;
   1.140 +    case cimg::keyF:
   1.141 +      if (disp.is_fullscreen) disp.resize(800,600); else disp.resize(disp.screen_dimx(),disp.screen_dimy());
   1.142 +      disp.toggle_fullscreen();
   1.143 +      break;
   1.144 +    default: rtype = (rtype+1)%7; break;
   1.145 +    }
   1.146 +  }
   1.147 +
   1.148 +  return 0;
   1.149 +}