Panda3D
destTextureImage.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 destTextureImage.cxx
10  * @author drose
11  * @date 2000-12-05
12  */
13 
14 #include "destTextureImage.h"
15 #include "sourceTextureImage.h"
16 #include "texturePlacement.h"
17 #include "textureImage.h"
18 #include "palettizer.h"
19 
20 #include "datagram.h"
21 #include "datagramIterator.h"
22 #include "bamReader.h"
23 #include "bamWriter.h"
24 
25 TypeHandle DestTextureImage::_type_handle;
26 
27 
28 /**
29  * The default constructor is only for the convenience of the Bam reader.
30  */
31 DestTextureImage::
32 DestTextureImage() {
33 }
34 
35 /**
36  *
37  */
38 DestTextureImage::
39 DestTextureImage(TexturePlacement *placement) {
40  TextureImage *texture = placement->get_texture();
41  _properties = texture->get_properties();
42  _size_known = texture->is_size_known();
43  if (_size_known) {
44  _x_size = texture->get_x_size();
45  _y_size = texture->get_y_size();
46 
47  if (pal->_force_power_2) {
48  _x_size = to_power_2(_x_size);
49  _y_size = to_power_2(_y_size);
50  } else {
51  _x_size = std::max(_x_size, 1);
52  _y_size = std::max(_y_size, 1);
53  }
54  }
55 
56  set_filename(placement->get_group(), texture->get_name());
57 }
58 
59 /**
60  * Unconditionally copies the source texture into the appropriate filename.
61  */
63 copy(TextureImage *texture) {
64  const PNMImage &source_image = texture->read_source_image();
65  if (source_image.is_valid()) {
66  PNMImage dest_image(_x_size, _y_size, texture->get_num_channels(),
67  source_image.get_maxval());
68  dest_image.quick_filter_from(source_image);
69  write(dest_image);
70 
71  } else {
72  // Couldn't read the texture, so fill it with red.
73  PNMImage dest_image(_x_size, _y_size, texture->get_num_channels());
74  dest_image.fill(1.0, 0.0, 0.0);
75  if (dest_image.has_alpha()) {
76  dest_image.alpha_fill(1.0);
77  }
78 
79  write(dest_image);
80  }
81 
82  texture->release_source_image();
83 }
84 
85 /**
86  * Copies the source texture into the appropriate filename only if the
87  * indicated old reference, which represents the way it was last copied, is
88  * now out-of-date.
89  */
91 copy_if_stale(const DestTextureImage *other, TextureImage *texture) {
92  if (other->get_x_size() != get_x_size() ||
93  other->get_y_size() != get_y_size() ||
94  other->get_num_channels() != get_num_channels()) {
95  copy(texture);
96 
97  } else {
98  // Also check the timestamps.
99  SourceTextureImage *source = texture->get_preferred_source();
100 
101  if (source != nullptr &&
102  source->get_filename().compare_timestamps(get_filename()) > 0) {
103  copy(texture);
104  }
105  }
106 }
107 
108 /**
109  * Returns the largest power of 2 less than or equal to value.
110  */
111 int DestTextureImage::
112 to_power_2(int value) {
113  int x = 1;
114  while ((x << 1) <= value) {
115  x = (x << 1);
116  }
117  return x;
118 }
119 
120 /**
121  * Registers the current object as something that can be read from a Bam file.
122  */
126  register_factory(get_class_type(), make_DestTextureImage);
127 }
128 
129 /**
130  * Fills the indicated datagram up with a binary representation of the current
131  * object, in preparation for writing to a Bam file.
132  */
134 write_datagram(BamWriter *writer, Datagram &datagram) {
135  ImageFile::write_datagram(writer, datagram);
136 }
137 
138 /**
139  * This method is called by the BamReader when an object of this type is
140  * encountered in a Bam file; it should allocate and return a new object with
141  * all the data read.
142  */
143 TypedWritable* DestTextureImage::
144 make_DestTextureImage(const FactoryParams &params) {
146  DatagramIterator scan;
147  BamReader *manager;
148 
149  parse_params(params, scan, manager);
150  me->fillin(scan, manager);
151  return me;
152 }
153 
154 /**
155  * Reads the binary data from the given datagram iterator, which was written
156  * by a previous call to write_datagram().
157  */
158 void DestTextureImage::
159 fillin(DatagramIterator &scan, BamReader *manager) {
160  ImageFile::fillin(scan, manager);
161 }
bool write(const PNMImage &image) const
Writes out the image in the indicated PNMImage to the _filename and/or _alpha_filename.
Definition: imageFile.cxx:339
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....
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
Definition: pnmImage.h:58
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
PaletteGroup * get_group() const
Returns the group that this placement represents.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_y_size() const
Returns the size of the image file in pixels in the Y direction.
Definition: imageFile.cxx:92
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
bool is_valid() const
Returns true if the image has been read in or correctly initialized with a height and width.
Definition: pnmImage.I:253
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
const TextureProperties & get_properties() const
Returns the grouping properties of the image.
Definition: imageFile.cxx:119
bool set_filename(PaletteGroup *group, const std::string &basename)
Sets the filename, and if applicable, the alpha_filename, from the indicated basename.
Definition: imageFile.cxx:150
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
get_maxval
Returns the maximum channel value allowable for any pixel in this image; for instance,...
bool is_size_known() const
Returns true if the size of the image file is known, false otherwise.
Definition: imageFile.cxx:73
int get_x_size() const
Returns the size of the image file in pixels in the X direction.
Definition: imageFile.cxx:82
void copy_if_stale(const DestTextureImage *other, TextureImage *texture)
Copies the source texture into the appropriate filename only if the indicated old reference,...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
int get_num_channels() const
Returns the number of channels of the image.
Definition: imageFile.cxx:111
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This corresponds to a particular assignment of a TextureImage with a PaletteGroup,...
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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:1417
TextureImage * get_texture() const
Returns the texture that this placement represents.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
const PNMImage & read_source_image()
Reads in the original image, if it has not already been read, and returns it.
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
void fill(float red, float green, float blue)
Sets the entire image (except the alpha channel) to the given color.
Definition: pnmImage.I:157
const Filename & get_filename() const
Returns the primary filename of the image file.
Definition: imageFile.cxx:225
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
virtual void write_datagram(BamWriter *writer, Datagram &datagram)
Fills the indicated datagram up with a binary representation of the current object,...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void release_source_image()
Frees the memory that was allocated by a previous call to read_source_image().