Mon, 03 Aug 2009 23:41:04 +0100
added dep/*.d and obj/*.o to hgignore
1 /*
2 #
3 # File : tutorial.cpp
4 # ( C++ source file )
5 #
6 # Description : View the color profile of an image, along the X-axis.
7 # This file is a part of the CImg Library project.
8 # ( http://cimg.sourceforge.net )
9 #
10 # Copyright : David Tschumperle
11 # ( http://www.greyc.ensicaen.fr/~dtschump/ )
12 #
13 # License : CeCILL v2.0
14 # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
15 #
16 # This software is governed by the CeCILL license under French law and
17 # abiding by the rules of distribution of free software. You can use,
18 # modify and/ or redistribute the software under the terms of the CeCILL
19 # license as circulated by CEA, CNRS and INRIA at the following URL
20 # "http://www.cecill.info".
21 #
22 # As a counterpart to the access to the source code and rights to copy,
23 # modify and redistribute granted by the license, users are provided only
24 # with a limited warranty and the software's author, the holder of the
25 # economic rights, and the successive licensors have only limited
26 # liability.
27 #
28 # In this respect, the user's attention is drawn to the risks associated
29 # with loading, using, modifying and/or developing or reproducing the
30 # software by the user in light of its specific status of free software,
31 # that may mean that it is complicated to manipulate, and that also
32 # therefore means that it is reserved for developers and experienced
33 # professionals having in-depth computer knowledge. Users are therefore
34 # encouraged to load and test the software's suitability as regards their
35 # requirements in conditions enabling the security of their systems and/or
36 # data to be ensured and, more generally, to use and operate it in the
37 # same conditions as regards security.
38 #
39 # The fact that you are presently reading this means that you have had
40 # knowledge of the CeCILL license and that you accept its terms.
41 #
42 */
44 // Include CImg library file and use its main namespace
45 #include "CImg.h"
46 using namespace cimg_library;
48 #ifndef cimg_imagepath
49 #define cimg_imagepath "img/"
50 #endif
52 // Begin the program
53 int main(int argc,char **argv) {
55 // Define program usage and read command line parameters
56 //-------------------------------------------------------
58 // Display program usage, when invoked from the command line with option '-h'.
59 cimg_usage("View the color profile of an image along the X axis");
61 // Read image filename from the command line (or set it to "img/parrot_original.ppm" if option '-i' is not provided).
62 const char* file_i = cimg_option("-i",cimg_imagepath "parrot_original.ppm","Input image");
64 // Read pre-blurring variance from the command line (or set it to 1.0 if option '-blur' is not provided).
65 const double sigma = cimg_option("-blur",1.0,"Variance of gaussian pre-blurring");
67 // Init variables
68 //----------------
70 // Load an image, transform it to a color image (if necessary) and blur it with the standard deviation sigma.
71 const CImg<unsigned char> image = CImg<>(file_i).normalize(0,255).blur((float)sigma).resize(-100,-100,1,3);
73 // Create two display window, one for the image, the other for the color profile.
74 CImgDisplay
75 main_disp(image,"Color image (Try to move mouse pointer over)",0),
76 draw_disp(500,400,"Color profile of the X-axis",0);
78 // Define colors used to plot the profile, and a hatch to draw the vertical line
79 unsigned long hatch = 0xF0F0F0F0;
80 const unsigned char
81 red[] = { 255,0,0 },
82 green[] = { 0,255,0 },
83 blue [] = { 0,0,255 },
84 black[] = { 0,0,0 };
86 // Enter event loop. This loop ends when one of the two display window is closed or when the keys 'ESC' or 'Q' are pressed.
87 while (!main_disp.is_closed && !draw_disp.is_closed &&
88 !main_disp.is_keyESC && !draw_disp.is_keyESC && !main_disp.is_keyQ && !draw_disp.is_keyQ) {
90 // Handle display window resizing (if any)
91 if (main_disp.is_resized) main_disp.resize().display(image);
92 draw_disp.resize();
94 if (main_disp.mouse_x>=0 && main_disp.mouse_y>=0) { // Mouse pointer is over the image
96 const int
97 xm = main_disp.mouse_x, // X-coordinate of the mouse pointer over the image
98 ym = main_disp.mouse_y, // Y-coordinate of the mouse pointer over the image
99 xl = xm*draw_disp.dimx()/main_disp.dimx(), // Corresponding X-coordinate of the hatched line
100 x = xm*image.dimx()/main_disp.dimx(), // Corresponding X-coordinate of the pointed pixel in the image
101 y = ym*image.dimy()/main_disp.dimy(); // Corresponding Y-coordinate of the pointex pixel in the image
103 // Retrieve color component values at pixel (x,y)
104 const unsigned int
105 val_red = image(x,y,0),
106 val_green = image(x,y,1),
107 val_blue = image(x,y,2);
109 // Create and display the image of the intensity profile
110 CImg<unsigned char>(draw_disp.dimx(),draw_disp.dimy(),1,3,255).
111 draw_grid(-50*100.0f/image.dimx(),-50*100.0f/256,0,0,false,true,black,0.2f,0xCCCCCCCC,0xCCCCCCCC).
112 draw_axis(0,image.dimx()-1.0f,255.0f,0.0f,black).
113 draw_graph(image.get_shared_line(y,0,0),red,1,1,0,255,1).
114 draw_graph(image.get_shared_line(y,0,1),green,1,1,0,255,1).
115 draw_graph(image.get_shared_line(y,0,2),blue,1,1,0,255,1).
116 draw_text(30,5,"Pixel (%d,%d)={%d %d %d}",black,0,1,11,
117 main_disp.mouse_x,main_disp.mouse_y,val_red,val_green,val_blue).
118 draw_line(xl,0,xl,draw_disp.dimy()-1,black,0.5f,hatch=cimg::rol(hatch)).
119 display(draw_disp);
120 } else
121 // else display a text in the profile display window.
122 CImg<unsigned char>(draw_disp.dimx(),draw_disp.dimy()).fill(255).
123 draw_text(draw_disp.dimx()/2-110,draw_disp.dimy()/2-5,"Mouse pointer is outside the image",black).display(draw_disp);
125 // Temporize event loop
126 cimg::wait(20);
127 }
129 return 0;
130 }