textureImage.h

00001 // Filename: textureImage.h
00002 // Created by:  drose (28Nov00)
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 #ifndef TEXTUREIMAGE_H
00016 #define TEXTUREIMAGE_H
00017 
00018 #include "pandatoolbase.h"
00019 
00020 #include "imageFile.h"
00021 #include "paletteGroups.h"
00022 #include "textureRequest.h"
00023 
00024 #include "namable.h"
00025 #include "filename.h"
00026 #include "pnmImage.h"
00027 #include "eggRenderMode.h"
00028 
00029 #include "pmap.h"
00030 #include "pset.h"
00031 
00032 class SourceTextureImage;
00033 class DestTextureImage;
00034 class TexturePlacement;
00035 class EggFile;
00036 
00037 ////////////////////////////////////////////////////////////////////
00038 //       Class : TextureImage
00039 // Description : This represents a single source texture that is
00040 //               referenced by one or more egg files.  It may be
00041 //               assigned to multiple PaletteGroups, and thus placed
00042 //               on multiple PaletteImages (up to one per
00043 //               PaletteGroup).
00044 //
00045 //               Since a TextureImage may be referenced by multiple
00046 //               egg files that are each assigned to a different set
00047 //               of groups, it tries to maximize sharing between egg
00048 //               files and minimize the number of different
00049 //               PaletteGroups it is assigned to.
00050 ////////////////////////////////////////////////////////////////////
00051 class TextureImage : public ImageFile, public Namable {
00052 public:
00053   TextureImage();
00054 
00055   void note_egg_file(EggFile *egg_file);
00056   void assign_groups();
00057 
00058   const PaletteGroups &get_groups() const;
00059   TexturePlacement *get_placement(PaletteGroup *group) const;
00060   void force_replace();
00061   void mark_eggs_stale();
00062 
00063   void mark_texture_named();
00064   bool is_texture_named() const;
00065 
00066   void pre_txa_file();
00067   void post_txa_file();
00068   bool got_txa_file() const;
00069   void determine_placement_size();
00070 
00071   bool get_omit() const;
00072   double get_coverage_threshold() const;
00073   int get_margin() const;
00074   bool is_surprise() const;
00075   bool is_used() const;
00076   EggRenderMode::AlphaMode get_alpha_mode() const;
00077 
00078   EggTexture::WrapMode get_txa_wrap_u() const;
00079   EggTexture::WrapMode get_txa_wrap_v() const;
00080 
00081   SourceTextureImage *get_source(const Filename &filename,
00082                                  const Filename &alpha_filename,
00083                                  int alpha_file_channel);
00084 
00085   SourceTextureImage *get_preferred_source();
00086   void clear_source_basic_properties();
00087 
00088   void copy_unplaced(bool redo_all);
00089 
00090   const PNMImage &read_source_image();
00091   void release_source_image();
00092   void set_source_image(const PNMImage &image);
00093   void read_header();
00094   bool is_newer_than(const Filename &reference_filename);
00095 
00096   void write_source_pathnames(ostream &out, int indent_level = 0) const;
00097   void write_scale_info(ostream &out, int indent_level = 0);
00098 
00099 private:
00100   typedef pset<EggFile *> EggFiles;
00101   typedef pvector<EggFile *> WorkingEggs;
00102   typedef pmap<string, SourceTextureImage *> Sources;
00103   typedef pmap<string, DestTextureImage *> Dests;
00104 
00105   static int compute_egg_count(PaletteGroup *group,
00106                                const WorkingEggs &egg_files);
00107 
00108   void assign_to_groups(const PaletteGroups &groups);
00109   void consider_grayscale();
00110   void consider_alpha();
00111 
00112   void remove_old_dests(const Dests &a, const Dests &b);
00113   void copy_new_dests(const Dests &a, const Dests &b);
00114 
00115   string get_source_key(const Filename &filename,
00116                         const Filename &alpha_filename,
00117                         int alpha_file_channel);
00118 
00119 private:
00120   TextureRequest _request;
00121   TextureProperties _pre_txa_properties;
00122   EggRenderMode::AlphaMode _pre_txa_alpha_mode;
00123   SourceTextureImage *_preferred_source;
00124   bool _is_surprise;
00125 
00126   bool _ever_read_image;
00127   bool _forced_grayscale;
00128 
00129   enum AlphaBits {
00130     // consider_alpha() sets alpha_bits to the union of all of these
00131     // pixel values that might be found in the alpha channel.
00132     AB_one   = 0x01,
00133     AB_mid   = 0x02,
00134     AB_zero  = 0x04,
00135     AB_all   = 0x07 // == AB_zero | AB_mid | AB_one
00136   };
00137   int _alpha_bits;
00138   double _mid_pixel_ratio;
00139   bool _is_cutout;
00140   EggRenderMode::AlphaMode _alpha_mode;
00141   EggTexture::WrapMode _txa_wrap_u, _txa_wrap_v;
00142 
00143   PaletteGroups _explicitly_assigned_groups;
00144   PaletteGroups _actual_assigned_groups;
00145 
00146   EggFiles _egg_files;
00147 
00148   typedef pmap<PaletteGroup *, TexturePlacement *> Placement;
00149   Placement _placement;
00150 
00151   Sources _sources;
00152   Dests _dests;
00153 
00154   bool _read_source_image;
00155   bool _allow_release_source_image;
00156   PNMImage _source_image;
00157   bool _texture_named;
00158   bool _got_txa_file;
00159 
00160 
00161   // The TypedWritable interface follows.
00162 public:
00163   static void register_with_read_factory();
00164   virtual void write_datagram(BamWriter *writer, Datagram &datagram);
00165   virtual int complete_pointers(TypedWritable **p_list,
00166                                 BamReader *manager);
00167 
00168 protected:
00169   static TypedWritable *make_TextureImage(const FactoryParams &params);
00170   void fillin(DatagramIterator &scan, BamReader *manager);
00171 
00172 private:
00173   // These values are only filled in while reading from the bam file;
00174   // don't use them otherwise.
00175   int _num_placement;
00176   int _num_sources;
00177   int _num_dests;
00178 
00179 public:
00180   static TypeHandle get_class_type() {
00181     return _type_handle;
00182   }
00183   static void init_type() {
00184     ImageFile::init_type();
00185     Namable::init_type();
00186     register_type(_type_handle, "TextureImage",
00187                   ImageFile::get_class_type(),
00188                   Namable::get_class_type());
00189   }
00190   virtual TypeHandle get_type() const {
00191     return get_class_type();
00192   }
00193 
00194 private:
00195   static TypeHandle _type_handle;
00196 
00197   friend class TxaLine;
00198 };
00199 
00200 #endif
00201