From a617a5cb97478278c619ef0d894dacfecc143c6e Mon Sep 17 00:00:00 2001 From: rdb Date: Mon, 17 May 2010 13:11:43 +0000 Subject: [PATCH] Add get_average_xel, get_average_xel_a, get_average_gray --- panda/src/pnmimage/pnmImage.cxx | 72 +++++++++++++++++++++++++++++++++ panda/src/pnmimage/pnmImage.h | 4 ++ 2 files changed, 76 insertions(+) diff --git a/panda/src/pnmimage/pnmImage.cxx b/panda/src/pnmimage/pnmImage.cxx index ec329d790f..d90e03f4d7 100644 --- a/panda/src/pnmimage/pnmImage.cxx +++ b/panda/src/pnmimage/pnmImage.cxx @@ -1317,6 +1317,78 @@ setup_rc() { } } +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::get_average_xel +// Access: Published +// Description: Returns the average color of all of the pixels +// in the image. +//////////////////////////////////////////////////////////////////// +RGBColord PNMImage:: +get_average_xel() const { + RGBColord color (RGBColord::zero()); + if (_x_size == 0 || _y_size == 0) { + return color; + } + + int x, y; + for (x = 0; x < _x_size; ++x) { + for (y = 0; y < _y_size; ++y) { + color += get_xel(x, y); + } + } + + color *= 1.0 / (_x_size * _y_size); + return color; +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::get_average_xel_a +// Access: Published +// Description: Returns the average color of all of the pixels +// in the image, including the alpha channel. +//////////////////////////////////////////////////////////////////// +Colord PNMImage:: +get_average_xel_a() const { + Colord color (Colord::zero()); + if (_x_size == 0 || _y_size == 0) { + return color; + } + + int x, y; + for (x = 0; x < _x_size; ++x) { + for (y = 0; y < _y_size; ++y) { + color += get_xel_a(x, y); + } + } + + color *= 1.0 / (_x_size * _y_size); + return color; +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::get_average_gray +// Access: Published +// Description: Returns the average grayscale component of all of +// the pixels in the image. +//////////////////////////////////////////////////////////////////// +double PNMImage:: +get_average_gray() const { + double color = 0.0; + if (_x_size == 0 || _y_size == 0) { + return color; + } + + int x, y; + for (x = 0; x < _x_size; ++x) { + for (y = 0; y < _y_size; ++y) { + color += get_gray(x, y); + } + } + + color *= 1.0 / (_x_size * _y_size); + return color; +} + //////////////////////////////////////////////////////////////////// // Function: PNMImage::operator ~ // Access: Published diff --git a/panda/src/pnmimage/pnmImage.h b/panda/src/pnmimage/pnmImage.h index 1b7265d23b..f0c8fd9552 100644 --- a/panda/src/pnmimage/pnmImage.h +++ b/panda/src/pnmimage/pnmImage.h @@ -236,6 +236,10 @@ PUBLISHED: unsigned long seed = 0); void perlin_noise_fill(StackedPerlinNoise2 &perlin); + RGBColord get_average_xel() const; + Colord get_average_xel_a() const; + double get_average_gray() const; + private: INLINE void allocate_array(); INLINE void allocate_alpha();