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::supports_floating_point
00033 //       Access: Public, Virtual
00034 //  Description: Returns true if this PNMFileType can accept a
00035 //               floating-point image type, false if it can only
00036 //               accept a normal, integer type.  If this returns true,
00037 //               write_pfm() is implemented.
00038 ////////////////////////////////////////////////////////////////////
00039 bool PNMWriter::
00040 supports_floating_point() {
00041   return false;
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: PNMWriter::supports_integer
00046 //       Access: Public, Virtual
00047 //  Description: Returns true if this PNMFileType can accept an
00048 //               integer image type, false if it can only
00049 //               accept a floating-point type.  If this returns true,
00050 //               write_data() or write_row() is implemented.
00051 ////////////////////////////////////////////////////////////////////
00052 bool PNMWriter::
00053 supports_integer() {
00054   return true;
00055 }
00056 
00057 ////////////////////////////////////////////////////////////////////
00058 //     Function: PNMWriter::write_pfm
00059 //       Access: Public, Virtual
00060 //  Description: Writes floating-point data from the indicated
00061 //               PfmFile.  Returns true on success, false on failure.
00062 ////////////////////////////////////////////////////////////////////
00063 bool PNMWriter::
00064 write_pfm(const PfmFile &pfm) {
00065   return false;
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: PNMWriter::write_data
00070 //       Access: Public, Virtual
00071 //  Description: Writes out an entire image all at once, including the
00072 //               header, based on the image data stored in the given
00073 //               _x_size * _y_size array and alpha pointers.  (If the
00074 //               image type has no alpha channel, alpha is ignored.)
00075 //               Returns the number of rows correctly written.
00076 //
00077 //               It is the user's responsibility to fill in the header
00078 //               data via calls to set_x_size(), set_num_channels(),
00079 //               etc., or copy_header_from(), before calling
00080 //               write_data().
00081 //
00082 //               It is important to delete the PNMWriter class after
00083 //               successfully writing the data.  Failing to do this
00084 //               may result in some data not getting flushed!
00085 //
00086 //               Derived classes need not override this if they
00087 //               instead provide supports_streaming() and write_row(),
00088 //               below.
00089 ////////////////////////////////////////////////////////////////////
00090 int PNMWriter::
00091 write_data(xel *array, xelval *alpha) {
00092   if (_x_size <= 0 || _y_size <= 0) {
00093     return 0;
00094   }
00095 
00096   if (!write_header()) {
00097     return 0;
00098   }
00099 
00100   int y;
00101   for (y = 0; y < _y_size; y++) {
00102     if (!write_row(array + y * _x_size, alpha + y * _x_size)) {
00103       Thread::consider_yield();
00104       return y;
00105     }
00106   }
00107 
00108   return _y_size;
00109 }
00110 
00111 ////////////////////////////////////////////////////////////////////
00112 //     Function: PNMWriter::supports_write_row
00113 //       Access: Public, Virtual
00114 //  Description: Returns true if this particular PNMWriter supports a
00115 //               streaming interface to writing the data: that is, it
00116 //               is capable of writing the image one row at a time,
00117 //               via repeated calls to write_row().  Returns false if
00118 //               the only way to write from this file is all at once,
00119 //               via write_data().
00120 ////////////////////////////////////////////////////////////////////
00121 bool PNMWriter::
00122 supports_write_row() const {
00123   return false;
00124 }
00125 
00126 ////////////////////////////////////////////////////////////////////
00127 //     Function: PNMWriter::supports_grayscale
00128 //       Access: Public, Virtual
00129 //  Description: Returns true if this particular PNMWriter understands
00130 //               grayscale images.  If this is false, then the rgb
00131 //               values of the xel array will be pre-filled with the
00132 //               same value across all three channels, to allow the
00133 //               writer to simply write out RGB data for a grayscale
00134 //               image.
00135 ////////////////////////////////////////////////////////////////////
00136 bool PNMWriter::
00137 supports_grayscale() const {
00138   return true;
00139 }
00140 
00141 ////////////////////////////////////////////////////////////////////
00142 //     Function: PNMWriter::write_header
00143 //       Access: Public, Virtual
00144 //  Description: If supports_write_row(), above, returns true, this
00145 //               function may be called to write out the image header
00146 //               in preparation to writing out the image data one row
00147 //               at a time.  Returns true if the header is
00148 //               successfully written, false if there is an error.
00149 //
00150 //               It is the user's responsibility to fill in the header
00151 //               data via calls to set_x_size(), set_num_channels(),
00152 //               etc., or copy_header_from(), before calling
00153 //               write_header().
00154 ////////////////////////////////////////////////////////////////////
00155 bool PNMWriter::
00156 write_header() {
00157   return false;
00158 }
00159 
00160 ////////////////////////////////////////////////////////////////////
00161 //     Function: PNMWriter::write_row
00162 //       Access: Public, Virtual
00163 //  Description: If supports_write_row(), above, returns true, this
00164 //               function may be called repeatedly to write the image,
00165 //               one horizontal row at a time, beginning from the top.
00166 //               Returns true if the row is successfully written,
00167 //               false if there is an error.
00168 //
00169 //               You must first call write_header() before writing the
00170 //               individual rows.  It is also important to delete the
00171 //               PNMWriter class after successfully writing the last
00172 //               row.  Failing to do this may result in some data not
00173 //               getting flushed!
00174 ////////////////////////////////////////////////////////////////////
00175 bool PNMWriter::
00176 write_row(xel *, xelval *) {
00177   return false;
00178 }
00179 
00180 ////////////////////////////////////////////////////////////////////
00181 //     Function: PNMWriter::supports_stream_write
00182 //       Access: Public, Virtual
00183 //  Description: Returns true if this particular PNMWriter can write
00184 //               to a general stream (including pipes, etc.), or
00185 //               false if the writer must occasionally fseek() on its
00186 //               output stream, and thus only disk streams are
00187 //               supported.
00188 ////////////////////////////////////////////////////////////////////
00189 bool PNMWriter::
00190 supports_stream_write() const {
00191   return false;
00192 }