Bläddra i källkod

add fade image

David Rose 24 år sedan
förälder
incheckning
751326a3f9

+ 8 - 0
pandaapp/src/stitchbase/stitchCommand.cxx

@@ -95,6 +95,10 @@ operator << (ostream &out, StitchCommand::Command c) {
     return out << "filename";
     break;
 
+  case StitchCommand::C_fade:
+    return out << "fade";
+    break;
+
   case StitchCommand::C_point2d:
   case StitchCommand::C_point3d:
     return out << "point";
@@ -712,6 +716,10 @@ create_image() {
       image->setup_grid((int)(*ci)->_n[0], (int)(*ci)->_n[1]);
       break;
 
+    case C_fade:
+      image->set_fade_filename((*ci)->get_str());
+      break;
+
     default:
       break;
     }

+ 1 - 0
pandaapp/src/stitchbase/stitchCommand.h

@@ -51,6 +51,7 @@ public:
     C_singularity_tolerance,
     C_resolution,
     C_filename,
+    C_fade,
     C_point2d,
     C_point3d,
     C_show_points,

+ 71 - 2
pandaapp/src/stitchbase/stitchImage.cxx

@@ -129,6 +129,21 @@ get_filename() const {
   return _filename;
 }
 
+bool StitchImage::
+has_fade_filename() const {
+  return !_fade_filename.empty();
+}
+
+string StitchImage::
+get_fade_filename() const {
+  return _fade_filename;
+}
+
+void StitchImage::
+set_fade_filename(const Filename &filename) {
+  _fade_filename = filename;
+}
+
 bool StitchImage::
 read_file() {
   if (_data != NULL) {
@@ -232,8 +247,11 @@ close_output_file() {
     if (_data == NULL) {
       result = false;
     } else {
-      nout << "Writing " << _filename << "\n";
       resize_data();
+      if (has_fade_filename()) {
+        fade_out();
+      }
+      nout << "Writing " << _filename << "\n";
       result = _data->write(_filename);
     }
   }
@@ -242,6 +260,7 @@ close_output_file() {
   return result;
 }
 
+
 void StitchImage::
 clear_transform() {
   _transform = LMatrix3d::ident_mat();
@@ -334,6 +353,7 @@ get_size_pixels() const {
   return _size_pixels;
 }
 
+
 ////////////////////////////////////////////////////////////////////
 //     Function: StitchImage::set_size_pixels
 //       Access: Public
@@ -482,7 +502,56 @@ resize_data() {
   cerr << "Filtering to " << reduced->get_x_size() << " by " 
        << reduced->get_y_size() << "\n";
 
-  reduced->box_filter_from(1.0, *_data);
+  reduced->box_filter_from(0.5, *_data);
   delete _data;
   _data = reduced;
 }
+
+////////////////////////////////////////////////////////////////////
+//     Function: StitchImage::fade_out
+//       Access: Private
+//  Description: Fades out the image (presumably at the edges) by
+//               multiplying each pixel by the corresponding pixel in
+//               the named fade image.
+//
+//               This is generally useful for generating output images
+//               that are intended to overlap meaningfully, for
+//               instance when projected through overlapping
+//               projectors.
+////////////////////////////////////////////////////////////////////
+void StitchImage::
+fade_out() {
+  assert(_data != (PNMImage *)NULL);
+
+  cerr << "Reading fade image " << get_fade_filename() << "\n";
+  PNMImage *fade = new PNMImage(get_fade_filename());
+  if (!fade->is_valid()) {
+    cerr << "Unable to read fade image.\n";
+    return;
+  }
+
+  if (fade->get_x_size() != _data->get_x_size() ||
+      fade->get_y_size() != _data->get_y_size()) {
+    cerr << "Resizing fade image to " << _data->get_x_size()
+         << " by " << _data->get_y_size() << "\n";
+    PNMImage *resized_fade = 
+      new PNMImage(_data->get_x_size(), _data->get_y_size(),
+                   fade->get_num_channels(), fade->get_maxval());
+    resized_fade->quick_filter_from(*fade);
+    delete fade;
+    fade = resized_fade;
+  }
+
+  // Now apply the fade factor to darken each of our pixels.
+  cerr << "Applying fade image.\n";
+  for (int y = 0; y < _data->get_y_size(); y++) {
+    for (int x = 0; x < _data->get_x_size(); x++) {
+      double bright = fade->get_bright(x, y);
+      _data->set_red_val(x, y, (xelval)_data->get_red_val(x, y) * bright);
+      _data->set_green_val(x, y, (xelval)_data->get_green_val(x, y) * bright);
+      _data->set_blue_val(x, y, (xelval)_data->get_blue_val(x, y) * bright);
+    }
+  }
+
+  delete fade;
+}

+ 5 - 0
pandaapp/src/stitchbase/stitchImage.h

@@ -48,6 +48,9 @@ public:
   string get_name() const;
   bool has_filename() const;
   string get_filename() const;
+  bool has_fade_filename() const;
+  string get_fade_filename() const;
+  void set_fade_filename(const Filename &filename);
 
   // This function reads the image file if it is available.
   bool read_file();
@@ -152,9 +155,11 @@ public:
 private:
   void setup_pixel_scales();
   void resize_data();
+  void fade_out();
 
 private:
   Filename _filename;
+  Filename _fade_filename;
   string _name;
 
   int _x_verts, _y_verts;

+ 2 - 0
pandaapp/src/stitchbase/stitchLexer.lxx

@@ -341,6 +341,8 @@ NUMERIC         ([+-]?(([0-9]+[.]?)|([0-9]*[.][0-9]+))([eE][+-]?[0-9]+)?)
     return KW_RESOLUTION;
   } else if (cmp_nocase_uh(str, "filename") == 0) {
     return KW_FILENAME;
+  } else if (cmp_nocase_uh(str, "fade") == 0) {
+    return KW_FADE;
   } else if (cmp_nocase_uh(str, "point") == 0) {
     return KW_POINT;
   } else if (cmp_nocase_uh(str, "show_points") == 0) {

+ 6 - 0
pandaapp/src/stitchbase/stitchParser.yxx

@@ -47,6 +47,7 @@ stitch_init_parser(istream &in, const string &filename,
 %token KW_SINGULARITY_TOLERANCE
 %token KW_RESOLUTION
 %token KW_FILENAME
+%token KW_FADE
 %token KW_POINT
 %token KW_SHOW_POINTS
 %token KW_IMAGE_SIZE
@@ -210,6 +211,11 @@ simple_command:
 {
   $$ = new StitchCommand(parent, StitchCommand::C_filename);
   $$->set_str($2);
+}
+        | KW_FADE STRING
+{
+  $$ = new StitchCommand(parent, StitchCommand::C_fade);
+  $$->set_str($2);
 }
         | KW_POINT name point
 {