Panda3D
txaFileFilter.cxx
1 // Filename: txaFileFilter.cxx
2 // Created by: drose (27Jul06)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "txaFileFilter.h"
16 #include "palettizer.h"
17 #include "txaFile.h"
18 #include "textureImage.h"
19 #include "sourceTextureImage.h"
20 #include "texturePool.h"
21 #include "dconfig.h"
22 #include "configVariableFilename.h"
23 #include "virtualFileSystem.h"
24 #include "config_util.h"
25 
26 NotifyCategoryDeclNoExport(txafile);
27 NotifyCategoryDef(txafile, "");
28 
29 // A few lines to register this filter type with the TexturePool when
30 // the shared library is loaded.
31 Configure(config_txaFileFilter);
32 ConfigureFn(config_txaFileFilter) {
33  TxaFileFilter::init_type();
35  pool->register_filter(new TxaFileFilter);
36 }
37 
38 TypeHandle TxaFileFilter::_type_handle;
39 TxaFile *TxaFileFilter::_txa_file;
40 bool TxaFileFilter::_got_txa_file;
41 
42 ////////////////////////////////////////////////////////////////////
43 // Function: TxaFileFilter::post_load
44 // Access: Public, Virtual
45 // Description: This method is called after each texture has been
46 // loaded from disk, via the TexturePool, for the first
47 // time. By the time this method is called, the Texture
48 // has already been fully read from disk. This method
49 // should return the Texture pointer that the
50 // TexturePool should actually return (usually it is the
51 // same as the pointer supplied).
52 ////////////////////////////////////////////////////////////////////
53 PT(Texture) TxaFileFilter::
54 post_load(Texture *tex) {
55  if (!_got_txa_file) {
56  read_txa_file();
57  }
58 
59  TextureImage tex_image;
60  string name = tex->get_filename().get_basename_wo_extension();
61  tex_image.set_name(name);
62 
63  SourceTextureImage *source = tex_image.get_source
64  (tex->get_fullpath(), tex->get_alpha_fullpath(), 0);
65  PNMImage pnm_image;
66  tex->store(pnm_image);
67  source->set_header(pnm_image);
68  tex_image.set_source_image(pnm_image);
69 
70  tex_image.pre_txa_file();
71 
72  bool matched = _txa_file->match_texture(&tex_image);
73  if (txafile_cat.is_debug()) {
74  if (!matched) {
75  txafile_cat.debug()
76  << "Not matched: " << name << "\n";
77  } else {
78  txafile_cat.debug()
79  << "Matched: " << name << "\n";
80  }
81  }
82 
83  tex_image.post_txa_file();
84 
85  PNMImage dest(tex_image.get_x_size(),
86  tex_image.get_y_size(),
87  tex_image.get_num_channels(),
88  pnm_image.get_maxval());
89  dest.quick_filter_from(pnm_image);
90 
91  tex->load(dest);
92 
93  // Create an EggTexture to pass back the requested alpha mode to
94  // the egg loader, if the texture is now being loaded from an egg
95  // file.
96  PT_EggTexture egg_tex = new EggTexture(tex->get_name(), tex->get_fullpath());
97  const TextureProperties &props = tex_image.get_properties();
98 
99  egg_tex->set_alpha_mode(tex_image.get_alpha_mode());
100  egg_tex->set_format(props._format);
101  egg_tex->set_minfilter(props._minfilter);
102  egg_tex->set_minfilter(props._magfilter);
103  egg_tex->set_anisotropic_degree(props._anisotropic_degree);
104 
105  tex->set_aux_data("egg", egg_tex);
106 
107  return tex;
108 }
109 
110 ////////////////////////////////////////////////////////////////////
111 // Function: TxaFileFilter::read_txa_file
112 // Access: Private, Static
113 // Description: Reads the textures.txa file named by the variable
114 // txa-file. Called only once, at startup.
115 ////////////////////////////////////////////////////////////////////
116 void TxaFileFilter::
117 read_txa_file() {
119 
120  // We need to create a global Palettizer object to hold some of the
121  // global properties that may be specified in a txa file.
122  if (pal == (Palettizer *)NULL) {
123  pal = new Palettizer;
124  }
125 
126  _txa_file = new TxaFile;
127  _got_txa_file = true;
128 
129  ConfigVariableFilename txa_file
130  ("txa-file", Filename("textures.txa"),
131  PRC_DESC("Specify the name of the txa file to load when the txafile texture filter"
132  "is in effect."));
133 
134  Filename filename = txa_file;
135  vfs->resolve_filename(filename, get_model_path());
136 
137  if (!vfs->exists(filename)) {
138  txafile_cat.warning()
139  << "Filename " << filename << " not found.\n";
140  } else {
141  filename.set_text();
142  istream *ifile = vfs->open_read_file(filename, true);
143  if (ifile == (istream *)NULL) {
144  txafile_cat.warning()
145  << "Filename " << filename << " cannot be read.\n";
146  } else {
147  if (!_txa_file->read(*ifile, filename)) {
148  txafile_cat.warning()
149  << "Syntax errors in " << filename << "\n";
150  } else {
151  txafile_cat.info()
152  << "Read " << filename << "\n";
153  }
154  vfs->close_read_file(ifile);
155  }
156  }
157 }
const Filename & get_fullpath() const
Returns the fullpath that has been set.
Definition: texture.I:600
bool resolve_filename(Filename &filename, const DSearchPath &searchpath, const string &default_extension=string()) const
Searches the given search path for the filename.
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
Definition: pnmImage.h:68
void pre_txa_file()
Updates any internal state prior to reading the .txa file.
This is the main engine behind egg-palettize.
Definition: palettizer.h:43
This is a convenience class to specialize ConfigVariable as a Filename type.
A hierarchy of directories and files that appears to be one continuous file system, even though the files may originate from several different sources that may not be related to the actual OS&#39;s file system.
istream * open_read_file(const Filename &filename, bool auto_unwrap) const
Convenience function; returns a newly allocated istream if the file exists and can be read...
SourceTextureImage * get_source(const Filename &filename, const Filename &alpha_filename, int alpha_file_channel)
Returns the SourceTextureImage corresponding to the given filename(s).
Defines a texture map that may be applied to geometry.
Definition: eggTexture.h:33
int get_y_size() const
Returns the size of the image file in pixels in the Y direction.
Definition: imageFile.cxx:106
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
void set_text()
Indicates that the filename represents a text file.
Definition: filename.I:507
bool store(PNMImage &pnmimage) const
Saves the texture to the indicated PNMImage, but does not write it to disk.
Definition: texture.I:472
const Filename & get_alpha_fullpath() const
Returns the alpha_fullpath that has been set.
Definition: texture.I:626
void register_filter(TexturePoolFilter *filter)
Records a TexturePoolFilter object that may operate on texture images as they are loaded from disk...
Definition: texturePool.cxx:74
static void close_read_file(istream *stream)
Closes a file opened by a previous call to open_read_file().
void quick_filter_from(const PNMImage &copy, int xborder=0, int yborder=0)
Resizes from the given image, with a fixed radius of 0.5.
This is an abstract base class, a placeholder for any number of different classes that may wish to im...
Definition: txaFileFilter.h:46
const TextureProperties & get_properties() const
Returns the grouping properties of the image.
Definition: imageFile.cxx:140
bool exists(const Filename &filename) const
Convenience function; returns true if the named file exists.
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...
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
int get_x_size() const
Returns the size of the image file in pixels in the X direction.
Definition: imageFile.cxx:93
xelval get_maxval() const
Returns the maximum channel value allowable for any pixel in this image; for instance, 255 for a typical 8-bit-per-channel image.
void set_header(const PNMImageHeader &header)
Sets the header information associated with this image, as if it were loaded from the disk...
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
int get_num_channels() const
Returns the number of channels of the image.
Definition: imageFile.cxx:130
void set_aux_data(const string &key, TypedReferenceCount *aux_data)
Records an arbitrary object in the Texture, associated with a specified key.
Definition: texture.cxx:611
This is a texture image reference as it appears in an egg file: the source image of the texture...
string get_basename_wo_extension() const
Returns the basename part of the filename, without the file extension.
Definition: filename.I:460
static TexturePool * get_global_ptr()
Initializes and/or returns the global pointer to the one TexturePool object in the system...
bool load(const PNMImage &pnmimage, const LoaderOptions &options=LoaderOptions())
Replaces the texture with the indicated image.
Definition: texture.I:382
void set_source_image(const PNMImage &image)
Accepts the indicated source image as if it had been read from disk.
bool read(istream &in, const string &filename)
Reads the indicated stream, and returns true if successful, or false if there is an error...
Definition: txaFile.cxx:40
This represents a single source texture that is referenced by one or more egg files.
Definition: textureImage.h:51
void post_txa_file()
Once the .txa file has been read and the TextureImage matched against it, considers applying the requ...
This is the preferred interface for loading textures from image files.
Definition: texturePool.h:40
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This represents the .txa file (usually textures.txa) that contains the user instructions for resizing...
Definition: txaFile.h:33
bool match_texture(TextureImage *texture) const
Searches for a matching line in the .txa file for the given texture and applies its specifications...
Definition: txaFile.cxx:158
const Filename & get_filename() const
Returns the filename that has been set.
Definition: texture.I:549
This is the set of characteristics of a texture that, if different from another texture, prevent the two textures from sharing a PaletteImage.