FastLED 3.9.7
Loading...
Searching...
No Matches
noise.cpp
Go to the documentation of this file.
1
3
4#include <string.h>
5
6#if defined(__clang__)
7// Clang doesn't have variable length arrays. Therefore we need to emulate them using
8// alloca.
9#define VARIABLE_LENGTH_ARRAY_NEEDS_EMULATION 1
10#else
11// Else, assume the compiler is gcc, which has variable length arrays
12#define VARIABLE_LENGTH_ARRAY_NEEDS_EMULATION 0
13#endif
14
15#if !VARIABLE_LENGTH_ARRAY_NEEDS_EMULATION
16#define VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) TYPE NAME[SIZE]
17#else
18#include <alloca.h>
19#define VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
20 TYPE* NAME = reinterpret_cast<TYPE*>(alloca(sizeof(TYPE) * (SIZE)))
21#endif
22
23
24
26#define FASTLED_INTERNAL
27#include "FastLED.h"
28
29
30// Compiler throws a warning about stack usage possibly being unbounded even
31// though bounds are checked, silence that so users don't see it
32#pragma GCC diagnostic push
33#if defined(__GNUC__) && (__GNUC__ >= 6)
34 #pragma GCC diagnostic ignored "-Wstack-usage="
35#else
36 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
37#endif
38
39
40
42#define P(x) FL_PGM_READ_BYTE_NEAR(p + x)
43
45
46
47FL_PROGMEM static uint8_t const p[] = {
48 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225,
49 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148,
50 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32,
51 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175,
52 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122,
53 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54,
54 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169,
55 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64,
56 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212,
57 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213,
58 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
59 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104,
60 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241,
61 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157,
62 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93,
63 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,
64 151};
65
66
67// Start Doxygen define hiding
69
70#if FASTLED_NOISE_ALLOW_AVERAGE_TO_OVERFLOW == 1
71#define AVG15(U,V) (((U)+(V)) >> 1)
72#else
73// See if we should use the inlined avg15 for AVR with MUL instruction
74#if defined(__AVR__) && (LIB8_ATTINY == 0)
75#define AVG15(U,V) (avg15_inline_avr_mul((U),(V)))
76// inlined copy of avg15 for AVR with MUL instruction; cloned from math8.h
77// Forcing this inline in the 3-D 16bit noise produces a 12% speedup overall,
78// at a cost of just +8 bytes of net code size.
79static int16_t inline __attribute__((always_inline)) avg15_inline_avr_mul( int16_t i, int16_t j)
80{
81 asm volatile(
82 /* first divide j by 2, throwing away lowest bit */
83 "asr %B[j] \n\t"
84 "ror %A[j] \n\t"
85 /* now divide i by 2, with lowest bit going into C */
86 "asr %B[i] \n\t"
87 "ror %A[i] \n\t"
88 /* add j + C to i */
89 "adc %A[i], %A[j] \n\t"
90 "adc %B[i], %B[j] \n\t"
91 : [i] "+r" (i)
92 : [j] "r" (j) );
93 return i;
94}
95#else
96#define AVG15(U,V) (avg15((U),(V)))
97#endif
98#endif
99
100// See fastled_config.h for notes on this;
101// "#define FASTLED_NOISE_FIXED 1" is the correct value
102#if FASTLED_NOISE_FIXED == 0
103#define EASE8(x) (FADE(x) )
104#define EASE16(x) (FADE(x) )
105#else
106#define EASE8(x) (ease8InOutQuad(x) )
107#define EASE16(x) (ease16InOutQuad(x))
108#endif
109//
110// #define FADE_12
111#define FADE_16
112
113#ifdef FADE_12
114#define FADE logfade12
115#define LERP(a,b,u) lerp15by12(a,b,u)
116#else
117#define FADE(x) scale16(x,x)
118#define LERP(a,b,u) lerp15by16(a,b,u)
119#endif
120
121// end Doxygen define hiding
123
124static int16_t inline __attribute__((always_inline)) grad16(uint8_t hash, int16_t x, int16_t y, int16_t z) {
125#if 0
126 switch(hash & 0xF) {
127 case 0: return (( x) + ( y))>>1;
128 case 1: return ((-x) + ( y))>>1;
129 case 2: return (( x) + (-y))>>1;
130 case 3: return ((-x) + (-y))>>1;
131 case 4: return (( x) + ( z))>>1;
132 case 5: return ((-x) + ( z))>>1;
133 case 6: return (( x) + (-z))>>1;
134 case 7: return ((-x) + (-z))>>1;
135 case 8: return (( y) + ( z))>>1;
136 case 9: return ((-y) + ( z))>>1;
137 case 10: return (( y) + (-z))>>1;
138 case 11: return ((-y) + (-z))>>1;
139 case 12: return (( y) + ( x))>>1;
140 case 13: return ((-y) + ( z))>>1;
141 case 14: return (( y) + (-x))>>1;
142 case 15: return ((-y) + (-z))>>1;
143 }
144#else
145 hash = hash&15;
146 int16_t u = hash<8?x:y;
147 int16_t v = hash<4?y:hash==12||hash==14?x:z;
148 if(hash&1) { u = -u; }
149 if(hash&2) { v = -v; }
150
151 return AVG15(u,v);
152#endif
153}
154
155static int16_t inline __attribute__((always_inline)) grad16(uint8_t hash, int16_t x, int16_t y) {
156 hash = hash & 7;
157 int16_t u,v;
158 if(hash < 4) { u = x; v = y; } else { u = y; v = x; }
159 if(hash&1) { u = -u; }
160 if(hash&2) { v = -v; }
161
162 return AVG15(u,v);
163}
164
165static int16_t inline __attribute__((always_inline)) grad16(uint8_t hash, int16_t x) {
166 hash = hash & 15;
167 int16_t u,v;
168 if(hash > 8) { u=x;v=x; }
169 else if(hash < 4) { u=x;v=1; }
170 else { u=1;v=x; }
171 if(hash&1) { u = -u; }
172 if(hash&2) { v = -v; }
173
174 return AVG15(u,v);
175}
176
177// selectBasedOnHashBit performs this:
178// result = (hash & (1<<bitnumber)) ? a : b
179// but with an AVR asm version that's smaller and quicker than C
180// (and probably not worth including in lib8tion)
181static int8_t inline __attribute__((always_inline)) selectBasedOnHashBit(uint8_t hash, uint8_t bitnumber, int8_t a, int8_t b) {
182 int8_t result;
183#if !defined(__AVR__)
184 result = (hash & (1<<bitnumber)) ? a : b;
185#else
186 asm volatile(
187 "mov %[result],%[a] \n\t"
188 "sbrs %[hash],%[bitnumber] \n\t"
189 "mov %[result],%[b] \n\t"
190 : [result] "=r" (result)
191 : [hash] "r" (hash),
192 [bitnumber] "M" (bitnumber),
193 [a] "r" (a),
194 [b] "r" (b)
195 );
196#endif
197 return result;
198}
199
200static int8_t inline __attribute__((always_inline)) grad8(uint8_t hash, int8_t x, int8_t y, int8_t z) {
201#if 0
202 switch(hash & 0xF) {
203 case 0: return (( x) + ( y))>>1;
204 case 1: return ((-x) + ( y))>>1;
205 case 2: return (( x) + (-y))>>1;
206 case 3: return ((-x) + (-y))>>1;
207 case 4: return (( x) + ( z))>>1;
208 case 5: return ((-x) + ( z))>>1;
209 case 6: return (( x) + (-z))>>1;
210 case 7: return ((-x) + (-z))>>1;
211 case 8: return (( y) + ( z))>>1;
212 case 9: return ((-y) + ( z))>>1;
213 case 10: return (( y) + (-z))>>1;
214 case 11: return ((-y) + (-z))>>1;
215 case 12: return (( y) + ( x))>>1;
216 case 13: return ((-y) + ( z))>>1;
217 case 14: return (( y) + (-x))>>1;
218 case 15: return ((-y) + (-z))>>1;
219 }
220#else
221
222 hash &= 0xF;
223
224 int8_t u, v;
225 //u = (hash&8)?y:x;
226 u = selectBasedOnHashBit( hash, 3, y, x);
227
228#if 1
229 v = hash<4?y:hash==12||hash==14?x:z;
230#else
231 // Verbose version for analysis; generates idenitical code.
232 if( hash < 4) { // 00 01 02 03
233 v = y;
234 } else {
235 if( hash==12 || hash==14) { // 0C 0E
236 v = x;
237 } else {
238 v = z; // 04 05 06 07 08 09 0A 0B 0D 0F
239 }
240 }
241#endif
242
243 if(hash&1) { u = -u; }
244 if(hash&2) { v = -v; }
245
246 return avg7(u,v);
247#endif
248}
249
250static int8_t inline __attribute__((always_inline)) grad8(uint8_t hash, int8_t x, int8_t y)
251{
252 // since the tests below can be done bit-wise on the bottom
253 // three bits, there's no need to mask off the higher bits
254 // hash = hash & 7;
255
256 int8_t u,v;
257 if( hash & 4) {
258 u = y; v = x;
259 } else {
260 u = x; v = y;
261 }
262
263 if(hash&1) { u = -u; }
264 if(hash&2) { v = -v; }
265
266 return avg7(u,v);
267}
268
269static int8_t inline __attribute__((always_inline)) grad8(uint8_t hash, int8_t x)
270{
271 // since the tests below can be done bit-wise on the bottom
272 // four bits, there's no need to mask off the higher bits
273 // hash = hash & 15;
274
275 int8_t u,v;
276 if(hash & 8) {
277 u=x; v=x;
278 } else {
279 if(hash & 4) {
280 u=1; v=x;
281 } else {
282 u=x; v=1;
283 }
284 }
285
286 if(hash&1) { u = -u; }
287 if(hash&2) { v = -v; }
288
289 return avg7(u,v);
290}
291
292
293#ifdef FADE_12
294uint16_t logfade12(uint16_t val) {
295 return scale16(val,val)>>4;
296}
297
298static int16_t inline __attribute__((always_inline)) lerp15by12( int16_t a, int16_t b, fract16 frac)
299{
300 //if(1) return (lerp(frac,a,b));
301 int16_t result;
302 if( b > a) {
303 uint16_t delta = b - a;
304 uint16_t scaled = scale16(delta,frac<<4);
305 result = a + scaled;
306 } else {
307 uint16_t delta = a - b;
308 uint16_t scaled = scale16(delta,frac<<4);
309 result = a - scaled;
310 }
311 return result;
312}
313#endif
314
315static int8_t inline __attribute__((always_inline)) lerp7by8( int8_t a, int8_t b, fract8 frac)
316{
317 // int8_t delta = b - a;
318 // int16_t prod = (uint16_t)delta * (uint16_t)frac;
319 // int8_t scaled = prod >> 8;
320 // int8_t result = a + scaled;
321 // return result;
322 int8_t result;
323 if( b > a) {
324 uint8_t delta = b - a;
325 uint8_t scaled = scale8( delta, frac);
326 result = a + scaled;
327 } else {
328 uint8_t delta = a - b;
329 uint8_t scaled = scale8( delta, frac);
330 result = a - scaled;
331 }
332 return result;
333}
334
335int16_t inoise16_raw(uint32_t x, uint32_t y, uint32_t z)
336{
337 // Find the unit cube containing the point
338 uint8_t X = (x>>16)&0xFF;
339 uint8_t Y = (y>>16)&0xFF;
340 uint8_t Z = (z>>16)&0xFF;
341
342 // Hash cube corner coordinates
343 uint8_t A = P(X)+Y;
344 uint8_t AA = P(A)+Z;
345 uint8_t AB = P(A+1)+Z;
346 uint8_t B = P(X+1)+Y;
347 uint8_t BA = P(B) + Z;
348 uint8_t BB = P(B+1)+Z;
349
350 // Get the relative position of the point in the cube
351 uint16_t u = x & 0xFFFF;
352 uint16_t v = y & 0xFFFF;
353 uint16_t w = z & 0xFFFF;
354
355 // Get a signed version of the above for the grad function
356 int16_t xx = (u >> 1) & 0x7FFF;
357 int16_t yy = (v >> 1) & 0x7FFF;
358 int16_t zz = (w >> 1) & 0x7FFF;
359 uint16_t N = 0x8000L;
360
361 u = EASE16(u); v = EASE16(v); w = EASE16(w);
362
363 // skip the log fade adjustment for the moment, otherwise here we would
364 // adjust fade values for u,v,w
365 int16_t X1 = LERP(grad16(P(AA), xx, yy, zz), grad16(P(BA), xx - N, yy, zz), u);
366 int16_t X2 = LERP(grad16(P(AB), xx, yy-N, zz), grad16(P(BB), xx - N, yy - N, zz), u);
367 int16_t X3 = LERP(grad16(P(AA+1), xx, yy, zz-N), grad16(P(BA+1), xx - N, yy, zz-N), u);
368 int16_t X4 = LERP(grad16(P(AB+1), xx, yy-N, zz-N), grad16(P(BB+1), xx - N, yy - N, zz - N), u);
369
370 int16_t Y1 = LERP(X1,X2,v);
371 int16_t Y2 = LERP(X3,X4,v);
372
373 int16_t ans = LERP(Y1,Y2,w);
374
375 return ans;
376}
377
378uint16_t inoise16(uint32_t x, uint32_t y, uint32_t z) {
379 int32_t ans = inoise16_raw(x,y,z);
380 ans = ans + 19052L;
381 uint32_t pan = ans;
382 // pan = (ans * 220L) >> 7. That's the same as:
383 // pan = (ans * 440L) >> 8. And this way avoids a 7X four-byte shift-loop on AVR.
384 // Identical math, except for the highest bit, which we don't care about anyway,
385 // since we're returning the 'middle' 16 out of a 32-bit value anyway.
386 pan *= 440L;
387 return (pan>>8);
388
389 // // return scale16by8(pan,220)<<1;
390 // return ((inoise16_raw(x,y,z)+19052)*220)>>7;
391 // return scale16by8(inoise16_raw(x,y,z)+19052,220)<<1;
392}
393
394int16_t inoise16_raw(uint32_t x, uint32_t y)
395{
396 // Find the unit cube containing the point
397 uint8_t X = x>>16;
398 uint8_t Y = y>>16;
399
400 // Hash cube corner coordinates
401 uint8_t A = P(X)+Y;
402 uint8_t AA = P(A);
403 uint8_t AB = P(A+1);
404 uint8_t B = P(X+1)+Y;
405 uint8_t BA = P(B);
406 uint8_t BB = P(B+1);
407
408 // Get the relative position of the point in the cube
409 uint16_t u = x & 0xFFFF;
410 uint16_t v = y & 0xFFFF;
411
412 // Get a signed version of the above for the grad function
413 int16_t xx = (u >> 1) & 0x7FFF;
414 int16_t yy = (v >> 1) & 0x7FFF;
415 uint16_t N = 0x8000L;
416
417 u = EASE16(u); v = EASE16(v);
418
419 int16_t X1 = LERP(grad16(P(AA), xx, yy), grad16(P(BA), xx - N, yy), u);
420 int16_t X2 = LERP(grad16(P(AB), xx, yy-N), grad16(P(BB), xx - N, yy - N), u);
421
422 int16_t ans = LERP(X1,X2,v);
423
424 return ans;
425}
426
427uint16_t inoise16(uint32_t x, uint32_t y) {
428 int32_t ans = inoise16_raw(x,y);
429 ans = ans + 17308L;
430 uint32_t pan = ans;
431 // pan = (ans * 242L) >> 7. That's the same as:
432 // pan = (ans * 484L) >> 8. And this way avoids a 7X four-byte shift-loop on AVR.
433 // Identical math, except for the highest bit, which we don't care about anyway,
434 // since we're returning the 'middle' 16 out of a 32-bit value anyway.
435 pan *= 484L;
436 return (pan>>8);
437
438 // return (uint32_t)(((int32_t)inoise16_raw(x,y)+(uint32_t)17308)*242)>>7;
439 // return scale16by8(inoise16_raw(x,y)+17308,242)<<1;
440}
441
442int16_t inoise16_raw(uint32_t x)
443{
444 // Find the unit cube containing the point
445 uint8_t X = x>>16;
446
447 // Hash cube corner coordinates
448 uint8_t A = P(X);
449 uint8_t AA = P(A);
450 uint8_t B = P(X+1);
451 uint8_t BA = P(B);
452
453 // Get the relative position of the point in the cube
454 uint16_t u = x & 0xFFFF;
455
456 // Get a signed version of the above for the grad function
457 int16_t xx = (u >> 1) & 0x7FFF;
458 uint16_t N = 0x8000L;
459
460 u = EASE16(u);
461
462 int16_t ans = LERP(grad16(P(AA), xx), grad16(P(BA), xx - N), u);
463
464 return ans;
465}
466
467uint16_t inoise16(uint32_t x) {
468 return ((uint32_t)((int32_t)inoise16_raw(x) + 17308L)) << 1;
469}
470
471int8_t inoise8_raw(uint16_t x, uint16_t y, uint16_t z)
472{
473 // Find the unit cube containing the point
474 uint8_t X = x>>8;
475 uint8_t Y = y>>8;
476 uint8_t Z = z>>8;
477
478 // Hash cube corner coordinates
479 uint8_t A = P(X)+Y;
480 uint8_t AA = P(A)+Z;
481 uint8_t AB = P(A+1)+Z;
482 uint8_t B = P(X+1)+Y;
483 uint8_t BA = P(B) + Z;
484 uint8_t BB = P(B+1)+Z;
485
486 // Get the relative position of the point in the cube
487 uint8_t u = x;
488 uint8_t v = y;
489 uint8_t w = z;
490
491 // Get a signed version of the above for the grad function
492 int8_t xx = ((uint8_t)(x)>>1) & 0x7F;
493 int8_t yy = ((uint8_t)(y)>>1) & 0x7F;
494 int8_t zz = ((uint8_t)(z)>>1) & 0x7F;
495 uint8_t N = 0x80;
496
497 u = EASE8(u); v = EASE8(v); w = EASE8(w);
498
499 int8_t X1 = lerp7by8(grad8(P(AA), xx, yy, zz), grad8(P(BA), xx - N, yy, zz), u);
500 int8_t X2 = lerp7by8(grad8(P(AB), xx, yy-N, zz), grad8(P(BB), xx - N, yy - N, zz), u);
501 int8_t X3 = lerp7by8(grad8(P(AA+1), xx, yy, zz-N), grad8(P(BA+1), xx - N, yy, zz-N), u);
502 int8_t X4 = lerp7by8(grad8(P(AB+1), xx, yy-N, zz-N), grad8(P(BB+1), xx - N, yy - N, zz - N), u);
503
504 int8_t Y1 = lerp7by8(X1,X2,v);
505 int8_t Y2 = lerp7by8(X3,X4,v);
506
507 int8_t ans = lerp7by8(Y1,Y2,w);
508
509 return ans;
510}
511
512uint8_t inoise8(uint16_t x, uint16_t y, uint16_t z) {
513 //return scale8(76+(inoise8_raw(x,y,z)),215)<<1;
514 int8_t n = inoise8_raw( x, y, z); // -64..+64
515 n+= 64; // 0..128
516 uint8_t ans = qadd8( n, n); // 0..255
517 return ans;
518}
519
520int8_t inoise8_raw(uint16_t x, uint16_t y)
521{
522 // Find the unit cube containing the point
523 uint8_t X = x>>8;
524 uint8_t Y = y>>8;
525
526 // Hash cube corner coordinates
527 uint8_t A = P(X)+Y;
528 uint8_t AA = P(A);
529 uint8_t AB = P(A+1);
530 uint8_t B = P(X+1)+Y;
531 uint8_t BA = P(B);
532 uint8_t BB = P(B+1);
533
534 // Get the relative position of the point in the cube
535 uint8_t u = x;
536 uint8_t v = y;
537
538 // Get a signed version of the above for the grad function
539 int8_t xx = ((uint8_t)(x)>>1) & 0x7F;
540 int8_t yy = ((uint8_t)(y)>>1) & 0x7F;
541 uint8_t N = 0x80;
542
543 u = EASE8(u); v = EASE8(v);
544
545 int8_t X1 = lerp7by8(grad8(P(AA), xx, yy), grad8(P(BA), xx - N, yy), u);
546 int8_t X2 = lerp7by8(grad8(P(AB), xx, yy-N), grad8(P(BB), xx - N, yy - N), u);
547
548 int8_t ans = lerp7by8(X1,X2,v);
549
550 return ans;
551 // return scale8((70+(ans)),234)<<1;
552}
553
554
555
556uint8_t inoise8(uint16_t x, uint16_t y) {
557 //return scale8(69+inoise8_raw(x,y),237)<<1;
558 int8_t n = inoise8_raw( x, y); // -64..+64
559 n+= 64; // 0..128
560 uint8_t ans = qadd8( n, n); // 0..255
561 return ans;
562}
563
564// output range = -64 .. +64
565int8_t inoise8_raw(uint16_t x)
566{
567 // Find the unit cube containing the point
568 uint8_t X = x>>8;
569
570 // Hash cube corner coordinates
571 uint8_t A = P(X);
572 uint8_t AA = P(A);
573 uint8_t B = P(X+1);
574 uint8_t BA = P(B);
575
576 // Get the relative position of the point in the cube
577 uint8_t u = x;
578
579 // Get a signed version of the above for the grad function
580 int8_t xx = ((uint8_t)(x)>>1) & 0x7F;
581 uint8_t N = 0x80;
582
583 u = EASE8( u);
584
585 int8_t ans = lerp7by8(grad8(P(AA), xx), grad8(P(BA), xx - N), u);
586
587 return ans;
588}
589
590uint8_t inoise8(uint16_t x) {
591 int8_t n = inoise8_raw(x); //-64..+64
592 n += 64; // 0..128
593 uint8_t ans = qadd8(n,n); // 0..255
594 return ans;
595}
596
597// struct q44 {
598// uint8_t i:4;
599// uint8_t f:4;
600// q44(uint8_t _i, uint8_t _f) {i=_i; f=_f; }
601// };
602
603// uint32_t mul44(uint32_t v, q44 mulby44) {
604// return (v *mulby44.i) + ((v * mulby44.f) >> 4);
605// }
606//
607// uint16_t mul44_16(uint16_t v, q44 mulby44) {
608// return (v *mulby44.i) + ((v * mulby44.f) >> 4);
609// }
610
611void fill_raw_noise8(uint8_t *pData, uint8_t num_points, uint8_t octaves, uint16_t x, int scale, uint16_t time) {
612 uint32_t _xx = x;
613 uint32_t scx = scale;
614 for(int o = 0; o < octaves; ++o) {
615 for(int i = 0,xx=_xx; i < num_points; ++i, xx+=scx) {
616 pData[i] = qadd8(pData[i],inoise8(xx,time)>>o);
617 }
618
619 _xx <<= 1;
620 scx <<= 1;
621 }
622}
623
624void fill_raw_noise16into8(uint8_t *pData, uint8_t num_points, uint8_t octaves, uint32_t x, int scale, uint32_t time) {
625 uint32_t _xx = x;
626 uint32_t scx = scale;
627 for(int o = 0; o < octaves; ++o) {
628 for(int i = 0,xx=_xx; i < num_points; ++i, xx+=scx) {
629 uint32_t accum = (inoise16(xx,time))>>o;
630 accum += (pData[i]<<8);
631 if(accum > 65535) { accum = 65535; }
632 pData[i] = accum>>8;
633 }
634
635 _xx <<= 1;
636 scx <<= 1;
637 }
638}
639
654void fill_raw_2dnoise8(uint8_t *pData, int width, int height, uint8_t octaves, q44 freq44, fract8 amplitude, int skip, uint16_t x, int16_t scalex, uint16_t y, int16_t scaley, uint16_t time) {
655 if(octaves > 1) {
656 fill_raw_2dnoise8(pData, width, height, octaves-1, freq44, amplitude, skip+1, x*freq44, freq44 * scalex, y*freq44, freq44 * scaley, time);
657 } else {
658 // amplitude is always 255 on the lowest level
659 amplitude=255;
660 }
661
662 scalex *= skip;
663 scaley *= skip;
664
665 fract8 invamp = 255-amplitude;
666 uint16_t xx = x;
667 for(int i = 0; i < height; ++i, y+=scaley) {
668 uint8_t *pRow = pData + (i*width);
669 xx = x;
670 for(int j = 0; j < width; ++j, xx+=scalex) {
671 uint8_t noise_base = inoise8(xx,y,time);
672 noise_base = (0x80 & noise_base) ? (noise_base - 127) : (127 - noise_base);
673 noise_base = scale8(noise_base<<1,amplitude);
674 if(skip == 1) {
675 pRow[j] = scale8(pRow[j],invamp) + noise_base;
676 } else {
677 for(int ii = i; ii<(i+skip) && ii<height; ++ii) {
678 uint8_t *pRow = pData + (ii*width);
679 for(int jj=j; jj<(j+skip) && jj<width; ++jj) {
680 pRow[jj] = scale8(pRow[jj],invamp) + noise_base;
681 }
682 }
683 }
684 }
685 }
686}
687
688void fill_raw_2dnoise8(uint8_t *pData, int width, int height, uint8_t octaves, uint16_t x, int scalex, uint16_t y, int scaley, uint16_t time) {
689 fill_raw_2dnoise8(pData, width, height, octaves, q44(2,0), 128, 1, x, scalex, y, scaley, time);
690}
691
692void fill_raw_2dnoise16(uint16_t *pData, int width, int height, uint8_t octaves, q88 freq88, fract16 amplitude, int skip, uint32_t x, int32_t scalex, uint32_t y, int32_t scaley, uint32_t time) {
693 if(octaves > 1) {
694 fill_raw_2dnoise16(pData, width, height, octaves-1, freq88, amplitude, skip, x *freq88 , scalex *freq88, y * freq88, scaley * freq88, time);
695 } else {
696 // amplitude is always 255 on the lowest level
697 amplitude=65535;
698 }
699
700 scalex *= skip;
701 scaley *= skip;
702 fract16 invamp = 65535-amplitude;
703 for(int i = 0; i < height; i+=skip, y+=scaley) {
704 uint16_t *pRow = pData + (i*width);
705 for(int j = 0,xx=x; j < width; j+=skip, xx+=scalex) {
706 uint16_t noise_base = inoise16(xx,y,time);
707 noise_base = (0x8000 & noise_base) ? noise_base - (32767) : 32767 - noise_base;
708 noise_base = scale16(noise_base<<1, amplitude);
709 if(skip==1) {
710 pRow[j] = scale16(pRow[j],invamp) + noise_base;
711 } else {
712 for(int ii = i; ii<(i+skip) && ii<height; ++ii) {
713 uint16_t *pRow = pData + (ii*width);
714 for(int jj=j; jj<(j+skip) && jj<width; ++jj) {
715 pRow[jj] = scale16(pRow[jj],invamp) + noise_base;
716 }
717 }
718 }
719 }
720 }
721}
722
725int32_t nmin=11111110;
728int32_t nmax=0;
729
730void fill_raw_2dnoise16into8(uint8_t *pData, int width, int height, uint8_t octaves, q44 freq44, fract8 amplitude, int skip, uint32_t x, int32_t scalex, uint32_t y, int32_t scaley, uint32_t time) {
731 if(octaves > 1) {
732 fill_raw_2dnoise16into8(pData, width, height, octaves-1, freq44, amplitude, skip+1, x*freq44, scalex *freq44, y*freq44, scaley * freq44, time);
733 } else {
734 // amplitude is always 255 on the lowest level
735 amplitude=255;
736 }
737
738 scalex *= skip;
739 scaley *= skip;
740 uint32_t xx;
741 fract8 invamp = 255-amplitude;
742 for(int i = 0; i < height; i+=skip, y+=scaley) {
743 uint8_t *pRow = pData + (i*width);
744 xx = x;
745 for(int j = 0; j < width; j+=skip, xx+=scalex) {
746 uint16_t noise_base = inoise16(xx,y,time);
747 noise_base = (0x8000 & noise_base) ? noise_base - (32767) : 32767 - noise_base;
748 noise_base = scale8(noise_base>>7,amplitude);
749 if(skip==1) {
750 pRow[j] = qadd8(scale8(pRow[j],invamp),noise_base);
751 } else {
752 for(int ii = i; ii<(i+skip) && ii<height; ++ii) {
753 uint8_t *pRow = pData + (ii*width);
754 for(int jj=j; jj<(j+skip) && jj<width; ++jj) {
755 pRow[jj] = scale8(pRow[jj],invamp) + noise_base;
756 }
757 }
758 }
759 }
760 }
761}
762
763void fill_raw_2dnoise16into8(uint8_t *pData, int width, int height, uint8_t octaves, uint32_t x, int scalex, uint32_t y, int scaley, uint32_t time) {
764 fill_raw_2dnoise16into8(pData, width, height, octaves, q44(2,0), 171, 1, x, scalex, y, scaley, time);
765}
766
767void fill_noise8(CRGB *leds, int num_leds,
768 uint8_t octaves, uint16_t x, int scale,
769 uint8_t hue_octaves, uint16_t hue_x, int hue_scale,
770 uint16_t time) {
771
772 if (num_leds <= 0) return;
773
774 for (int j = 0; j < num_leds; j += 255) {
775 const int LedsRemaining = num_leds - j;
776 const int LedsPer = LedsRemaining > 255 ? 255 : LedsRemaining; // limit to 255 max
777
778 if (LedsPer <= 0) continue;
779 VARIABLE_LENGTH_ARRAY(uint8_t, V, LedsPer);
780 VARIABLE_LENGTH_ARRAY(uint8_t, H, LedsPer);
781
782 memset(V, 0, LedsPer);
783 memset(H, 0, LedsPer);
784
785 fill_raw_noise8(V, LedsPer, octaves, x, scale, time);
786 fill_raw_noise8(H, LedsPer, hue_octaves, hue_x, hue_scale, time);
787
788 for (int i = 0; i < LedsPer; ++i) {
789 leds[i + j] = CHSV(H[i], 255, V[i]);
790 }
791 }
792}
793
794void fill_noise16(CRGB *leds, int num_leds,
795 uint8_t octaves, uint16_t x, int scale,
796 uint8_t hue_octaves, uint16_t hue_x, int hue_scale,
797 uint16_t time, uint8_t hue_shift) {
798
799 if (num_leds <= 0) return;
800
801 for (int j = 0; j < num_leds; j += 255) {
802 const int LedsRemaining = num_leds - j;
803 const int LedsPer = LedsRemaining > 255 ? 255 : LedsRemaining; // limit to 255 max
804 if (LedsPer <= 0) continue;
805 VARIABLE_LENGTH_ARRAY(uint8_t, V, LedsPer);
806 VARIABLE_LENGTH_ARRAY(uint8_t, H, LedsPer);
807
808 memset(V, 0, LedsPer);
809 memset(H, 0, LedsPer);
810
811 fill_raw_noise16into8(V, LedsPer, octaves, x, scale, time);
812 fill_raw_noise8(H, LedsPer, hue_octaves, hue_x, hue_scale, time);
813
814 for (int i = 0; i < LedsPer; ++i) {
815 leds[i + j] = CHSV(H[i] + hue_shift, 255, V[i]);
816 }
817 }
818}
819
820void fill_2dnoise8(CRGB *leds, int width, int height, bool serpentine,
821 uint8_t octaves, uint16_t x, int xscale, uint16_t y, int yscale, uint16_t time,
822 uint8_t hue_octaves, uint16_t hue_x, int hue_xscale, uint16_t hue_y, uint16_t hue_yscale,uint16_t hue_time,bool blend) {
823 const size_t array_size = (size_t)height * width;
824 if (array_size <= 0) return;
825 VARIABLE_LENGTH_ARRAY(uint8_t, V, array_size);
826 VARIABLE_LENGTH_ARRAY(uint8_t, H, array_size);
827
828 memset(V,0,height*width);
829 memset(H,0,height*width);
830
831 fill_raw_2dnoise8((uint8_t*)V,width,height,octaves,x,xscale,y,yscale,time);
832 fill_raw_2dnoise8((uint8_t*)H,width,height,hue_octaves,hue_x,hue_xscale,hue_y,hue_yscale,hue_time);
833
834 int w1 = width-1;
835 int h1 = height-1;
836 for(int i = 0; i < height; ++i) {
837 int wb = i*width;
838 for(int j = 0; j < width; ++j) {
839 CRGB led(CHSV(H[(h1-i)*width + (w1-j)], 255, V[i*width + j]));
840
841 int pos = j;
842 if(serpentine && (i & 0x1)) {
843 pos = w1-j;
844 }
845
846 if(blend) {
847 // Safer blending to avoid potential undefined behavior
848 CRGB temp = leds[wb+pos];
849 temp.nscale8(128); // Scale by 50%
850 led.nscale8(128);
851 leds[wb+pos] = temp + led;
852 } else {
853 leds[wb+pos] = led;
854 }
855 }
856 }
857}
858
859
860void fill_2dnoise16(CRGB *leds, int width, int height, bool serpentine,
861 uint8_t octaves, uint32_t x, int xscale, uint32_t y, int yscale, uint32_t time,
862 uint8_t hue_octaves, uint16_t hue_x, int hue_xscale, uint16_t hue_y, uint16_t hue_yscale,uint16_t hue_time, bool blend, uint16_t hue_shift) {
863
864 VARIABLE_LENGTH_ARRAY(uint8_t, V, height*width);
865 VARIABLE_LENGTH_ARRAY(uint8_t, H, height*width);
866
867 memset(V,0,height*width);
868 memset(H,0,height*width);
869
870 fill_raw_2dnoise16into8((uint8_t*)V,width,height,octaves,q44(2,0),171,1,x,xscale,y,yscale,time);
871 // fill_raw_2dnoise16into8((uint8_t*)V,width,height,octaves,x,xscale,y,yscale,time);
872 // fill_raw_2dnoise8((uint8_t*)V,width,height,hue_octaves,x,xscale,y,yscale,time);
873 fill_raw_2dnoise8((uint8_t*)H,width,height,hue_octaves,hue_x,hue_xscale,hue_y,hue_yscale,hue_time);
874
875
876 int w1 = width-1;
877 int h1 = height-1;
878 hue_shift >>= 8;
879
880 for(int i = 0; i < height; ++i) {
881 int wb = i*width;
882 for(int j = 0; j < width; ++j) {
883 CRGB led(CHSV(hue_shift + (H[(h1-i)*width + (w1-j)]), 196, V[i*width + j]));
884
885 int pos = j;
886 if(serpentine && (i & 0x1)) {
887 pos = w1-j;
888 }
889
890 if(blend) {
891 leds[wb+pos] >>= 1; leds[wb+pos] += (led>>=1);
892 } else {
893 leds[wb+pos] = led;
894 }
895 }
896 }
897}
898
900
901#pragma GCC diagnostic pop
central include file for FastLED, defines the CFastLED class/object
Template class for representing fractional ints.
Definition qfx.h:16
#define FL_PROGMEM
PROGMEM keyword for storage.
CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
Computes a new color blended some fraction of the way between two other colors.
qfx< uint8_t, 4, 4 > q44
A 4.4 integer (4 bits integer, 4 bits fraction)
Definition qfx.h:48
uint8_t fract8
ANSI: unsigned short _Fract.
Definition types.h:36
uint16_t fract16
ANSI: unsigned _Fract.
Definition types.h:46
LIB8STATIC_ALWAYS_INLINE uint8_t qadd8(uint8_t i, uint8_t j)
Add one byte to another, saturating at 0xFF.
Definition math8.h:31
LIB8STATIC_ALWAYS_INLINE int8_t avg7(int8_t i, int8_t j)
Calculate an integer average of two signed 7-bit integers (int8_t).
Definition math8.h:306
void fill_raw_noise8(uint8_t *pData, uint8_t num_points, uint8_t octaves, uint16_t x, int scale, uint16_t time)
Fill a 1D 8-bit buffer with noise, using inoise8()
Definition noise.cpp:611
void fill_raw_2dnoise16into8(uint8_t *pData, int width, int height, uint8_t octaves, q44 freq44, fract8 amplitude, int skip, uint32_t x, int32_t scalex, uint32_t y, int32_t scaley, uint32_t time)
Fill a 2D 8-bit buffer with noise, using inoise16()
Definition noise.cpp:730
void fill_raw_2dnoise8(uint8_t *pData, int width, int height, uint8_t octaves, q44 freq44, fract8 amplitude, int skip, uint16_t x, int16_t scalex, uint16_t y, int16_t scaley, uint16_t time)
Fill a 2D 8-bit buffer with noise, using inoise8()
Definition noise.cpp:654
void fill_2dnoise8(CRGB *leds, int width, int height, bool serpentine, uint8_t octaves, uint16_t x, int xscale, uint16_t y, int yscale, uint16_t time, uint8_t hue_octaves, uint16_t hue_x, int hue_xscale, uint16_t hue_y, uint16_t hue_yscale, uint16_t hue_time, bool blend)
Fill an LED matrix with random colors, using 8-bit noise.
Definition noise.cpp:820
void fill_2dnoise16(CRGB *leds, int width, int height, bool serpentine, uint8_t octaves, uint32_t x, int xscale, uint32_t y, int yscale, uint32_t time, uint8_t hue_octaves, uint16_t hue_x, int hue_xscale, uint16_t hue_y, uint16_t hue_yscale, uint16_t hue_time, bool blend, uint16_t hue_shift)
Fill an LED matrix with random colors, using 16-bit noise.
Definition noise.cpp:860
void fill_noise8(CRGB *leds, int num_leds, uint8_t octaves, uint16_t x, int scale, uint8_t hue_octaves, uint16_t hue_x, int hue_scale, uint16_t time)
Fill an LED array with random colors, using 8-bit noise.
Definition noise.cpp:767
void fill_noise16(CRGB *leds, int num_leds, uint8_t octaves, uint16_t x, int scale, uint8_t hue_octaves, uint16_t hue_x, int hue_scale, uint16_t time, uint8_t hue_shift)
Fill an LED array with random colors, using 16-bit noise.
Definition noise.cpp:794
void fill_raw_2dnoise16(uint16_t *pData, int width, int height, uint8_t octaves, q88 freq88, fract16 amplitude, int skip, uint32_t x, int32_t scalex, uint32_t y, int32_t scaley, uint32_t time)
Fill a 2D 16-bit buffer with noise, using inoise16()
Definition noise.cpp:692
void fill_raw_noise16into8(uint8_t *pData, uint8_t num_points, uint8_t octaves, uint32_t x, int scale, uint32_t time)
Fill a 1D 8-bit buffer with noise, using inoise16()
Definition noise.cpp:624
int8_t inoise8_raw(uint16_t x, uint16_t y, uint16_t z)
8-bit, fixed point implementation of Perlin's noise without scaling.
Definition noise.cpp:471
int16_t inoise16_raw(uint32_t x, uint32_t y, uint32_t z)
16-bit, fixed point implementation of Perlin's noise without scaling.
Definition noise.cpp:335
uint16_t inoise16(uint32_t x, uint32_t y, uint32_t z)
16-bit, fixed point implementation of Perlin's noise.
Definition noise.cpp:378
uint8_t inoise8(uint16_t x, uint16_t y, uint16_t z)
8-Bit, fixed point implementation of Perlin's noise.
Definition noise.cpp:512
LIB8STATIC uint16_t scale16(uint16_t i, fract16 scale)
Scale a 16-bit unsigned value by an 16-bit value, which is treated as the numerator of a fraction who...
Definition scale8.h:540
LIB8STATIC_ALWAYS_INLINE uint8_t scale8(uint8_t i, fract8 scale)
Scale one byte by a second one, which is treated as the numerator of a fraction whose denominator is ...
Definition scale8.h:34
#define FASTLED_NAMESPACE_END
End of the FastLED namespace.
Definition namespace.h:16
#define FASTLED_NAMESPACE_BEGIN
Start of the FastLED namespace.
Definition namespace.h:14
#define P(x)
Reads a single byte from the p array.
Definition noise.cpp:42
int32_t nmin
Unused.
Definition noise.cpp:725
int32_t nmax
Unused.
Definition noise.cpp:728
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition chsv.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54
FASTLED_FORCE_INLINE CRGB & nscale8(uint8_t scaledown)
Scale down a RGB to N/256ths of its current brightness, using "plain math" dimming rules.
Definition crgb.hpp:111