blob: ab09d8a9394f72355731782bf59cc2cb5804498e [file] [log] [blame]
Austin Schuh8c794d52019-03-03 21:17:37 -08001/*
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.eu )
10 #
11 # Copyright : David Tschumperle
12 # ( http://tschumperle.users.greyc.fr/ )
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*/
44
45#include "CImg.h"
46using namespace cimg_library;
47
48// Generate macro(s) 'cimg_forN(i,bound)'
49//----------------------------------------
50void generate_forN(const unsigned int N) {
51 if (N>=2) {
52 const unsigned int Nn = N/2, Np = Nn - ((N + 1)%2);
53 std::printf("#define cimg_for%u(bound,i) for (int i = 0, \\\n",N);
54 for (unsigned int k = 0; k<Np; ++k) std::printf(" _p%u##i = 0,",Np - k);
55 std::printf(" \\\n");
56 for (unsigned int k = 1; k<=Nn; ++k)
57 std::printf(" _n%u##i = %u>=(int)(bound)?(int)(bound) - 1:%u%c \\\n",k,k,k,k==Nn?';':',');
58 std::printf(" _n%u##i<(int)(bound) || ",Nn);
59 for (unsigned int k = Nn - 1; k>=1; --k) std::printf("_n%u##i==--_n%u##i || ",k,k + 1);
60 std::printf("\\\n i==(");
61 for (unsigned int k = Nn; k>=2; --k) std::printf("_n%u##i = ",k);
62 std::printf("--_n1##i); \\\n ");
63 for (unsigned int k = Np; k>=2; --k) std::printf("_p%u##i = _p%u##i, ",k,k - 1);
64 if (Np) std::printf("_p1##i = i++, \\\n ");
65 else std::printf(" ++i, ");
66 for (unsigned int k = 1; k<Nn; ++k) std::printf("++_n%u##i, ",k);
67 std::printf("++_n%u##i)\n\n",Nn);
68
69 std::printf("#define cimg_for%uX(img,x) cimg_for%u((img)._width,x)\n",N,N);
70 std::printf("#define cimg_for%uY(img,y) cimg_for%u((img)._height,y)\n",N,N);
71 std::printf("#define cimg_for%uZ(img,z) cimg_for%u((img)._depth,z)\n",N,N);
72 std::printf("#define cimg_for%uC(img,c) cimg_for%u((img)._spectrum,c)\n",N,N);
73 std::printf("#define cimg_for%uXY(img,x,y) cimg_for%uY(img,y) cimg_for%uX(img,x)\n",N,N,N);
74 std::printf("#define cimg_for%uXZ(img,x,z) cimg_for%uZ(img,z) cimg_for%uX(img,x)\n",N,N,N);
75 std::printf("#define cimg_for%uXC(img,x,c) cimg_for%uC(img,c) cimg_for%uX(img,x)\n",N,N,N);
76 std::printf("#define cimg_for%uYZ(img,y,z) cimg_for%uZ(img,z) cimg_for%uY(img,y)\n",N,N,N);
77 std::printf("#define cimg_for%uYC(img,y,c) cimg_for%uC(img,c) cimg_for%uY(img,y)\n",N,N,N);
78 std::printf("#define cimg_for%uZC(img,z,c) cimg_for%uC(img,c) cimg_for%uZ(img,z)\n",N,N,N);
79 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);
80 std::printf("#define cimg_for%uXZC(img,x,z,c) cimg_for%uC(img,c) cimg_for%uXZ(img,x,z)\n",N,N,N);
81 std::printf("#define cimg_for%uYZC(img,y,z,c) cimg_for%uC(img,c) cimg_for%uYZ(img,y,z)\n",N,N,N);
82 std::printf("#define cimg_for%uXYZC(img,x,y,z,c) cimg_for%uC(img,c) cimg_for%uXYZ(img,x,y,z)\n\n",N,N,N);
83 }
84}
85
86// Generate macro(s) 'cimg_for_inN(i,bound)'
87//------------------------------------------
88void generate_for_inN(const unsigned int N) {
89 if (N>=2) {
90 const unsigned int Nn = N/2, Np = Nn - ((N + 1)%2);
91 std::printf("#define cimg_for_in%u(bound,i0,i1,i) for (int i = (int)(i0)<0?0:(int)(i0), \\\n",N);
92 for (unsigned int k = 0; k<Np; ++k)
93 std::printf(" _p%u##i = i - %u<0?0:i-%u, \\\n",Np - k,Np - k,Np - k);
94 for (unsigned int k = 1; k<=Nn; ++k)
95 std::printf(" _n%u##i = i + %u>=(int)(bound)?(int)(bound) - 1:i + %u%c \\\n",k,k,k,k==Nn?';':',');
96 std::printf(" i<=(int)(i1) && (_n%u##i<(int)(bound) || ",Nn);
97 for (unsigned int k = Nn - 1; k>=1; --k) std::printf("_n%u##i==--_n%u##i || ",k,k + 1);
98 std::printf("\\\n i==(");
99 for (unsigned int k = Nn; k>=2; --k) std::printf("_n%u##i = ",k);
100 std::printf("--_n1##i)); \\\n ");
101 for (unsigned int k = Np; k>=2; --k) std::printf("_p%u##i = _p%u##i, ",k,k - 1);
102 if (Np) std::printf("_p1##i = i++, \\\n ");
103 else std::printf(" ++i, ");
104 for (unsigned int k = 1; k<Nn; ++k) std::printf("++_n%u##i, ",k);
105 std::printf("++_n%u##i)\n\n",Nn);
106 }
107
108 std::printf("#define cimg_for_in%uX(img,x0,x1,x) cimg_for_in%u((img)._width,x0,x1,x)\n",N,N);
109 std::printf("#define cimg_for_in%uY(img,y0,y1,y) cimg_for_in%u((img)._height,y0,y1,y)\n",N,N);
110 std::printf("#define cimg_for_in%uZ(img,z0,z1,z) cimg_for_in%u((img)._depth,z0,z1,z)\n",N,N);
111 std::printf("#define cimg_for_in%uC(img,c0,c1,c) cimg_for_in%u((img)._spectrum,c0,c1,c)\n",N,N);
112 std::printf("#define cimg_for_in%uXY(img,x0,y0,x1,y1,x,y) cimg_for_in%uY(img,y0,y1,y) "
113 "cimg_for_in%uX(img,x0,x1,x)\n",N,N,N);
114 std::printf("#define cimg_for_in%uXZ(img,x0,z0,x1,z1,x,z) cimg_for_in%uZ(img,z0,z1,z) "
115 "cimg_for_in%uX(img,x0,x1,x)\n",N,N,N);
116 std::printf("#define cimg_for_in%uXC(img,x0,c0,x1,c1,x,c) cimg_for_in%uC(img,c0,c1,c) "
117 "cimg_for_in%uX(img,x0,x1,x)\n",N,N,N);
118 std::printf("#define cimg_for_in%uYZ(img,y0,z0,y1,z1,y,z) cimg_for_in%uZ(img,z0,z1,z) "
119 "cimg_for_in%uY(img,y0,y1,y)\n",N,N,N);
120 std::printf("#define cimg_for_in%uYC(img,y0,c0,y1,c1,y,c) cimg_for_in%uC(img,c0,c1,c) "
121 "cimg_for_in%uY(img,y0,y1,y)\n",N,N,N);
122 std::printf("#define cimg_for_in%uZC(img,z0,c0,z1,c1,z,c) cimg_for_in%uC(img,c0,c1,c) "
123 "cimg_for_in%uZ(img,z0,z1,z)\n",N,N,N);
124 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) "
125 "cimg_for_in%uXY(img,x0,y0,x1,y1,x,y)\n",N,N,N);
126 std::printf("#define cimg_for_in%uXZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in%uC(img,c0,c1,c) "
127 "cimg_for_in%uXZ(img,x0,y0,x1,y1,x,z)\n",N,N,N);
128 std::printf("#define cimg_for_in%uYZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in%uC(img,c0,c1,c) "
129 "cimg_for_in%uYZ(img,y0,z0,y1,z1,y,z)\n",N,N,N);
130 std::printf("#define cimg_for_in%uXYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) cimg_for_in%uC(img,c0,c1,c) "
131 "cimg_for_in%uXYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)\n\n",N,N,N);
132
133}
134
135// Generate macro 'cimg_forMxN[xP](img,x,y,z,c,I,T)'
136//--------------------------------------------------
137void generate_forMxNxP(const unsigned int M, const unsigned int N, const unsigned int P) {
138 char indx[16], indy[16], indz[16];
139 const int
140 Mn = (int)(M/2), Mp = (int)(Mn - ((M + 1)%2)),
141 Nn = (int)(N/2), Np = (int)(Nn - ((N + 1)%2)),
142 Pn = (int)(P/2), Pp = (int)(Pn - ((P + 1)%2)),
143 last = (int)(M*N*P);
144
145 if (P>1) std::printf("#define cimg_for%ux%ux%u(img,x,y,z,c,I,T) \\\n cimg_for%u((img)._depth,z)",M,N,P,P);
146 else std::printf("#define cimg_for%ux%u(img,x,y,z,c,I,T) \\\n",M,N);
147 if (N>1) std::printf(" cimg_for%u((img)._height,y) ",N);
148 else std::printf(" cimg_forY(img,y) ");
149
150 std::printf("for (int x = 0%c \\\n",M>1?',':';');
151 for (int k = Mp; k>=1; --k) std::printf(" _p%u##x = 0%s",k,k==1?", \\\n":",");
152 for (int k = 1; k<Mn; ++k) std::printf(" _n%u##x = %u>=((img)._width)?(img).width() - 1:%u, \\\n",k,k,k);
153
154 if (M>1) {
155 std::printf(" _n%u##x = (int)( \\\n ",Mn);
156 for (int k = 0, z = -Pp; z<=Pn; ++z)
157 for (int y = -Np; y<=Nn; ++y) {
158 for (int x = -Mp; x<=0; ++x) { std::printf("%sI[%d] =",k && x==-Mp?" (":(x==-Mp?"(":" "),k); ++k; }
159 k+=Mn;
160 if (y<0) std::sprintf(indy,"_p%d##",-y); else if (y>0) std::sprintf(indy,"_n%d##",y); else indy[0]='\0';
161 if (z<0) std::sprintf(indz,"_p%d##",-z); else if (z>0) std::sprintf(indz,"_n%d##",z); else indz[0]='\0';
162 std::printf(" (T)(img)(0,%sy,%sz,c))%s",indy,indz,k>=last?",":", \\\n");
163 }
164
165 std::printf(" \\\n");
166 for (int x = 1; x<Mn; ++x)
167 for (int z = -Pp; z<=Pn; ++z)
168 for (int y = -Np; y<=Nn; ++y) {
169 if (y<0) std::sprintf(indy,"_p%d##",-y); else if (y>0) std::sprintf(indy,"_n%d##",y); else indy[0]='\0';
170 if (z<0) std::sprintf(indz,"_p%d##",-z); else if (z>0) std::sprintf(indz,"_n%d##",z); else indz[0]='\0';
171 std::printf(" (I[%d] = (T)(img)(_n%d##x,%sy,%sz,c)), \\\n",(Mp + x) + (y + Np)*M + (z + Pp)*M*N,x,indy,indz);
172 }
173 std::printf(" %u>=((img)._width)?(img).width() - 1:%u); \\\n",Mn,Mn);
174 }
175
176 if (M>1) std::printf(" (_n%u##x",Mn); else std::printf(" (x");
177 std::printf("<(img).width() && ( \\\n");
178
179 for (int z = -Pp; z<=Pn; ++z)
180 for (int y = -Np; y<=Nn; ++y) {
181 if (M>1) std::sprintf(indx,"_n%d##",Mn); else indx[0]='\0';
182 if (y<0) std::sprintf(indy,"_p%d##",-y); else if (y>0) std::sprintf(indy,"_n%d##",y); else indy[0]='\0';
183 if (z<0) std::sprintf(indz,"_p%d##",-z); else if (z>0) std::sprintf(indz,"_n%d##",z); else indz[0]='\0';
184 std::printf(" (I[%d] = (T)(img)(%sx,%sy,%sz,c))%s",M - 1 + (y + Np)*M + (z + Pp)*M*N,indx,indy,indz,
185 z==Pn && y==Nn?",1))":", \\\n");
186 }
187
188 if (M>1) {
189 std::printf(" || \\\n ");
190 for (int k = Mn - 1; k>=1; --k) std::printf("_n%d##x==--_n%u##x || ",k,k + 1);
191 std::printf("x==(");
192 for (int k = Mn; k>=2; --k) std::printf("_n%d##x = ",k);
193 std::printf("--_n1##x); \\\n");
194 } else std::printf("; \\\n");
195
196 if (M>1) {
197 for (unsigned int k = 0, z = 0; z<P; ++z)
198 for (unsigned int y = 0; y<N; ++y) {
199 for (unsigned int x = 0; x<M - 1; ++x) {
200 std::printf(" I[%d] = I[%d],",k,k + 1);
201 ++k;
202 }
203 std::printf(" \\\n");
204 ++k;
205 }
206 std::printf(" ");
207 for (int k = Mp; k>=2; --k) std::printf("_p%d##x = _p%d##x, ",k,k - 1);
208 if (M>2) std::printf("_p1##x = x++, "); else std::printf("++x, ");
209 for (int k = 1; k<=Mn - 1; ++k) std::printf("++_n%d##x, ",k);
210 std::printf("++_n%d##x)\n\n",Mn);
211 } else std::printf(" ++x)\n\n");
212}
213
214// Generate macro 'cimg_for_inMxN[xP](img,x,y,z,c,I,T)'
215//-----------------------------------------------------
216void generate_for_inMxNxP(const unsigned int M, const unsigned int N, const unsigned int P) {
217 char indx[16], indy[16], indz[16];
218 const int
219 Mn = (int)(M/2), Mp = (int)(Mn - ((M + 1)%2)),
220 Nn = (int)(N/2), Np = (int)(Nn - ((N + 1)%2)),
221 Pn = (int)(P/2), Pp = (int)(Pn - ((P + 1)%2));
222
223 if (P>1)
224 std::printf("#define cimg_for_in%ux%ux%u(img,x0,y0,z0,x1,y1,z1,x,y,z,c,I,T) \\\n "
225 "cimg_for_in%u((img)._depth,z0,z1,z)",M,N,P,P);
226 else std::printf("#define cimg_for_in%ux%u(img,x0,y0,x1,y1,x,y,z,c,I,T) \\\n",M,N);
227 if (N>1) std::printf(" cimg_for_in%u((img)._height,y0,y1,y) ",N);
228 else std::printf(" cimg_for_inY(img,y0,y1,y) ");
229
230 std::printf("for (int x = (int)(x0)<0?0:(int)(x0)%c \\\n",M>1?',':';');
231 for (int k = Mp; k>=1; --k) std::printf(" _p%u##x = x - %u<0?0:x - %u, \\\n",k,k,k);
232 for (int k = 1; k<Mn; ++k) std::printf(" _n%u##x = x + %u>=(img).width()?(img).width() - 1:x + %u, \\\n",k,k,k);
233
234 if (M>1) {
235 std::printf(" _n%u##x = (int)( \\\n",Mn);
236 for (int x = -Mp; x<Mn; ++x)
237 for (int z = -Pp; z<=Pn; ++z)
238 for (int y = -Np; y<=Nn; ++y) {
239 if (x<0) std::sprintf(indx,"_p%d##",-x); else if (x>0) std::sprintf(indx,"_n%d##",x); else indx[0]='\0';
240 if (y<0) std::sprintf(indy,"_p%d##",-y); else if (y>0) std::sprintf(indy,"_n%d##",y); else indy[0]='\0';
241 if (z<0) std::sprintf(indz,"_p%d##",-z); else if (z>0) std::sprintf(indz,"_n%d##",z); else indz[0]='\0';
242 std::printf(" (I[%d] = (T)(img)(%sx,%sy,%sz,c)), \\\n",(Mp + x) + (y + Np)*M + (z + Pp)*M*N,indx,indy,indz);
243 }
244 std::printf(" x + %u>=(img).width()?(img).width() - 1:x + %u); \\\n",Mn,Mn);
245 }
246 std::printf(" x<=(int)(x1) && (");
247 if (M>1) std::printf("(_n%u##x",Mn); else std::printf("(x");
248 std::printf("<(img).width() && ( \\\n");
249
250 for (int z = -Pp; z<=Pn; ++z)
251 for (int y = -Np; y<=Nn; ++y) {
252 if (M>1) std::sprintf(indx,"_n%d##",Mn); else indx[0]='\0';
253 if (y<0) std::sprintf(indy,"_p%d##",-y); else if (y>0) std::sprintf(indy,"_n%d##",y); else indy[0]='\0';
254 if (z<0) std::sprintf(indz,"_p%d##",-z); else if (z>0) std::sprintf(indz,"_n%d##",z); else indz[0]='\0';
255 std::printf(" (I[%d] = (T)(img)(%sx,%sy,%sz,c))%s",M - 1 + (y + Np)*M + (z + Pp)*M*N,indx,indy,indz,
256 z==Pn && y==Nn?",1))":", \\\n");
257 }
258
259 if (M>1) {
260 std::printf(" || \\\n ");
261 for (int k = Mn - 1; k>=1; --k) std::printf("_n%d##x==--_n%u##x || ",k,k + 1);
262 std::printf("x==(");
263 for (int k = Mn; k>=2; --k) std::printf("_n%d##x = ",k);
264 std::printf("--_n1##x)); \\\n");
265 } else std::printf("); \\\n");
266
267 if (M>1) {
268 for (unsigned int k = 0, z = 0; z<P; ++z)
269 for (unsigned int y = 0; y<N; ++y) {
270 for (unsigned int x = 0; x<M - 1; ++x) {
271 std::printf(" I[%d] = I[%d],",k,k + 1);
272 ++k;
273 }
274 std::printf(" \\\n");
275 ++k;
276 }
277 std::printf(" ");
278 for (int k = Mp; k>=2; --k) std::printf("_p%d##x = _p%d##x, ",k,k - 1);
279 if (M>2) std::printf("_p1##x = x++, "); else std::printf("++x, ");
280 for (int k = 1; k<=Mn - 1; ++k) std::printf("++_n%d##x, ",k);
281 std::printf("++_n%d##x)\n\n",Mn);
282 } else std::printf(" ++x)\n\n");
283}
284
285// Generate macro 'cimg_getMxN[xP](img,x,y,z,c,I,T)'
286//--------------------------------------------------
287void generate_getMxNxP(const unsigned int M, const unsigned int N, const unsigned int P) {
288 const int
289 Mn = (int)(M/2), Mp = (int)(Mn - ((M + 1)%2)),
290 Nn = (int)(N/2), Np = (int)(Nn - ((N + 1)%2)),
291 Pn = (int)(P/2), Pp = (int)(Pn - ((P + 1)%2)),
292 last = M*N*P - 1;
293 if (P>1) std::printf("#define cimg_get%ux%ux%u(img,x,y,z,c,I,T) \\\n",M,N,P);
294 else std::printf("#define cimg_get%ux%u(img,x,y,z,c,I,T) \\\n",M,N);
295 char indx[16], indy[16], indz[16];
296 for (int k = 0, z = -Pp; z<=Pn; ++z)
297 for (int y = -Np; y<=Nn; ++y)
298 for (int x = -Mp; x<=Mn; ++x) {
299 if (x<0) std::sprintf(indx,"_p%d##",-x); else if (x>0) std::sprintf(indx,"_n%d##",x); else indx[0]='\0';
300 if (y<0) std::sprintf(indy,"_p%d##",-y); else if (y>0) std::sprintf(indy,"_n%d##",y); else indy[0]='\0';
301 if (z<0) std::sprintf(indz,"_p%d##",-z); else if (z>0) std::sprintf(indz,"_n%d##",z); else indz[0]='\0';
302 std::printf(" I[%u] = (T)(img)(%sx,%sy,%sz,c)%s",k,indx,indy,indz,
303 k==last?";\n\n":(x==Mn?", \\\n":","));
304 ++k;
305 }
306}
307
308//-----------------
309// Main Procedure
310//-----------------
311int main(int argc, char **argv) {
312
313 cimg_usage("Generate C++ macros to deal with MxN[xP] neighborhood loops within the CImg Library");
314
315 // Read command line arguments
316 //----------------------------
317 const char *const size = cimg_option("-s","5x4","Size of the neighborhood");
318 const bool do_forN = cimg_option("-forN",true,"Generate 'cimg_forN()'");
319 const bool do_for_inN = cimg_option("-for_inN",true,"Generate 'cimg_for_inN()'");
320 const bool do_for = cimg_option("-for",true,"Generate 'cimg_forMxNxP()'");
321 const bool do_for_in = cimg_option("-for_in",true,"Generate 'cimg_for_inMxNxP()'");
322 const bool do_get = cimg_option("-get",true,"Generate 'cimg_getMxNxP()'");
323 if (cimg_option("-h",false,0)) std::exit(0);
324
325 unsigned int M = 1, N = 1 , P = 1;
326 std::sscanf(size,"%u%*c%u%*c%u",&M,&N,&P);
327 if (!M || !N || !P || (M==1 && N==1 && P==1)) {
328 std::fprintf(stderr,"\n%s : Error, bad neighborhood size '%s'\n",argv[0],size);
329 std::exit(0);
330 }
331 if (!do_forN && !do_get && !do_for) return 0;
332
333 if (P>1)
334 std::printf("// Define %ux%ux%u loop macros\n"
335 "//----------------------------\n",M,N,P);
336 else
337 std::printf("// Define %ux%u loop macros\n"
338 "//-------------------------\n",M,N);
339
340 if (do_forN) {
341 if (N>1) generate_forN(N);
342 if (P>1 && P!=N) generate_forN(P);
343 }
344 if (do_for_inN) {
345 if (N>1) generate_for_inN(N);
346 if (P>1 && P!=N) generate_for_inN(P);
347 }
348 if (do_for) generate_forMxNxP(M,N,P);
349 if (do_for_in) generate_for_inMxNxP(M,N,P);
350 if (do_get) generate_getMxNxP(M,N,P);
351
352 return 0;
353}