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