Browse Source

add PNMImage::read(istream) and write(ostream)

David Rose 21 years ago
parent
commit
51cba437b9
2 changed files with 54 additions and 0 deletions
  1. 50 0
      panda/src/pnmimage/pnmImage.cxx
  2. 4 0
      panda/src/pnmimage/pnmImage.h

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

@@ -168,6 +168,30 @@ read(const Filename &filename, PNMFileType *type) {
   return read(reader);
   return read(reader);
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: PNMImage::read
+//       Access: Public
+//  Description: Reads the image data from the indicated stream.  
+//
+//               The filename is advisory only, and may be used
+//               suggest a type if it has a known extension.
+//
+//               If type is non-NULL, it is a suggestion for the type
+//               of file it is.  Returns true if successful, false on
+//               error.
+////////////////////////////////////////////////////////////////////
+bool PNMImage::
+read(istream &data, const string &filename, PNMFileType *type) {
+  clear();
+
+  PNMReader *reader = PNMImageHeader::make_reader
+    (&data, false, filename, string(), type);
+  if (reader == (PNMReader *)NULL) {
+    return false;
+  }
+  return read(reader);
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PNMImage::read
 //     Function: PNMImage::read
 //       Access: Public
 //       Access: Public
@@ -231,6 +255,32 @@ write(const Filename &filename, PNMFileType *type) const {
   return write(writer);
   return write(writer);
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: PNMImage::write
+//       Access: Public
+//  Description: Writes the image to the indicated ostream.
+//
+//               The filename is advisory only, and may be used
+//               suggest a type if it has a known extension.
+//
+//               If type is non-NULL, it is a suggestion for the type
+//               of image file to write.
+////////////////////////////////////////////////////////////////////
+bool PNMImage::
+write(ostream &data, const string &filename, PNMFileType *type) const {
+  if (!is_valid()) {
+    return false;
+  }
+
+  PNMWriter *writer = PNMImageHeader::make_writer
+    (&data, false, filename, type);
+  if (writer == (PNMWriter *)NULL) {
+    return false;
+  }
+
+  return write(writer);
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PNMImage::write
 //     Function: PNMImage::write
 //       Access: Public
 //       Access: Public

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

@@ -88,9 +88,13 @@ PUBLISHED:
   void alpha_fill_val(xelval alpha = 0);
   void alpha_fill_val(xelval alpha = 0);
 
 
   bool read(const Filename &filename, PNMFileType *type = NULL);
   bool read(const Filename &filename, PNMFileType *type = NULL);
+  bool read(istream &data, const string &filename = string(), 
+            PNMFileType *type = NULL);
   bool read(PNMReader *reader);
   bool read(PNMReader *reader);
 
 
   bool write(const Filename &filename, PNMFileType *type = NULL) const;
   bool write(const Filename &filename, PNMFileType *type = NULL) const;
+  bool write(ostream &data, const string &filename = string(),
+             PNMFileType *type = NULL) const;
   bool write(PNMWriter *writer) const;
   bool write(PNMWriter *writer) const;
 
 
   INLINE bool is_valid() const;
   INLINE bool is_valid() const;