PTdecode/CImg-1.3.0/examples/use_RGBclass.cpp

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