Austin Schuh | 8c794d5 | 2019-03-03 21:17:37 -0800 | [diff] [blame] | 1 | /*------------------------------------------------------------------------ |
| 2 | # |
| 3 | # File : CImg_documentation.h |
| 4 | # |
| 5 | # Description : Extra documentation file for the CImg Library. |
| 6 | # Used by doxygen to generate the reference documentation. |
| 7 | # ( http://cimg.eu ) |
| 8 | # |
| 9 | # Copyright : David Tschumperle |
| 10 | # ( http://tschumperle.users.greyc.fr/ ) |
| 11 | # |
| 12 | # |
| 13 | -------------------------------------------------------------------------*/ |
| 14 | |
| 15 | /*----------------------------------- |
| 16 | |
| 17 | Main reference documentation page |
| 18 | |
| 19 | -------------------------------------*/ |
| 20 | |
| 21 | /** |
| 22 | \mainpage |
| 23 | |
| 24 | This is the reference documentation of <a href="http://cimg.eu">the CImg Library</a>, |
| 25 | the C++ template image processing library. |
| 26 | This documentation have been generated using the tool <a href="http://www.doxygen.org">doxygen</a>. |
| 27 | It contains a detailed description of all classes and functions of the %CImg Library. |
| 28 | |
| 29 | Use the menu above to navigate through the documentation pages. |
| 30 | As a first step, you may look at the list of <a href="modules.html">available modules</a>. |
| 31 | |
| 32 | You may be interested also in the |
| 33 | <a href="../CImg_slides.pdf">presentation slides</a> presenting an overview |
| 34 | of the %CImg Library capabilities. |
| 35 | |
| 36 | **/ |
| 37 | |
| 38 | /*----------------------------------- |
| 39 | |
| 40 | CImg Library overview |
| 41 | |
| 42 | -------------------------------------*/ |
| 43 | |
| 44 | /** \addtogroup cimg_overview CImg Library Overview */ |
| 45 | /*@{*/ |
| 46 | /** |
| 47 | \page foo2 |
| 48 | |
| 49 | The <b>CImg Library</b> is an image processing library, designed for C++ programmers. |
| 50 | It provides useful classes and functions to load/save, display and process various types of images. |
| 51 | |
| 52 | \section s1 Library structure |
| 53 | |
| 54 | The %CImg Library consists in a single header file <tt>CImg.h</tt> providing a set of C++ template classes that |
| 55 | can be used in your own sources, to load/save, process and display images or list of images. |
| 56 | Very portable (Unix/X11,Windows, MacOS X, FreeBSD,..), efficient, simple to use, it's a pleasant toolkit |
| 57 | for coding image processing stuff in C++. |
| 58 | |
| 59 | The header file <tt>CImg.h</tt> contains all the classes and functions that compose the library itself. |
| 60 | This is one originality of the %CImg Library. This particularly means that : |
| 61 | - No pre-compilation of the library is needed, since the compilation of the CImg functions is done at the same time as |
| 62 | the compilation of your own C++ code. |
| 63 | - No complex dependencies have to be handled : Just include the <tt>CImg.h</tt> file, and you get a working C++ image processing toolkit. |
| 64 | - The compilation is done on the fly : only CImg functionalities really used by your program are compiled and appear in the |
| 65 | compiled executable program. This leads to very compact code, without any unused stuff. |
| 66 | - Class members and functions are inlined, leading to better performance during the program execution. |
| 67 | |
| 68 | The %CImg Library is structured as follows : |
| 69 | |
| 70 | - All library classes and functions are defined in the namespace \ref cimg_library. This namespace |
| 71 | encapsulates the library functionalities and avoid any class name collision that could happen with |
| 72 | other includes. Generally, one uses this namespace as a default namespace : |
| 73 | \code |
| 74 | #include "CImg.h" |
| 75 | using namespace cimg_library; |
| 76 | ... |
| 77 | \endcode |
| 78 | |
| 79 | - The namespace \ref cimg_library::cimg defines a set of \e low-level functions and variables used by the library. |
| 80 | Documented functions in this namespace can be safely used in your own program. But, \b never use the |
| 81 | \ref cimg_library::cimg namespace as a default namespace, since it contains functions whose names are already |
| 82 | defined in the standard C/C++ library. |
| 83 | |
| 84 | - The class \ref cimg_library::CImg represents images up to 4-dimensions wide, containing pixels of type \c T |
| 85 | (template parameter). This is actually the main class of the library. |
| 86 | |
| 87 | - The class \ref cimg_library::CImgList represents lists of cimg_library::CImg<T> images. It can be used for instance |
| 88 | to store different frames of an image sequence. |
| 89 | |
| 90 | - The class \ref cimg_library::CImgDisplay is able to display images or image lists into graphical display windows. |
| 91 | As you may guess, the code of this class is highly system-dependent but this is transparent for the programmer, |
| 92 | as environment variables are automatically set by the CImg library (see also \ref cimg_environment). |
| 93 | |
| 94 | - The class \ref cimg_library::CImgException (and its subclasses) are used by the library to throw exceptions |
| 95 | when errors occur. Those exceptions can be caught with a <tt>try { ..} catch (CImgException) { .. }</tt> block. |
| 96 | Subclasses define precisely the type of encountered errors. |
| 97 | |
| 98 | Knowing these four classes is \b enough to get benefit of the %CImg Library functionalities. |
| 99 | |
| 100 | |
| 101 | \section s2 CImg version of "Hello world". |
| 102 | |
| 103 | Below is some very simple code that creates a "Hello World" image. This shows you basically how a CImg program looks like. |
| 104 | |
| 105 | \code |
| 106 | #include "CImg.h" |
| 107 | using namespace cimg_library; |
| 108 | |
| 109 | int main() { |
| 110 | CImg<unsigned char> img(640,400,1,3); // Define a 640x400 color image with 8 bits per color component. |
| 111 | img.fill(0); // Set pixel values to 0 (color : black) |
| 112 | unsigned char purple[] = { 255,0,255 }; // Define a purple color |
| 113 | img.draw_text(100,100,"Hello World",purple); // Draw a purple "Hello world" at coordinates (100,100). |
| 114 | img.display("My first CImg code"); // Display the image in a display window. |
| 115 | return 0; |
| 116 | } |
| 117 | \endcode |
| 118 | |
| 119 | Which can be also written in a more compact way as : |
| 120 | |
| 121 | \code |
| 122 | #include "CImg.h" |
| 123 | using namespace cimg_library; |
| 124 | |
| 125 | int main() { |
| 126 | const unsigned char purple[] = { 255,0,255 }; |
| 127 | CImg<unsigned char>(640,400,1,3,0).draw_text(100,100,"Hello World",purple).display("My first CImg code"); |
| 128 | return 0; |
| 129 | } |
| 130 | \endcode |
| 131 | |
| 132 | Generally, you can write very small code that performs complex image processing tasks. The %CImg Library is very simple |
| 133 | to use and provides a lot of interesting algorithms for image manipulation. |
| 134 | |
| 135 | \section s3 How to compile ? |
| 136 | |
| 137 | The CImg library is a very light and user-friendly library : only standard system libraries are used. |
| 138 | It avoids handling complex dependencies and problems with library compatibility. |
| 139 | The only thing you need is a (quite modern) C++ compiler : |
| 140 | |
| 141 | - <b>Microsoft Visual Studio.NET and Visual Express Edition</b> : Use the project files and solution files provided in the |
| 142 | %CImg Library package (directory 'compilation/') to see how it works. |
| 143 | - <b>Intel ICL compiler</b> : Use the following command to compile a CImg-based program with ICL : |
| 144 | \code |
| 145 | icl /Ox hello_world.cpp user32.lib gdi32.lib |
| 146 | \endcode |
| 147 | - <b>g++ (MingW windows version)</b> : Use the following command to compile a CImg-based program with g++, on Windows : |
| 148 | \code |
| 149 | g++ -o hello_word.exe hello_word.cpp -O2 -lgdi32 |
| 150 | \endcode |
| 151 | - <b>g++ (Linux version)</b> : Use the following command to compile a CImg-based program with g++, on Linux : |
| 152 | \code |
| 153 | g++ -o hello_word.exe hello_world.cpp -O2 -L/usr/X11R6/lib -lm -lpthread -lX11 |
| 154 | \endcode |
| 155 | - <b>g++ (Solaris version)</b> : Use the following command to compile a CImg-based program with g++, on Solaris : |
| 156 | \code |
| 157 | g++ -o hello_word.exe hello_world.cpp -O2 -lm -lpthread -R/usr/X11R6/lib -lrt -lnsl -lsocket |
| 158 | \endcode |
| 159 | - <b>g++ (Mac OS X version)</b> : Use the following command to compile a CImg-based program with g++, on Mac OS X : |
| 160 | \code |
| 161 | g++ -o hello_word.exe hello_world.cpp -O2 -lm -lpthread -I/usr/X11R6/include -L/usr/X11R6/lib -lm -lpthread -lX11 |
| 162 | \endcode |
| 163 | - <b>Dev-Cpp</b> : Use the project file provided in the CImg library package to see how it works. |
| 164 | |
| 165 | If you are using other compilers and encounter problems, please |
| 166 | <a href="http://www.greyc.ensicaen.fr/~dtschump">write me</a> since maintaining compatibility is one |
| 167 | of the priorities of the %CImg Library. Nevertheless, old compilers that do not respect the C++ standard will not |
| 168 | support the %CImg Library. |
| 169 | |
| 170 | \section s4 What's next ? |
| 171 | |
| 172 | If you are ready to get more, and to start writing more serious programs |
| 173 | with CImg, you are invited to go to the \ref cimg_tutorial section. |
| 174 | |
| 175 | **/ |
| 176 | /*@}*/ |
| 177 | |
| 178 | /*----------------------------------- |
| 179 | |
| 180 | FAQ : Frequently Asked Questions |
| 181 | |
| 182 | -------------------------------------*/ |
| 183 | |
| 184 | /** \addtogroup cimg_faq FAQ : Frequently Asked Questions. */ |
| 185 | /*@{*/ |
| 186 | /** |
| 187 | \page foofaq |
| 188 | |
| 189 | \section ssf0 FAQ Summary |
| 190 | |
| 191 | - <a href="#sf1">General information and availability</a> |
| 192 | - <a href="#ssf11">What is the CImg Library ?</a> |
| 193 | - <a href="#ssf12">What platforms are supported ?</a> |
| 194 | - <a href="#ssf13">How is CImg distributed ?</a> |
| 195 | - <a href="#ssf14">What kind of people are concerned by CImg ?</a> |
| 196 | - <a href="#ssf15">What are the specificities of the CeCILL license ?</a> |
| 197 | - <a href="#ssf16">Who is behind CImg ?</a> |
| 198 | |
| 199 | - <a href="#sf2">C++ related questions</a> |
| 200 | - <a href="#ssf21">What is the level of C++ knowledge needed to use CImg ?</a> |
| 201 | - <a href="#ssf22">How to use CImg in my own C++ program ?</a> |
| 202 | - <a href="#ssf23">Why is CImg entirely contained in a single header file ?</a> |
| 203 | |
| 204 | - <a href="#sf3">Other resources</a> |
| 205 | - <a href="#ssf31">Translations</a> |
| 206 | |
| 207 | \section sf1 1. General information and availability |
| 208 | |
| 209 | \subsection ssf11 1.1. What is the CImg Library ? |
| 210 | |
| 211 | The CImg Library is an <i>open-source C++ toolkit for image processing</i>.\n |
| 212 | |
| 213 | It mainly consists in a (big) single header file |
| 214 | <a href="https://framagit.org/dtschump/CImg/raw/master/CImg.h">CImg.h</a> |
| 215 | providing a set of C++ classes and functions that can be used in your own sources, |
| 216 | to load/save, manage/process and display generic images. |
| 217 | It's actually a very simple and pleasant toolkit for coding image processing stuff in C++ : |
| 218 | Just include the header file <tt>CImg.h</tt>, and you are ready to handle images in your C++ programs. |
| 219 | |
| 220 | \subsection ssf12 1.2. What platforms are supported ? |
| 221 | |
| 222 | CImg has been designed with <i>portability</i> in mind. |
| 223 | It is regularly tested on different architectures and compilers, |
| 224 | and should also work on any decent OS having a decent C++ compiler. |
| 225 | Before each release, the CImg Library is compiled under these different configurations : |
| 226 | \li PC Linux 32/64 bits, with g++. |
| 227 | \li PC Windows 32/64 bits, with Visual C++ Express Edition. |
| 228 | |
| 229 | CImg has a minimal number of dependencies. In its minimal version, it can be compiled only with standard C++ headers. |
| 230 | Anyway, it has interesting extension capabilities and can use external libraries to perform specific tasks more |
| 231 | efficiently (Fourier Transform computation using FFTW for instance). |
| 232 | |
| 233 | \subsection ssf13 1.3. How is CImg distributed ? |
| 234 | |
| 235 | The CImg Library is freely distributed as a complete .zip compressed package, hosted at the |
| 236 | <a href="http://cimg.eu/files">CImg server</a>.\n |
| 237 | The package is distributed under the <a href="http://www.cecill.info">CeCILL license</a>. |
| 238 | |
| 239 | This package contains : |
| 240 | - The main library file <a href="https://framagit.org/dtschump/CImg/raw/master/CImg.h">CImg.h</a> (C++ header file). |
| 241 | - Several C++ source code showing <a href="https://framagit.org/dtschump/CImg/tree/master/examples">examples of using CImg</a>. |
| 242 | - A complete library documentation, in <a href="../CImg_reference.pdf">PDF</a> format. |
| 243 | - Additional <a href="https://framagit.org/dtschump/CImg/tree/master/plugins">library plug-ins</a> that can be used to extend |
| 244 | library capabilities for specific uses. |
| 245 | |
| 246 | The CImg Library is a quite lightweight library which is easy to maintain (due to its particular structure), and thus |
| 247 | has a fast rythm of release. A new version of the CImg package is released approximately every three months. |
| 248 | |
| 249 | \subsection ssf14 1.4. What kind of people are concerned by CImg ? |
| 250 | |
| 251 | The CImg library is an <i>image processing</i> library, primarily intended for computer scientists or students working in the fields |
| 252 | of image processing or computer vision, and knowing bases of C++. |
| 253 | As the library is handy and really easy to use, it can be also used by any programmer |
| 254 | needing occasional tools for dealing with images in C++, since there are no standard library yet |
| 255 | for this purpose. |
| 256 | |
| 257 | \subsection ssf15 1.5. What are the specificities of the CeCILL license ? |
| 258 | |
| 259 | The <a href="http://www.cecill.info">CeCILL license</a> governs the use of the CImg Library. |
| 260 | This is an <i>open-source</i> license which gives you rights to access, use, modify and redistribute the source code, |
| 261 | under certains conditions. |
| 262 | There are two different variants of the CeCILL license used in CImg |
| 263 | (namely |
| 264 | <a href="http://www.cecill.info/licences/Licence_CeCILL_V2-en.html">CeCILL</a> and |
| 265 | <a href="http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html">CeCILL-C</a>, all open-source), |
| 266 | corresponding to different constraints on the source files : |
| 267 | - The <a href="http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html">CeCILL-C</a> license is the most permissive one, close to |
| 268 | the <i>GNU LGPL license</i>, and <i>applies <b>only</b> on the main library file |
| 269 | <a href="https://framagit.org/dtschump/CImg/raw/master/CImg.h">CImg.h</a></i>. |
| 270 | Basically, this license allows to use <a href="https://framagit.org/dtschump/CImg/raw/master/CImg.h">CImg.h</a> |
| 271 | in a closed-source product without forcing you to redistribute the entire software source code. Anyway, |
| 272 | if one modifies the <a href="https://framagit.org/dtschump/CImg/raw/master/CImg.h">CImg.h</a> source file, one has to redistribute |
| 273 | the modified version of the file that must be governed by the same <a href="http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html">CeCILL-C</a> license. |
| 274 | |
| 275 | - The <a href="http://www.cecill.info/licences/Licence_CeCILL_V2-en.html">CeCILL</a> license applies to all other files |
| 276 | (source examples, plug-ins and documentation) of the CImg Library package, and is close (even <i>compatible</i>) |
| 277 | with the <i>GNU GPL license</i>. It <i>does not allow</i> the use of these files in closed-source products. |
| 278 | |
| 279 | You are invited to read the complete descriptions of the |
| 280 | the <a href="http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html">CeCILL-C</a> |
| 281 | and <a href="http://www.cecill.info/licences/Licence_CeCILL_V2-en.html">CeCILL</a> licenses before releasing a |
| 282 | software based on the CImg Library. |
| 283 | |
| 284 | \subsection ssf16 1.6. Who is behind CImg ? |
| 285 | |
| 286 | CImg has been started by |
| 287 | <a href="http://tschumperle.users.greyc.fr/">David Tschumperle</a> at the beginning of his PhD thesis, in October 1999. |
| 288 | He is still the main coordinator of the project. |
| 289 | Since the first release, a growing number of contributors has appeared. |
| 290 | Due to the very simple and compact form of the library, submitting a contribution is quite easy and can be |
| 291 | fastly integrated into the supported releases. |
| 292 | List of contributors can be found on the front page. |
| 293 | |
| 294 | \section sf2 2. C++ related questions |
| 295 | |
| 296 | \subsection ssf21 2.1 What is the level of C++ knowledge needed to use CImg ? |
| 297 | |
| 298 | The CImg Library has been designed using C++ templates and object-oriented programming techniques, |
| 299 | but in a very accessible level. |
| 300 | There are only public classes without any derivation (just like C structures) and |
| 301 | there is at most one template parameter for each CImg class (defining the pixel type of the images). |
| 302 | The design is simple but clean, making the library accessible even for non professional C++ programmers, while proposing |
| 303 | strong extension capabilities for C++ experts. |
| 304 | |
| 305 | \subsection ssf22 2.2 How to use CImg in my own C++ program ? |
| 306 | |
| 307 | Basically, you need to add these two lines in your C++ source code, in order |
| 308 | to be able to work with CImg images : |
| 309 | \code |
| 310 | #include "CImg.h" |
| 311 | using namespace cimg_library; |
| 312 | \endcode |
| 313 | |
| 314 | \subsection ssf23 2.3 Why is CImg entirely contained in a single header file ? |
| 315 | |
| 316 | People are often surprised to see that the complete code of the library is contained in a single (big) C++ header file |
| 317 | <a href="https://framagit.org/dtschump/CImg/raw/master/CImg.h">CImg.h</a>. |
| 318 | There are good practical and technical reasons to do that. Some arguments are listed below to justify this approach, |
| 319 | so (I hope) you won't think this is a awkwardly C++ design of the CImg library :\n |
| 320 | |
| 321 | - First, the library is based on <i>template datatypes</i> (images with generic pixel type), |
| 322 | meaning that the programmer is free to decide what type of image he instanciates in his code. |
| 323 | Even if there are roughly a limited number of fully supported types (basically, the "atomic" types of C++ : <i>unsigned char, int, float, ...</i>), |
| 324 | this is <i>not imaginable</i> to pre-compile the library classes and functions for <i>all possible atomic datatypes</i>, |
| 325 | since many functions and methods can have two or three arguments having different template parameters. |
| 326 | This really means <i>a huge number</i> of possible combinations. The size of the object binary file generated to cover all possible cases |
| 327 | would be just <i>colossal</i>. Is the STL library a pre-compiled one ? No, CImg neither. |
| 328 | CImg is not using a classical <i>.cpp</i> and <i>.h</i> mechanism, just like the STL. |
| 329 | Architectures of C++ <i>template-based</i> libraries are somewhat special in this sense. This is a proven technical fact. |
| 330 | |
| 331 | - Second, why CImg does not have several header files, just like the STL does (one for each class for instance) ? |
| 332 | This would be possible of course. |
| 333 | There are only 4 classes in CImg, the two most important being <i>CImg<T></i> and <i>CImgList<T></i> representing respectively |
| 334 | an image and a collection of images. |
| 335 | But contrary to the STL library, these two CImg classes are strongly <i>inter-dependent</i>. All CImg algorithms |
| 336 | are actually not defined as separate functions acting on containers (as the STL does with his header \<algorithm\>), |
| 337 | but are directly methods of the image and image collection classes. This inter-dependence practically means that you |
| 338 | will undoubtly need these two main classes at the same time if you are using CImg. |
| 339 | If they were defined in separate header files, you would be forced to include both of them. What is the gain then ? No gain.\n |
| 340 | Concerning the two other classes : You can disable the third most important class <i>CImgDisplay</i> of the CImg library, by setting the compilation |
| 341 | macro <i>cimg_display</i> to 0, avoiding thus to compile this class if you don't use display capabilities of CImg in your code. |
| 342 | But to be honest, this is a quite small class and doing this doesn't save much compilation time. |
| 343 | The last and fourth class is <i>CImgException</i>, which is only few lines long and is obviously required in almost all methods of CImg. |
| 344 | Including this one is <i>mandatory</i>.\n |
| 345 | As a consequence, having a single header file instead of several ones is just a way for you to avoid including all of them, |
| 346 | without any consequences on compilation time. This is both good technical and practical reasons to do like this. |
| 347 | |
| 348 | - Third, having a single header file has plenty of advantages : Simplicity for the user, and for the developers (maintenance is in fact easier). |
| 349 | Look at the <tt>CImg.h</tt> file, it looks like a mess at a first glance, but it is in fact very well organized and structured. |
| 350 | Finding pieces of code in CImg functions or methods is particularly easy and fast. |
| 351 | Also, how about the fact that library installation problems just disappear ? |
| 352 | Just bring <tt>CImg.h</tt> with you, put it in your source directory, and the library is ready to go ! |
| 353 | |
| 354 | I admit the compilation time of CImg-based programs can be sometime long, but don't think that it is due to the fact that you are |
| 355 | using a single header file. Using several header files wouldn't arrange anything since you would need all of them. |
| 356 | Having a pre-compiled library object would be the only solution to speed up compilation time, but it is not possible at all, |
| 357 | due to the too much generic nature of the library. |
| 358 | |
| 359 | \section sf3 3. Other resources |
| 360 | \subsection ssf31 3.1 Translations |
| 361 | |
| 362 | This FAQ has been translated to <a href="http://science.webhostinggeeks.com/cimg-biblioteka">Serbo-Croatian</a> language by <a href="http://webhostinggeeks.com/"> Web Geeks </a>. |
| 363 | |
| 364 | **/ |
| 365 | /*@}*/ |
| 366 | |
| 367 | /*----------------------------------- |
| 368 | |
| 369 | Setting Environment Variables |
| 370 | |
| 371 | -------------------------------------*/ |
| 372 | |
| 373 | /** \addtogroup cimg_environment Setting Environment Variables */ |
| 374 | /*@{*/ |
| 375 | /** |
| 376 | \page foo1 |
| 377 | |
| 378 | The CImg library is a multiplatform library, working on a wide variety of systems. |
| 379 | This implies the existence of some \e environment \e variables that must be correctly defined |
| 380 | depending on your current system. |
| 381 | Most of the time, the %CImg Library defines these variables automatically |
| 382 | (for popular systems). Anyway, if your system is not recognized, you will have to set the environment |
| 383 | variables by hand. Here is a quick explanations of environment variables.\n |
| 384 | |
| 385 | Setting the environment variables is done with the <tt>\#define</tt> keyword. |
| 386 | This setting must be done <i>before including the file <tt>CImg.h</tt></i> in your source code. |
| 387 | For instance, |
| 388 | defining the environment variable \c cimg_display would be done like this : |
| 389 | \code |
| 390 | #define cimg_display 0 |
| 391 | #include "CImg.h" |
| 392 | ... |
| 393 | \endcode |
| 394 | |
| 395 | Here are the different environment variables used by the %CImg Library : |
| 396 | |
| 397 | - \b \c cimg_OS : This variable defines the type of your Operating System. It can be set to \b 1 (\e Unix), |
| 398 | \b 2 (\e Windows), or \b 0 (\e Other \e configuration). |
| 399 | It should be actually auto-detected by the CImg library. If this is not the case (<tt>cimg_OS=0</tt>), you |
| 400 | will probably have to tune the environment variables described below. |
| 401 | |
| 402 | - \b \c cimg_display : This variable defines the type of graphical library used to |
| 403 | display images in windows. It can be set to 0 (no display library available), \b 1 (X11-based display) or |
| 404 | \b 2 (Windows-GDI display). |
| 405 | If you are running on a system without X11 or Windows-GDI ability, please set this variable to \c 0. |
| 406 | This will disable the display support, since the %CImg Library doesn't contain the necessary code to display |
| 407 | images on systems other than X11 or Windows GDI. |
| 408 | |
| 409 | - \b \c cimg_use_vt100 : This variable tells the library if the system terminal has VT100 color capabilities. |
| 410 | It can be \e defined or \e not \e defined. Define this variable to get colored output on your terminal, |
| 411 | when using the %CImg Library. |
| 412 | |
| 413 | - \b \c cimg_verbosity : This variable defines the level of run-time debug messages that will be displayed by |
| 414 | the %CImg Library. It can be set to 0 (no debug messages), 1 (normal debug messages displayed on |
| 415 | standard error), 2 (normal debug messages displayed in modal windows, which is |
| 416 | the default value), or 3 (high debug messages). Note that setting this value to 3 may slow down your |
| 417 | program since more debug tests are made by the library (particularly to check if pixel access is made outside |
| 418 | image boundaries). See also CImgException to better understand how debug messages are working. |
| 419 | |
| 420 | - \b \c cimg_plugin : This variable tells the library to use a plugin file to add features to the CImg<T> class. |
| 421 | Define it with the path of your plugin file, if you want to add member functions to the CImg<T> class, |
| 422 | without having to modify directly the \c "<tt>CImg.h</tt>" file. An include of the plugin file is performed in the CImg<T> |
| 423 | class. If \c cimg_plugin if not specified (default), no include is done. |
| 424 | |
| 425 | - \b \c cimglist_plugin : Same as \c cimg_plugin, but to add features to the CImgList<T> class. |
| 426 | |
| 427 | - \b \c cimgdisplay_plugin : Same as \c cimg_plugin, but to add features to the CImgDisplay<T> class. |
| 428 | |
| 429 | All these compilation variables can be checked, using the function cimg_library::cimg::info(), which |
| 430 | displays a list of the different configuration variables and their values on the standard error output. |
| 431 | **/ |
| 432 | /*@}*/ |
| 433 | |
| 434 | |
| 435 | /** \addtogroup cimg_visual2005 How to use CImg library with Visual C++ 2005 Express Edition ?. */ |
| 436 | /*@{*/ |
| 437 | /** |
| 438 | \page foo89198 |
| 439 | |
| 440 | \section s13968 How to use CImg library with Visual C++ 2005 Express Edition ? |
| 441 | |
| 442 | This section has been written by Vincent Garcia and Alexandre Fournier from I3S/Sophia_Antipolis. |
| 443 | |
| 444 | - Download CImg library |
| 445 | - Download and install Visual C++ 2005 Express Edition |
| 446 | - Download and install Microsoft Windows SDK |
| 447 | - Configure Visual C++ to take into account Microsoft SDK |
| 448 | - 1. Go to menu "Tools -> options" |
| 449 | - 2. Select option "Projects and Solutions -> VC++ Directories" |
| 450 | - 3. In the select liste "Show directories for", choose "include files", and add C:\\Program Files\\Microsoft Platform SDK\\Include (adapt if needed) |
| 451 | - 4. In the select liste "Show directories for", choose "library files", and add C:\\Program Files\\Microsoft Platform SDK\\Lib |
| 452 | (adapt if needed) Edit file C:\\Program Files\\Microsoft Visual Studio 8\\VC\\VCProjectDefaults\\corewin_express.vsprops (adapt if needed) |
| 453 | - 6. 7. Remplace the line AdditionalDependencies="kernel32.lib" /> by AdditionalDependencies="kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib" /> |
| 454 | - Restart Visual C++ |
| 455 | - Import CImg library in your main file |
| 456 | |
| 457 | **/ |
| 458 | /*@}*/ |
| 459 | |
| 460 | |
| 461 | /*----------------------------------- |
| 462 | |
| 463 | Tutorial : Getting started |
| 464 | |
| 465 | -------------------------------------*/ |
| 466 | |
| 467 | /** \addtogroup cimg_tutorial Tutorial : Getting Started. */ |
| 468 | /*@{*/ |
| 469 | /** |
| 470 | \page foo3 |
| 471 | |
| 472 | Let's start to write our first program to get the idea. This will demonstrate how to load and create images, as well as handle image |
| 473 | display and mouse events. |
| 474 | Assume we want to load a color image <tt>lena.jpg</tt>, smooth it, display it in a windows, and enter an event loop so that clicking a |
| 475 | point in the image will draw the (R,G,B) intensity profiles of the corresponding image line (in another window). |
| 476 | Yes, that sounds quite complex for a first code, but don't worry, it will be very simple using the CImg library ! Well, just look |
| 477 | at the code below, it does the task : |
| 478 | |
| 479 | \code |
| 480 | #include "CImg.h" |
| 481 | using namespace cimg_library; |
| 482 | |
| 483 | int main() { |
| 484 | CImg<unsigned char> image("lena.jpg"), visu(500,400,1,3,0); |
| 485 | const unsigned char red[] = { 255,0,0 }, green[] = { 0,255,0 }, blue[] = { 0,0,255 }; |
| 486 | image.blur(2.5); |
| 487 | CImgDisplay main_disp(image,"Click a point"), draw_disp(visu,"Intensity profile"); |
| 488 | while (!main_disp.is_closed() && !draw_disp.is_closed()) { |
| 489 | main_disp.wait(); |
| 490 | if (main_disp.button() && main_disp.mouse_y()>=0) { |
| 491 | const int y = main_disp.mouse_y(); |
| 492 | visu.fill(0).draw_graph(image.get_crop(0,y,0,0,image.width()-1,y,0,0),red,1,1,0,255,0); |
| 493 | visu.draw_graph(image.get_crop(0,y,0,1,image.width()-1,y,0,1),green,1,1,0,255,0); |
| 494 | visu.draw_graph(image.get_crop(0,y,0,2,image.width()-1,y,0,2),blue,1,1,0,255,0).display(draw_disp); |
| 495 | } |
| 496 | } |
| 497 | return 0; |
| 498 | } |
| 499 | \endcode |
| 500 | |
| 501 | Here is a screenshot of the resulting program : |
| 502 | |
| 503 | <img SRC="../img/tutorial.jpg"> |
| 504 | |
| 505 | And here is the detailled explanation of the source, line by line : |
| 506 | |
| 507 | \code #include "CImg.h" \endcode |
| 508 | Include the main and only header file of the CImg library. |
| 509 | \code using namespace cimg_library; \endcode |
| 510 | Use the library namespace to ease the declarations afterward. |
| 511 | \code int main() { \endcode |
| 512 | Definition of the main function. |
| 513 | \code CImg<unsigned char> image("lena.jpg"), visu(500,400,1,3,0); \endcode |
| 514 | Creation of two instances of images of \c unsigned \c char pixels. |
| 515 | The first image \c image is initialized by reading an image file from the disk. |
| 516 | Here, <tt>lena.jpg</tt> must be in the same directory as the current program. |
| 517 | Note that you must also have installed the \e ImageMagick package in order to be able to read JPG images. |
| 518 | The second image \c visu is initialized as a black color image with dimension <tt>dx=500</tt>, <tt>dy=400</tt>, |
| 519 | <tt>dz=1</tt> (here, it is a 2D image, not a 3D one), and <tt>dv=3</tt> (each pixel has 3 'vector' channels R,G,B). |
| 520 | The last argument in the constructor defines the default value of the pixel values |
| 521 | (here \c 0, which means that \c visu will be initially black). |
| 522 | \code const unsigned char red[] = { 255,0,0 }, green[] = { 0,255,0 }, blue[] = { 0,0,255 }; \endcode |
| 523 | Definition of three different colors as array of unsigned char. This will be used to draw plots with different colors. |
| 524 | \code image.blur(2.5); \endcode |
| 525 | Blur the image, with a gaussian blur and a standard variation of 2.5. Note that most of the CImg functions have two versions : |
| 526 | one that acts in-place (which is the case of blur), and one that returns the result as a new image (the name of the function |
| 527 | begins then with <tt>get_</tt> ). In this case, one could have also written <tt>image = image.get_blur(2.5);</tt> |
| 528 | (more expensive, since it needs an additional copy operation). |
| 529 | \code CImgDisplay main_disp(image,"Click a point"), draw_disp(visu,"Intensity profile"); \endcode |
| 530 | Creation of two display windows, one for the input image image, and one for the image visu which will be display intensity profiles. |
| 531 | By default, CImg displays handles events (mouse,keyboard,..). On Windows, there is a way to create fullscreen displays. |
| 532 | \code while (!main_disp.is_closed() && !draw_disp.is_closed()) { \endcode |
| 533 | Enter the event loop, the code will exit when one of the two display windows is closed. |
| 534 | \code main_disp.wait(); \endcode |
| 535 | Wait for an event (mouse, keyboard,..) in the display window \c main_disp. |
| 536 | \code if (main_disp.button() && main_disp.mouse_y()>=0) { \endcode |
| 537 | Test if the mouse button has been clicked on the image area. |
| 538 | One may distinguish between the 3 different mouse buttons, |
| 539 | but in this case it is not necessary |
| 540 | \code const int y = main_disp.mouse_y(); \endcode |
| 541 | Get the image line y-coordinate that has been clicked. |
| 542 | \code visu.fill(0).draw_graph(image.get_crop(0,y,0,0,image.width()-1,y,0,0),red,1,0,256,0); \endcode |
| 543 | This line illustrates the pipeline property of most of the CImg class functions. The first function <tt>fill(0)</tt> simply sets |
| 544 | all pixel values with 0 (i.e. clear the image \c visu). The interesting thing is that it returns a reference to |
| 545 | \c visu and then, can be pipelined with the function \c draw_graph() which draws a plot in the image \c visu. |
| 546 | The plot data are given by another image (the first argument of \c draw_graph()). In this case, the given image is |
| 547 | the red-component of the line y of the original image, retrieved by the function \c get_crop() which returns a |
| 548 | sub-image of the image \c image. Remember that images coordinates are 4D (x,y,z,c) and for color images, |
| 549 | the R,G,B channels are respectively given by <tt>v=0, v=1</tt> and <tt>v=2</tt>. |
| 550 | \code visu.draw_graph(image.get_crop(0,y,0,1,image.width()-1,y,0,1),green,1,0,256,0); \endcode |
| 551 | Plot the intensity profile for the green channel of the clicked line. |
| 552 | \code visu.draw_graph(image.get_crop(0,y,0,2,image.width()-1,y,0,2),blue,1,0,256,0).display(draw_disp); \endcode |
| 553 | Same thing for the blue channel. Note how the function (which return a reference to \c visu) is pipelined with the function |
| 554 | \c display() that just paints the image visu in the corresponding display window. |
| 555 | \code ...till the end \endcode |
| 556 | I don't think you need more explanations ! |
| 557 | |
| 558 | As you have noticed, the CImg library allows to write very small and intuitive code. Note also that this source will perfectly |
| 559 | work on Unix and Windows systems. Take also a look to the examples provided in the CImg package ( |
| 560 | directory \c examples/ ). It will show you how CImg-based code can be surprisingly small. |
| 561 | Moreover, there is surely one example close to what you want to do. |
| 562 | A good start will be to look at the file <tt>CImg_demo.cpp</tt> which contains small and various examples of what you can do |
| 563 | with the %CImg Library. All CImg classes are used in this source, and the code can be easily modified to see what happens. |
| 564 | |
| 565 | **/ |
| 566 | /*@}*/ |
| 567 | |
| 568 | /*----------------------------------- |
| 569 | |
| 570 | Using drawing functions |
| 571 | |
| 572 | -------------------------------------*/ |
| 573 | |
| 574 | /** \addtogroup cimg_drawing Using Drawing Functions. */ |
| 575 | /*@{*/ |
| 576 | /** |
| 577 | \page foo5 |
| 578 | |
| 579 | \section s5 Using Drawing Functions. |
| 580 | |
| 581 | This section tells more about drawing features in CImg images. |
| 582 | Drawing functions list can be found in <a href="structCImg.html">the CImg functions list</a> |
| 583 | (section \b Drawing Functions), |
| 584 | and are all defined on a common basis. Here are the important points to understand before using |
| 585 | drawing functions : |
| 586 | |
| 587 | - Drawing is performed on the instance image. Drawing functions parameters |
| 588 | are defined as \e const variables and return a reference to the current instance <tt>(*this)</tt>, |
| 589 | so that drawing functions can be pipelined (see examples below). |
| 590 | Drawing is usually done in 2D color images but can be performed in 3D images with any vector-valued dimension, |
| 591 | and with any possible pixel type. |
| 592 | |
| 593 | - A color parameter is always needed to draw features in an image. The color must be defined as a C-style array |
| 594 | whose dimension is at least |
| 595 | |
| 596 | **/ |
| 597 | /*@}*/ |
| 598 | |
| 599 | /*----------------------------------- |
| 600 | |
| 601 | Using image loops |
| 602 | |
| 603 | -------------------------------------*/ |
| 604 | |
| 605 | /** \addtogroup cimg_loops Using Image Loops. */ |
| 606 | /*@{*/ |
| 607 | /** |
| 608 | \page foo_lo |
| 609 | The %CImg Library provides different macros that define useful iterative loops over an image. |
| 610 | Basically, it can be used to replace one or several <tt>for(..)</tt> instructions, but it also proposes |
| 611 | interesting extensions to classical loops. |
| 612 | Below is a list of all existing loop macros, classified in four different categories : |
| 613 | - \ref lo1 |
| 614 | - \ref lo4 |
| 615 | - \ref lo5 |
| 616 | - \ref lo6 |
| 617 | |
| 618 | \section lo1 Loops over the pixel buffer |
| 619 | |
| 620 | Loops over the pixel buffer are really basic loops that iterate a pointer on the pixel data buffer |
| 621 | of a \c cimg_library::CImg image. Two macros are defined for this purpose : |
| 622 | |
| 623 | - \b cimg_for(img,ptr,T) : |
| 624 | This macro loops over the pixel data buffer of the image \c img, using a pointer <tt>T* ptr</tt>, |
| 625 | starting from the beginning of the buffer (first pixel) till the end of the buffer (last pixel). |
| 626 | - \c img must be a (non empty) \c cimg_library::CImg image of pixels \c T. |
| 627 | - \c ptr is a pointer of type \c T*. |
| 628 | This kind of loop should not appear a lot in your own source code, since this is a low-level loop |
| 629 | and many functions of the CImg class may be used instead. Here is an example of use : |
| 630 | \code |
| 631 | CImg<float> img(320,200); |
| 632 | cimg_for(img,ptr,float) { *ptr=0; } // Equivalent to 'img.fill(0);' |
| 633 | \endcode |
| 634 | |
| 635 | - \b cimg_rof(img,ptr,T) : |
| 636 | This macro does the same as \c cimg_for() but from the end to the beginning of the pixel buffer. |
| 637 | |
| 638 | - \b cimg_foroff(img,off) : |
| 639 | This macro loops over the pixel data buffer of the image \c img, using an offset \c , |
| 640 | starting from the beginning of the buffer (first pixel, \c off=0) |
| 641 | till the end of the buffer (last pixel value, <tt>off = img.size()-1</tt>). |
| 642 | - \c img must be a (non empty) cimg_library::CImg<T> image of pixels \c T. |
| 643 | - \c off is an inner-loop variable, only defined inside the scope of the loop. |
| 644 | |
| 645 | Here is an example of use : |
| 646 | \code |
| 647 | CImg<float> img(320,200); |
| 648 | cimg_foroff(img,off) { img[off]=0; } // Equivalent to 'img.fill(0);' |
| 649 | \endcode |
| 650 | |
| 651 | \section lo4 Loops over image dimensions |
| 652 | |
| 653 | The following loops are probably the most used loops in image processing programs. |
| 654 | They allow to loop over the image along one or several dimensions, along a raster scan course. |
| 655 | Here is the list of such loop macros for a single dimension : |
| 656 | - \b cimg_forX(img,x) : equivalent to : <tt>for (int x = 0; x<img.width(); ++x)</tt>. |
| 657 | - \b cimg_forY(img,y) : equivalent to : <tt>for (int y = 0; y<img.height(); ++y)</tt>. |
| 658 | - \b cimg_forZ(img,z) : equivalent to : <tt>for (int z = 0; z<img.depth(); ++z)</tt>. |
| 659 | - \b cimg_forC(img,c) : equivalent to : <tt>for (int c = 0; c<img.spectrum(); ++c)</tt>. |
| 660 | |
| 661 | Combinations of these macros are also defined as other loop macros, allowing to loop directly over 2D, 3D or 4D images : |
| 662 | - \b cimg_forXY(img,x,y) : equivalent to : \c cimg_forY(img,y) \c cimg_forX(img,x). |
| 663 | - \b cimg_forXZ(img,x,z) : equivalent to : \c cimg_forZ(img,z) \c cimg_forX(img,x). |
| 664 | - \b cimg_forYZ(img,y,z) : equivalent to : \c cimg_forZ(img,z) \c cimg_forY(img,y). |
| 665 | - \b cimg_forXC(img,x,c) : equivalent to : \c cimg_forC(img,c) \c cimg_forX(img,x). |
| 666 | - \b cimg_forYC(img,y,c) : equivalent to : \c cimg_forC(img,c) \c cimg_forY(img,y). |
| 667 | - \b cimg_forZC(img,z,c) : equivalent to : \c cimg_forC(img,c) \c cimg_forZ(img,z). |
| 668 | - \b cimg_forXYZ(img,x,y,z) : equivalent to : \c cimg_forZ(img,z) \c cimg_forXY(img,x,y). |
| 669 | - \b cimg_forXYC(img,x,y,c) : equivalent to : \c cimg_forC(img,c) \c cimg_forXY(img,x,y). |
| 670 | - \b cimg_forXZC(img,x,z,c) : equivalent to : \c cimg_forC(img,c) \c cimg_forXZ(img,x,z). |
| 671 | - \b cimg_forYZC(img,y,z,c) : equivalent to : \c cimg_forC(img,c) \c cimg_forYZ(img,y,z). |
| 672 | - \b cimg_forXYZC(img,x,y,z,c) : equivalent to : \c cimg_forC(img,c) \c cimg_forXYZ(img,x,y,z). |
| 673 | |
| 674 | - For all these loops, \c x,\c y,\c z and \c v are inner-defined variables only visible inside the scope of the loop. |
| 675 | They don't have to be defined before the call of the macro. |
| 676 | - \c img must be a (non empty) cimg_library::CImg image. |
| 677 | |
| 678 | Here is an example of use that creates an image with a smooth color gradient : |
| 679 | \code |
| 680 | CImg<unsigned char> img(256,256,1,3); // Define a 256x256 color image |
| 681 | cimg_forXYC(img,x,y,c) { img(x,y,c) = (x+y)*(c+1)/6; } |
| 682 | img.display("Color gradient"); |
| 683 | \endcode |
| 684 | |
| 685 | \section lo5 Loops over interior regions and borders. |
| 686 | |
| 687 | Similar macros are also defined to loop only on the border of an image, or inside the image (excluding the border). |
| 688 | The border may be several pixel wide : |
| 689 | |
| 690 | - \b cimg_for_insideX(img,x,n) : Loop along the x-axis, except for pixels inside a border of \p n pixels wide. |
| 691 | - \b cimg_for_insideY(img,y,n) : Loop along the y-axis, except for pixels inside a border of \p n pixels wide. |
| 692 | - \b cimg_for_insideZ(img,z,n) : Loop along the z-axis, except for pixels inside a border of \p n pixels wide. |
| 693 | - \b cimg_for_insideC(img,c,n) : Loop along the c-axis, except for pixels inside a border of \p n pixels wide. |
| 694 | - \b cimg_for_insideXY(img,x,y,n) : Loop along the (x,y)-axes, excepted for pixels inside a border of \p n pixels wide. |
| 695 | - \b cimg_for_insideXYZ(img,x,y,z,n) : Loop along the (x,y,z)-axes, excepted for pixels inside a border of \p n pixels wide. |
| 696 | |
| 697 | And also : |
| 698 | |
| 699 | - \b cimg_for_borderX(img,x,n) : Loop along the x-axis, only for pixels inside a border of \p n pixels wide. |
| 700 | - \b cimg_for_borderY(img,y,n) : Loop along the y-axis, only for pixels inside a border of \p n pixels wide. |
| 701 | - \b cimg_for_borderZ(img,z,n) : Loop along the z-axis, only for pixels inside a border of \p n pixels wide. |
| 702 | - \b cimg_for_borderC(img,c,n) : Loop along the c-axis, only for pixels inside a border of \p n pixels wide. |
| 703 | - \b cimg_for_borderXY(img,x,y,n) : Loop along the (x,y)-axes, only for pixels inside a border of \p n pixels wide. |
| 704 | - \b cimg_for_borderXYZ(img,x,y,z,n) : Loop along the (x,y,z)-axes, only for pixels inside a border of \p n pixels wide. |
| 705 | |
| 706 | - For all these loops, \c x,\c y,\c z and \c c are inner-defined variables only visible inside the scope of the loop. |
| 707 | They don't have to be defined before the call of the macro. |
| 708 | - \c img must be a (non empty) cimg_library::CImg image. |
| 709 | - The constant \c n stands for the size of the border. |
| 710 | |
| 711 | Here is an example of use, to create a 2d grayscale image with two different intensity gradients : |
| 712 | \code |
| 713 | CImg<> img(256,256); |
| 714 | cimg_for_insideXY(img,x,y,50) img(x,y) = x+y; |
| 715 | cimg_for_borderXY(img,x,y,50) img(x,y) = x-y; |
| 716 | img.display(); |
| 717 | \endcode |
| 718 | |
| 719 | \section lo6 Loops using neighborhoods. |
| 720 | |
| 721 | Inside an image loop, it is often useful to get values of neighborhood pixels of the |
| 722 | current pixel at the loop location. |
| 723 | The %CImg Library provides a very smart and fast mechanism for this purpose, with the definition |
| 724 | of several loop macros that remember the neighborhood values of the pixels. |
| 725 | The use of these macros can highly optimize your code, and also simplify your program. |
| 726 | |
| 727 | \subsection lo7 Neighborhood-based loops for 2D images |
| 728 | |
| 729 | For 2D images, the neighborhood-based loop macros are : |
| 730 | |
| 731 | - \b cimg_for2x2(img,x,y,z,c,I,T) : Loop along the (x,y)-axes using a centered 2x2 neighborhood. |
| 732 | - \b cimg_for3x3(img,x,y,z,c,I,T) : Loop along the (x,y)-axes using a centered 3x3 neighborhood. |
| 733 | - \b cimg_for4x4(img,x,y,z,c,I,T) : Loop along the (x,y)-axes using a centered 4x4 neighborhood. |
| 734 | - \b cimg_for5x5(img,x,y,z,c,I,T) : Loop along the (x,y)-axes using a centered 5x5 neighborhood. |
| 735 | |
| 736 | For all these loops, \c x and \c y are inner-defined variables only visible inside the scope of the loop. |
| 737 | They don't have to be defined before the call of the macro. |
| 738 | \c img is a non empty CImg<T> image. \c z and \c c are constants that define on which image slice and |
| 739 | vector channel the loop must apply (usually both 0 for grayscale 2D images). |
| 740 | Finally, \c I is the 2x2, 3x3, 4x4 or 5x5 neighborhood of type \c T that will be updated with the correct pixel values |
| 741 | during the loop (see \ref lo9). |
| 742 | |
| 743 | \subsection lo8 Neighborhood-based loops for 3D images |
| 744 | |
| 745 | For 3D images, the neighborhood-based loop macros are : |
| 746 | |
| 747 | - \b cimg_for2x2x2(img,x,y,z,c,I,T) : Loop along the (x,y,z)-axes using a centered 2x2x2 neighborhood. |
| 748 | - \b cimg_for3x3x3(img,x,y,z,c,I,T) : Loop along the (x,y,z)-axes using a centered 3x3x3 neighborhood. |
| 749 | |
| 750 | For all these loops, \c x, \c y and \c z are inner-defined variables only visible inside the scope of the loop. |
| 751 | They don't have to be defined before the call of the macro. |
| 752 | \c img is a non empty CImg<T> image. \c c is a constant that defines on which image channel |
| 753 | the loop must apply (usually 0 for grayscale 3D images). |
| 754 | Finally, \c I is the 2x2x2 or 3x3x3 neighborhood of type \c T that will be updated with the correct pixel values |
| 755 | during the loop (see \ref lo9). |
| 756 | |
| 757 | \subsection lo9 Defining neighborhoods |
| 758 | |
| 759 | A neighborhood is defined as an instance of a class having operator[] defined. |
| 760 | This particularly includes classical C-array, as well as CImg<T> objects. |
| 761 | |
| 762 | For instance, a 3x3 neighborhood can be defined either as a 'float[9]' or a |
| 763 | 'CImg<float>(3,3)' variable. |
| 764 | |
| 765 | \subsection lo10 Using alternate variable names |
| 766 | |
| 767 | There are also some useful macros that can be used to define variables that |
| 768 | reference the neighborhood elements. There are : |
| 769 | |
| 770 | - \b CImg_2x2(I,type) : Define a 2x2 neighborhood named \c I, of type \c type. |
| 771 | - \b CImg_3x3(I,type) : Define a 3x3 neighborhood named \c I, of type \c type. |
| 772 | - \b CImg_4x4(I,type) : Define a 4x4 neighborhood named \c I, of type \c type. |
| 773 | - \b CImg_5x5(I,type) : Define a 5x5 neighborhood named \c I, of type \c type. |
| 774 | - \b CImg_2x2x2(I,type) : Define a 2x2x2 neighborhood named \c I, of type \c type. |
| 775 | - \b CImg_3x3x3(I,type) : Define a 3x3x3 neighborhood named \c I, of type \c type. |
| 776 | |
| 777 | Actually, \c I is a \e generic \e name for the neighborhood. In fact, these macros declare |
| 778 | a \e set of new variables. |
| 779 | For instance, defining a 3x3 neighborhood \c CImg_3x3(I,float) declares 9 different float variables |
| 780 | \c Ipp,\c Icp,\c Inp,\c Ipc,\c Icc,\c Inc,\c Ipn,\c Icn,\c Inn which correspond to each pixel value of |
| 781 | a 3x3 neighborhood. |
| 782 | Variable indices are \c p,\c c or \c n, and stand respectively for \e 'previous', \e 'current' and \e 'next'. |
| 783 | First indice denotes the \c x-axis, second indice denotes the \c y-axis. |
| 784 | Then, the names of the variables are directly related to the position of the corresponding pixels |
| 785 | in the neighborhood. For 3D neighborhoods, a third indice denotes the \c z-axis. |
| 786 | Then, inside a neighborhood loop, you will have the following equivalence : |
| 787 | - <tt>Ipp = img(x-1,y-1)</tt> |
| 788 | - <tt>Icn = img(x,y+1)</tt> |
| 789 | - <tt>Inp = img(x+1,y-1)</tt> |
| 790 | - <tt>Inpc = img(x+1,y-1,z)</tt> |
| 791 | - <tt>Ippn = img(x-1,y-1,z+1)</tt> |
| 792 | - and so on... |
| 793 | |
| 794 | For bigger neighborhoods, such as 4x4 or 5x5 neighborhoods, two additionnal indices are introduced : |
| 795 | \c a (stands for \e 'after') and \c b (stands for \e 'before'), so that : |
| 796 | - <tt>Ibb = img(x-2,y-2)</tt> |
| 797 | - <tt>Ina = img(x+1,y+2)</tt> |
| 798 | - and so on... |
| 799 | |
| 800 | The value of a neighborhood pixel outside the image range (image border problem) is automatically set to the same |
| 801 | values as the nearest valid pixel in the image (this is also called the \e Neumann \e border \e condition). |
| 802 | |
| 803 | \subsection lo11 Example codes |
| 804 | More than a long discussion, the above example will demonstrate how to compute the gradient norm of a 3D volume |
| 805 | using the \c cimg_for3x3x3() loop macro : |
| 806 | |
| 807 | \code |
| 808 | CImg<float> volume("IRM.hdr"); // Load an IRM volume from an Analyze7.5 file |
| 809 | CImg_3x3x3(I,float); // Define a 3x3x3 neighborhood |
| 810 | CImg<float> gradnorm(volume); // Create an image with same size as 'volume' |
| 811 | cimg_for3x3x3(volume,x,y,z,0,I,float) { // Loop over the volume, using the neighborhood I |
| 812 | const float ix = 0.5f*(Incc-Ipcc); // Compute the derivative along the x-axis. |
| 813 | const float iy = 0.5f*(Icnc-Icpc); // Compute the derivative along the y-axis. |
| 814 | const float iz = 0.5f*(Iccn-Iccp); // Compute the derivative along the z-axis. |
| 815 | gradnorm(x,y,z) = std::sqrt(ix*ix+iy*iy+iz*iz); // Set the gradient norm in the destination image |
| 816 | } |
| 817 | gradnorm.display("Gradient norm"); |
| 818 | \endcode |
| 819 | |
| 820 | And the following example shows how to deal with neighborhood references to blur a color image by averaging |
| 821 | pixel values on a 5x5 neighborhood. |
| 822 | |
| 823 | \code |
| 824 | CImg<unsigned char> src("image_color.jpg"), dest(src,false); // Image definitions. |
| 825 | typedef unsigned char uchar; // Avoid space in the second parameter of the macro CImg_5x5x1 below. |
| 826 | CImg<> N(5,5); // Define a 5x5 neighborhood as a 5x5 image. |
| 827 | cimg_forC(src,k) // Standard loop on color channels |
| 828 | cimg_for5x5(src,x,y,0,k,N,float) // 5x5 neighborhood loop. |
| 829 | dest(x,y,k) = N.sum()/(5*5); // Averaging pixels to filter the color image. |
| 830 | CImgList<unsigned char> visu(src,dest); |
| 831 | visu.display("Original + Filtered"); // Display both original and filtered image. |
| 832 | \endcode |
| 833 | |
| 834 | As you can see, explaining the use of the CImg neighborhood macros is actually more difficult than using them ! |
| 835 | |
| 836 | **/ |
| 837 | /*@}*/ |
| 838 | |
| 839 | /*----------------------------------- |
| 840 | |
| 841 | Using display windows |
| 842 | |
| 843 | -------------------------------------*/ |
| 844 | |
| 845 | /** \addtogroup cimg_displays Using Display Windows. */ |
| 846 | /*@{*/ |
| 847 | /** |
| 848 | \page foo_di |
| 849 | |
| 850 | When opening a display window, you can choose the way the pixel values will be normalized |
| 851 | before being displayed on the screen. Screen displays only support color values between [0,255], |
| 852 | and some |
| 853 | |
| 854 | When displaying an image into the display window using CImgDisplay::display(), values of |
| 855 | the image pixels can be eventually linearly normalized between [0,255] for visualization purposes. |
| 856 | This may be useful for instance when displaying \p CImg<double> images with pixel values |
| 857 | between [0,1]. |
| 858 | The normalization behavior depends on the value of \p normalize which can be either \p 0,\p 1 or \p 2 : |
| 859 | - \p 0 : No pixel normalization is performed when displaying an image. This is the fastest |
| 860 | process, but you must be sure your displayed image have pixel values inside the range [0,255]. |
| 861 | - \p 1 : Pixel value normalization is done for each new image display. Image pixels are |
| 862 | not modified themselves, only displayed pixels are normalized. |
| 863 | - \p 2 : Pixel value normalization is done for the first image display, then the |
| 864 | normalization parameters are kept and used for all the next image displays. |
| 865 | |
| 866 | **/ |
| 867 | /*@}*/ |
| 868 | |
| 869 | /*----------------------------------- |
| 870 | |
| 871 | How pixel data are stored |
| 872 | |
| 873 | -------------------------------------*/ |
| 874 | |
| 875 | /** \addtogroup cimg_storage How pixel data are stored with CImg. */ |
| 876 | /*@{*/ |
| 877 | /** |
| 878 | \page foo_store |
| 879 | |
| 880 | First, CImg<T> are *very* basic structures, which means that there are no memory tricks, weird memory alignments or |
| 881 | disk caches used to store pixel data of images. When an image is instanced, all its pixel values are stored in memory at |
| 882 | the same time (yes, you should avoid working with huge images when dealing with CImg, if you have only 64kb of RAM). |
| 883 | |
| 884 | A CImg<T> is basically a 4th-dimensional array (width,height,depth,dim), and its pixel data are stored linearly in a single |
| 885 | memory buffer of general size (width*height*depth*dim). Nothing more, nothing less. The address of this memory buffer can be |
| 886 | retrieved by the function CImg<T>::data(). |
| 887 | As each image value is stored as a type T (T being known by the programmer of course), this pointer is a 'T*', or a 'const T*' if your image is 'const'. |
| 888 | so, 'T *ptr = img.data()' gives you the pointer to the first value of the image 'img'. The overall size of the used memory for one |
| 889 | instance image (in bytes) is then 'width*height*depth*dim*sizeof(T)'. |
| 890 | |
| 891 | Now, the ordering of the pixel values in this buffer follows these rules : |
| 892 | The values are *not* interleaved, and are ordered first along the X,Y,Z and V axis respectively (corresponding to the width,height,depth,dim dimensions), |
| 893 | starting from the upper-left pixel to the bottom-right pixel of the instane image, with a classical scanline run. |
| 894 | |
| 895 | So, a color image with dim=3 and depth=1, will be stored in memory as : |
| 896 | |
| 897 | R1R2R3R4R5R6......G1G2G3G4G5G6.......B1B2B3B4B5B6.... (i.e following a 'planar' structure) |
| 898 | |
| 899 | and *not* as R1G1B1R2G2B2R3G3B3... (interleaved channels), |
| 900 | where R1 = img(0,0,0,0) is the first upper-left pixel of the red component of the image, |
| 901 | R2 is img(1,0,0,0), G1 = img(0,0,0,1), G2 = img(1,0,0,1), B1 = img(0,0,0,2), and so on... |
| 902 | |
| 903 | Another example, a (1x5x1x1) CImg<T> (column vector A) will be stored as : A1A2A3A4A5 |
| 904 | where A1 = img(0,0), A2 = img(0,1), ... , A5 = img(0,4). |
| 905 | |
| 906 | As you see, it is *very* simple and intuitive : no interleaving, no padding, just simple. |
| 907 | This is cool not only because it is simple, but this has in fact a number of interesting properties. For instance, a 2D color image |
| 908 | is stored in memory exactly as a 3D scalar image having a depth=3, meaning that when you are dealing with 2D color images, you can write 'img(x,y,k)' |
| 909 | instead of 'img(x,y,0,k)' to access the kth channel of the (x,y) pixel. More generally, if you have one dimension that is 1 in |
| 910 | your image, you can just skip it in the call to the operator(). Similarly, values of a column vector stored as an image with |
| 911 | width=depth=spectrum=1 can be accessed by 'img(y)' instead of 'img(0,y)'. This is very convenient. |
| 912 | |
| 913 | Another cool thing is that it allows you to work easily with 'shared' images. A shared image is a CImg<T> instance that shares |
| 914 | its memory with another one (the 'base' image). Destroying a shared image does nothing in fact. Shared images is a convenient |
| 915 | way of modifying only *portions* (consecutive in memory) of an image. For instance, if 'img' is a 2D color image, you can write : |
| 916 | |
| 917 | img.get_shared_channel(0).blur(2); |
| 918 | img.get_shared_channels(1,2).mirror('x'); |
| 919 | |
| 920 | which just blur the red channel of the image, and mirror the two others along the X-axis. |
| 921 | This is possible since channels of an image are not interleaved but are stored as different consecutive planes in memory, so you see that constructing a shared image is possible (and trivial). |
| 922 | |
| 923 | **/ |
| 924 | /*@}*/ |
| 925 | |
| 926 | /*----------------------------------- |
| 927 | |
| 928 | Files IO |
| 929 | |
| 930 | -------------------------------------*/ |
| 931 | |
| 932 | /** \addtogroup cimg_files_io Files IO in CImg. */ |
| 933 | /*@{*/ |
| 934 | /** |
| 935 | \page foo_fi |
| 936 | |
| 937 | The %CImg Library can NATIVELY handle the following file formats : |
| 938 | - RAW : consists in a very simple header (in ascii), then the image data. |
| 939 | - ASC (Ascii) |
| 940 | - HDR (Analyze 7.5) |
| 941 | - INR (Inrimage) |
| 942 | - PPM/PGM (Portable Pixmap) |
| 943 | - BMP (uncompressed) |
| 944 | - PAN (Pandore-5) |
| 945 | - DLM (Matlab ASCII) |
| 946 | |
| 947 | If ImageMagick is installed, The %CImg Library can save image in formats handled by ImageMagick : JPG, GIF, PNG, TIF,... |
| 948 | |
| 949 | **/ |
| 950 | /*@}*/ |
| 951 | |
| 952 | /*----------------------------------- |
| 953 | |
| 954 | Retrieving command line arguments |
| 955 | |
| 956 | -------------------------------------*/ |
| 957 | |
| 958 | /** \addtogroup cimg_options Retrieving Command Line Arguments. */ |
| 959 | /*@{*/ |
| 960 | /** |
| 961 | \page foo_so |
| 962 | |
| 963 | The CImg library offers facilities to retrieve command line arguments in a console-based |
| 964 | program, as it is a commonly needed operation. |
| 965 | Three macros \c cimg_usage(), \c cimg_help() and \c cimg_option() are defined for this purpose. |
| 966 | Using these macros allows to easily retrieve options values from the command line. |
| 967 | Invoking the compiled executable with the option \c -h or \c --help will |
| 968 | automatically display the program usage, followed by the list of requested options. |
| 969 | |
| 970 | \section so1 The cimg_usage() macro |
| 971 | |
| 972 | The macro \c cimg_usage(usage) may be used to describe the program goal and usage. |
| 973 | It is generally inserted one time after the <tt>int main(int argc,char **argv)</tt> definition. |
| 974 | |
| 975 | \param usage : A string describing the program goal and usage. |
| 976 | \pre The function where \c cimg_usage() is used must have correctly defined \c argc and \c argv variables. |
| 977 | |
| 978 | \section so1_5 The cimg_help() macro |
| 979 | |
| 980 | The macro \c cimg_help(str) will display the string \c str only if the \c -help or \c --help option |
| 981 | are invoked when running the programm. |
| 982 | |
| 983 | \section so2 The cimg_option() macro |
| 984 | |
| 985 | The macro \c cimg_option(name,default,usage) may be used to retrieve an option value from the command line. |
| 986 | |
| 987 | \param name : The name of the option to be retrieved from the command line. |
| 988 | \param default : The default value returned by the macro if no options \p name has been specified when running the program. |
| 989 | \param usage : A brief explanation of the option. If \c usage==0, the option won't appear on the option list |
| 990 | when invoking the executable with options \c -h or \c --help (hidden option). |
| 991 | |
| 992 | \return \c cimg_option() returns an object that has the \e same \e type as the default value \c default. |
| 993 | The return value is equal to the one specified on the command line. If no such option have been specified, |
| 994 | the return value is equal to the default value \c default. |
| 995 | Warning, this can be confusing in some situations (look at the end of the next section). |
| 996 | \pre The function where \c cimg_option() is used must have correctly defined \c argc and \c argv variables. |
| 997 | |
| 998 | \section so3 Example of use |
| 999 | |
| 1000 | The code below uses the macros \c cimg_usage() and \c cimg_option(). |
| 1001 | It loads an image, smoothes it an quantifies it with a specified number of values. |
| 1002 | \code |
| 1003 | #include "CImg.h" |
| 1004 | using namespace cimg_library; |
| 1005 | int main(int argc,char **argv) { |
| 1006 | cimg_usage("Retrieve command line arguments"); |
| 1007 | const char* filename = cimg_option("-i","image.gif","Input image file"); |
| 1008 | const char* output = cimg_option("-o",(char*)0,"Output image file"); |
| 1009 | const double sigma = cimg_option("-s",1.0,"Standard variation of the gaussian smoothing"); |
| 1010 | const int nblevels = cimg_option("-n",16,"Number of quantification levels"); |
| 1011 | const bool hidden = cimg_option("-hidden",false,0); // This is a hidden option |
| 1012 | |
| 1013 | CImg<unsigned char> img(filename); |
| 1014 | img.blur(sigma).quantize(nblevels); |
| 1015 | if (output) img.save(output); else img.display("Output image"); |
| 1016 | if (hidden) std::fprintf(stderr,"You found me !\n"); |
| 1017 | return 0; |
| 1018 | } |
| 1019 | \endcode |
| 1020 | |
| 1021 | Invoking the corresponding executable with <tt>test -h -hidden -n 20 -i foo.jpg</tt> will display : |
| 1022 | \verbatim |
| 1023 | ./test -h -hidden -n 20 -i foo.jpg |
| 1024 | |
| 1025 | test : Retrieve command line arguments (Oct 16 2004, 12:34:26) |
| 1026 | |
| 1027 | -i = foo.jpg : Input image file |
| 1028 | -o = 0 : Output image file |
| 1029 | -s = 1 : Standard variation of the gaussian smoothing |
| 1030 | -n = 20 : Number of quantification levels |
| 1031 | |
| 1032 | You found me ! |
| 1033 | \endverbatim |
| 1034 | |
| 1035 | \warning As the type of object returned by the macro \c cimg_option(option,default,usage) |
| 1036 | is defined by the type of \c default, undesired casts may appear when writting code such as : |
| 1037 | \code |
| 1038 | const double sigma = cimg_option("-val",0,"A floating point value"); |
| 1039 | \endcode |
| 1040 | In this case, \c sigma will always be equal to an integer (since the default value \c 0 is an integer). |
| 1041 | When passing a float value on the command line, a \e float \e to \e integer cast is then done, |
| 1042 | truncating the given parameter to an integer value (this is surely not a desired behavior). |
| 1043 | You must specify <tt>0.0</tt> as the default value in this case. |
| 1044 | |
| 1045 | \section so4 How to learn more about command line options ? |
| 1046 | You should take a look at the examples <tt>examples/gmic.cpp</tt> provided in the %CImg Library package. |
| 1047 | This is a command line based image converter which intensively uses the \c cimg_option() and \c cimg_usage() |
| 1048 | macros to retrieve command line parameters. |
| 1049 | **/ |
| 1050 | /*@}*/ |