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