Panda3D
 All Classes Functions Variables Enumerations
pnmWriter.cxx
1 // Filename: pnmWriter.cxx
2 // Created by: drose (14Jun00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "pnmWriter.h"
16 #include "thread.h"
17 
18 ////////////////////////////////////////////////////////////////////
19 // Function: PNMWriter::Destructor
20 // Access: Public, Virtual
21 // Description:
22 ////////////////////////////////////////////////////////////////////
23 PNMWriter::
24 ~PNMWriter() {
25  if (_owns_file) {
26  delete _file;
27  }
28  _file = (ostream *)NULL;
29 }
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function: PNMWriter::supports_floating_point
33 // Access: Public, Virtual
34 // Description: Returns true if this PNMFileType can accept a
35 // floating-point image type, false if it can only
36 // accept a normal, integer type. If this returns true,
37 // write_pfm() is implemented.
38 ////////////////////////////////////////////////////////////////////
39 bool PNMWriter::
41  return false;
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: PNMWriter::supports_integer
46 // Access: Public, Virtual
47 // Description: Returns true if this PNMFileType can accept an
48 // integer image type, false if it can only
49 // accept a floating-point type. If this returns true,
50 // write_data() or write_row() is implemented.
51 ////////////////////////////////////////////////////////////////////
52 bool PNMWriter::
54  return true;
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: PNMWriter::write_pfm
59 // Access: Public, Virtual
60 // Description: Writes floating-point data from the indicated
61 // PfmFile. Returns true on success, false on failure.
62 ////////////////////////////////////////////////////////////////////
63 bool PNMWriter::
64 write_pfm(const PfmFile &pfm) {
65  return false;
66 }
67 
68 ////////////////////////////////////////////////////////////////////
69 // Function: PNMWriter::write_data
70 // Access: Public, Virtual
71 // Description: Writes out an entire image all at once, including the
72 // header, based on the image data stored in the given
73 // _x_size * _y_size array and alpha pointers. (If the
74 // image type has no alpha channel, alpha is ignored.)
75 // Returns the number of rows correctly written.
76 //
77 // It is the user's responsibility to fill in the header
78 // data via calls to set_x_size(), set_num_channels(),
79 // etc., or copy_header_from(), before calling
80 // write_data().
81 //
82 // It is important to delete the PNMWriter class after
83 // successfully writing the data. Failing to do this
84 // may result in some data not getting flushed!
85 //
86 // Derived classes need not override this if they
87 // instead provide supports_streaming() and write_row(),
88 // below.
89 ////////////////////////////////////////////////////////////////////
90 int PNMWriter::
91 write_data(xel *array, xelval *alpha) {
92  if (_x_size <= 0 || _y_size <= 0) {
93  return 0;
94  }
95 
96  if (!write_header()) {
97  return 0;
98  }
99 
100  int y;
101  for (y = 0; y < _y_size; y++) {
102  if (!write_row(array + y * _x_size, alpha + y * _x_size)) {
104  return y;
105  }
106  }
107 
108  return _y_size;
109 }
110 
111 ////////////////////////////////////////////////////////////////////
112 // Function: PNMWriter::supports_write_row
113 // Access: Public, Virtual
114 // Description: Returns true if this particular PNMWriter supports a
115 // streaming interface to writing the data: that is, it
116 // is capable of writing the image one row at a time,
117 // via repeated calls to write_row(). Returns false if
118 // the only way to write from this file is all at once,
119 // via write_data().
120 ////////////////////////////////////////////////////////////////////
121 bool PNMWriter::
123  return false;
124 }
125 
126 ////////////////////////////////////////////////////////////////////
127 // Function: PNMWriter::supports_grayscale
128 // Access: Public, Virtual
129 // Description: Returns true if this particular PNMWriter understands
130 // grayscale images. If this is false, then the rgb
131 // values of the xel array will be pre-filled with the
132 // same value across all three channels, to allow the
133 // writer to simply write out RGB data for a grayscale
134 // image.
135 ////////////////////////////////////////////////////////////////////
136 bool PNMWriter::
138  return true;
139 }
140 
141 ////////////////////////////////////////////////////////////////////
142 // Function: PNMWriter::write_header
143 // Access: Public, Virtual
144 // Description: If supports_write_row(), above, returns true, this
145 // function may be called to write out the image header
146 // in preparation to writing out the image data one row
147 // at a time. Returns true if the header is
148 // successfully written, false if there is an error.
149 //
150 // It is the user's responsibility to fill in the header
151 // data via calls to set_x_size(), set_num_channels(),
152 // etc., or copy_header_from(), before calling
153 // write_header().
154 ////////////////////////////////////////////////////////////////////
155 bool PNMWriter::
157  return false;
158 }
159 
160 ////////////////////////////////////////////////////////////////////
161 // Function: PNMWriter::write_row
162 // Access: Public, Virtual
163 // Description: If supports_write_row(), above, returns true, this
164 // function may be called repeatedly to write the image,
165 // one horizontal row at a time, beginning from the top.
166 // Returns true if the row is successfully written,
167 // false if there is an error.
168 //
169 // You must first call write_header() before writing the
170 // individual rows. It is also important to delete the
171 // PNMWriter class after successfully writing the last
172 // row. Failing to do this may result in some data not
173 // getting flushed!
174 ////////////////////////////////////////////////////////////////////
175 bool PNMWriter::
176 write_row(xel *, xelval *) {
177  return false;
178 }
179 
180 ////////////////////////////////////////////////////////////////////
181 // Function: PNMWriter::supports_stream_write
182 // Access: Public, Virtual
183 // Description: Returns true if this particular PNMWriter can write
184 // to a general stream (including pipes, etc.), or
185 // false if the writer must occasionally fseek() on its
186 // output stream, and thus only disk streams are
187 // supported.
188 ////////////////////////////////////////////////////////////////////
189 bool PNMWriter::
191  return false;
192 }
virtual bool supports_grayscale() const
Returns true if this particular PNMWriter understands grayscale images.
Definition: pnmWriter.cxx:137
virtual bool write_pfm(const PfmFile &pfm)
Writes floating-point data from the indicated PfmFile.
Definition: pnmWriter.cxx:64
virtual bool supports_write_row() const
Returns true if this particular PNMWriter supports a streaming interface to writing the data: that is...
Definition: pnmWriter.cxx:122
static void consider_yield()
Possibly suspends the current thread for the rest of the current epoch, if it has run for enough this...
Definition: thread.I:263
virtual bool supports_floating_point()
Returns true if this PNMFileType can accept a floating-point image type, false if it can only accept ...
Definition: pnmWriter.cxx:40
virtual bool write_header()
If supports_write_row(), above, returns true, this function may be called to write out the image head...
Definition: pnmWriter.cxx:156
Defines a pfm file, a 2-d table of floating-point numbers, either 3-component or 1-component, or with a special extension, 2- or 4-component.
Definition: pfmFile.h:34
virtual bool supports_stream_write() const
Returns true if this particular PNMWriter can write to a general stream (including pipes...
Definition: pnmWriter.cxx:190
virtual int write_data(xel *array, xelval *alpha)
Writes out an entire image all at once, including the header, based on the image data stored in the g...
Definition: pnmWriter.cxx:91
virtual bool write_row(xel *array, xelval *alpha)
If supports_write_row(), above, returns true, this function may be called repeatedly to write the ima...
Definition: pnmWriter.cxx:176
virtual bool supports_integer()
Returns true if this PNMFileType can accept an integer image type, false if it can only accept a floa...
Definition: pnmWriter.cxx:53