Wed, 05 Aug 2009 17:10:56 +0100
add README
1 /*
2 #
3 # File : generate_loop_macros.cpp
4 # ( C++ source file )
5 #
6 # Description : Generate C++ macros to deal with MxN[xP] neighborhood
7 # loops within the CImg Library.
8 # This file is a part of the CImg Library project.
9 # ( http://cimg.sourceforge.net )
10 #
11 # Copyright : David Tschumperle
12 # ( http://www.greyc.ensicaen.fr/~dtschump/ )
13 #
14 # License : CeCILL v2.0
15 # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
16 #
17 # This software is governed by the CeCILL license under French law and
18 # abiding by the rules of distribution of free software. You can use,
19 # modify and/ or redistribute the software under the terms of the CeCILL
20 # license as circulated by CEA, CNRS and INRIA at the following URL
21 # "http://www.cecill.info".
22 #
23 # As a counterpart to the access to the source code and rights to copy,
24 # modify and redistribute granted by the license, users are provided only
25 # with a limited warranty and the software's author, the holder of the
26 # economic rights, and the successive licensors have only limited
27 # liability.
28 #
29 # In this respect, the user's attention is drawn to the risks associated
30 # with loading, using, modifying and/or developing or reproducing the
31 # software by the user in light of its specific status of free software,
32 # that may mean that it is complicated to manipulate, and that also
33 # therefore means that it is reserved for developers and experienced
34 # professionals having in-depth computer knowledge. Users are therefore
35 # encouraged to load and test the software's suitability as regards their
36 # requirements in conditions enabling the security of their systems and/or
37 # data to be ensured and, more generally, to use and operate it in the
38 # same conditions as regards security.
39 #
40 # The fact that you are presently reading this means that you have had
41 # knowledge of the CeCILL license and that you accept its terms.
42 #
43 */
45 #include "CImg.h"
46 using namespace cimg_library;
48 // The lines below are necessary when using a non-standard compiler as visualcpp6.
49 #ifdef cimg_use_visualcpp6
50 #define std
51 #endif
53 // Generate macro(s) 'cimg_forN(i,bound)'
54 //----------------------------------------
55 void generate_forN(const unsigned int N) {
56 if (N>=2) {
57 const unsigned int Nn = N/2, Np = Nn-((N+1)%2);
58 std::printf("#define cimg_for%u(bound,i) for (int i = 0, \\\n",N);
59 { for (unsigned int k=0; k<Np; ++k) std::printf(" _p%u##i = 0,",Np-k); }
60 std::printf(" \\\n");
61 { for (unsigned int k=1; k<=Nn; ++k) std::printf(" _n%u##i = %u>=(int)(bound)?(int)(bound)-1:%u%c \\\n",k,k,k,k==Nn?';':','); }
62 std::printf(" _n%u##i<(int)(bound) || ",Nn);
63 { for (unsigned int k=Nn-1; k>=1; --k) std::printf("_n%u##i==--_n%u##i || ",k,k+1); }
64 std::printf("\\\n i==(");
65 { for (unsigned int k=Nn; k>=2; --k) std::printf("_n%u##i = ",k); }
66 std::printf("--_n1##i); \\\n ");
67 { for (unsigned int k=Np; k>=2; --k) std::printf("_p%u##i = _p%u##i, ",k,k-1); }
68 if (Np) std::printf("_p1##i = i++, \\\n ");
69 else std::printf(" ++i, ");
70 { for (unsigned int k=1; k<Nn; ++k) std::printf("++_n%u##i, ",k); }
71 std::printf("++_n%u##i)\n\n",Nn);
73 std::printf("#define cimg_for%uX(img,x) cimg_for%u((img).width,x)\n",N,N);
74 std::printf("#define cimg_for%uY(img,y) cimg_for%u((img).height,y)\n",N,N);
75 std::printf("#define cimg_for%uZ(img,z) cimg_for%u((img).depth,z)\n",N,N);
76 std::printf("#define cimg_for%uV(img,v) cimg_for%u((img).dim,v)\n",N,N);
77 std::printf("#define cimg_for%uXY(img,x,y) cimg_for%uY(img,y) cimg_for%uX(img,x)\n",N,N,N);
78 std::printf("#define cimg_for%uXZ(img,x,z) cimg_for%uZ(img,z) cimg_for%uX(img,x)\n",N,N,N);
79 std::printf("#define cimg_for%uXV(img,x,v) cimg_for%uV(img,v) cimg_for%uX(img,x)\n",N,N,N);
80 std::printf("#define cimg_for%uYZ(img,y,z) cimg_for%uZ(img,z) cimg_for%uY(img,y)\n",N,N,N);
81 std::printf("#define cimg_for%uYV(img,y,v) cimg_for%uV(img,v) cimg_for%uY(img,y)\n",N,N,N);
82 std::printf("#define cimg_for%uZV(img,z,v) cimg_for%uV(img,v) cimg_for%uZ(img,z)\n",N,N,N);
83 std::printf("#define cimg_for%uXYZ(img,x,y,z) cimg_for%uZ(img,z) cimg_for%uXY(img,x,y)\n",N,N,N);
84 std::printf("#define cimg_for%uXZV(img,x,z,v) cimg_for%uV(img,v) cimg_for%uXZ(img,x,z)\n",N,N,N);
85 std::printf("#define cimg_for%uYZV(img,y,z,v) cimg_for%uV(img,v) cimg_for%uYZ(img,y,z)\n",N,N,N);
86 std::printf("#define cimg_for%uXYZV(img,x,y,z,v) cimg_for%uV(img,v) cimg_for%uXYZ(img,x,y,z)\n\n",N,N,N);
87 }
88 }
90 // Generate macro(s) 'cimg_for_inN(i,bound)'
91 //------------------------------------------
92 void generate_for_inN(const unsigned int N) {
93 if (N>=2) {
94 const unsigned int Nn = N/2, Np = Nn-((N+1)%2);
95 std::printf("#define cimg_for_in%u(bound,i0,i1,i) for (int i = (int)(i0)<0?0:(int)(i0), \\\n",N);
96 { for (unsigned int k=0; k<Np; ++k) std::printf(" _p%u##i = i-%u<0?0:i-%u, \\\n",Np-k,Np-k,Np-k); }
97 { for (unsigned int k=1; k<=Nn; ++k) std::printf(" _n%u##i = i+%u>=(int)(bound)?(int)(bound)-1:i+%u%c \\\n",k,k,k,k==Nn?';':','); }
98 std::printf(" i<=(int)(i1) && (_n%u##i<(int)(bound) || ",Nn);
99 { for (unsigned int k=Nn-1; k>=1; --k) std::printf("_n%u##i==--_n%u##i || ",k,k+1); }
100 std::printf("\\\n i==(");
101 { for (unsigned int k=Nn; k>=2; --k) std::printf("_n%u##i = ",k); }
102 std::printf("--_n1##i)); \\\n ");
103 { for (unsigned int k=Np; k>=2; --k) std::printf("_p%u##i = _p%u##i, ",k,k-1); }
104 if (Np) std::printf("_p1##i = i++, \\\n ");
105 else std::printf(" ++i, ");
106 { for (unsigned int k=1; k<Nn; ++k) std::printf("++_n%u##i, ",k); }
107 std::printf("++_n%u##i)\n\n",Nn);
108 }
110 std::printf("#define cimg_for_in%uX(img,x0,x1,x) cimg_for_in%u((img).width,x0,x1,x)\n",N,N);
111 std::printf("#define cimg_for_in%uY(img,y0,y1,y) cimg_for_in%u((img).height,y0,y1,y)\n",N,N);
112 std::printf("#define cimg_for_in%uZ(img,z0,z1,z) cimg_for_in%u((img).depth,z0,z1,z)\n",N,N);
113 std::printf("#define cimg_for_in%uV(img,v0,v1,v) cimg_for_in%u((img).dim,v0,v1,v)\n",N,N);
114 std::printf("#define cimg_for_in%uXY(img,x0,y0,x1,y1,x,y) cimg_for_in%uY(img,y0,y1,y) cimg_for_in%uX(img,x0,x1,x)\n",N,N,N);
115 std::printf("#define cimg_for_in%uXZ(img,x0,z0,x1,z1,x,z) cimg_for_in%uZ(img,z0,z1,z) cimg_for_in%uX(img,x0,x1,x)\n",N,N,N);
116 std::printf("#define cimg_for_in%uXV(img,x0,v0,x1,v1,x,v) cimg_for_in%uV(img,v0,v1,v) cimg_for_in%uX(img,x0,x1,x)\n",N,N,N);
117 std::printf("#define cimg_for_in%uYZ(img,y0,z0,y1,z1,y,z) cimg_for_in%uZ(img,z0,z1,z) cimg_for_in%uY(img,y0,y1,y)\n",N,N,N);
118 std::printf("#define cimg_for_in%uYV(img,y0,v0,y1,v1,y,v) cimg_for_in%uV(img,v0,v1,v) cimg_for_in%uY(img,y0,y1,y)\n",N,N,N);
119 std::printf("#define cimg_for_in%uZV(img,z0,v0,z1,v1,z,v) cimg_for_in%uV(img,v0,v1,v) cimg_for_in%uZ(img,z0,z1,z)\n",N,N,N);
120 std::printf("#define cimg_for_in%uXYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in%uZ(img,z0,z1,z) cimg_for_in%uXY(img,x0,y0,x1,y1,x,y)\n",N,N,N);
121 std::printf("#define cimg_for_in%uXZV(img,x0,z0,v0,x1,y1,v1,x,z,v) cimg_for_in%uV(img,v0,v1,v) cimg_for_in%uXZ(img,x0,y0,x1,y1,x,z)\n",N,N,N);
122 std::printf("#define cimg_for_in%uYZV(img,y0,z0,v0,y1,z1,v1,y,z,v) cimg_for_in%uV(img,v0,v1,v) cimg_for_in%uYZ(img,y0,z0,y1,z1,y,z)\n",N,N,N);
123 std::printf("#define cimg_for_in%uXYZV(img,x0,y0,z0,v0,x1,y1,z1,v1,x,y,z,v) cimg_for_in%uV(img,v0,v1,v) cimg_for_in%uXYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)\n\n",N,N,N);
125 }
127 // Generate macro 'cimg_forMxN[xP](img,x,y,z,v,I)'
128 //------------------------------------------------
129 void generate_forMxNxP(const unsigned int M, const unsigned int N, const unsigned int P) {
130 char indx[16], indy[16], indz[16];
131 const int
132 Mn = (int)(M/2), Mp = (int)(Mn-((M+1)%2)),
133 Nn = (int)(N/2), Np = (int)(Nn-((N+1)%2)),
134 Pn = (int)(P/2), Pp = (int)(Pn-((P+1)%2)),
135 last = (int)(M*N*P);
137 if (P>1) std::printf("#define cimg_for%ux%ux%u(img,x,y,z,v,I) \\\n cimg_for%u((img).depth,z)",M,N,P,P);
138 else std::printf("#define cimg_for%ux%u(img,x,y,z,v,I) \\\n",M,N);
139 if (N>1) std::printf(" cimg_for%u((img).height,y) ",N);
140 else std::printf(" cimg_forY(img,y) ");
142 std::printf("for (int x = 0%c \\\n",M>1?',':';');
143 { for (int k=Mp; k>=1; --k) std::printf(" _p%u##x = 0%s",k,k==1?", \\\n":","); }
144 { for (int k=1; k<Mn; ++k) std::printf(" _n%u##x = %u>=((img).width)?(int)((img).width)-1:%u, \\\n",k,k,k); }
146 if (M>1) {
147 std::printf(" _n%u##x = (int)( \\\n ",Mn);
148 { for (int k=0, z=-Pp; z<=Pn; ++z)
149 for (int y=-Np; y<=Nn; ++y) {
150 for (int x=-Mp; x<=0; ++x) { std::printf("%sI[%d] =",k && x==-Mp?" (":(x==-Mp?"(":" "),k); ++k; }
151 k+=Mn;
152 if (y<0) std::sprintf(indy,"_p%d##",-y); else if (y>0) std::sprintf(indy,"_n%d##",y); else indy[0]='\0';
153 if (z<0) std::sprintf(indz,"_p%d##",-z); else if (z>0) std::sprintf(indz,"_n%d##",z); else indz[0]='\0';
154 std::printf(" (img)(0,%sy,%sz,v))%s",indy,indz,k>=last?",":", \\\n");
155 }}
157 std::printf(" \\\n");
158 for (int x=1; x<Mn; ++x)
159 for (int z=-Pp; z<=Pn; ++z)
160 for (int y=-Np; y<=Nn; ++y) {
161 if (y<0) std::sprintf(indy,"_p%d##",-y); else if (y>0) std::sprintf(indy,"_n%d##",y); else indy[0]='\0';
162 if (z<0) std::sprintf(indz,"_p%d##",-z); else if (z>0) std::sprintf(indz,"_n%d##",z); else indz[0]='\0';
163 std::printf(" (I[%d] = (img)(_n%d##x,%sy,%sz,v)), \\\n",(Mp+x)+(y+Np)*M+(z+Pp)*M*N,x,indy,indz);
164 }
165 std::printf(" %u>=((img).width)?(int)((img).width)-1:%u); \\\n",Mn,Mn);
166 }
168 if (M>1) std::printf(" (_n%u##x",Mn); else std::printf(" (x");
169 std::printf("<(int)((img).width) && ( \\\n");
171 { for (int z=-Pp; z<=Pn; ++z)
172 for (int y=-Np; y<=Nn; ++y) {
173 if (M>1) std::sprintf(indx,"_n%d##",Mn); else indx[0]='\0';
174 if (y<0) std::sprintf(indy,"_p%d##",-y); else if (y>0) std::sprintf(indy,"_n%d##",y); else indy[0]='\0';
175 if (z<0) std::sprintf(indz,"_p%d##",-z); else if (z>0) std::sprintf(indz,"_n%d##",z); else indz[0]='\0';
176 std::printf(" (I[%d] = (img)(%sx,%sy,%sz,v))%s",M-1+(y+Np)*M+(z+Pp)*M*N,indx,indy,indz,
177 z==Pn && y==Nn?",1))":", \\\n");
178 }}
180 if (M>1) {
181 std::printf(" || \\\n ");
182 { for (int k=Mn-1; k>=1; --k) std::printf("_n%d##x==--_n%u##x || ",k,k+1); }
183 std::printf("x==(");
184 { for (int k=Mn; k>=2; --k) std::printf("_n%d##x = ",k); }
185 std::printf("--_n1##x); \\\n");
186 } else std::printf("; \\\n");
188 if (M>1) {
189 { for (unsigned int k=0, z=0; z<P; ++z)
190 for (unsigned int y=0; y<N; ++y) {
191 for (unsigned int x=0; x<M-1; ++x) {
192 std::printf(" I[%d] = I[%d],",k,k+1);
193 ++k;
194 }
195 std::printf(" \\\n");
196 ++k;
197 }}
198 std::printf(" ");
199 { for (int k=Mp; k>=2; --k) std::printf("_p%d##x = _p%d##x, ",k,k-1); }
200 if (M>2) std::printf("_p1##x = x++, "); else std::printf("++x, ");
201 { for (int k=1; k<=Mn-1; ++k) std::printf("++_n%d##x, ",k); }
202 std::printf("++_n%d##x)\n\n",Mn);
203 } else std::printf(" ++x)\n\n");
204 }
206 // Generate macro 'cimg_for_inMxN[xP](img,x,y,z,v,I)'
207 //---------------------------------------------------
208 void generate_for_inMxNxP(const unsigned int M, const unsigned int N, const unsigned int P) {
209 char indx[16], indy[16], indz[16];
210 const int
211 Mn = (int)(M/2), Mp = (int)(Mn-((M+1)%2)),
212 Nn = (int)(N/2), Np = (int)(Nn-((N+1)%2)),
213 Pn = (int)(P/2), Pp = (int)(Pn-((P+1)%2));
215 if (P>1) std::printf("#define cimg_for_in%ux%ux%u(img,x0,y0,z0,x1,y1,z1,x,y,z,v,I) \\\n cimg_for_in%u((img).depth,z0,z1,z)",M,N,P,P);
216 else std::printf("#define cimg_for_in%ux%u(img,x0,y0,x1,y1,x,y,z,v,I) \\\n",M,N);
217 if (N>1) std::printf(" cimg_for_in%u((img).height,y0,y1,y) ",N);
218 else std::printf(" cimg_for_inY(img,y0,y1,y) ");
220 std::printf("for (int x = (int)(x0)<0?0:(int)(x0)%c \\\n",M>1?',':';');
221 { for (int k=Mp; k>=1; --k) std::printf(" _p%u##x = x-%u<0?0:x-%u, \\\n",k,k,k); }
222 { for (int k=1; k<Mn; ++k) std::printf(" _n%u##x = x+%u>=(int)((img).width)?(int)((img).width)-1:x+%u, \\\n",k,k,k); }
224 if (M>1) {
225 std::printf(" _n%u##x = (int)( \\\n",Mn);
226 for (int x=-Mp; x<Mn; ++x)
227 for (int z=-Pp; z<=Pn; ++z)
228 for (int y=-Np; y<=Nn; ++y) {
229 if (x<0) std::sprintf(indx,"_p%d##",-x); else if (x>0) std::sprintf(indx,"_n%d##",x); else indx[0]='\0';
230 if (y<0) std::sprintf(indy,"_p%d##",-y); else if (y>0) std::sprintf(indy,"_n%d##",y); else indy[0]='\0';
231 if (z<0) std::sprintf(indz,"_p%d##",-z); else if (z>0) std::sprintf(indz,"_n%d##",z); else indz[0]='\0';
232 std::printf(" (I[%d] = (img)(%sx,%sy,%sz,v)), \\\n",(Mp+x)+(y+Np)*M+(z+Pp)*M*N,indx,indy,indz);
233 }
234 std::printf(" x+%u>=(int)((img).width)?(int)((img).width)-1:x+%u); \\\n",Mn,Mn);
235 }
236 std::printf(" x<=(int)(x1) && (");
237 if (M>1) std::printf("(_n%u##x",Mn); else std::printf("(x");
238 std::printf("<(int)((img).width) && ( \\\n");
240 { for (int z=-Pp; z<=Pn; ++z)
241 for (int y=-Np; y<=Nn; ++y) {
242 if (M>1) std::sprintf(indx,"_n%d##",Mn); else indx[0]='\0';
243 if (y<0) std::sprintf(indy,"_p%d##",-y); else if (y>0) std::sprintf(indy,"_n%d##",y); else indy[0]='\0';
244 if (z<0) std::sprintf(indz,"_p%d##",-z); else if (z>0) std::sprintf(indz,"_n%d##",z); else indz[0]='\0';
245 std::printf(" (I[%d] = (img)(%sx,%sy,%sz,v))%s",M-1+(y+Np)*M+(z+Pp)*M*N,indx,indy,indz,
246 z==Pn && y==Nn?",1))":", \\\n");
247 }}
249 if (M>1) {
250 std::printf(" || \\\n ");
251 { for (int k=Mn-1; k>=1; --k) std::printf("_n%d##x==--_n%u##x || ",k,k+1); }
252 std::printf("x==(");
253 { for (int k=Mn; k>=2; --k) std::printf("_n%d##x = ",k); }
254 std::printf("--_n1##x)); \\\n");
255 } else std::printf("); \\\n");
257 if (M>1) {
258 { for (unsigned int k=0, z=0; z<P; ++z)
259 for (unsigned int y=0; y<N; ++y) {
260 for (unsigned int x=0; x<M-1; ++x) {
261 std::printf(" I[%d] = I[%d],",k,k+1);
262 ++k;
263 }
264 std::printf(" \\\n");
265 ++k;
266 }}
267 std::printf(" ");
268 { for (int k=Mp; k>=2; --k) std::printf("_p%d##x = _p%d##x, ",k,k-1); }
269 if (M>2) std::printf("_p1##x = x++, "); else std::printf("++x, ");
270 { for (int k=1; k<=Mn-1; ++k) std::printf("++_n%d##x, ",k); }
271 std::printf("++_n%d##x)\n\n",Mn);
272 } else std::printf(" ++x)\n\n");
273 }
275 // Generate macro 'cimg_getMxN[xP](img,x,y,z,v,I)'
276 //------------------------------------------------
277 void generate_getMxNxP(const unsigned int M, const unsigned int N, const unsigned int P) {
278 const int
279 Mn = (int)(M/2), Mp = (int)(Mn-((M+1)%2)),
280 Nn = (int)(N/2), Np = (int)(Nn-((N+1)%2)),
281 Pn = (int)(P/2), Pp = (int)(Pn-((P+1)%2)),
282 last = M*N*P-1;
283 if (P>1) std::printf("#define cimg_get%ux%ux%u(img,x,y,z,v,I) \\\n",M,N,P);
284 else std::printf("#define cimg_get%ux%u(img,x,y,z,v,I) \\\n",M,N);
285 char indx[16], indy[16], indz[16];
286 for (int k=0, z=-Pp; z<=Pn; ++z)
287 for (int y=-Np; y<=Nn; ++y)
288 for (int x=-Mp; x<=Mn; ++x) {
289 if (x<0) std::sprintf(indx,"_p%d##",-x); else if (x>0) std::sprintf(indx,"_n%d##",x); else indx[0]='\0';
290 if (y<0) std::sprintf(indy,"_p%d##",-y); else if (y>0) std::sprintf(indy,"_n%d##",y); else indy[0]='\0';
291 if (z<0) std::sprintf(indz,"_p%d##",-z); else if (z>0) std::sprintf(indz,"_n%d##",z); else indz[0]='\0';
292 std::printf(" I[%u] = (img)(%sx,%sy,%sz,v)%s",k,indx,indy,indz,
293 k==last?";\n\n":(x==Mn?", \\\n":","));
294 ++k;
295 }
296 }
298 //-----------------
299 // Main Procedure
300 //-----------------
301 int main(int argc, char **argv) {
303 cimg_usage("Generate C++ macros to deal with MxN[xP] neighborhood loops within the CImg Library");
305 // Read command line arguments
306 //----------------------------
307 const char *const size = cimg_option("-s","5x4","Size of the neighborhood");
308 const bool do_forN = cimg_option("-forN",true,"Generate 'cimg_forN()'");
309 const bool do_for_inN = cimg_option("-for_inN",true,"Generate 'cimg_for_inN()'");
310 const bool do_for = cimg_option("-for",true,"Generate 'cimg_forMxNxP()'");
311 const bool do_for_in = cimg_option("-for_in",true,"Generate 'cimg_for_inMxNxP()'");
312 const bool do_get = cimg_option("-get",true,"Generate 'cimg_getMxNxP()'");
313 if (cimg_option("-h",false,0)) std::exit(0);
315 unsigned int M = 1, N = 1 , P = 1;
316 std::sscanf(size,"%u%*c%u%*c%u",&M,&N,&P);
317 if (!M || !N || !P || (M==1 && N==1 && P==1)) {
318 std::fprintf(stderr,"\n%s : Error, bad neighborhood size '%s'\n",argv[0],size);
319 std::exit(0);
320 }
321 if (!do_forN && !do_get && !do_for) return 0;
323 if (P>1)
324 std::printf("// Define %ux%ux%u loop macros for CImg\n"
325 "//-------------------------------------\n",M,N,P);
326 else
327 std::printf("// Define %ux%u loop macros for CImg\n"
328 "//----------------------------------\n",M,N);
330 if (do_forN) {
331 if (N>1) generate_forN(N);
332 if (P>1 && P!=N) generate_forN(P);
333 }
334 if (do_for_inN) {
335 if (N>1) generate_for_inN(N);
336 if (P>1 && P!=N) generate_for_inN(P);
337 }
338 if (do_for) generate_forMxNxP(M,N,P);
339 if (do_for_in) generate_for_inMxNxP(M,N,P);
340 if (do_get) generate_getMxNxP(M,N,P);
342 return 0;
343 }