Panda3D
 All Classes Functions Variables Enumerations
sourceTextureImage.cxx
1 // Filename: sourceTextureImage.cxx
2 // Created by: drose (29Nov00)
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 "sourceTextureImage.h"
16 #include "textureImage.h"
17 #include "filenameUnifier.h"
18 
19 #include "pnmImageHeader.h"
20 #include "datagram.h"
21 #include "datagramIterator.h"
22 #include "bamReader.h"
23 #include "bamWriter.h"
24 
25 TypeHandle SourceTextureImage::_type_handle;
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: SourceTextureImage::Default Constructor
29 // Access: Private
30 // Description: The default constructor is only for the convenience
31 // of the Bam reader.
32 ////////////////////////////////////////////////////////////////////
33 SourceTextureImage::
34 SourceTextureImage() {
35  _texture = (TextureImage *)NULL;
36 
37  _egg_count = 0;
38  _read_header = false;
39  _successfully_read_header = false;
40 }
41 
42 ////////////////////////////////////////////////////////////////////
43 // Function: SourceTextureImage::Constructor
44 // Access: Public
45 // Description:
46 ////////////////////////////////////////////////////////////////////
47 SourceTextureImage::
48 SourceTextureImage(TextureImage *texture, const Filename &filename,
49  const Filename &alpha_filename, int alpha_file_channel) :
50  _texture(texture)
51 {
52  _filename = filename;
53  _alpha_filename = alpha_filename;
54  _alpha_file_channel = alpha_file_channel;
55  _egg_count = 0;
56  _read_header = false;
57  _successfully_read_header = false;
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: SourceTextureImage::get_texture
62 // Access: Public
63 // Description: Returns the particular texture that this image is one
64 // of the sources for.
65 ////////////////////////////////////////////////////////////////////
67 get_texture() const {
68  return _texture;
69 }
70 
71 ////////////////////////////////////////////////////////////////////
72 // Function: SourceTextureImage::increment_egg_count
73 // Access: Public
74 // Description: Increments by one the number of egg files that are
75 // known to reference this SourceTextureImage.
76 ////////////////////////////////////////////////////////////////////
79  _egg_count++;
80 }
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function: SourceTextureImage::get_egg_count
84 // Access: Public
85 // Description: Returns the number of egg files that share this
86 // SourceTextureImage.
87 ////////////////////////////////////////////////////////////////////
89 get_egg_count() const {
90  return _egg_count;
91 }
92 
93 ////////////////////////////////////////////////////////////////////
94 // Function: SourceTextureImage::get_size
95 // Access: Public
96 // Description: Determines the size of the SourceTextureImage, if it
97 // is not already known. Returns true if the size was
98 // successfully determined (or if was already known), or
99 // false if the size could not be determined (for
100 // instance, because the image file is missing). After
101 // this call returns true, get_x_size() etc. may be
102 // safely called to return the size.
103 ////////////////////////////////////////////////////////////////////
106  if (!_size_known) {
107  return read_header();
108  }
109  return true;
110 }
111 
112 ////////////////////////////////////////////////////////////////////
113 // Function: SourceTextureImage::read_header
114 // Access: Public
115 // Description: Reads the actual image header to determine the image
116 // properties, like its size. Returns true if the image
117 // header is successfully read (or if has previously
118 // been successfully read this session), false
119 // otherwise. After this call returns true,
120 // get_x_size() etc. may be safely called to return the
121 // newly determined size.
122 ////////////////////////////////////////////////////////////////////
125  if (_read_header) {
126  return _successfully_read_header;
127  }
128 
129  _read_header = true;
130  _successfully_read_header = false;
131 
132  PNMImageHeader header;
133  if (!header.read_header(_filename)) {
134  nout << "Warning: cannot read texture "
135  << FilenameUnifier::make_user_filename(_filename) << "\n";
136  return false;
137  }
138 
139  set_header(header);
140 
141  return true;
142 }
143 
144 ////////////////////////////////////////////////////////////////////
145 // Function: SourceTextureImage::set_header
146 // Access: Public
147 // Description: Sets the header information associated with this
148 // image, as if it were loaded from the disk.
149 ////////////////////////////////////////////////////////////////////
151 set_header(const PNMImageHeader &header) {
152  _x_size = header.get_x_size();
153  _y_size = header.get_y_size();
154  int num_channels = header.get_num_channels();
155 
156  if (!_alpha_filename.empty() && _alpha_filename.exists()) {
157  // Assume if we have an alpha filename, that we have an additional
158  // alpha channel.
159  if (num_channels == 1 || num_channels == 3) {
160  num_channels++;
161  }
162  }
163  _properties.set_num_channels(num_channels);
164 
165  _size_known = true;
166  _successfully_read_header = true;
167 }
168 
169 
170 ////////////////////////////////////////////////////////////////////
171 // Function: SourceTextureImage::register_with_read_factory
172 // Access: Public, Static
173 // Description: Registers the current object as something that can be
174 // read from a Bam file.
175 ////////////////////////////////////////////////////////////////////
179  register_factory(get_class_type(), make_SourceTextureImage);
180 }
181 
182 ////////////////////////////////////////////////////////////////////
183 // Function: SourceTextureImage::write_datagram
184 // Access: Public, Virtual
185 // Description: Fills the indicated datagram up with a binary
186 // representation of the current object, in preparation
187 // for writing to a Bam file.
188 ////////////////////////////////////////////////////////////////////
190 write_datagram(BamWriter *writer, Datagram &datagram) {
191  ImageFile::write_datagram(writer, datagram);
192  writer->write_pointer(datagram, _texture);
193 
194  // We don't store _egg_count; instead, we count these up again each
195  // session.
196 
197  // We don't store _read_header or _successfully_read_header in the
198  // Bam file; these are transitory and we need to reread the image
199  // header for each session (in case the image files change between
200  // sessions).
201 }
202 
203 ////////////////////////////////////////////////////////////////////
204 // Function: SourceTextureImage::complete_pointers
205 // Access: Public, Virtual
206 // Description: Called after the object is otherwise completely read
207 // from a Bam file, this function's job is to store the
208 // pointers that were retrieved from the Bam file for
209 // each pointer object written. The return value is the
210 // number of pointers processed from the list.
211 ////////////////////////////////////////////////////////////////////
214  int pi = ImageFile::complete_pointers(p_list, manager);
215 
216  DCAST_INTO_R(_texture, p_list[pi++], pi);
217  return pi;
218 }
219 
220 ////////////////////////////////////////////////////////////////////
221 // Function: SourceTextureImage::make_SourceTextureImage
222 // Access: Protected
223 // Description: This method is called by the BamReader when an object
224 // of this type is encountered in a Bam file; it should
225 // allocate and return a new object with all the data
226 // read.
227 ////////////////////////////////////////////////////////////////////
228 TypedWritable *SourceTextureImage::
229 make_SourceTextureImage(const FactoryParams &params) {
231  DatagramIterator scan;
232  BamReader *manager;
233 
234  parse_params(params, scan, manager);
235  me->fillin(scan, manager);
236  return me;
237 }
238 
239 ////////////////////////////////////////////////////////////////////
240 // Function: SourceTextureImage::fillin
241 // Access: Protected
242 // Description: Reads the binary data from the given datagram
243 // iterator, which was written by a previous call to
244 // write_datagram().
245 ////////////////////////////////////////////////////////////////////
246 void SourceTextureImage::
247 fillin(DatagramIterator &scan, BamReader *manager) {
248  ImageFile::fillin(scan, manager);
249  manager->read_pointer(scan); // _texture
250 }
void increment_egg_count()
Increments by one the number of egg files that are known to reference this SourceTextureImage.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Called after the object is otherwise completely read from a Bam file, this function&#39;s job is to store...
Definition: imageFile.cxx:519
int get_num_channels() const
Returns the number of channels in the image.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
bool read_header()
Reads the actual image header to determine the image properties, like its size.
bool read_header(const Filename &filename, PNMFileType *type=NULL, bool report_unknown_type=true)
Opens up the image file and tries to read its header information to determine its size...
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
int get_x_size() const
Returns the number of pixels in the X direction.
int get_y_size() const
Returns the number of pixels in the Y direction.
int get_egg_count() const
Returns the number of egg files that share this SourceTextureImage.
virtual void write_datagram(BamWriter *writer, Datagram &datagram)
Fills the indicated datagram up with a binary representation of the current object, in preparation for writing to a Bam file.
Definition: imageFile.cxx:498
virtual void write_datagram(BamWriter *writer, Datagram &datagram)
Fills the indicated datagram up with a binary representation of the current object, in preparation for writing to a Bam file.
static Filename make_user_filename(Filename filename)
Returns a new filename that&#39;s made relative to the current directory, suitable for reporting to the u...
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Called after the object is otherwise completely read from a Bam file, this function&#39;s job is to store...
void set_header(const PNMImageHeader &header)
Sets the header information associated with this image, as if it were loaded from the disk...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
TextureImage * get_texture() const
Returns the particular texture that this image is one of the sources for.
bool get_size()
Determines the size of the SourceTextureImage, if it is not already known.
This is a texture image reference as it appears in an egg file: the source image of the texture...
static void register_with_read_factory()
Registers the current object as something that can be read from a Bam file.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
This is the base class of PNMImage, PNMReader, and PNMWriter.
A class to retrieve the individual data elements previously stored in a Datagram. ...
This represents a single source texture that is referenced by one or more egg files.
Definition: textureImage.h:51
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
Definition: bamWriter.cxx:279
void read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:652