Panda3D
Loading...
Searching...
No Matches
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
24TypeHandle SourceTextureImage::_type_handle;
25
26/**
27 * The default constructor is only for the convenience of the Bam reader.
28 */
29SourceTextureImage::
30SourceTextureImage() {
31 _texture = nullptr;
32
33 _egg_count = 0;
34 _read_header = false;
35 _successfully_read_header = false;
36}
37
38/**
39 *
40 */
41SourceTextureImage::
42SourceTextureImage(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 */
58get_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 */
75get_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 */
87get_size() {
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 */
102read_header() {
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 */
127set_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 */
160write_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 */
178complete_pointers(TypedWritable **p_list, BamReader *manager) {
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 */
190TypedWritable *SourceTextureImage::
191make_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 */
205void SourceTextureImage::
206fillin(DatagramIterator &scan, BamReader *manager) {
207 ImageFile::fillin(scan, manager);
208 manager->read_pointer(scan); // _texture
209}
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition bamReader.h:110
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition bamReader.I:177
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition bamWriter.h:63
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
A class to retrieve the individual data elements previously stored in a Datagram.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
An instance of this class is passed to the Factory when requesting it to do its business and construc...
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:44
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...
virtual void write_datagram(BamWriter *writer, Datagram &datagram)
Fills the indicated datagram up with a binary representation of the current object,...
This is the base class of PNMImage, PNMReader, and PNMWriter.
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,...
int get_x_size() const
Returns the number of pixels in the X direction.
get_num_channels
Returns the number of channels in the image.
int get_y_size() const
Returns the number of pixels in the Y direction.
This is a texture image reference as it appears in an egg file: the source image of the texture.
bool get_size()
Determines the size of the SourceTextureImage, if it is not already known.
virtual void write_datagram(BamWriter *writer, Datagram &datagram)
Fills the indicated datagram up with a binary representation of the current object,...
static void register_with_read_factory()
Registers the current object as something that can be read from a Bam file.
bool read_header()
Reads the actual image header to determine the image properties, like its size.
void increment_egg_count()
Increments by one the number of egg files that are known to reference this SourceTextureImage.
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...
TextureImage * get_texture() const
Returns the particular texture that this image is one of the sources for.
void set_header(const PNMImageHeader &header)
Sets the header information associated with this image, as if it were loaded from the disk.
int get_egg_count() const
Returns the number of egg files that share this SourceTextureImage.
This represents a single source texture that is referenced by one or more egg files.
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
Base class for objects that can be written to and read from Bam files.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.