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 }
ImageFile::complete_pointers
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
ImageFile::write_datagram
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
SourceTextureImage::complete_pointers
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: sourceTextureImage.cxx:178
SourceTextureImage::get_egg_count
int get_egg_count() const
Returns the number of egg files that share this SourceTextureImage.
Definition: sourceTextureImage.cxx:75
PNMImageHeader::read_header
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,...
Definition: pnmImageHeader.cxx:33
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition: datagramIterator.h:27
pnmImageHeader.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BamReader
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
SourceTextureImage::get_texture
TextureImage * get_texture() const
Returns the particular texture that this image is one of the sources for.
Definition: sourceTextureImage.cxx:58
BamWriter
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
BamWriter::write_pointer
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
BamReader::get_factory
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
bamReader.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
textureImage.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
SourceTextureImage::set_header
void set_header(const PNMImageHeader &header)
Sets the header information associated with this image, as if it were loaded from the disk.
Definition: sourceTextureImage.cxx:127
TypedWritable
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PNMImageHeader::get_x_size
int get_x_size() const
Returns the number of pixels in the X direction.
Definition: pnmImageHeader.I:144
PNMImageHeader::get_num_channels
get_num_channels
Returns the number of channels in the image.
Definition: pnmImageHeader.h:60
SourceTextureImage
This is a texture image reference as it appears in an egg file: the source image of the texture.
Definition: sourceTextureImage.h:28
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PNMImageHeader
This is the base class of PNMImage, PNMReader, and PNMWriter.
Definition: pnmImageHeader.h:40
filenameUnifier.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
FactoryParams
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
SourceTextureImage::get_size
bool get_size()
Determines the size of the SourceTextureImage, if it is not already known.
Definition: sourceTextureImage.cxx:87
PNMImageHeader::get_y_size
int get_y_size() const
Returns the number of pixels in the Y direction.
Definition: pnmImageHeader.I:153
SourceTextureImage::read_header
bool read_header()
Reads the actual image header to determine the image properties, like its size.
Definition: sourceTextureImage.cxx:102
SourceTextureImage::register_with_read_factory
static void register_with_read_factory()
Registers the current object as something that can be read from a Bam file.
Definition: sourceTextureImage.cxx:150
SourceTextureImage::write_datagram
virtual void write_datagram(BamWriter *writer, Datagram &datagram)
Fills the indicated datagram up with a binary representation of the current object,...
Definition: sourceTextureImage.cxx:160
datagram.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
sourceTextureImage.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
FilenameUnifier::make_user_filename
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...
Definition: filenameUnifier.cxx:97
BamReader::read_pointer
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:610
datagramIterator.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bamWriter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
parse_params
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
TextureImage
This represents a single source texture that is referenced by one or more egg files.
Definition: textureImage.h:46
SourceTextureImage::increment_egg_count
void increment_egg_count()
Increments by one the number of egg files that are known to reference this SourceTextureImage.
Definition: sourceTextureImage.cxx:67
Filename
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39