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 */
62 void DestTextureImage::
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 */
90 void DestTextureImage::
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 */
123 void DestTextureImage::
124 register_with_read_factory() {
125 BamReader::get_factory()->
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 */
133 void DestTextureImage::
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) {
145 DestTextureImage *me = new DestTextureImage;
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 }
bamReader.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
bamWriter.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
BamReader::get_factory
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
BamWriter
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition: datagramIterator.h:27
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
DestTextureImage
This represents a texture filename as it has been resized and copied to the map directory (e....
Definition: destTextureImage.h:28
DestTextureImage::copy
void copy(TextureImage *texture)
Unconditionally copies the source texture into the appropriate filename.
Definition: destTextureImage.cxx:63
DestTextureImage::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: destTextureImage.cxx:124
DestTextureImage::write_datagram
virtual void write_datagram(BamWriter *writer, Datagram &datagram)
Fills the indicated datagram up with a binary representation of the current object,...
Definition: destTextureImage.cxx:134
DestTextureImage::copy_if_stale
void copy_if_stale(const DestTextureImage *other, TextureImage *texture)
Copies the source texture into the appropriate filename only if the indicated old reference,...
Definition: destTextureImage.cxx:91
FactoryParams
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
Filename::compare_timestamps
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
ImageFile::get_num_channels
int get_num_channels() const
Returns the number of channels of the image.
Definition: imageFile.cxx:111
ImageFile::write
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
ImageFile::set_filename
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
ImageFile::get_x_size
int get_x_size() const
Returns the size of the image file in pixels in the X direction.
Definition: imageFile.cxx:82
ImageFile::get_properties
const TextureProperties & get_properties() const
Returns the grouping properties of the image.
Definition: imageFile.cxx:119
ImageFile::get_filename
const Filename & get_filename() const
Returns the primary filename of the image file.
Definition: imageFile.cxx:225
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
ImageFile::is_size_known
bool is_size_known() const
Returns true if the size of the image file is known, false otherwise.
Definition: imageFile.cxx:73
ImageFile::get_y_size
int get_y_size() const
Returns the size of the image file in pixels in the Y direction.
Definition: imageFile.cxx:92
PNMImageHeader::get_maxval
get_maxval
Returns the maximum channel value allowable for any pixel in this image; for instance,...
Definition: pnmImageHeader.h:70
PNMImageHeader::has_alpha
static bool has_alpha(ColorType color_type)
This static variant of has_alpha() returns true if the indicated image type includes an alpha channel...
Definition: pnmImageHeader.I:106
PNMImage
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
PNMImage::alpha_fill
void alpha_fill(float alpha=0.0)
Sets the entire alpha channel to the given level.
Definition: pnmImage.I:272
PNMImage::quick_filter_from
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.
Definition: pnm-image-filter.cxx:754
PNMImage::fill
void fill(float red, float green, float blue)
Sets the entire image (except the alpha channel) to the given color.
Definition: pnmImage.I:246
PNMImage::is_valid
bool is_valid() const
Returns true if the image has been read in or correctly initialized with a height and width.
Definition: pnmImage.I:342
SourceTextureImage
This is a texture image reference as it appears in an egg file: the source image of the texture.
Definition: sourceTextureImage.h:28
TextureImage
This represents a single source texture that is referenced by one or more egg files.
Definition: textureImage.h:46
TextureImage::get_preferred_source
SourceTextureImage * get_preferred_source()
Determines the preferred source image for examining size and reading pixels, etc.
Definition: textureImage.cxx:547
TextureImage::release_source_image
void release_source_image()
Frees the memory that was allocated by a previous call to read_source_image().
Definition: textureImage.cxx:729
TextureImage::read_source_image
const PNMImage & read_source_image()
Reads in the original image, if it has not already been read, and returns it.
Definition: textureImage.cxx:709
TexturePlacement
This corresponds to a particular assignment of a TextureImage with a PaletteGroup,...
Definition: texturePlacement.h:41
TexturePlacement::get_group
PaletteGroup * get_group() const
Returns the group that this placement represents.
Definition: texturePlacement.cxx:119
TexturePlacement::get_texture
TextureImage * get_texture() const
Returns the texture that this placement represents.
Definition: texturePlacement.cxx:103
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
TypedWritable
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
datagramIterator.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
datagram.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
destTextureImage.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
palettizer.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
sourceTextureImage.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
textureImage.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
texturePlacement.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.