From 7aa44fe7e3678becf731a51d167ee1213ab5c91a Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 29 Jun 2012 13:55:23 +0000 Subject: [PATCH] add scale-by-color --- panda/src/pnmimage/pnmImage.I | 13 ++++++++ panda/src/pnmimage/pnmImage.cxx | 54 ++++++++++++++++++++++++--------- panda/src/pnmimage/pnmImage.h | 2 ++ 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/panda/src/pnmimage/pnmImage.I b/panda/src/pnmimage/pnmImage.I index 31e26b5b31..30a0222917 100644 --- a/panda/src/pnmimage/pnmImage.I +++ b/panda/src/pnmimage/pnmImage.I @@ -1105,3 +1105,16 @@ operator * (double multiplier) const { target *= multiplier; return target; } + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::operator * +// Access: Published +// Description: Returns a new PNMImage in which the provided color +// is multiplied to each pixel in the provided image. +//////////////////////////////////////////////////////////////////// +INLINE PNMImage PNMImage:: +operator * (const LColord &other) const { + PNMImage target (*this); + target *= other; + return target; +} diff --git a/panda/src/pnmimage/pnmImage.cxx b/panda/src/pnmimage/pnmImage.cxx index 42b5296b9e..453ed13b02 100644 --- a/panda/src/pnmimage/pnmImage.cxx +++ b/panda/src/pnmimage/pnmImage.cxx @@ -1595,10 +1595,9 @@ operator ~ () const { //////////////////////////////////////////////////////////////////// // Function: PNMImage::operator += // Access: Published -// Description: Returns a new PNMImage in which each pixel value -// is the sum of the corresponding pixel values -// in the two given images. -// Only valid when both images have the same size. +// Description: Sets each pixel value to the sum of the corresponding +// pixel values in the two given images. Only valid +// when both images have the same size. //////////////////////////////////////////////////////////////////// void PNMImage:: operator += (const PNMImage &other) { @@ -1629,8 +1628,7 @@ operator += (const PNMImage &other) { //////////////////////////////////////////////////////////////////// // Function: PNMImage::operator += // Access: Published -// Description: Returns a new PNMImage in which the provided color -// is added to each pixel in the provided image. +// Description: Adds the provided color to each pixel in this image. //////////////////////////////////////////////////////////////////// void PNMImage:: operator += (const LColord &other) { @@ -1664,10 +1662,9 @@ operator += (const LColord &other) { //////////////////////////////////////////////////////////////////// // Function: PNMImage::operator -= // Access: Published -// Description: Returns a new PNMImage in which each pixel value -// from the right image is subtracted from each -// pixel value from the left image. -// Only valid when both images have the same size. +// Description: Subtracts each pixel from the right image from each +// pixel value in this image. Only valid when both +// images have the same size. //////////////////////////////////////////////////////////////////// void PNMImage:: operator -= (const PNMImage &other) { @@ -1698,8 +1695,8 @@ operator -= (const PNMImage &other) { //////////////////////////////////////////////////////////////////// // Function: PNMImage::operator -= // Access: Published -// Description: Returns a new PNMImage in which the provided color -// is subtracted from each pixel in the provided image. +// Description: Subtracts the provided color from each pixel in this +// image. //////////////////////////////////////////////////////////////////// void PNMImage:: operator -= (const LColord &other) { @@ -1709,8 +1706,7 @@ operator -= (const LColord &other) { //////////////////////////////////////////////////////////////////// // Function: PNMImage::operator *= // Access: Published -// Description: Returns a new PNMImage in which each pixel value -// from the left image is multiplied by each +// Description: Multiples each pixel in this image by each // pixel value from the right image. Note that the // floating-point values in the 0..1 range are // multiplied, not in the 0..maxval range. @@ -1771,3 +1767,33 @@ operator *= (double multiplier) { } } +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::operator *= +// Access: Published +// Description: Multiples the provided color to each pixel in this +// image. +//////////////////////////////////////////////////////////////////// +void PNMImage:: +operator *= (const LColord &other) { + size_t array_size = _x_size * _y_size; + + if (_array != NULL && _alpha != NULL) { + for (size_t i = 0; i < array_size; ++i) { + _array[i].r = clamp_val(_array[i].r * other[0]); + _array[i].g = clamp_val(_array[i].g * other[1]); + _array[i].b = clamp_val(_array[i].b * other[2]); + _alpha[i] = clamp_val(_alpha[i] * other[3]); + } + } else if (_array != NULL) { + for (size_t i = 0; i < array_size; ++i) { + _array[i].r = clamp_val(_array[i].r * other[0]); + _array[i].g = clamp_val(_array[i].g * other[1]); + _array[i].b = clamp_val(_array[i].b * other[2]); + } + } else if (_alpha != NULL) { + for (size_t i = 0; i < array_size; ++i) { + _alpha[i] = clamp_val(_alpha[i] * other[3]); + } + } +} + diff --git a/panda/src/pnmimage/pnmImage.h b/panda/src/pnmimage/pnmImage.h index 34d7bdd0fb..ea57f86263 100644 --- a/panda/src/pnmimage/pnmImage.h +++ b/panda/src/pnmimage/pnmImage.h @@ -278,12 +278,14 @@ PUBLISHED: INLINE PNMImage operator - (const LColord &other) const; INLINE PNMImage operator * (const PNMImage &other) const; INLINE PNMImage operator * (double multiplier) const; + INLINE PNMImage operator * (const LColord &other) const; void operator += (const PNMImage &other); void operator += (const LColord &other); void operator -= (const PNMImage &other); void operator -= (const LColord &other); void operator *= (const PNMImage &other); void operator *= (double multiplier); + void operator *= (const LColord &other); private: xel *_array;