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

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