From 81384baf080d869dadbebffa117fb9f6794b363c Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 3 Feb 2012 22:58:16 +0000 Subject: [PATCH] don't write blue images for grayscale bmp files --- panda/src/pnmimage/pnmImage.cxx | 9 +++++++++ panda/src/pnmimage/pnmWriter.cxx | 15 +++++++++++++++ panda/src/pnmimage/pnmWriter.h | 1 + panda/src/pnmimagetypes/pnmFileTypeBMP.h | 1 + panda/src/pnmimagetypes/pnmFileTypeBMPWriter.cxx | 15 +++++++++++++++ 5 files changed, 41 insertions(+) diff --git a/panda/src/pnmimage/pnmImage.cxx b/panda/src/pnmimage/pnmImage.cxx index a6c755fb5f..9638bfa824 100644 --- a/panda/src/pnmimage/pnmImage.cxx +++ b/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; xset_xel_val(x, y, get_gray_val(x, y)); + } + } + } + int result = writer->write_data(_array, _alpha); delete writer; diff --git a/panda/src/pnmimage/pnmWriter.cxx b/panda/src/pnmimage/pnmWriter.cxx index 52d47c0607..a921572b66 100644 --- a/panda/src/pnmimage/pnmWriter.cxx +++ b/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 diff --git a/panda/src/pnmimage/pnmWriter.h b/panda/src/pnmimage/pnmWriter.h index 3649b6d022..732c1bd367 100644 --- a/panda/src/pnmimage/pnmWriter.h +++ b/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); diff --git a/panda/src/pnmimagetypes/pnmFileTypeBMP.h b/panda/src/pnmimagetypes/pnmFileTypeBMP.h index d57df31dd7..e6fc3b84d0 100644 --- a/panda/src/pnmimagetypes/pnmFileTypeBMP.h +++ b/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; }; diff --git a/panda/src/pnmimagetypes/pnmFileTypeBMPWriter.cxx b/panda/src/pnmimagetypes/pnmFileTypeBMPWriter.cxx index 22a9f35f57..4dabd297a7 100644 --- a/panda/src/pnmimagetypes/pnmFileTypeBMPWriter.cxx +++ b/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