Panda3D
textureImage.h
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 textureImage.h
10  * @author drose
11  * @date 2000-11-28
12  */
13 
14 #ifndef TEXTUREIMAGE_H
15 #define TEXTUREIMAGE_H
16 
17 #include "pandatoolbase.h"
18 
19 #include "imageFile.h"
20 #include "paletteGroups.h"
21 #include "textureRequest.h"
22 
23 #include "namable.h"
24 #include "filename.h"
25 #include "pnmImage.h"
26 #include "eggRenderMode.h"
27 
28 #include "pmap.h"
29 #include "pset.h"
30 
31 class SourceTextureImage;
32 class DestTextureImage;
33 class TexturePlacement;
34 class EggFile;
35 
36 /**
37  * This represents a single source texture that is referenced by one or more
38  * egg files. It may be assigned to multiple PaletteGroups, and thus placed
39  * on multiple PaletteImages (up to one per PaletteGroup).
40  *
41  * Since a TextureImage may be referenced by multiple egg files that are each
42  * assigned to a different set of groups, it tries to maximize sharing between
43  * egg files and minimize the number of different PaletteGroups it is assigned
44  * to.
45  */
46 class TextureImage : public ImageFile, public Namable {
47 public:
48  TextureImage();
49 
50  void note_egg_file(EggFile *egg_file);
51  void assign_groups();
52 
53  const PaletteGroups &get_groups() const;
55  void force_replace();
56  void mark_eggs_stale();
57 
58  void mark_texture_named();
59  bool is_texture_named() const;
60 
61  void pre_txa_file();
62  void post_txa_file();
63  bool got_txa_file() const;
65 
66  bool get_omit() const;
67  double get_coverage_threshold() const;
68  int get_margin() const;
69  bool is_surprise() const;
70  bool is_used() const;
71  EggRenderMode::AlphaMode get_alpha_mode() const;
72 
73  EggTexture::WrapMode get_txa_wrap_u() const;
74  EggTexture::WrapMode get_txa_wrap_v() const;
75 
76  SourceTextureImage *get_source(const Filename &filename,
77  const Filename &alpha_filename,
78  int alpha_file_channel);
79 
82 
83  void copy_unplaced(bool redo_all);
84 
85  const PNMImage &read_source_image();
86  void release_source_image();
87  void set_source_image(const PNMImage &image);
88  void read_header();
89  bool is_newer_than(const Filename &reference_filename);
90 
91  void write_source_pathnames(std::ostream &out, int indent_level = 0) const;
92  void write_scale_info(std::ostream &out, int indent_level = 0);
93 
94 private:
95  typedef pset<EggFile *> EggFiles;
99 
100  static int compute_egg_count(PaletteGroup *group,
101  const WorkingEggs &egg_files);
102 
103  void assign_to_groups(const PaletteGroups &groups);
104  void consider_grayscale();
105  void consider_alpha();
106 
107  void remove_old_dests(const Dests &a, const Dests &b);
108  void copy_new_dests(const Dests &a, const Dests &b);
109 
110  std::string get_source_key(const Filename &filename,
111  const Filename &alpha_filename,
112  int alpha_file_channel);
113 
114 private:
115  TextureRequest _request;
116  TextureProperties _pre_txa_properties;
117  EggRenderMode::AlphaMode _pre_txa_alpha_mode;
118  SourceTextureImage *_preferred_source;
119  bool _is_surprise;
120 
121  bool _ever_read_image;
122  bool _forced_grayscale;
123 
124  enum AlphaBits {
125  // consider_alpha() sets alpha_bits to the union of all of these pixel
126  // values that might be found in the alpha channel.
127  AB_one = 0x01,
128  AB_mid = 0x02,
129  AB_zero = 0x04,
130  AB_all = 0x07 // == AB_zero | AB_mid | AB_one
131  };
132  int _alpha_bits;
133  double _mid_pixel_ratio;
134  bool _is_cutout;
135  EggRenderMode::AlphaMode _alpha_mode;
136  EggTexture::WrapMode _txa_wrap_u, _txa_wrap_v;
137 
138  PaletteGroups _explicitly_assigned_groups;
139  PaletteGroups _actual_assigned_groups;
140 
141  EggFiles _egg_files;
142 
144  Placement _placement;
145 
146  Sources _sources;
147  Dests _dests;
148 
149  bool _read_source_image;
150  bool _allow_release_source_image;
151  PNMImage _source_image;
152  bool _texture_named;
153  bool _got_txa_file;
154 
155 
156  // The TypedWritable interface follows.
157 public:
158  static void register_with_read_factory();
159  virtual void write_datagram(BamWriter *writer, Datagram &datagram);
160  virtual int complete_pointers(TypedWritable **p_list,
161  BamReader *manager);
162 
163 protected:
164  static TypedWritable *make_TextureImage(const FactoryParams &params);
165  void fillin(DatagramIterator &scan, BamReader *manager);
166 
167 private:
168  // These values are only filled in while reading from the bam file; don't
169  // use them otherwise.
170  int _num_placement;
171  int _num_sources;
172  int _num_dests;
173 
174 public:
175  static TypeHandle get_class_type() {
176  return _type_handle;
177  }
178  static void init_type() {
179  ImageFile::init_type();
180  Namable::init_type();
181  register_type(_type_handle, "TextureImage",
182  ImageFile::get_class_type(),
183  Namable::get_class_type());
184  }
185  virtual TypeHandle get_type() const {
186  return get_class_type();
187  }
188 
189 private:
190  static TypeHandle _type_handle;
191 
192  friend class TxaLine;
193 };
194 
195 #endif
TextureImage::got_txa_file
bool got_txa_file() const
Returns true if this TextureImage has been looked up in the .txa file this session,...
Definition: textureImage.cxx:412
TextureImage::get_source
SourceTextureImage * get_source(const Filename &filename, const Filename &alpha_filename, int alpha_file_channel)
Returns the SourceTextureImage corresponding to the given filename(s).
Definition: textureImage.cxx:519
TextureImage::assign_groups
void assign_groups()
Assigns the texture to all of the PaletteGroups the various egg files that use it need.
Definition: textureImage.cxx:76
TextureImage::read_header
void read_header()
Causes the header part of the image to be reread, usually to confirm that its image properties (size,...
Definition: textureImage.cxx:754
PaletteGroup
This is the highest level of grouping for TextureImages.
Definition: paletteGroup.h:43
EggFile
This represents a single egg file known to the palettizer.
Definition: eggFile.h:36
pvector< EggFile * >
TextureImage::get_coverage_threshold
double get_coverage_threshold() const
Returns the appropriate coverage threshold for this texture.
Definition: textureImage.cxx:446
TextureImage::complete_pointers
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Called after the object is otherwise completely read from a Bam file, this function's job is to store...
Definition: textureImage.cxx:1300
pnmImage.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition: datagramIterator.h:27
pmap< std::string, SourceTextureImage * >
register_type
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
BamReader
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
TextureImage::mark_eggs_stale
void mark_eggs_stale()
Marks all the egg files that reference this texture stale.
Definition: textureImage.cxx:234
imageFile.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BamWriter
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
TextureImage::pre_txa_file
void pre_txa_file()
Updates any internal state prior to reading the .txa file.
Definition: textureImage.cxx:265
filename.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
TypedWritable
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
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
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
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::post_txa_file
void post_txa_file()
Once the .txa file has been read and the TextureImage matched against it, considers applying the requ...
Definition: textureImage.cxx:289
TextureImage::get_omit
bool get_omit() const
Returns true if the user specifically requested to omit this texture via the "omit" keyword in the ....
Definition: textureImage.cxx:435
paletteGroups.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
TxaLine
This is a single matching line in the .txa file.
Definition: txaLine.h:36
pmap.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
textureRequest.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TextureRequest
These are the things that a user might explicitly request to adjust on a texture via a line in the ....
Definition: textureRequest.h:28
namable.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
FactoryParams
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
TextureImage::is_surprise
bool is_surprise() const
Returns true if this particular texture is a 'surprise', i.e.
Definition: textureImage.cxx:465
TextureImage::note_egg_file
void note_egg_file(EggFile *egg_file)
Records that a particular egg file references this texture.
Definition: textureImage.cxx:65
TextureImage::get_groups
const PaletteGroups & get_groups() const
Once assign_groups() has been called, this returns the actual set of groups the TextureImage has been...
Definition: textureImage.cxx:194
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::set_source_image
void set_source_image(const PNMImage &image)
Accepts the indicated source image as if it had been read from disk.
Definition: textureImage.cxx:742
TextureImage::is_used
bool is_used() const
Returns true if this particular texture has been placed somewhere, anywhere, or false if it is not us...
Definition: textureImage.cxx:480
TextureImage::clear_source_basic_properties
void clear_source_basic_properties()
Calls clear_basic_properties() on each source texture image used by this texture, to reset the proper...
Definition: textureImage.cxx:633
eggRenderMode.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TextureImage::force_replace
void force_replace()
Removes the texture from any PaletteImages it is assigned to, but does not remove it from the groups.
Definition: textureImage.cxx:220
TextureImage::is_newer_than
bool is_newer_than(const Filename &reference_filename)
Returns true if the source image is newer than the indicated file, false otherwise.
Definition: textureImage.cxx:768
ImageFile
This is the base class of both TextureImage and PaletteImage.
Definition: imageFile.h:33
TextureProperties
This is the set of characteristics of a texture that, if different from another texture,...
Definition: textureProperties.h:30
TextureImage::write_scale_info
void write_scale_info(std::ostream &out, int indent_level=0)
Writes the information about the texture's size and placement.
Definition: textureImage.cxx:858
TexturePlacement
This corresponds to a particular assignment of a TextureImage with a PaletteGroup,...
Definition: texturePlacement.h:41
TextureImage::write_datagram
virtual void write_datagram(BamWriter *writer, Datagram &datagram)
Fills the indicated datagram up with a binary representation of the current object,...
Definition: textureImage.cxx:1245
TextureImage::get_txa_wrap_u
EggTexture::WrapMode get_txa_wrap_u() const
Returns the wrap mode specified in the u direction in the txa file, or WM_unspecified.
Definition: textureImage.cxx:499
Namable
A base class for all things which can have a name.
Definition: namable.h:26
pset.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TextureImage::get_placement
TexturePlacement * get_placement(PaletteGroup *group) const
Gets the TexturePlacement object which represents the assignment of this texture to the indicated gro...
Definition: textureImage.cxx:204
TextureImage::get_txa_wrap_v
EggTexture::WrapMode get_txa_wrap_v() const
Returns the wrap mode specified in the v direction in the txa file, or WM_unspecified.
Definition: textureImage.cxx:508
TextureImage::copy_unplaced
void copy_unplaced(bool redo_all)
Copies the texture to whichever destination directories are appropriate for the groups in which it ha...
Definition: textureImage.cxx:649
TextureImage::determine_placement_size
void determine_placement_size()
Calls determine_size() on each TexturePlacement for the texture, to ensure that each TexturePlacement...
Definition: textureImage.cxx:422
pandatoolbase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TextureImage::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: textureImage.cxx:1235
TextureImage::get_margin
int get_margin() const
Returns the appropriate margin for this texture.
Definition: textureImage.cxx:456
PaletteGroups
A set of PaletteGroups.
Definition: paletteGroups.h:28
DestTextureImage
This represents a texture filename as it has been resized and copied to the map directory (e....
Definition: destTextureImage.h:28
TextureImage::write_source_pathnames
void write_source_pathnames(std::ostream &out, int indent_level=0) const
Writes the list of source pathnames that might contribute to this texture to the indicated output str...
Definition: textureImage.cxx:785
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::mark_texture_named
void mark_texture_named()
Indicates that this particular texture has been named by the user for processing this session,...
Definition: textureImage.cxx:247
TextureImage::get_alpha_mode
EggRenderMode::AlphaMode get_alpha_mode() const
Returns the alpha mode that should be used to render objects with this texture, as specified by the u...
Definition: textureImage.cxx:490
TextureImage::is_texture_named
bool is_texture_named() const
Returns true if this particular texture has been named by the user for procession this session,...
Definition: textureImage.cxx:257
TextureImage
This represents a single source texture that is referenced by one or more egg files.
Definition: textureImage.h:46
Filename
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
pset< EggFile * >