Panda3D
 All Classes Functions Variables Enumerations
destTextureImage.cxx
1 // Filename: destTextureImage.cxx
2 // Created by: drose (05Dec00)
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 "destTextureImage.h"
16 #include "sourceTextureImage.h"
17 #include "texturePlacement.h"
18 #include "textureImage.h"
19 #include "palettizer.h"
20 
21 #include "datagram.h"
22 #include "datagramIterator.h"
23 #include "bamReader.h"
24 #include "bamWriter.h"
25 
26 TypeHandle DestTextureImage::_type_handle;
27 
28 
29 ////////////////////////////////////////////////////////////////////
30 // Function: DestTextureImage::Default Constructor
31 // Access: Private
32 // Description: The default constructor is only for the convenience
33 // of the Bam reader.
34 ////////////////////////////////////////////////////////////////////
35 DestTextureImage::
36 DestTextureImage() {
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: DestTextureImage::Constructor
41 // Access: Public
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 DestTextureImage::
45 DestTextureImage(TexturePlacement *placement) {
46  TextureImage *texture = placement->get_texture();
47  _properties = texture->get_properties();
48  _size_known = texture->is_size_known();
49  if (_size_known) {
50  _x_size = texture->get_x_size();
51  _y_size = texture->get_y_size();
52 
53  if (pal->_force_power_2) {
54  _x_size = to_power_2(_x_size);
55  _y_size = to_power_2(_y_size);
56  } else {
57  _x_size = max(_x_size, 1);
58  _y_size = max(_y_size, 1);
59  }
60  }
61 
62  set_filename(placement->get_group(), texture->get_name());
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: DestTextureImage::copy
67 // Access: Public
68 // Description: Unconditionally copies the source texture into the
69 // appropriate filename.
70 ////////////////////////////////////////////////////////////////////
72 copy(TextureImage *texture) {
73  const PNMImage &source_image = texture->read_source_image();
74  if (source_image.is_valid()) {
75  PNMImage dest_image(_x_size, _y_size, texture->get_num_channels(),
76  source_image.get_maxval());
77  dest_image.quick_filter_from(source_image);
78  write(dest_image);
79 
80  } else {
81  // Couldn't read the texture, so fill it with red.
82  PNMImage dest_image(_x_size, _y_size, texture->get_num_channels());
83  dest_image.fill(1.0, 0.0, 0.0);
84  if (dest_image.has_alpha()) {
85  dest_image.alpha_fill(1.0);
86  }
87 
88  write(dest_image);
89  }
90 
91  texture->release_source_image();
92 }
93 
94 ////////////////////////////////////////////////////////////////////
95 // Function: DestTextureImage::copy_if_stale
96 // Access: Public
97 // Description: Copies the source texture into the appropriate
98 // filename only if the indicated old reference, which
99 // represents the way it was last copied, is now
100 // out-of-date.
101 ////////////////////////////////////////////////////////////////////
103 copy_if_stale(const DestTextureImage *other, TextureImage *texture) {
104  if (other->get_x_size() != get_x_size() ||
105  other->get_y_size() != get_y_size() ||
106  other->get_num_channels() != get_num_channels()) {
107  copy(texture);
108 
109  } else {
110  // Also check the timestamps.
111  SourceTextureImage *source = texture->get_preferred_source();
112 
113  if (source != (SourceTextureImage *)NULL &&
114  source->get_filename().compare_timestamps(get_filename()) > 0) {
115  copy(texture);
116  }
117  }
118 }
119 
120 ////////////////////////////////////////////////////////////////////
121 // Function: DestTextureImage::to_power_2
122 // Access: Private, Static
123 // Description: Returns the largest power of 2 less than or equal to
124 // value.
125 ////////////////////////////////////////////////////////////////////
126 int DestTextureImage::
127 to_power_2(int value) {
128  int x = 1;
129  while ((x << 1) <= value) {
130  x = (x << 1);
131  }
132  return x;
133 }
134 
135 ////////////////////////////////////////////////////////////////////
136 // Function: DestTextureImage::register_with_read_factory
137 // Access: Public, Static
138 // Description: Registers the current object as something that can be
139 // read from a Bam file.
140 ////////////////////////////////////////////////////////////////////
144  register_factory(get_class_type(), make_DestTextureImage);
145 }
146 
147 ////////////////////////////////////////////////////////////////////
148 // Function: DestTextureImage::write_datagram
149 // Access: Public, Virtual
150 // Description: Fills the indicated datagram up with a binary
151 // representation of the current object, in preparation
152 // for writing to a Bam file.
153 ////////////////////////////////////////////////////////////////////
155 write_datagram(BamWriter *writer, Datagram &datagram) {
156  ImageFile::write_datagram(writer, datagram);
157 }
158 
159 ////////////////////////////////////////////////////////////////////
160 // Function: DestTextureImage::make_DestTextureImage
161 // Access: Protected
162 // Description: This method is called by the BamReader when an object
163 // of this type is encountered in a Bam file; it should
164 // allocate and return a new object with all the data
165 // read.
166 ////////////////////////////////////////////////////////////////////
167 TypedWritable* DestTextureImage::
168 make_DestTextureImage(const FactoryParams &params) {
170  DatagramIterator scan;
171  BamReader *manager;
172 
173  parse_params(params, scan, manager);
174  me->fillin(scan, manager);
175  return me;
176 }
177 
178 ////////////////////////////////////////////////////////////////////
179 // Function: DestTextureImage::fillin
180 // Access: Protected
181 // Description: Reads the binary data from the given datagram
182 // iterator, which was written by a previous call to
183 // write_datagram().
184 ////////////////////////////////////////////////////////////////////
185 void DestTextureImage::
186 fillin(DatagramIterator &scan, BamReader *manager) {
187  ImageFile::fillin(scan, manager);
188 }
static void register_with_read_factory()
Registers the current object as something that can be read from a Bam file.
This represents a texture filename as it has been resized and copied to the map directory (e...
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
Definition: pnmImage.h:68
bool is_valid() const
Returns true if the image has been read in or correctly initialized with a height and width...
Definition: pnmImage.I:309
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
bool set_filename(PaletteGroup *group, const string &basename)
Sets the filename, and if applicable, the alpha_filename, from the indicated basename.
Definition: imageFile.cxx:181
int get_num_channels() const
Returns the number of channels of the image.
Definition: imageFile.cxx:130
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
int get_y_size() const
Returns the size of the image file in pixels in the Y direction.
Definition: imageFile.cxx:106
void quick_filter_from(const PNMImage &copy, int xborder=0, int yborder=0)
Resizes from the given image, with a fixed radius of 0.5.
SourceTextureImage * get_preferred_source()
Determines the preferred source image for examining size and reading pixels, etc. ...
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
void copy_if_stale(const DestTextureImage *other, TextureImage *texture)
Copies the source texture into the appropriate filename only if the indicated old reference...
const Filename & get_filename() const
Returns the primary filename of the image file.
Definition: imageFile.cxx:263
bool write(const PNMImage &image) const
Writes out the image in the indicated PNMImage to the _filename and/or _alpha_filename.
Definition: imageFile.cxx:391
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
This corresponds to a particular assignment of a TextureImage with a PaletteGroup, and specifically describes which PaletteImage (if any), and where on the PaletteImage, the TextureImage has been assigned to.
This is a texture image reference as it appears in an egg file: the source image of the texture...
void copy(TextureImage *texture)
Unconditionally copies the source texture into the appropriate filename.
PaletteGroup * get_group() const
Returns the group that this placement represents.
int get_x_size() const
Returns the size of the image file in pixels in the X direction.
Definition: imageFile.cxx:93
int compare_timestamps(const Filename &other, bool this_missing_is_old=true, bool other_missing_is_old=true) const
Returns a number less than zero if the file named by this object is older than the given file...
Definition: filename.cxx:1521
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
const PNMImage & read_source_image()
Reads in the original image, if it has not already been read, and returns it.
xelval get_maxval() const
Returns the maximum channel value allowable for any pixel in this image; for instance, 255 for a typical 8-bit-per-channel image.
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
TextureImage * get_texture() const
Returns the texture that this placement represents.
void fill(float red, float green, float blue)
Sets the entire image (except the alpha channel) to the given color.
Definition: pnmImage.I:186
const TextureProperties & get_properties() const
Returns the grouping properties of the image.
Definition: imageFile.cxx:140
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
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.
void release_source_image()
Frees the memory that was allocated by a previous call to read_source_image().
bool is_size_known() const
Returns true if the size of the image file is known, false otherwise.
Definition: imageFile.cxx:81