瀏覽代碼

add scale-by-color

David Rose 13 年之前
父節點
當前提交
7aa44fe7e3
共有 3 個文件被更改,包括 55 次插入14 次删除
  1. 13 0
      panda/src/pnmimage/pnmImage.I
  2. 40 14
      panda/src/pnmimage/pnmImage.cxx
  3. 2 0
      panda/src/pnmimage/pnmImage.h

+ 13 - 0
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;
+}

+ 40 - 14
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]);
+    }
+  }
+}
+

+ 2 - 0
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;