Panda3D
sourceTextureImage.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file sourceTextureImage.cxx
10  * @author drose
11  * @date 2000-11-29
12  */
13 
14 #include "sourceTextureImage.h"
15 #include "textureImage.h"
16 #include "filenameUnifier.h"
17 
18 #include "pnmImageHeader.h"
19 #include "datagram.h"
20 #include "datagramIterator.h"
21 #include "bamReader.h"
22 #include "bamWriter.h"
23 
24 TypeHandle SourceTextureImage::_type_handle;
25 
26 /**
27  * The default constructor is only for the convenience of the Bam reader.
28  */
29 SourceTextureImage::
30 SourceTextureImage() {
31  _texture = nullptr;
32 
33  _egg_count = 0;
34  _read_header = false;
35  _successfully_read_header = false;
36 }
37 
38 /**
39  *
40  */
41 SourceTextureImage::
42 SourceTextureImage(TextureImage *texture, const Filename &filename,
43  const Filename &alpha_filename, int alpha_file_channel) :
44  _texture(texture)
45 {
46  _filename = filename;
47  _alpha_filename = alpha_filename;
48  _alpha_file_channel = alpha_file_channel;
49  _egg_count = 0;
50  _read_header = false;
51  _successfully_read_header = false;
52 }
53 
54 /**
55  * Returns the particular texture that this image is one of the sources for.
56  */
58 get_texture() const {
59  return _texture;
60 }
61 
62 /**
63  * Increments by one the number of egg files that are known to reference this
64  * SourceTextureImage.
65  */
68  _egg_count++;
69 }
70 
71 /**
72  * Returns the number of egg files that share this SourceTextureImage.
73  */
75 get_egg_count() const {
76  return _egg_count;
77 }
78 
79 /**
80  * Determines the size of the SourceTextureImage, if it is not already known.
81  * Returns true if the size was successfully determined (or if was already
82  * known), or false if the size could not be determined (for instance, because
83  * the image file is missing). After this call returns true, get_x_size()
84  * etc. may be safely called to return the size.
85  */
88  if (!_size_known) {
89  return read_header();
90  }
91  return true;
92 }
93 
94 /**
95  * Reads the actual image header to determine the image properties, like its
96  * size. Returns true if the image header is successfully read (or if has
97  * previously been successfully read this session), false otherwise. After
98  * this call returns true, get_x_size() etc. may be safely called to return
99  * the newly determined size.
100  */
103  if (_read_header) {
104  return _successfully_read_header;
105  }
106 
107  _read_header = true;
108  _successfully_read_header = false;
109 
110  PNMImageHeader header;
111  if (!header.read_header(_filename)) {
112  nout << "Warning: cannot read texture "
113  << FilenameUnifier::make_user_filename(_filename) << "\n";
114  return false;
115  }
116 
117  set_header(header);
118 
119  return true;
120 }
121 
122 /**
123  * Sets the header information associated with this image, as if it were
124  * loaded from the disk.
125  */
127 set_header(const PNMImageHeader &header) {
128  _x_size = header.get_x_size();
129  _y_size = header.get_y_size();
130  int num_channels = header.get_num_channels();
131 
132  if (!_alpha_filename.empty() && _alpha_filename.exists()) {
133  // Assume if we have an alpha filename, that we have an additional alpha
134  // channel.
135  if (num_channels == 1 || num_channels == 3) {
136  num_channels++;
137  }
138  }
139  _properties.set_num_channels(num_channels);
140 
141  _size_known = true;
142  _successfully_read_header = true;
143 }
144 
145 
146 /**
147  * Registers the current object as something that can be read from a Bam file.
148  */
152  register_factory(get_class_type(), make_SourceTextureImage);
153 }
154 
155 /**
156  * Fills the indicated datagram up with a binary representation of the current
157  * object, in preparation for writing to a Bam file.
158  */
160 write_datagram(BamWriter *writer, Datagram &datagram) {
161  ImageFile::write_datagram(writer, datagram);
162  writer->write_pointer(datagram, _texture);
163 
164  // We don't store _egg_count; instead, we count these up again each session.
165 
166  // We don't store _read_header or _successfully_read_header in the Bam file;
167  // these are transitory and we need to reread the image header for each
168  // session (in case the image files change between sessions).
169 }
170 
171 /**
172  * Called after the object is otherwise completely read from a Bam file, this
173  * function's job is to store the pointers that were retrieved from the Bam
174  * file for each pointer object written. The return value is the number of
175  * pointers processed from the list.
176  */
179  int pi = ImageFile::complete_pointers(p_list, manager);
180 
181  DCAST_INTO_R(_texture, p_list[pi++], pi);
182  return pi;
183 }
184 
185 /**
186  * This method is called by the BamReader when an object of this type is
187  * encountered in a Bam file; it should allocate and return a new object with
188  * all the data read.
189  */
190 TypedWritable *SourceTextureImage::
191 make_SourceTextureImage(const FactoryParams &params) {
193  DatagramIterator scan;
194  BamReader *manager;
195 
196  parse_params(params, scan, manager);
197  me->fillin(scan, manager);
198  return me;
199 }
200 
201 /**
202  * Reads the binary data from the given datagram iterator, which was written
203  * by a previous call to write_datagram().
204  */
205 void SourceTextureImage::
206 fillin(DatagramIterator &scan, BamReader *manager) {
207  ImageFile::fillin(scan, manager);
208  manager->read_pointer(scan); // _texture
209 }
bool read_header(const Filename &filename, PNMFileType *type=nullptr, bool report_unknown_type=true)
Opens up the image file and tries to read its header information to determine its size,...
void increment_egg_count()
Increments by one the number of egg files that are known to reference this SourceTextureImage.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
get_num_channels
Returns the number of channels in the image.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Called after the object is otherwise completely read from a Bam file, this function's job is to store...
Definition: imageFile.cxx:454
int get_egg_count() const
Returns the number of egg files that share this SourceTextureImage.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
TextureImage * get_texture() const
Returns the particular texture that this image is one of the sources for.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool read_header()
Reads the actual image header to determine the image properties, like its size.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
int get_y_size() const
Returns the number of pixels in the Y direction.
int get_x_size() const
Returns the number of pixels in the X direction.
virtual void write_datagram(BamWriter *writer, Datagram &datagram)
Fills the indicated datagram up with a binary representation of the current object,...
Definition: imageFile.cxx:436
virtual void write_datagram(BamWriter *writer, Datagram &datagram)
Fills the indicated datagram up with a binary representation of the current object,...
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
static Filename make_user_filename(Filename filename)
Returns a new filename that'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:39
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Called after the object is otherwise completely read from a Bam file, this function'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:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static void register_with_read_factory()
Registers the current object as something that can be read from a Bam file.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:610
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:46
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
Definition: bamWriter.cxx:317