PTdecode/CImg-1.3.0/examples/use_RGBclass.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/use_RGBclass.cpp	Mon Aug 03 14:09:20 2009 +0100
     1.3 @@ -0,0 +1,142 @@
     1.4 +/*
     1.5 + #
     1.6 + #  File        : use_RGBclass.cpp
     1.7 + #                ( C++ source file )
     1.8 + #
     1.9 + #  Description : A small code that shows how to write a CImg plugin to
    1.10 + #                handle color image manipulation using a user-defined RGB
    1.11 + #                class, instead of using classical pixel access of CImg<T>
    1.12 + #                with operator().
    1.13 + #                This file is a part of the CImg Library project.
    1.14 + #                ( http://cimg.sourceforge.net )
    1.15 + #
    1.16 + #  Note        : This file cannot be compiled on VC++ 6.
    1.17 + #
    1.18 + #  Copyright   : David Tschumperle
    1.19 + #                ( http://www.greyc.ensicaen.fr/~dtschump/ )
    1.20 + #
    1.21 + #  License     : CeCILL v2.0
    1.22 + #                ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
    1.23 + #
    1.24 + #  This software is governed by the CeCILL  license under French law and
    1.25 + #  abiding by the rules of distribution of free software.  You can  use,
    1.26 + #  modify and/ or redistribute the software under the terms of the CeCILL
    1.27 + #  license as circulated by CEA, CNRS and INRIA at the following URL
    1.28 + #  "http://www.cecill.info".
    1.29 + #
    1.30 + #  As a counterpart to the access to the source code and rights to copy,
    1.31 + #  modify and redistribute granted by the license, users are provided only
    1.32 + #  with a limited warranty  and the software's author,  the holder of the
    1.33 + #  economic rights,  and the successive licensors  have only  limited
    1.34 + #  liability.
    1.35 + #
    1.36 + #  In this respect, the user's attention is drawn to the risks associated
    1.37 + #  with loading,  using,  modifying and/or developing or reproducing the
    1.38 + #  software by the user in light of its specific status of free software,
    1.39 + #  that may mean  that it is complicated to manipulate,  and  that  also
    1.40 + #  therefore means  that it is reserved for developers  and  experienced
    1.41 + #  professionals having in-depth computer knowledge. Users are therefore
    1.42 + #  encouraged to load and test the software's suitability as regards their
    1.43 + #  requirements in conditions enabling the security of their systems and/or
    1.44 + #  data to be ensured and,  more generally, to use and operate it in the
    1.45 + #  same conditions as regards security.
    1.46 + #
    1.47 + #  The fact that you are presently reading this means that you have had
    1.48 + #  knowledge of the CeCILL license and that you accept its terms.
    1.49 + #
    1.50 +*/
    1.51 +
    1.52 +#ifndef cimg_plugin
    1.53 +#define cimg_plugin "examples/use_RGBclass.cpp"  // Path of the plugin is relative to the CImg.h file.
    1.54 +#include "CImg.h"
    1.55 +using namespace cimg_library;
    1.56 +
    1.57 +#ifndef cimg_imagepath
    1.58 +#define cimg_imagepath "img/"
    1.59 +#endif
    1.60 +
    1.61 +//----------------
    1.62 +// Main procedure
    1.63 +//----------------
    1.64 +int main() {
    1.65 +
    1.66 +  // Load images.
    1.67 +  CImg<short> img1(cimg_imagepath "milla.bmp");
    1.68 +  const CImg<float> img2 = CImg<float>(cimg_imagepath "lena.pgm").resize(img1,3);
    1.69 +  const float default_color[] = { 30,30,80 };
    1.70 +
    1.71 +  // Modify 'img1' using the RGB pixel accessor.
    1.72 +  cimg_forXY(img1,x,y)
    1.73 +    if (!((x*y)%31)) img1.RGB_at(x,y) = default_color;
    1.74 +    else if ((x+y)%2) img1.RGB_at(x,y) = img2.RGB_at(x,y);
    1.75 +  img1.display();
    1.76 +
    1.77 +  // Quit.
    1.78 +  return 0;
    1.79 +}
    1.80 +
    1.81 +#else
    1.82 +
    1.83 +//-------------------------
    1.84 +// Start of the plugin code
    1.85 +//-------------------------
    1.86 +
    1.87 +// Define a simple structure of *references* to R,G,B values.
    1.88 +//-----------------------------------------------------------
    1.89 +// (Feel free to add your own operators in there !)
    1.90 +struct st_RGB {
    1.91 +  T _R,_G,_B,&R,&G,&B;
    1.92 +
    1.93 +  // Construct from R,G,B references of values.
    1.94 +  st_RGB(const T& nR, const T& nG, const T& nB):_R(nR),_G(nG),_B(nB),R(_R),G(_G),B(_B) {}
    1.95 +  st_RGB(T& nR, T& nG, T& nB):R(nR),G(nG),B(nB) {}
    1.96 +
    1.97 +  // Copy constructors.
    1.98 +  st_RGB(const st_RGB& rgb):_R(rgb.R),_G(rgb.G),_B(rgb.B),R(_R),G(_G),B(_B) {}
    1.99 +  template<typename t>
   1.100 +  st_RGB(const t& rgb):_R(rgb[0]),_G(rgb[1]),_B(rgb[2]) {}
   1.101 +
   1.102 +  // Assignement operator.
   1.103 +  st_RGB& operator=(const st_RGB& rgb) {
   1.104 +    R = (T)(rgb[0]); G = (T)(rgb[1]); B = (T)(rgb[2]);
   1.105 +    return *this;
   1.106 +  }
   1.107 +  template<typename t>
   1.108 +  st_RGB& operator=(const t& rgb) {
   1.109 +    R = (T)(rgb[0]); G = (T)(rgb[1]); B = (T)(rgb[2]);
   1.110 +    return *this;
   1.111 +  }
   1.112 +
   1.113 +  // Data (R,G or B) access operator.
   1.114 +  const T& operator[](const unsigned int i) const {
   1.115 +    return i==2?B:(i==1?G:R);
   1.116 +  }
   1.117 +  T& operator[](const unsigned int i) {
   1.118 +    return i==2?B:(i==1?G:R);
   1.119 +  }
   1.120 +
   1.121 +  // Print instance on the standard error.
   1.122 +  const st_RGB& print() const {
   1.123 +    std::fprintf(stderr,"{ %d %d %d }\n",(int)R,(int)G,(int)B);
   1.124 +    return *this;
   1.125 +  }
   1.126 +};
   1.127 +
   1.128 +// Define CImg<T> member functions which return pixel values as st_RGB instances.
   1.129 +//--------------------------------------------------------------------------------
   1.130 +const st_RGB RGB_at(const int x, const int y=0, const int z=0) const {
   1.131 +  const long whz = width*height*depth;
   1.132 +  const T *const pR = data + x + y*width + z*width*height, *const pG = pR + whz, *const pB = pG + whz;
   1.133 +  return st_RGB(*pR,*pG,*pB);
   1.134 +}
   1.135 +
   1.136 +st_RGB RGB_at(const int x, const int y=0, const int z=0) {
   1.137 +  const long whz = width*height*depth;
   1.138 +  T *const pR = data + x + y*width + z*width*height, *const pG = pR + whz, *const pB = pG + whz;
   1.139 +  return st_RGB(*pR,*pG,*pB);
   1.140 +}
   1.141 +
   1.142 +//------------------------
   1.143 +// End of the plugin code
   1.144 +//------------------------
   1.145 +#endif