Austin Schuh | 8c794d5 | 2019-03-03 21:17:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | # |
| 3 | # File : fade_images.cpp |
| 4 | # ( C++ source file ) |
| 5 | # |
| 6 | # Description : Compute a linear fading between two images. |
| 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" |
| 45 | #ifndef cimg_imagepath |
| 46 | #define cimg_imagepath "img/" |
| 47 | #endif |
| 48 | #undef min |
| 49 | #undef max |
| 50 | |
| 51 | // Main procedure |
| 52 | //--------------- |
| 53 | int main(int argc,char **argv) { |
| 54 | |
| 55 | // Read and check command line parameters. |
| 56 | cimg_usage("Compute a linear fading between two 2D images"); |
| 57 | const char *file_i1 = cimg_option("-i1",cimg_imagepath "sh0r.pgm","Input Image 1"); |
| 58 | const char *file_i2 = cimg_option("-i2",cimg_imagepath "milla.bmp","Input Image 2"); |
| 59 | const char *file_o = cimg_option("-o",(char*)0,"Output Image"); |
| 60 | const bool visu = cimg_option("-visu",true,"Visualization mode"); |
| 61 | const double pmin = cimg_option("-min",40.0,"Begin of the fade (in %)")/100.0; |
| 62 | const double pmax = cimg_option("-max",60.0,"End of the fade (in %)")/100.0; |
| 63 | const double angle = cimg_option("-angle",0.0,"Fade angle")*cil::cimg::PI/180; |
| 64 | |
| 65 | // Init images. |
| 66 | cil::CImg<unsigned char> img1(file_i1), img2(file_i2); |
| 67 | if (!img2.is_sameXYZC(img1)) { |
| 68 | int |
| 69 | dx = std::max(img1.width(),img2.width()), |
| 70 | dy = std::max(img1.height(),img2.height()), |
| 71 | dz = std::max(img1.depth(),img2.depth()), |
| 72 | dv = std::max(img1.spectrum(),img2.spectrum()); |
| 73 | img1.resize(dx,dy,dz,dv,3); |
| 74 | img2.resize(dx,dy,dz,dv,3); |
| 75 | } |
| 76 | cil::CImg<unsigned char> dest(img1); |
| 77 | |
| 78 | // Compute the faded image. |
| 79 | const double ca = std::cos(angle), sa = std::sin(angle); |
| 80 | double alpha; |
| 81 | cimg_forXYZC(dest,x,y,z,k) { |
| 82 | const double X = ((double)x/img1.width() - 0.5)*ca + ((double)y/img1.height() - 0.5)*sa; |
| 83 | if (X + 0.5<pmin) alpha = 0; else { |
| 84 | if (X + 0.5>pmax) alpha = 1; else |
| 85 | alpha = (X + 0.5 - pmin)/(pmax - pmin); |
| 86 | } |
| 87 | dest(x,y,z,k) = (unsigned char)((1 - alpha)*img1(x,y,z,k) + alpha*img2(x,y,z,k)); |
| 88 | } |
| 89 | |
| 90 | // Save and exit |
| 91 | if (file_o) dest.save(file_o); |
| 92 | if (visu) dest.display("Image fading"); |
| 93 | return 0; |
| 94 | } |