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