File: Noise.cpp

package info (click to toggle)
between 6%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 3,532 kB
  • sloc: cpp: 28,110; php: 718; ansic: 638; objc: 245; sh: 236; makefile: 99; perl: 67
file content (312 lines) | stat: -rw-r--r-- 7,340 bytes parent folder | download | duplicates (30)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Jason Rohrer
// Noise.cpp

/**
*
*	Noise generation implementation
*
*
*	Created 11-3-99
*	Mods:	
*		Jason Rohrer	12-20-2000	Changed genFractalNoise2d function to make
*									it less blocky.
*
*/


#include "Noise.h"


// fills 2d image with ARGB noise
void genRandNoise2d(unsigned long *buff, int buffHigh, int buffWide) {
	
	int *yOffset = new int[buffHigh];
	
	// precalc y offsets
	for( int y=0; y<buffHigh; y++) {
		yOffset[y] = buffWide*y;
		}
	
	
	int red = (int)(255 * floatRand());
	int green = (int)(255 *  floatRand());
	int blue = (int)(255 *  floatRand());
	int alpha = (int)(255 *  floatRand());
	
	for( int y=0; y<buffHigh; y++) {
		int yContrib = yOffset[y];
		
		
		
		for( int x=0; x<buffWide; x++) {
		
		
/*			int red = (int)(255 * floatRand());
			int green = (int)(255 *  floatRand());
			int blue = (int)(255 *  floatRand());
			int alpha = (int)(255 *  floatRand());
*/			
			buff[ yContrib + x] = blue | green << 8 | red << 16 | alpha << 24;
		
			}
		}
		
	delete [] yOffset;
	}


// fills 2d image with ARGB fractal noise
void genFractalNoise2d(unsigned long *buff, int buffHigh, int buffWide) {
	
	int *yOffset = new int[buffHigh];
		
	// precalc y offsets
	for( int y=0; y<buffHigh; y++) {
		yOffset[y] = buffWide*y;
		}
	
	// first, zero out the buffer
	
	for( int y=0; y<buffHigh; y++) {
		int yContrib = yOffset[y];
		for( int x=0; x<buffWide; x++) {		
			buff[ yContrib + x] = 127 | 127 << 8 | 127 << 16 | 127 << 24;
			}
		}
	
	
	// walk through each frequency....
	// weight in sum of frequencies by 1/f
	
	// frequency in cycles per buffer width
	// stop when frequency == buffer width
	
	for( int f=1; f<=buffWide; f++) {		// for each frequency
		
		float weight = 1 / (float)(f);
		
		// number of pixels in each cycle
		int blockWide = (int)(buffWide * weight );	
		int blockHigh = (int)(buffHigh * weight );
		
		

		int buffY = 0;
		
		while( buffY < buffHigh ) {
			
			int startY = buffY;
			
			int buffX = 0;
			while( buffX < buffWide ) {
				
				buffY = startY;
				int startX = buffX;
				
				// color components for this block
				// weighted by frequency
				int red = (int)((255 * floatRand() - 127) * weight);
				int green = (int)((255 * floatRand() - 127) * weight);
				int blue = (int)((255 * floatRand() - 127) * weight);
				int alpha = (int)((255 * floatRand() - 127) * weight);
				
				
				int blockY = 0;
				while( blockY < blockHigh) {
					int blockX = 0;
					buffX = startX;
					while( blockX < blockWide) {
						
						unsigned long buffARGB = buff[ yOffset[buffY] + buffX];
						int buffAlpha = (buffARGB >> 24 & 0xFF) + alpha;
						int buffRed = (buffARGB >> 16 & 0xFF) + red;
						int buffGreen = (buffARGB >> 8 & 0xFF) + green;
						int buffBlue = (buffARGB & 0xFF) + blue;
						
						
						if( buffAlpha < 0) buffAlpha = 0;
						if( buffRed < 0) buffRed = 0;
						if( buffGreen < 0) buffGreen = 0;
						if( buffBlue < 0) buffBlue = 0;
						
						if( buffAlpha > 255) buffAlpha = 255;
						if( buffRed > 255) buffRed = 255;
						if( buffGreen > 255) buffGreen = 255;
						if( buffBlue > 255) buffBlue = 255;
						
						
						buff[ yOffset[buffY] + buffX] = buffBlue | buffGreen << 8 | buffRed << 16 | buffAlpha << 24;
						
						buffX++;
						blockX++;
						if( buffX >= buffWide ) blockX = blockWide;	// if this block hangs outside buffer
					
						}
					buffY++;
					blockY++;
					if( buffY >= buffHigh ) blockY = blockHigh;	// if this block hangs outside buffer
					
					}
				buffX = startX + blockWide;
				}
			buffY = startY + blockHigh;
			}
		}
		
	delete [] yOffset;
	}



