Browse Source

Merge pull request #33 from eswartz/pr-pnmimage-premultiply

Add PNMImage premultiply/unpremultiply methods.
rdb 10 years ago
parent
commit
a73b5b3fd1
2 changed files with 60 additions and 0 deletions
  1. 57 0
      panda/src/pnmimage/pnmImage.cxx
  2. 3 0
      panda/src/pnmimage/pnmImage.h

+ 57 - 0
panda/src/pnmimage/pnmImage.cxx

@@ -622,6 +622,63 @@ make_grayscale(float rc, float gc, float bc) {
   setup_rc();
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: PNMImage::premultiply_alpha
+//       Access: Published
+//  Description: Converts an image in-place to its "premultiplied"
+//               form, where, for every pixel in the image, the
+//               red, green, and blue components are multiplied by
+//               that pixel's alpha value.
+//
+//               This does not modify any alpha values.
+////////////////////////////////////////////////////////////////////
+void PNMImage::
+premultiply_alpha() {
+  if (!has_alpha()) {
+    return;
+  }
+
+  for (int y = 0; y < get_y_size(); y++) {
+    for (int x = 0; x < get_x_size(); x++) {
+      float alpha = get_alpha(x, y);
+      float r = get_red(x, y) * alpha;
+      float g = get_green(x, y) * alpha;
+      float b = get_blue(x, y) * alpha;
+      set_xel(x, y, r, g, b);
+    }
+  }
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PNMImage::unpremultiply_alpha
+//       Access: Published
+//  Description: Converts an image in-place to its "straight alpha"
+//               form (presumably from a "premultiplied" form),
+//               where, for every pixel in the image, the red,
+//               green, and blue components are divided by that
+//               pixel's alpha value.
+//
+//               This does not modify any alpha values.
+////////////////////////////////////////////////////////////////////
+void PNMImage::
+unpremultiply_alpha() {
+  if (!has_alpha()) {
+    return;
+  }
+
+  for (int y = 0; y < get_y_size(); y++) {
+    for (int x = 0; x < get_x_size(); x++) {
+      float alpha = get_alpha(x, y);
+      if (alpha > 0) {
+        float r = get_red(x, y) / alpha;
+        float g = get_green(x, y) / alpha;
+        float b = get_blue(x, y) / alpha;
+        set_xel(x, y, r, g, b);
+      }
+    }
+  }
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: PNMImage::reverse_rows
 //       Access: Published

+ 3 - 0
panda/src/pnmimage/pnmImage.h

@@ -133,6 +133,9 @@ PUBLISHED:
   void make_grayscale(float rc, float gc, float bc);
   INLINE void make_rgb();
 
+  void premultiply_alpha();
+  void unpremultiply_alpha();
+
   BLOCKING void reverse_rows();
   BLOCKING void flip(bool flip_x, bool flip_y, bool transpose);