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