From 609a328946540b10fd6a98070aa58bc80d6c1aec Mon Sep 17 00:00:00 2001 From: rdb Date: Sat, 8 May 2010 14:49:41 +0000 Subject: [PATCH] Add PNMImage::perlin_noise_fill --- panda/src/pnmimage/Sources.pp | 2 +- panda/src/pnmimage/pnmImage.cxx | 47 +++++++++++++++++++++++++++++++-- panda/src/pnmimage/pnmImage.h | 6 ++++- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/panda/src/pnmimage/Sources.pp b/panda/src/pnmimage/Sources.pp index 8e152c7402..92c50f9871 100644 --- a/panda/src/pnmimage/Sources.pp +++ b/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 diff --git a/panda/src/pnmimage/pnmImage.cxx b/panda/src/pnmimage/pnmImage.cxx index 96a49f1251..630b12cf0b 100644 --- a/panda/src/pnmimage/pnmImage.cxx +++ b/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 //////////////////////////////////////////////////////////////////// @@ -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 diff --git a/panda/src/pnmimage/pnmImage.h b/panda/src/pnmimage/pnmImage.h index 781a955ac1..d8d1d3e0ea 100644 --- a/panda/src/pnmimage/pnmImage.h +++ b/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 <, const PNMImage &ge); - + void copy_channel(const PNMImage ©, 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();