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