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