PTdecode/CImg-1.3.0/examples/use_RGBclass.cpp

Wed, 05 Aug 2009 17:10:56 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 05 Aug 2009 17:10:56 +0100
changeset 17
cf9d239ac1c9
parent 5
1204ebf9340d
permissions
-rwxr-xr-x

add README

     1 /*
     2  #
     3  #  File        : use_RGBclass.cpp
     4  #                ( C++ source file )
     5  #
     6  #  Description : A small code that shows how to write a CImg plugin to
     7  #                handle color image manipulation using a user-defined RGB
     8  #                class, instead of using classical pixel access of CImg<T>
     9  #                with operator().
    10  #                This file is a part of the CImg Library project.
    11  #                ( http://cimg.sourceforge.net )
    12  #
    13  #  Note        : This file cannot be compiled on VC++ 6.
    14  #
    15  #  Copyright   : David Tschumperle
    16  #                ( http://www.greyc.ensicaen.fr/~dtschump/ )
    17  #
    18  #  License     : CeCILL v2.0
    19  #                ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
    20  #
    21  #  This software is governed by the CeCILL  license under French law and
    22  #  abiding by the rules of distribution of free software.  You can  use,
    23  #  modify and/ or redistribute the software under the terms of the CeCILL
    24  #  license as circulated by CEA, CNRS and INRIA at the following URL
    25  #  "http://www.cecill.info".
    26  #
    27  #  As a counterpart to the access to the source code and rights to copy,
    28  #  modify and redistribute granted by the license, users are provided only
    29  #  with a limited warranty  and the software's author,  the holder of the
    30  #  economic rights,  and the successive licensors  have only  limited
    31  #  liability.
    32  #
    33  #  In this respect, the user's attention is drawn to the risks associated
    34  #  with loading,  using,  modifying and/or developing or reproducing the
    35  #  software by the user in light of its specific status of free software,
    36  #  that may mean  that it is complicated to manipulate,  and  that  also
    37  #  therefore means  that it is reserved for developers  and  experienced
    38  #  professionals having in-depth computer knowledge. Users are therefore
    39  #  encouraged to load and test the software's suitability as regards their
    40  #  requirements in conditions enabling the security of their systems and/or
    41  #  data to be ensured and,  more generally, to use and operate it in the
    42  #  same conditions as regards security.
    43  #
    44  #  The fact that you are presently reading this means that you have had
    45  #  knowledge of the CeCILL license and that you accept its terms.
    46  #
    47 */
    49 #ifndef cimg_plugin
    50 #define cimg_plugin "examples/use_RGBclass.cpp"  // Path of the plugin is relative to the CImg.h file.
    51 #include "CImg.h"
    52 using namespace cimg_library;
    54 #ifndef cimg_imagepath
    55 #define cimg_imagepath "img/"
    56 #endif
    58 //----------------
    59 // Main procedure
    60 //----------------
    61 int main() {
    63   // Load images.
    64   CImg<short> img1(cimg_imagepath "milla.bmp");
    65   const CImg<float> img2 = CImg<float>(cimg_imagepath "lena.pgm").resize(img1,3);
    66   const float default_color[] = { 30,30,80 };
    68   // Modify 'img1' using the RGB pixel accessor.
    69   cimg_forXY(img1,x,y)
    70     if (!((x*y)%31)) img1.RGB_at(x,y) = default_color;
    71     else if ((x+y)%2) img1.RGB_at(x,y) = img2.RGB_at(x,y);
    72   img1.display();
    74   // Quit.
    75   return 0;
    76 }
    78 #else
    80 //-------------------------
    81 // Start of the plugin code
    82 //-------------------------
    84 // Define a simple structure of *references* to R,G,B values.
    85 //-----------------------------------------------------------
    86 // (Feel free to add your own operators in there !)
    87 struct st_RGB {
    88   T _R,_G,_B,&R,&G,&B;
    90   // Construct from R,G,B references of values.
    91   st_RGB(const T& nR, const T& nG, const T& nB):_R(nR),_G(nG),_B(nB),R(_R),G(_G),B(_B) {}
    92   st_RGB(T& nR, T& nG, T& nB):R(nR),G(nG),B(nB) {}
    94   // Copy constructors.
    95   st_RGB(const st_RGB& rgb):_R(rgb.R),_G(rgb.G),_B(rgb.B),R(_R),G(_G),B(_B) {}
    96   template<typename t>
    97   st_RGB(const t& rgb):_R(rgb[0]),_G(rgb[1]),_B(rgb[2]) {}
    99   // Assignement operator.
   100   st_RGB& operator=(const st_RGB& rgb) {
   101     R = (T)(rgb[0]); G = (T)(rgb[1]); B = (T)(rgb[2]);
   102     return *this;
   103   }
   104   template<typename t>
   105   st_RGB& operator=(const t& rgb) {
   106     R = (T)(rgb[0]); G = (T)(rgb[1]); B = (T)(rgb[2]);
   107     return *this;
   108   }
   110   // Data (R,G or B) access operator.
   111   const T& operator[](const unsigned int i) const {
   112     return i==2?B:(i==1?G:R);
   113   }
   114   T& operator[](const unsigned int i) {
   115     return i==2?B:(i==1?G:R);
   116   }
   118   // Print instance on the standard error.
   119   const st_RGB& print() const {
   120     std::fprintf(stderr,"{ %d %d %d }\n",(int)R,(int)G,(int)B);
   121     return *this;
   122   }
   123 };
   125 // Define CImg<T> member functions which return pixel values as st_RGB instances.
   126 //--------------------------------------------------------------------------------
   127 const st_RGB RGB_at(const int x, const int y=0, const int z=0) const {
   128   const long whz = width*height*depth;
   129   const T *const pR = data + x + y*width + z*width*height, *const pG = pR + whz, *const pB = pG + whz;
   130   return st_RGB(*pR,*pG,*pB);
   131 }
   133 st_RGB RGB_at(const int x, const int y=0, const int z=0) {
   134   const long whz = width*height*depth;
   135   T *const pR = data + x + y*width + z*width*height, *const pG = pR + whz, *const pB = pG + whz;
   136   return st_RGB(*pR,*pG,*pB);
   137 }
   139 //------------------------
   140 // End of the plugin code
   141 //------------------------
   142 #endif