00001 // Filename: destTextureImage.cxx 00002 // Created by: drose (05Dec00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "destTextureImage.h" 00020 #include "sourceTextureImage.h" 00021 #include "texturePlacement.h" 00022 #include "textureImage.h" 00023 #include "palettizer.h" 00024 00025 #include <datagram.h> 00026 #include <datagramIterator.h> 00027 #include <bamReader.h> 00028 #include <bamWriter.h> 00029 00030 TypeHandle DestTextureImage::_type_handle; 00031 00032 00033 //////////////////////////////////////////////////////////////////// 00034 // Function: DestTextureImage::Default Constructor 00035 // Access: Private 00036 // Description: The default constructor is only for the convenience 00037 // of the Bam reader. 00038 //////////////////////////////////////////////////////////////////// 00039 DestTextureImage:: 00040 DestTextureImage() { 00041 } 00042 00043 //////////////////////////////////////////////////////////////////// 00044 // Function: DestTextureImage::Constructor 00045 // Access: Public 00046 // Description: 00047 //////////////////////////////////////////////////////////////////// 00048 DestTextureImage:: 00049 DestTextureImage(TexturePlacement *placement) { 00050 TextureImage *texture = placement->get_texture(); 00051 _properties = texture->get_properties(); 00052 _size_known = texture->is_size_known(); 00053 if (_size_known) { 00054 _x_size = texture->get_x_size(); 00055 _y_size = texture->get_y_size(); 00056 } 00057 00058 if (pal->_force_power_2) { 00059 _x_size = to_power_2(_x_size); 00060 _y_size = to_power_2(_y_size); 00061 } 00062 00063 set_filename(placement->get_group(), texture->get_name()); 00064 } 00065 00066 //////////////////////////////////////////////////////////////////// 00067 // Function: DestTextureImage::copy 00068 // Access: Public 00069 // Description: Unconditionally copies the source texture into the 00070 // appropriate filename. 00071 //////////////////////////////////////////////////////////////////// 00072 void DestTextureImage:: 00073 copy(TextureImage *texture) { 00074 const PNMImage &source_image = texture->read_source_image(); 00075 if (source_image.is_valid()) { 00076 PNMImage dest_image(_x_size, _y_size, texture->get_num_channels(), 00077 source_image.get_maxval()); 00078 dest_image.quick_filter_from(source_image); 00079 write(dest_image); 00080 00081 } else { 00082 // Couldn't read the texture, so fill it with red. 00083 PNMImage dest_image(_x_size, _y_size, texture->get_num_channels()); 00084 dest_image.fill(1.0, 0.0, 0.0); 00085 write(dest_image); 00086 } 00087 } 00088 00089 //////////////////////////////////////////////////////////////////// 00090 // Function: DestTextureImage::copy_if_stale 00091 // Access: Public 00092 // Description: Copies the source texture into the appropriate 00093 // filename only if the indicated old reference, which 00094 // represents the way it was last copied, is now 00095 // out-of-date. 00096 //////////////////////////////////////////////////////////////////// 00097 void DestTextureImage:: 00098 copy_if_stale(const DestTextureImage *other, TextureImage *texture) { 00099 if (other->get_x_size() != get_x_size() || 00100 other->get_y_size() != get_y_size() || 00101 other->get_num_channels() != get_num_channels()) { 00102 copy(texture); 00103 00104 } else { 00105 // Also check the timestamps. 00106 SourceTextureImage *source = texture->get_preferred_source(); 00107 00108 if (source != (SourceTextureImage *)NULL && 00109 source->get_filename().compare_timestamps(get_filename()) > 0) { 00110 copy(texture); 00111 } 00112 } 00113 } 00114 00115 //////////////////////////////////////////////////////////////////// 00116 // Function: DestTextureImage::to_power_2 00117 // Access: Private, Static 00118 // Description: Returns the largest power of 2 less than or equal to 00119 // value. 00120 //////////////////////////////////////////////////////////////////// 00121 int DestTextureImage:: 00122 to_power_2(int value) { 00123 int x = 1; 00124 while ((x << 1) <= value) { 00125 x = (x << 1); 00126 } 00127 return x; 00128 } 00129 00130 //////////////////////////////////////////////////////////////////// 00131 // Function: DestTextureImage::register_with_read_factory 00132 // Access: Public, Static 00133 // Description: Registers the current object as something that can be 00134 // read from a Bam file. 00135 //////////////////////////////////////////////////////////////////// 00136 void DestTextureImage:: 00137 register_with_read_factory() { 00138 BamReader::get_factory()-> 00139 register_factory(get_class_type(), make_DestTextureImage); 00140 } 00141 00142 //////////////////////////////////////////////////////////////////// 00143 // Function: DestTextureImage::write_datagram 00144 // Access: Public, Virtual 00145 // Description: Fills the indicated datagram up with a binary 00146 // representation of the current object, in preparation 00147 // for writing to a Bam file. 00148 //////////////////////////////////////////////////////////////////// 00149 void DestTextureImage:: 00150 write_datagram(BamWriter *writer, Datagram &datagram) { 00151 ImageFile::write_datagram(writer, datagram); 00152 } 00153 00154 //////////////////////////////////////////////////////////////////// 00155 // Function: DestTextureImage::make_DestTextureImage 00156 // Access: Protected 00157 // Description: This method is called by the BamReader when an object 00158 // of this type is encountered in a Bam file; it should 00159 // allocate and return a new object with all the data 00160 // read. 00161 //////////////////////////////////////////////////////////////////// 00162 TypedWritable* DestTextureImage:: 00163 make_DestTextureImage(const FactoryParams ¶ms) { 00164 DestTextureImage *me = new DestTextureImage; 00165 DatagramIterator scan; 00166 BamReader *manager; 00167 00168 parse_params(params, scan, manager); 00169 me->fillin(scan, manager); 00170 return me; 00171 } 00172 00173 //////////////////////////////////////////////////////////////////// 00174 // Function: DestTextureImage::fillin 00175 // Access: Protected 00176 // Description: Reads the binary data from the given datagram 00177 // iterator, which was written by a previous call to 00178 // write_datagram(). 00179 //////////////////////////////////////////////////////////////////// 00180 void DestTextureImage:: 00181 fillin(DatagramIterator &scan, BamReader *manager) { 00182 ImageFile::fillin(scan, manager); 00183 }