Panda3D
 All Classes Functions Variables Enumerations
pnmWriter.cxx
00001 // Filename: pnmWriter.cxx
00002 // Created by:  drose (14Jun00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "pnmWriter.h"
00016 #include "thread.h"
00017 
00018 ////////////////////////////////////////////////////////////////////
00019 //     Function: PNMWriter::Destructor
00020 //       Access: Public, Virtual
00021 //  Description:
00022 ////////////////////////////////////////////////////////////////////
00023 PNMWriter::
00024 ~PNMWriter() {
00025   if (_owns_file) {
00026     delete _file;
00027   }
00028   _file = (ostream *)NULL;
00029 }
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //     Function: PNMWriter::write_data
00033 //       Access: Public, Virtual
00034 //  Description: Writes out an entire image all at once, including the
00035 //               header, based on the image data stored in the given
00036 //               _x_size * _y_size array and alpha pointers.  (If the
00037 //               image type has no alpha channel, alpha is ignored.)
00038 //               Returns the number of rows correctly written.
00039 //
00040 //               It is the user's responsibility to fill in the header
00041 //               data via calls to set_x_size(), set_num_channels(),
00042 //               etc., or copy_header_from(), before calling
00043 //               write_data().
00044 //
00045 //               It is important to delete the PNMWriter class after
00046 //               successfully writing the data.  Failing to do this
00047 //               may result in some data not getting flushed!
00048 //
00049 //               Derived classes need not override this if they
00050 //               instead provide supports_streaming() and write_row(),
00051 //               below.
00052 ////////////////////////////////////////////////////////////////////
00053 int PNMWriter::
00054 write_data(xel *array, xelval *alpha) {
00055   if (_x_size <= 0 || _y_size <= 0) {
00056     return 0;
00057   }
00058 
00059   if (!write_header()) {
00060     return 0;
00061   }
00062 
00063   int y;
00064   for (y = 0; y < _y_size; y++) {
00065     if (!write_row(array + y * _x_size, alpha + y * _x_size)) {
00066       Thread::consider_yield();
00067       return y;
00068     }
00069   }
00070 
00071   return _y_size;
00072 }
00073 
00074 ////////////////////////////////////////////////////////////////////
00075 //     Function: PNMWriter::supports_write_row
00076 //       Access: Public, Virtual
00077 //  Description: Returns true if this particular PNMWriter supports a
00078 //               streaming interface to writing the data: that is, it
00079 //               is capable of writing the image one row at a time,
00080 //               via repeated calls to write_row().  Returns false if
00081 //               the only way to write from this file is all at once,
00082 //               via write_data().
00083 ////////////////////////////////////////////////////////////////////
00084 bool PNMWriter::
00085 supports_write_row() const {
00086   return false;
00087 }
00088 
00089 ////////////////////////////////////////////////////////////////////
00090 //     Function: PNMWriter::supports_grayscale
00091 //       Access: Public, Virtual
00092 //  Description: Returns true if this particular PNMWriter understands
00093 //               grayscale images.  If this is false, then the rgb
00094 //               values of the xel array will be pre-filled with the
00095 //               same value across all three channels, to allow the
00096 //               writer to simply write out RGB data for a grayscale
00097 //               image.
00098 ////////////////////////////////////////////////////////////////////
00099 bool PNMWriter::
00100 supports_grayscale() const {
00101   return true;
00102 }
00103 
00104 ////////////////////////////////////////////////////////////////////
00105 //     Function: PNMWriter::write_header
00106 //       Access: Public, Virtual
00107 //  Description: If supports_write_row(), above, returns true, this
00108 //               function may be called to write out the image header
00109 //               in preparation to writing out the image data one row
00110 //               at a time.  Returns true if the header is
00111 //               successfully written, false if there is an error.
00112 //
00113 //               It is the user's responsibility to fill in the header
00114 //               data via calls to set_x_size(), set_num_channels(),
00115 //               etc., or copy_header_from(), before calling
00116 //               write_header().
00117 ////////////////////////////////////////////////////////////////////
00118 bool PNMWriter::
00119 write_header() {
00120   return false;
00121 }
00122 
00123 ////////////////////////////////////////////////////////////////////
00124 //     Function: PNMWriter::write_row
00125 //       Access: Public, Virtual
00126 //  Description: If supports_write_row(), above, returns true, this
00127 //               function may be called repeatedly to write the image,
00128 //               one horizontal row at a time, beginning from the top.
00129 //               Returns true if the row is successfully written,
00130 //               false if there is an error.
00131 //
00132 //               You must first call write_header() before writing the
00133 //               individual rows.  It is also important to delete the
00134 //               PNMWriter class after successfully writing the last
00135 //               row.  Failing to do this may result in some data not
00136 //               getting flushed!
00137 ////////////////////////////////////////////////////////////////////
00138 bool PNMWriter::
00139 write_row(xel *, xelval *) {
00140   return false;
00141 }
00142 
00143 ////////////////////////////////////////////////////////////////////
00144 //     Function: PNMWriter::supports_stream_write
00145 //       Access: Public, Virtual
00146 //  Description: Returns true if this particular PNMWriter can write
00147 //               to a general stream (including pipes, etc.), or
00148 //               false if the writer must occasionally fseek() on its
00149 //               output stream, and thus only disk streams are
00150 //               supported.
00151 ////////////////////////////////////////////////////////////////////
00152 bool PNMWriter::
00153 supports_stream_write() const {
00154   return false;
00155 }
 All Classes Functions Variables Enumerations