Panda3D
|
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::write_header 00091 // Access: Public, Virtual 00092 // Description: If supports_write_row(), above, returns true, this 00093 // function may be called to write out the image header 00094 // in preparation to writing out the image data one row 00095 // at a time. Returns true if the header is 00096 // successfully written, false if there is an error. 00097 // 00098 // It is the user's responsibility to fill in the header 00099 // data via calls to set_x_size(), set_num_channels(), 00100 // etc., or copy_header_from(), before calling 00101 // write_header(). 00102 //////////////////////////////////////////////////////////////////// 00103 bool PNMWriter:: 00104 write_header() { 00105 return false; 00106 } 00107 00108 //////////////////////////////////////////////////////////////////// 00109 // Function: PNMWriter::write_row 00110 // Access: Public, Virtual 00111 // Description: If supports_write_row(), above, returns true, this 00112 // function may be called repeatedly to write the image, 00113 // one horizontal row at a time, beginning from the top. 00114 // Returns true if the row is successfully written, 00115 // false if there is an error. 00116 // 00117 // You must first call write_header() before writing the 00118 // individual rows. It is also important to delete the 00119 // PNMWriter class after successfully writing the last 00120 // row. Failing to do this may result in some data not 00121 // getting flushed! 00122 //////////////////////////////////////////////////////////////////// 00123 bool PNMWriter:: 00124 write_row(xel *, xelval *) { 00125 return false; 00126 } 00127 00128 //////////////////////////////////////////////////////////////////// 00129 // Function: PNMWriter::supports_stream_write 00130 // Access: Public, Virtual 00131 // Description: Returns true if this particular PNMWriter can write 00132 // to a general stream (including pipes, etc.), or 00133 // false if the writer must occasionally fseek() on its 00134 // output stream, and thus only disk streams are 00135 // supported. 00136 //////////////////////////////////////////////////////////////////// 00137 bool PNMWriter:: 00138 supports_stream_write() const { 00139 return false; 00140 }