Browse Source

store_mask() with a range limit

David Rose 10 years ago
parent
commit
059c929b79
2 changed files with 43 additions and 0 deletions
  1. 42 0
      panda/src/pnmimage/pfmFile.cxx
  2. 1 0
      panda/src/pnmimage/pfmFile.h

+ 42 - 0
panda/src/pnmimage/pfmFile.cxx

@@ -467,6 +467,48 @@ store_mask(PNMImage &pnmimage) const {
   return true;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: PfmFile::store_mask
+//       Access: Published
+//  Description: Stores 1 or 0 values into the indicated PNMImage,
+//               according to has_point() for each pixel.  Each valid
+//               point gets a 1 value; each nonexistent point gets a 0
+//               value.
+//
+//               This flavor of store_mask also checks whether the
+//               valid points are within the specified min/max range.
+//               Any valid points without the condition min_point[c]
+//               <= value[c] <= max_point[c], for any c, are stored
+//               with a 0 in the mask.
+////////////////////////////////////////////////////////////////////
+bool PfmFile::
+store_mask(PNMImage &pnmimage, const LVecBase4f &min_point, const LVecBase4f &max_point) const {
+  if (!is_valid()) {
+    pnmimage.clear();
+    return false;
+  }
+
+  pnmimage.clear(get_x_size(), get_y_size(), 1, 255);
+  for (int yi = 0; yi < get_y_size(); ++yi) {
+    for (int xi = 0; xi < get_x_size(); ++xi) {
+      if (!has_point(xi, yi)) {
+        pnmimage.set_gray(xi, yi, 0);
+      } else {
+        const float *value = &_table[(yi * _x_size + xi) * _num_channels];
+        bool in_range = true;
+        for (int ci = 0; ci < _num_channels; ++ci) {
+          if (value[ci] < min_point[ci] || value[ci] > max_point[ci]) {
+            in_range = false;
+            break;
+          }
+        }
+        pnmimage.set_gray(xi, yi, (float)in_range);
+      }
+    }
+  }
+  return true;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: PfmFile::fill
 //       Access: Published

+ 1 - 0
panda/src/pnmimage/pfmFile.h

@@ -50,6 +50,7 @@ PUBLISHED:
   BLOCKING bool load(const PNMImage &pnmimage);
   BLOCKING bool store(PNMImage &pnmimage) const;
   BLOCKING bool store_mask(PNMImage &pnmimage) const;
+  BLOCKING bool store_mask(PNMImage &pnmimage, const LVecBase4f &min_point, const LVecBase4f &max_point) const;
 
   INLINE bool is_valid() const;
   MAKE_PROPERTY(valid, is_valid);