Browse Source

don't write blue images for grayscale bmp files

David Rose 14 years ago
parent
commit
81384baf08

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

@@ -400,6 +400,15 @@ write(PNMWriter *writer) const {
   }
 
   writer->copy_header_from(*this);
+  if (is_grayscale() && !writer->supports_grayscale()) {
+    // Copy the gray values to all channels to help out the writer.
+    for (int y = 0; y < get_y_size(); y++) {
+      for (int x = 0; x<get_x_size(); x++) {
+        ((PNMImage *)this)->set_xel_val(x, y, get_gray_val(x, y));
+      }
+    }
+  }
+
   int result = writer->write_data(_array, _alpha);
   delete writer;
 

+ 15 - 0
panda/src/pnmimage/pnmWriter.cxx

@@ -86,6 +86,21 @@ supports_write_row() const {
   return false;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: PNMWriter::supports_grayscale
+//       Access: Public, Virtual
+//  Description: Returns true if this particular PNMWriter understands
+//               grayscale images.  If this is false, then the rgb
+//               values of the xel array will be pre-filled with the
+//               same value across all three channels, to allow the
+//               writer to simply write out RGB data for a grayscale
+//               image.
+////////////////////////////////////////////////////////////////////
+bool PNMWriter::
+supports_grayscale() const {
+  return true;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: PNMWriter::write_header
 //       Access: Public, Virtual

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

@@ -50,6 +50,7 @@ public:
 
   virtual int write_data(xel *array, xelval *alpha);
   virtual bool supports_write_row() const;
+  virtual bool supports_grayscale() const;
   virtual bool write_header();
   virtual bool write_row(xel *array, xelval *alpha);
 

+ 1 - 0
panda/src/pnmimagetypes/pnmFileTypeBMP.h

@@ -70,6 +70,7 @@ public:
     Writer(PNMFileType *type, ostream *file, bool owns_file);
 
     virtual int write_data(xel *array, xelval *alpha);
+    virtual bool supports_grayscale() const;
   };
 
 

+ 15 - 0
panda/src/pnmimagetypes/pnmFileTypeBMPWriter.cxx

@@ -634,4 +634,19 @@ write_data(xel *array, xelval *) {
   return _y_size;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: PNMFileTypeBMP::Writer::supports_grayscale
+//       Access: Public, Virtual
+//  Description: Returns true if this particular PNMWriter understands
+//               grayscale images.  If this is false, then the rgb
+//               values of the xel array will be pre-filled with the
+//               same value across all three channels, to allow the
+//               writer to simply write out RGB data for a grayscale
+//               image.
+////////////////////////////////////////////////////////////////////
+bool PNMFileTypeBMP::Writer::
+supports_grayscale() const {
+  return false;
+}
+
 #endif  // HAVE_BMP