void genFractalNoise2d( double *inBuffer, int inWidth, int inMaxFrequency, 
	double inFPower, char inInterpolate, RandomSource *inRandSource ) {
	
	RandomSource *r = inRandSource;
	
	int w = inWidth;
	
	int i, x, y, f;
	
	int numPoints = w * w;
	
	// first, fill surface with a uniform 0.5 value
	for( i=0; i<numPoints; i++ ) {
		inBuffer[i] = 0.5;
		}
		
	// for each frequency
	for( f=2; f<=inMaxFrequency; f = f * 2 ) {
		double weight = 1.0 / pow( f, inFPower );
		
		int blockSize = (int)( (double)w / (double)f + 1.0 );
		
		// one extra block to handle boundary case where x or y is 0
		int numBlocks = (f+1) * (f+1);
		double *blockValues = new double[ numBlocks ];
		
		// assign a random value to each block
		for( i=0; i<numBlocks; i++ ) {
			blockValues[i] = ( 2 * r->getRandomDouble() - 1 ) * weight;
			}
		
		// now walk though 2d array and perform 
		// bilinear interpolation between blocks
		for( y=0; y<w; y++ ) {
			// handle boundary case by skipping first row of blocks
			int yBlock = y / blockSize + 1;
			double yWeight; 
			yWeight = (double)(y % blockSize) / blockSize;

			
			for( x=0; x<w; x++ ) {
				// handle boundary case by skipping first column of blocks
				int xBlock = x / blockSize + 1;
				double xWeight; 
				xWeight = (double)(x % blockSize) / blockSize;

				
				// if interpolating take weighted sum with previous blocks
				double value; 
				if( inInterpolate ) {
					value =
						xWeight *
							( yWeight * 
								blockValues[ yBlock * f + xBlock ] +
							(1-yWeight) * 
								blockValues[ (yBlock-1) * f + xBlock ] ) +
						(1-xWeight) *
							( yWeight * 
								blockValues[ yBlock * f + xBlock - 1 ] +
							(1-yWeight) * 
								blockValues[ (yBlock-1) * f + xBlock - 1 ] );
					}
				else {
					value =
						blockValues[ yBlock * f + xBlock ];
					}
					
				// modulate current value by new value
				inBuffer[ y * w + x ] += value;
				
				// clip values as we go along
				if(	inBuffer[y * w + x] > 1.0 ) {
					inBuffer[y * w + x] = 1.0;
					}
				else if( inBuffer[y * w + x] < 0.0 ) {
					inBuffer[y * w + x] = 0.0;
					}
				}
			}
		
		delete [] blockValues;
		}	
	}
	

void genFractalNoise( double *inBuffer, int inWidth, int inMaxFrequency,
	double inFPower, char inInterpolate, RandomSource *inRandSource ) {
	
	RandomSource *r = inRandSource;
	
	int w = inWidth;
	
	int i, x, f;
	
	// first, fill array with uniform 0.5 values
	for( i=0; i<w; i++ ) {
		inBuffer[i] = 0.5;
		}
		
	// for each frequency
	for( f=2; f<=inMaxFrequency; f = f * 2 ) {
		double weight = 1.0 / pow( f, inFPower );
		
		int blockSize = (int)( (double)w / (double)f + 1.0 );
		
		// one extra block to handle boundary case where x is 0
		int numBlocks = (f+1);
		double *blockValues = new double[ numBlocks ];
		
		// assign a random value to each block
		for( i=0; i<numBlocks; i++ ) {
			blockValues[i] = ( 2 * r->getRandomDouble() - 1 ) * weight;
			}
		
		// now walk though array and perform linear interpolation between blocks

		for( x=0; x<w; x++ ) {
			// handle boundary case by skipping first column of blocks
			int xBlock = x / blockSize + 1;
			double xWeight; 
			xWeight = (double)(x % blockSize) / blockSize;


			// take weighted sum with previous blocks, but watch for
			// boundary cases
			double value; 
			if( inInterpolate ) {
				value =
					xWeight * blockValues[ xBlock ] +
					(1-xWeight) * blockValues[ xBlock - 1 ];
				}
			else {
				value =
					blockValues[ xBlock ];
				}

			// modulate current value by new value
			inBuffer[ x ] += value;

			// clip values as we go along
			if(	inBuffer[x] > 1.0 ) {
				inBuffer[x] = 1.0;
				}
			else if( inBuffer[x] < 0.0 ) {
				inBuffer[x] = 0.0;
				}
			}
		
		delete [] blockValues;
		}
	
	}