Răsfoiți Sursa

Add PNMImage::perlin_noise_fill

rdb 15 ani în urmă
părinte
comite
609a328946

+ 1 - 1
panda/src/pnmimage/Sources.pp

@@ -5,7 +5,7 @@
 #begin lib_target
   #define TARGET pnmimage
   #define LOCAL_LIBS \
-    linmath putil express
+    linmath putil express mathutil
 
   #define COMBINED_SOURCES $[TARGET]_composite1.cxx $[TARGET]_composite2.cxx 
 

+ 45 - 2
panda/src/pnmimage/pnmImage.cxx

@@ -17,6 +17,8 @@
 #include "pnmWriter.h"
 #include "pnmBrush.h"
 #include "config_pnmimage.h"
+#include "perlinNoise2.h"
+#include "stackedPerlinNoise2.h"
 #include <algorithm>
 
 ////////////////////////////////////////////////////////////////////
@@ -989,7 +991,7 @@ threshold(const PNMImage &select_image, int channel, double threshold,
             }
           }
         }
-        
+
       } else {
         // Don't copy alpha channel.
         for (y = 0; y < get_y_size(); y++) {
@@ -1017,7 +1019,7 @@ threshold(const PNMImage &select_image, int channel, double threshold,
             }
           }
         }
-        
+
       } else {
         // Don't copy alpha channel.
         for (y = 0; y < get_y_size(); y++) {
@@ -1253,6 +1255,47 @@ make_histogram(PNMImage::Histogram &histogram) {
   histogram.swap(pixels, hist_map);
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: PNMImage::perlin_noise_fill
+//       Access: Published
+//  Description: Fills the image with a grayscale perlin noise
+//               pattern based on the indicated parameters.
+//               Uses set_xel to set the grayscale values.
+//               The sx and sy parameters are in multiples
+//               of the size of this image.
+//               See also the PerlinNoise2 class in mathutil.
+////////////////////////////////////////////////////////////////////
+void PNMImage::
+perlin_noise_fill(double sx, double sy, int table_size, unsigned long seed) {
+  double x, y;
+  double noise;
+  PerlinNoise2 perlin (sx * _x_size, sy * _y_size, table_size, seed);
+  for (x = 0; x < _x_size; ++x) {
+    for (y = 0; y < _y_size; ++y) {
+      noise = perlin.noise(x, y);
+      set_xel(x, y, 0.5 * (noise + 1.0));
+    }
+  }
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PNMImage::perlin_noise_fill
+//       Access: Published
+//  Description: Variant of perlin_noise_fill that uses an
+//               existing StackedPerlinNoise2 object.
+////////////////////////////////////////////////////////////////////
+void PNMImage::
+perlin_noise_fill(StackedPerlinNoise2 &perlin) {
+  double x, y;
+  double noise;
+  for (x = 0; x < _x_size; ++x) {
+    for (y = 0; y < _y_size; ++y) {
+      noise = perlin.noise(x / (double) _x_size, y / (double) _y_size);
+      set_xel(x, y, 0.5 * (noise + 1.0));
+    }
+  }
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: PNMImage::setup_rc
 //       Access: Private

+ 5 - 1
panda/src/pnmimage/pnmImage.h

@@ -25,6 +25,7 @@
 class PNMReader;
 class PNMWriter;
 class PNMFileType;
+class StackedPerlinNoise2;
 
 ////////////////////////////////////////////////////////////////////
 //       Class : PNMImage
@@ -208,7 +209,7 @@ PUBLISHED:
                          double pixel_scale = 1.0);
   void threshold(const PNMImage &select_image, int channel, double threshold,
                  const PNMImage &lt, const PNMImage &ge);
-  
+
   void copy_channel(const PNMImage &copy, int xto, int yto, int cto,
                     int xfrom = 0, int yfrom = 0, int cfrom = 0,
                     int x_size = -1, int y_size = -1);
@@ -231,6 +232,9 @@ PUBLISHED:
                          int xborder = 0, int yborder = 0);
 
   void make_histogram(Histogram &hist);
+  void perlin_noise_fill(double sx, double sy, int table_size = 256,
+                         unsigned long seed = 0);
+  void perlin_noise_fill(StackedPerlinNoise2 &perlin);
 
 private:
   INLINE void allocate_array();