00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "txaFileFilter.h"
00016 #include "palettizer.h"
00017 #include "txaFile.h"
00018 #include "textureImage.h"
00019 #include "sourceTextureImage.h"
00020 #include "texturePool.h"
00021 #include "dconfig.h"
00022 #include "configVariableFilename.h"
00023 #include "virtualFileSystem.h"
00024 #include "config_util.h"
00025
00026 NotifyCategoryDeclNoExport(txafile);
00027 NotifyCategoryDef(txafile, "");
00028
00029
00030
00031 Configure(config_txaFileFilter);
00032 ConfigureFn(config_txaFileFilter) {
00033 TxaFileFilter::init_type();
00034 TexturePool *pool = TexturePool::get_global_ptr();
00035 pool->register_filter(new TxaFileFilter);
00036 }
00037
00038 TypeHandle TxaFileFilter::_type_handle;
00039 TxaFile *TxaFileFilter::_txa_file;
00040 bool TxaFileFilter::_got_txa_file;
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 PT(Texture) TxaFileFilter::
00054 post_load(Texture *tex) {
00055 if (!_got_txa_file) {
00056 read_txa_file();
00057 }
00058
00059 TextureImage tex_image;
00060 string name = tex->get_filename().get_basename_wo_extension();
00061 tex_image.set_name(name);
00062
00063 SourceTextureImage *source = tex_image.get_source
00064 (tex->get_fullpath(), tex->get_alpha_fullpath(), 0);
00065 PNMImage pnm_image;
00066 tex->store(pnm_image);
00067 source->set_header(pnm_image);
00068 tex_image.set_source_image(pnm_image);
00069
00070 tex_image.pre_txa_file();
00071
00072 bool matched = _txa_file->match_texture(&tex_image);
00073 if (txafile_cat.is_debug()) {
00074 if (!matched) {
00075 txafile_cat.debug()
00076 << "Not matched: " << name << "\n";
00077 } else {
00078 txafile_cat.debug()
00079 << "Matched: " << name << "\n";
00080 }
00081 }
00082
00083 tex_image.post_txa_file();
00084
00085 PNMImage dest(tex_image.get_x_size(),
00086 tex_image.get_y_size(),
00087 tex_image.get_num_channels(),
00088 pnm_image.get_maxval());
00089 dest.quick_filter_from(pnm_image);
00090
00091 tex->load(dest);
00092
00093
00094
00095
00096 PT_EggTexture egg_tex = new EggTexture(tex->get_name(), tex->get_fullpath());
00097 const TextureProperties &props = tex_image.get_properties();
00098
00099 egg_tex->set_alpha_mode(tex_image.get_alpha_mode());
00100 egg_tex->set_format(props._format);
00101 egg_tex->set_minfilter(props._minfilter);
00102 egg_tex->set_minfilter(props._magfilter);
00103 egg_tex->set_anisotropic_degree(props._anisotropic_degree);
00104
00105 tex->set_aux_data("egg", egg_tex);
00106
00107 return tex;
00108 }
00109
00110
00111
00112
00113
00114
00115
00116 void TxaFileFilter::
00117 read_txa_file() {
00118 VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
00119
00120
00121
00122 if (pal == (Palettizer *)NULL) {
00123 pal = new Palettizer;
00124 }
00125
00126 _txa_file = new TxaFile;
00127 _got_txa_file = true;
00128
00129 ConfigVariableFilename txa_file
00130 ("txa-file", Filename("textures.txa"),
00131 PRC_DESC("Specify the name of the txa file to load when the txafile texture filter"
00132 "is in effect."));
00133
00134 Filename filename = txa_file;
00135 vfs->resolve_filename(filename, get_model_path());
00136
00137 if (!vfs->exists(filename)) {
00138 txafile_cat.warning()
00139 << "Filename " << filename << " not found.\n";
00140 } else {
00141 filename.set_text();
00142 istream *ifile = vfs->open_read_file(filename, true);
00143 if (ifile == (istream *)NULL) {
00144 txafile_cat.warning()
00145 << "Filename " << filename << " cannot be read.\n";
00146 } else {
00147 if (!_txa_file->read(*ifile, filename)) {
00148 txafile_cat.warning()
00149 << "Syntax errors in " << filename << "\n";
00150 } else {
00151 txafile_cat.info()
00152 << "Read " << filename << "\n";
00153 }
00154 vfs->close_read_file(ifile);
00155 }
00156 }
00157 }