Browse Source

Add PNMImage::perlin_noise_fill

rdb 15 years ago
parent
commit
609a328946
3 changed files with 51 additions and 4 deletions
  1. 1 1
      panda/src/pnmimage/Sources.pp
  2. 45 2
      panda/src/pnmimage/pnmImage.cxx
  3. 5 1
      panda/src/pnmimage/pnmImage.h

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

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

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

@@ -17,6 +17,8 @@
 #include "pnmWriter.h"
 #include "pnmWriter.h"
 #include "pnmBrush.h"
 #include "pnmBrush.h"
 #include "config_pnmimage.h"
 #include "config_pnmimage.h"
+#include "perlinNoise2.h"
+#include "stackedPerlinNoise2.h"
 #include <algorithm>
 #include <algorithm>
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -989,7 +991,7 @@ threshold(const PNMImage &select_image, int channel, double threshold,
             }
             }
           }
           }
         }
         }
-        
+
       } else {
       } else {
         // Don't copy alpha channel.
         // Don't copy alpha channel.
         for (y = 0; y < get_y_size(); y++) {
         for (y = 0; y < get_y_size(); y++) {
@@ -1017,7 +1019,7 @@ threshold(const PNMImage &select_image, int channel, double threshold,
             }
             }
           }
           }
         }
         }
-        
+
       } else {
       } else {
         // Don't copy alpha channel.
         // Don't copy alpha channel.
         for (y = 0; y < get_y_size(); y++) {
         for (y = 0; y < get_y_size(); y++) {
@@ -1253,6 +1255,47 @@ make_histogram(PNMImage::Histogram &histogram) {
   histogram.swap(pixels, hist_map);
   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
 //     Function: PNMImage::setup_rc
 //       Access: Private
 //       Access: Private

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

@@ -25,6 +25,7 @@
 class PNMReader;
 class PNMReader;
 class PNMWriter;
 class PNMWriter;
 class PNMFileType;
 class PNMFileType;
+class StackedPerlinNoise2;
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //       Class : PNMImage
 //       Class : PNMImage
@@ -208,7 +209,7 @@ PUBLISHED:
                          double pixel_scale = 1.0);
                          double pixel_scale = 1.0);
   void threshold(const PNMImage &select_image, int channel, double threshold,
   void threshold(const PNMImage &select_image, int channel, double threshold,
                  const PNMImage &lt, const PNMImage &ge);
                  const PNMImage &lt, const PNMImage &ge);
-  
+
   void copy_channel(const PNMImage &copy, int xto, int yto, int cto,
   void copy_channel(const PNMImage &copy, int xto, int yto, int cto,
                     int xfrom = 0, int yfrom = 0, int cfrom = 0,
                     int xfrom = 0, int yfrom = 0, int cfrom = 0,
                     int x_size = -1, int y_size = -1);
                     int x_size = -1, int y_size = -1);
@@ -231,6 +232,9 @@ PUBLISHED:
                          int xborder = 0, int yborder = 0);
                          int xborder = 0, int yborder = 0);
 
 
   void make_histogram(Histogram &hist);
   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:
 private:
   INLINE void allocate_array();
   INLINE void allocate_array();