1 /*
2  #
3  #  File        : use_draw_gradient.cpp
4  #                ( C++ source file )
5  #
6  #  Description : Example of use for the CImg plugin 'plugins/draw_gradient.h'.
7  #                This file is a part of the CImg Library project.
8  #                ( http://cimg.eu )
9  #
10  #  Copyright  : Jerome Boulanger
11  #                ( http://www.ricam.oeaw.ac.at/people/page.cgi?firstn=Jerome;lastn=Boulanger )
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 */
43 
44 #define cimg_plugin "plugins/draw_gradient.h"
45 #include "CImg.h"
46 using namespace cimg_library;
47 
48 // Main procedure
49 //---------------
main(int argc,char ** argv)50 int main(int argc,char **argv) {
51 
52   // Read command line arguments
53   //----------------------------
54   cimg_usage("Example of the use of draw_gradient CImg plugin");
55   const char *const file_i  = cimg_option("-i",(char*)0,"Input image");
56   const int shape  = cimg_option("-s",1,"shape [0,6]");
57   const int profile  = cimg_option("-p",0,"profile [0,7]");
58 
59   // Define an image
60   CImg<unsigned char> img;
61   if (file_i) img.load(file_i).resize(-100,-100,-100,3);
62   else img.assign(300,200,1,3,0);
63 
64   // Define the color of the gradient
65   CImg<unsigned char> col(3);
66   const unsigned char col1[3] = { 0,0,255 }, col2[3] = { 255,255,255 };
67   CImgDisplay disp(img,"Click and drag to create color gradient",0);
68   while (!disp.is_closed() && !disp.key()) {
69 
70     // Get a vector direction from the user.
71     const CImg<int> selection = img.get_select(disp,1);
72 
73     // Draw a gradient using the selected coordinated.
74     col.rand(100,255);
75     printf("Gradient with %s from color (%d,%d,%d) to (%d,%d,%d)\n",
76 	   CImg<>::get_gradient_str(shape,profile),col(0),col(1),col(2),col1[0],col1[1],col2[2]);
77     img.draw_gradient(selection(0),selection(1),selection(3),selection(4),
78                       col.data(),col1,shape,profile,.7f).display(disp);
79   }
80 
81   // color 0 to transparency
82   if (file_i) img.load(file_i).resize(-100,-100,-100,3);
83   else img.assign(300,200,1,3,0);
84   img.display(disp);
85   disp.show().flush();
86   while (!disp.is_closed() && !disp.key()) {
87 
88     // Get a vector direction from the user.
89     const CImg<int> selection = img.get_select(disp,1);
90 
91     // Draw a gradient using the selected coordinated.
92     col.rand(100,255);
93     printf("Gradient with %s from color (%d,%d,%d) to transparency\n",
94 	   CImg<>::get_gradient_str(shape,profile),col(0),col(1),col(2));
95     img.draw_gradient(selection(0),selection(1),selection(3),selection(4),
96                       col.data(),0,shape,profile,.7f).display(disp);
97   }
98 
99 
100   // transparency to color 1
101   if (file_i) img.load(file_i).resize(-100,-100,-100,3);
102   else img.assign(300,200,1,3,0);
103   img.display(disp);
104   disp.show().flush();
105   while (!disp.is_closed() && !disp.key()) {
106 
107     // Get a vector direction from the user.
108     const CImg<int> selection = img.get_select(disp,1);
109 
110     // Draw a gradient using the selected coordinated.
111     col.rand(100,255);
112     printf("Gradient with %s from transparency to color (%d,%d,%d)\n",
113 	   CImg<>::get_gradient_str(shape,profile),col(0),col(1),col(2));
114     img.draw_gradient(selection(0),selection(1),selection(3),selection(4),
115                       0,col.data(),shape,profile,.7f).display(disp);
116   }
117 
118   // random
119   if (file_i) img.load(file_i).resize(-100,-100,-100,3);
120   else img.assign(300,200,1,3,0);
121   disp.set_title("Random color gradient").show().flush();
122   CImg<unsigned char> visu(img);
123   visu.display(disp);
124   while (!disp.is_closed() && !disp.key()) {
125     const int
126       x = (int)(cimg::rand()*visu.width()),
127       y = (int)(cimg::rand()*visu.height()),
128       rx = (int)((cimg::rand()*25 + 5)*(cimg::rand()>.5?-1:1)),
129       ry = (int)((cimg::rand()*25 + 5)*(cimg::rand()>.5?-1:1));
130     col.rand(64,255);
131     img.draw_gradient(x,y,x + rx,y + ry,col.data(),0,shape,profile,.4f);
132     visu = img;
133     visu.draw_text(10,10,"%.1ffps",col2,0,1,13,disp.frames_per_second()).display(disp);
134     if (disp.is_resized()) disp.resize();
135   }
136 
137   return 0;
138 }
139