Hooks
static bool BeforeCvFractal_fracInitInternal(CvFractal* self, int iNewXs, int iNewYs, int iGrain, CvRandom& random, byte* pbyHints, int iHintsLength, int iFlags, CvFractal* pRifts, int iFracXExp, int iFracYExp);static void AfterCvFractal_fracInitInternal(CvFractal* self, int iNewXs, int iNewYs, int iGrain, CvRandom& random, byte* pbyHints, int iHintsLength, int iFlags, CvFractal* pRifts, int iFracXExp, int iFracYExp);static void ReplaceCvFractal_fracInitInternal(void (*original)(CvFractal*, int, int, int, CvRandom&, byte*, int, int, CvFractal*, int, int), CvFractal* self, int iNewXs, int iNewYs, int iGrain, CvRandom& random, byte* pbyHints, int iHintsLength, int iFlags, CvFractal* pRifts, int iFracXExp, int iFracYExp);// CivHooks.h gives this hook point no types, so nothing checks these hooks' parameters.host->RegisterHook(host->pluginIndex, CivHooks::CvFractal_fracInitInternal::ID, CIV_HOOK_BEFORE, (void*)&BeforeCvFractal_fracInitInternal);host->RegisterHook(host->pluginIndex, CivHooks::CvFractal_fracInitInternal::ID, CIV_HOOK_AFTER, (void*)&AfterCvFractal_fracInitInternal);host->RegisterHook(host->pluginIndex, CivHooks::CvFractal_fracInitInternal::ID, CIV_HOOK_REPLACE, (void*)&ReplaceCvFractal_fracInitInternal);
Code
73void CvFractal::fracInitInternal(int iNewXs, int iNewYs, int iGrain, CvRandom& random, byte* pbyHints, int iHintsLength, int iFlags, CvFractal* pRifts, int iFracXExp, int iFracYExp)74{75 FAssert(iNewXs < FRACTAL_MAX_DIMS);76 FAssert(iNewYs < FRACTAL_MAX_DIMS);7778 int iSmooth;79 int iScreen; // This screens out already marked spots in m_aaiFrac[][];80 int iPass;81 int iSum;82 int iX, iY;83 int iI;8485 reset();8687 if(iFracXExp < 0)88 {89 iFracXExp = DEFAULT_FRAC_X_EXP;90 }91 if(iFracYExp < 0)92 {93 iFracYExp = DEFAULT_FRAC_Y_EXP;94 }9596 m_iFracXExp = iFracXExp;97 m_iFracYExp = iFracYExp;98 m_iFracX = 1 << iFracXExp;99 m_iFracY = 1 << iFracYExp;100101 // Init m_aaiFrac to all zeroes:102 for(iX = 0; iX < m_iFracX + 1; iX++)103 {104 for(iY = 0; iY < m_iFracY + 1; iY++)105 {106 m_aaiFrac[iX][iY] = 0;107 }108 }109110 m_iXs = iNewXs;111 m_iYs = iNewYs;112 m_iFlags = iFlags;113 m_iXInc = ((m_iFracX * FLOAT_PRECISION) / m_iXs);114 m_iYInc = ((m_iFracY * FLOAT_PRECISION) / m_iYs);115116 int iMinExp = std::min(m_iFracXExp, m_iFracYExp);117 iSmooth = range(iMinExp - iGrain, 0, iMinExp);118119 int iHintsWidth = (1 << (m_iFracXExp - iSmooth)) + ((m_iFlags & FRAC_WRAP_X) ? 0 : 1);120 int iHintsHeight = (1 << (m_iFracYExp - iSmooth)) + ((m_iFlags & FRAC_WRAP_Y) ? 0 : 1);121 if(pbyHints != NULL)122 {123 CvAssertMsg(iHintsLength == iHintsWidth*iHintsHeight, "pbyHints is the wrong size!")124 }125126 for(iPass = iSmooth; iPass >= 0; iPass--)127 {128 iScreen = 0;129130 for(iI = 0; iI <= iPass; iI++)131 {132 iScreen |= (1 << iI);133 }134135 if(m_iFlags & FRAC_WRAP_Y)136 {137 for(iX = 0; iX < m_iFracX + 1; iX++)138 {139 m_aaiFrac[iX][m_iFracY] = m_aaiFrac[iX][0];140 }141 }142 else if(m_iFlags & FRAC_POLAR)143 {144 for(iX = 0; iX < m_iFracX + 1; iX++)145 {146 m_aaiFrac[iX][ 0 ] = 0;147 m_aaiFrac[iX][m_iFracY] = 0;148 }149 }150151 if(m_iFlags & FRAC_WRAP_X)152 {153 for(iY = 0; iY < m_iFracY + 1; iY++)154 {155 m_aaiFrac[m_iFracX][iY] = m_aaiFrac[0][iY];156 }157 }158 else if(m_iFlags & FRAC_POLAR)159 {160 for(iY = 0; iY < m_iFracY + 1; iY++)161 {162 m_aaiFrac[ 0 ][iY] = 0;163 m_aaiFrac[m_iFracX][iY] = 0;164 }165 }166167 if(m_iFlags & FRAC_CENTER_RIFT)168 {169 if(m_iFlags & FRAC_WRAP_Y)170 {171 for(iX = 0; iX < m_iFracX + 1; iX++)172 {173 for(iY = 0; iY < (m_iFracY / 6); iY++)174 {175 m_aaiFrac[iX][ iY ] /= (abs((m_iFracY / 12) - iY) + 1);176 m_aaiFrac[iX][(m_iFracY / 2) + iY] /= (abs((m_iFracY / 12) - iY) + 1);177 }178 }179 }180181 if(m_iFlags & FRAC_WRAP_X)182 {183 for(iY = 0; iY < m_iFracY + 1; iY++)184 {185 for(iX = 0; iX < (m_iFracX / 6); iX++)186 {187 m_aaiFrac[ iX ][iY] /= (abs((m_iFracX / 12) - iX) + 1);188 m_aaiFrac[(m_iFracX / 2) + iX][iY] /= (abs((m_iFracX / 12) - iX) + 1);189 }190 }191 }192 }193194 for(iX = 0; iX < (m_iFracX >> iPass) + ((m_iFlags & FRAC_WRAP_X) ? 0 : 1); iX++)195 {196 for(iY = 0; iY < (m_iFracY >> iPass) + ((m_iFlags & FRAC_WRAP_Y) ? 0 : 1); iY++)197 {198 if((iPass == iSmooth)) // If this is the first, pass, set the initial random spots199 {200 if(pbyHints == NULL)201 {202 m_aaiFrac[iX << iPass][iY << iPass] = random.get(256, "Fractal Gen");203 }204 else205 {206 int iXX = iX % iHintsWidth; // wrap207 int iYY = iY % iHintsHeight; // wrap208 int iHintsI = iYY*iHintsWidth + iXX;209210 DEBUG_VARIABLE(iHintsLength);211 CvAssertMsg(iHintsI < iHintsLength, "iHintsI out of range");212 m_aaiFrac[iX << iPass][iY << iPass] = pbyHints[iHintsI];213 }214 }215 else // Interpolate216 {217 iSum = 0;218 if((iX << iPass) & iScreen)219 {220 if((iY << iPass) & iScreen) // (center)221 {222 iSum += m_aaiFrac[(iX-1) << iPass][(iY-1) << iPass];223 iSum += m_aaiFrac[(iX+1) << iPass][(iY-1) << iPass];224 iSum += m_aaiFrac[(iX-1) << iPass][(iY+1) << iPass];225 iSum += m_aaiFrac[(iX+1) << iPass][(iY+1) << iPass];226 iSum >>= 2;227 iSum += random.get(1 << (8 - iSmooth + iPass), "Fractal Gen 2");228 iSum -= 1 << (7 - iSmooth + iPass);229 iSum = range(iSum, 0, 255);230 m_aaiFrac[iX << iPass][iY << iPass] = iSum;231 }232 else // (horizontal)233 {234 iSum += m_aaiFrac[(iX-1) << iPass][iY << iPass];235 iSum += m_aaiFrac[(iX+1) << iPass][iY << iPass];236 iSum >>= 1;237 iSum += random.get(1 << (8 - iSmooth + iPass), "Fractal Gen 3");238 iSum -= 1 << (7 - iSmooth + iPass);239 iSum = range(iSum, 0, 255);240 m_aaiFrac[iX << iPass][iY << iPass] = iSum;241 }242 }243 else244 {245 if((iY << iPass) & iScreen) // (vertical)246 {247 iSum += m_aaiFrac[iX << iPass][(iY-1) << iPass];248 iSum += m_aaiFrac[iX << iPass][(iY+1) << iPass];249 iSum >>= 1;250 iSum += random.get(1 << (8 - iSmooth + iPass), "Fractal Gen 4");251 iSum -= 1 << (7 - iSmooth + iPass);252 iSum = range(iSum, 0, 255);253 m_aaiFrac[iX << iPass][iY << iPass] = (BYTE) iSum;254 }255 else256 {257 continue; // (corner) This was already set in an earlier iPass.258 }259 }260 }261 }262 }263 }264265 if(pRifts)266 {267 tectonicAction(pRifts); // Assumes FRAC_WRAP_X is on.268 }269270 if(m_iFlags & FRAC_INVERT_HEIGHTS)271 {272 for(iX = 0; iX < m_iFracX; iX++)273 {274 for(iY = 0; iY < m_iFracY; iY++)275 {276 m_aaiFrac[iX][iY] = (255 - m_aaiFrac[iX][iY]);277 }278 }279 }280}