00001 // Filename: sourceTextureImage.cxx 00002 // Created by: drose (29Nov00) 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 "sourceTextureImage.h" 00020 #include "textureImage.h" 00021 #include "filenameUnifier.h" 00022 00023 #include <pnmImageHeader.h> 00024 #include <datagram.h> 00025 #include <datagramIterator.h> 00026 #include <bamReader.h> 00027 #include <bamWriter.h> 00028 00029 TypeHandle SourceTextureImage::_type_handle; 00030 00031 //////////////////////////////////////////////////////////////////// 00032 // Function: SourceTextureImage::Default Constructor 00033 // Access: Private 00034 // Description: The default constructor is only for the convenience 00035 // of the Bam reader. 00036 //////////////////////////////////////////////////////////////////// 00037 SourceTextureImage:: 00038 SourceTextureImage() { 00039 _texture = (TextureImage *)NULL; 00040 00041 _egg_count = 0; 00042 _read_header = false; 00043 _successfully_read_header = false; 00044 } 00045 00046 //////////////////////////////////////////////////////////////////// 00047 // Function: SourceTextureImage::Constructor 00048 // Access: Public 00049 // Description: 00050 //////////////////////////////////////////////////////////////////// 00051 SourceTextureImage:: 00052 SourceTextureImage(TextureImage *texture, const Filename &filename, 00053 const Filename &alpha_filename, int alpha_file_channel) : 00054 _texture(texture) 00055 { 00056 _filename = filename; 00057 _alpha_filename = alpha_filename; 00058 _alpha_file_channel = alpha_file_channel; 00059 _egg_count = 0; 00060 _read_header = false; 00061 _successfully_read_header = false; 00062 } 00063 00064 //////////////////////////////////////////////////////////////////// 00065 // Function: SourceTextureImage::get_texture 00066 // Access: Public 00067 // Description: Returns the particular texture that this image is one 00068 // of the sources for. 00069 //////////////////////////////////////////////////////////////////// 00070 TextureImage *SourceTextureImage:: 00071 get_texture() const { 00072 return _texture; 00073 } 00074 00075 //////////////////////////////////////////////////////////////////// 00076 // Function: SourceTextureImage::increment_egg_count 00077 // Access: Public 00078 // Description: Increments by one the number of egg files that are 00079 // known to reference this SourceTextureImage. 00080 //////////////////////////////////////////////////////////////////// 00081 void SourceTextureImage:: 00082 increment_egg_count() { 00083 _egg_count++; 00084 } 00085 00086 //////////////////////////////////////////////////////////////////// 00087 // Function: SourceTextureImage::get_egg_count 00088 // Access: Public 00089 // Description: Returns the number of egg files that share this 00090 // SourceTextureImage. 00091 //////////////////////////////////////////////////////////////////// 00092 int SourceTextureImage:: 00093 get_egg_count() const { 00094 return _egg_count; 00095 } 00096 00097 //////////////////////////////////////////////////////////////////// 00098 // Function: SourceTextureImage::get_size 00099 // Access: Public 00100 // Description: Determines the size of the SourceTextureImage, if it 00101 // is not already known. Returns true if the size was 00102 // successfully determined (or if was already known), or 00103 // false if the size could not be determined (for 00104 // instance, because the image file is missing). After 00105 // this call returns true, get_x_size() etc. may be 00106 // safely called to return the size. 00107 //////////////////////////////////////////////////////////////////// 00108 bool SourceTextureImage:: 00109 get_size() { 00110 if (!_size_known) { 00111 return read_header(); 00112 } 00113 return true; 00114 } 00115 00116 //////////////////////////////////////////////////////////////////// 00117 // Function: SourceTextureImage::read_header 00118 // Access: Public 00119 // Description: Reads the actual image header to determine the image 00120 // properties, like its size. Returns true if the image 00121 // header is successfully read (or if has previously 00122 // been successfully read this session), false 00123 // otherwise. After this call returns true, 00124 // get_x_size() etc. may be safely called to return the 00125 // newly determined size. 00126 //////////////////////////////////////////////////////////////////// 00127 bool SourceTextureImage:: 00128 read_header() { 00129 if (_read_header) { 00130 return _successfully_read_header; 00131 } 00132 00133 _read_header = true; 00134 _successfully_read_header = false; 00135 00136 PNMImageHeader header; 00137 if (!header.read_header(_filename)) { 00138 nout << "Warning: cannot read texture " << _filename << "\n"; 00139 return false; 00140 } 00141 00142 _x_size = header.get_x_size(); 00143 _y_size = header.get_y_size(); 00144 int num_channels = header.get_num_channels(); 00145 00146 if (!_alpha_filename.empty() && _alpha_filename.exists()) { 00147 // Assume if we have an alpha filename, that we have an additional 00148 // alpha channel. 00149 if (num_channels == 1 || num_channels == 3) { 00150 num_channels++; 00151 } 00152 } 00153 _properties.set_num_channels(num_channels); 00154 00155 _size_known = true; 00156 _successfully_read_header = true; 00157 00158 return true; 00159 } 00160 00161 00162 //////////////////////////////////////////////////////////////////// 00163 // Function: SourceTextureImage::register_with_read_factory 00164 // Access: Public, Static 00165 // Description: Registers the current object as something that can be 00166 // read from a Bam file. 00167 //////////////////////////////////////////////////////////////////// 00168 void SourceTextureImage:: 00169 register_with_read_factory() { 00170 BamReader::get_factory()-> 00171 register_factory(get_class_type(), make_SourceTextureImage); 00172 } 00173 00174 //////////////////////////////////////////////////////////////////// 00175 // Function: SourceTextureImage::write_datagram 00176 // Access: Public, Virtual 00177 // Description: Fills the indicated datagram up with a binary 00178 // representation of the current object, in preparation 00179 // for writing to a Bam file. 00180 //////////////////////////////////////////////////////////////////// 00181 void SourceTextureImage:: 00182 write_datagram(BamWriter *writer, Datagram &datagram) { 00183 ImageFile::write_datagram(writer, datagram); 00184 writer->write_pointer(datagram, _texture); 00185 00186 // We don't store _egg_count; instead, we count these up again each 00187 // session. 00188 00189 // We don't store _read_header or _successfully_read_header in the 00190 // Bam file; these are transitory and we need to reread the image 00191 // header for each session (in case the image files change between 00192 // sessions). 00193 } 00194 00195 //////////////////////////////////////////////////////////////////// 00196 // Function: SourceTextureImage::complete_pointers 00197 // Access: Public, Virtual 00198 // Description: Called after the object is otherwise completely read 00199 // from a Bam file, this function's job is to store the 00200 // pointers that were retrieved from the Bam file for 00201 // each pointer object written. The return value is the 00202 // number of pointers processed from the list. 00203 //////////////////////////////////////////////////////////////////// 00204 int SourceTextureImage:: 00205 complete_pointers(TypedWritable **p_list, BamReader *manager) { 00206 int pi = ImageFile::complete_pointers(p_list, manager); 00207 00208 DCAST_INTO_R(_texture, p_list[pi++], pi); 00209 return pi; 00210 } 00211 00212 //////////////////////////////////////////////////////////////////// 00213 // Function: SourceTextureImage::make_SourceTextureImage 00214 // Access: Protected 00215 // Description: This method is called by the BamReader when an object 00216 // of this type is encountered in a Bam file; it should 00217 // allocate and return a new object with all the data 00218 // read. 00219 //////////////////////////////////////////////////////////////////// 00220 TypedWritable *SourceTextureImage:: 00221 make_SourceTextureImage(const FactoryParams ¶ms) { 00222 SourceTextureImage *me = new SourceTextureImage; 00223 DatagramIterator scan; 00224 BamReader *manager; 00225 00226 parse_params(params, scan, manager); 00227 me->fillin(scan, manager); 00228 return me; 00229 } 00230 00231 //////////////////////////////////////////////////////////////////// 00232 // Function: SourceTextureImage::fillin 00233 // Access: Protected 00234 // Description: Reads the binary data from the given datagram 00235 // iterator, which was written by a previous call to 00236 // write_datagram(). 00237 //////////////////////////////////////////////////////////////////// 00238 void SourceTextureImage:: 00239 fillin(DatagramIterator &scan, BamReader *manager) { 00240 ImageFile::fillin(scan, manager); 00241 manager->read_pointer(scan); // _texture 00242 }