PTdecode/CImg-1.3.0/examples/fade_images.cpp

Mon, 03 Aug 2009 14:21:23 +0100

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

added keepme for ptdecode obj directory

philpem@5 1 /*
philpem@5 2 #
philpem@5 3 # File : fade_images.cpp
philpem@5 4 # ( C++ source file )
philpem@5 5 #
philpem@5 6 # Description : Compute a linear fading between two images.
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
philpem@5 46 // The lines below are necessary when using a non-standard compiler as visualcpp6.
philpem@5 47 #ifdef cimg_use_visualcpp6
philpem@5 48 #define std
philpem@5 49 #endif
philpem@5 50 #ifdef min
philpem@5 51 #undef min
philpem@5 52 #undef max
philpem@5 53 #endif
philpem@5 54
philpem@5 55 #ifndef cimg_imagepath
philpem@5 56 #define cimg_imagepath "img/"
philpem@5 57 #endif
philpem@5 58
philpem@5 59 // Main procedure
philpem@5 60 //---------------
philpem@5 61 int main(int argc,char **argv) {
philpem@5 62
philpem@5 63 // Read and check command line parameters
philpem@5 64 cimg_usage("Compute a linear fading between two 2D images");
philpem@5 65 const char *file_i1 = cimg_option("-i1",cimg_imagepath "sh0r.pgm","Input Image 1");
philpem@5 66 const char *file_i2 = cimg_option("-i2",cimg_imagepath "milla.bmp","Input Image 2");
philpem@5 67 const char *file_o = cimg_option("-o",(char*)0,"Output Image");
philpem@5 68 const bool visu = cimg_option("-visu",true,"Visualization mode");
philpem@5 69 const double pmin = cimg_option("-min",40.0,"Begin of the fade (in %)")/100.0;
philpem@5 70 const double pmax = cimg_option("-max",60.0,"End of the fade (in %)")/100.0;
philpem@5 71 const double angle = cimg_option("-angle",0.0,"Fade angle")*cimg_library::cimg::valuePI/180;
philpem@5 72
philpem@5 73 // Init images
philpem@5 74 cimg_library::CImg<unsigned char> img1(file_i1), img2(file_i2);
philpem@5 75 if (img2.dimx()!=img1.dimx() || img2.dimy()!=img1.dimy() || img2.dimz()!=img1.dimz() || img2.dimv()!=img1.dimv()) {
philpem@5 76 int
philpem@5 77 dx = cimg_library::cimg::max(img1.dimx(),img2.dimx()),
philpem@5 78 dy = cimg_library::cimg::max(img1.dimy(),img2.dimy()),
philpem@5 79 dz = cimg_library::cimg::max(img1.dimz(),img2.dimz()),
philpem@5 80 dv = cimg_library::cimg::max(img1.dimv(),img2.dimv());
philpem@5 81 img1.resize(dx,dy,dz,dv,3);
philpem@5 82 img2.resize(dx,dy,dz,dv,3);
philpem@5 83 }
philpem@5 84 cimg_library::CImg<unsigned char> dest(img1.dimx(),img1.dimy(),img1.dimz(),img1.dimv());
philpem@5 85
philpem@5 86 // Compute the faded image
philpem@5 87 const double ca=std::cos(angle), sa=std::sin(angle);
philpem@5 88 double alpha;
philpem@5 89 cimg_forXYZV(dest,x,y,z,k) {
philpem@5 90 const double X = ((double)x/img1.dimx()-0.5)*ca + ((double)y/img1.dimy()-0.5)*sa;
philpem@5 91 if (X+0.5<pmin) alpha=0; else {
philpem@5 92 if (X+0.5>pmax) alpha=1; else alpha=(X+0.5-pmin)/(pmax-pmin);
philpem@5 93 }
philpem@5 94 dest(x,y,z,k) = (unsigned char)((1-alpha)*img1(x,y,z,k) + alpha*img2(x,y,z,k));
philpem@5 95 }
philpem@5 96
philpem@5 97 // Save and exit
philpem@5 98 if (file_o) dest.save(file_o);
philpem@5 99 if (visu) dest.display("Image fading");
philpem@5 100 return 0;
philpem@5 101 }