perlin.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Perlin noise generator
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Perlin Noise generator
  23. */
  24. #ifndef AVFILTER_PERLIN_H
  25. #define AVFILTER_PERLIN_H
  26. #include <stdint.h>
  27. enum FFPerlinRandomMode {
  28. FF_PERLIN_RANDOM_MODE_RANDOM,
  29. FF_PERLIN_RANDOM_MODE_KEN,
  30. FF_PERLIN_RANDOM_MODE_SEED,
  31. FF_PERLIN_RANDOM_MODE_NB
  32. };
  33. /**
  34. * Perlin generator context. This needs to be initialized with the
  35. * parameters used to generate the Perlin noise.
  36. */
  37. typedef struct FFPerlin {
  38. /**
  39. * spatial repeat period, if negative it is ignored
  40. */
  41. double period;
  42. /**
  43. * total number of components making up the noise, each one with
  44. * doubled frequency
  45. */
  46. int octaves;
  47. /**
  48. * ratio used to compute the amplitude of the next octave
  49. * component with respect to the previous component
  50. */
  51. double persistence;
  52. /**
  53. * permutations array used to compute the Perlin noise hash
  54. */
  55. uint8_t permutations[512];
  56. /**
  57. * define how to compute the permutations array
  58. */
  59. enum FFPerlinRandomMode random_mode;
  60. /**
  61. * when random_mode is set FF_PERLIN_RANDOM_MODE_RANDOM, set random
  62. * seed used to compute the permutations array
  63. */
  64. unsigned int random_seed;
  65. } FFPerlin;
  66. /**
  67. * Initialize the Perlin noise generator with parameters.
  68. *
  69. * @param perlin Perlin noise generator context
  70. * @param period spatial repeat period, if negative it is ignored
  71. * @param octaves total number of components making up the noise, each one with doubled frequency
  72. * @param persistence define ratio used to compute the amplitude of the next octave
  73. * component with respect to the previous component
  74. * @param random_mode define how to compute the permutations array
  75. * @param random_seed when random_mode is set to FF_PERLIN_RANDOM_MODE_RANDOM, set random
  76. * seed used to compute the permutations array
  77. * @return a negative AVERROR code in case of error, a non negative value otherwise
  78. */
  79. int ff_perlin_init(FFPerlin *perlin, double period, int octaves, double persistence,
  80. enum FFPerlinRandomMode random_mode, unsigned int random_seed);
  81. /**
  82. * Compute Perlin noise given the x, y, z coordinates.
  83. *
  84. * @param perlin Perlin noise generator context
  85. * @return normalized value for the perlin noise, in the range [0, 1]
  86. */
  87. double ff_perlin_get(FFPerlin *perlin, double x, double y, double z);
  88. #endif /* AVFILTER_PERLIN_H */