From cf75acb3e128dc6883b41fe5ab92ed50175ff68c Mon Sep 17 00:00:00 2001 From: David Rose Date: Tue, 5 Dec 2000 16:08:30 +0000 Subject: [PATCH] *** empty log message *** --- pandatool/src/egg-palettize/eggPalettize.cxx | 95 +-------- pandatool/src/egg-palettize/eggPalettize.h | 23 -- pandatool/src/egg-palettize/palettizer.cxx | 14 +- pandatool/src/egg-palettize/palettizer.h | 1 + pandatool/src/egg-palettize/string_utils.cxx | 69 ++++++ pandatool/src/egg-palettize/string_utils.h | 5 + pandatool/src/egg-palettize/txaFile.cxx | 208 ++++++++++++++++++- pandatool/src/egg-palettize/txaFile.h | 6 + 8 files changed, 299 insertions(+), 122 deletions(-) diff --git a/pandatool/src/egg-palettize/eggPalettize.cxx b/pandatool/src/egg-palettize/eggPalettize.cxx index 8539facf4d..8824b8cd79 100644 --- a/pandatool/src/egg-palettize/eggPalettize.cxx +++ b/pandatool/src/egg-palettize/eggPalettize.cxx @@ -8,8 +8,6 @@ #include "eggFile.h" #include "string_utils.h" -#include -#include #include #include @@ -122,55 +120,7 @@ EggPalettize() : EggMultiFilter(true) { "line. Presumably a future make pass will cause them to be run " "through egg-palettize again.", &EggPalettize::dispatch_none, &_touch_eggs); - add_option - ("C", "", 0, - "Aggressively keep the map directory clean by deleting unused " - "textures from previous passes. This will remain in effect across " - "future sessions until -offC is specified.", - &EggPalettize::dispatch_none, &_aggressively_clean_mapdir); - add_option - ("offC", "", 0, - "Turn off the aggressive cleaning specified by a past -C.", - &EggPalettize::dispatch_none, &_off_aggressively_clean_mapdir); - add_option - ("2", "", 0, - "Force textures that have been left out of the palette to a size " - "which is an integer power of 2. They will be scaled down to " - "achieve this. This will remain in effect across future sessions " - "until -off2 is specified.", - &EggPalettize::dispatch_none, &_force_power_2); - add_option - ("off2", "", 0, - "Turn off the power-of-2 scaling specified by a past -2.", - &EggPalettize::dispatch_none, &_off_force_power_2); - add_option - ("type", "imagetype[,alphatype]", 0, - "Specify the type of image file to output. All image files, whether " - "palettes or unplaced textures, will be converted to files of this " - "type. If the optional alpha type is specified, then an alpha channel, " - "if present, will be written as a separate file of the indicated " - "type--useful if the primary image type does not support alpha. " - "Use '-type list' to show the available image types.", - &EggPalettize::dispatch_string, &_got_image_type, &_image_type); - add_option - ("m", "margin", 0, - "Specify the default margin size.", - &EggPalettize::dispatch_int, &_got_margin, &_margin); - add_option - ("r", "percent", 0, - "A repeating texture may still be palettized if it does not repeat " - "very many times, by adding multiple adjacent copies of the " - "texture to the palette. This parameter specifies the cutoff " - "threshold for this. This is the maximum " - "percentage a texture will be expanded to palettize a repeating " - "texture. If this is set to 100, no repeating textures will be " - "palettized; if this is set to 200, a texture that repeats twice " - "will be palettized by adding it to the palette twice.", - &EggPalettize::dispatch_double, &_got_repeat_threshold, &_repeat_threshold); - add_option - ("P", "x,y", 0, - "Specify the default palette size.", - &EggPalettize::dispatch_int_pair, &_got_palette_size, _pal_size); + add_option ("nolock", "", 0, "Don't attempt to lock the .pi file before rewriting it. Use " @@ -184,8 +134,6 @@ EggPalettize() : EggMultiFilter(true) { &EggPalettize::dispatch_none, &_describe_input_file); _txa_filename = "textures.txa"; - _color_type = (PNMFileType *)NULL; - _alpha_type = (PNMFileType *)NULL; } @@ -204,16 +152,6 @@ handle_args(ProgramBase::Args &args) { exit(1); } - if (_got_image_type) { - if (_image_type == "list" || - !parse_image_type_request(_image_type, _color_type, _alpha_type)) { - nout << "\nKnown image types are:\n"; - PNMFileTypeRegistry::get_ptr()->write_types(nout, 2); - nout << "\n"; - exit(1); - } - } - return EggMultiFilter::handle_args(args); } @@ -347,37 +285,6 @@ run() { exit(0); } - if (_got_image_type) { - pal->_color_type = _color_type; - pal->_alpha_type = _alpha_type; - } - - if (_got_margin) { - pal->_margin = _margin; - } - - if (_got_repeat_threshold) { - pal->_repeat_threshold = _repeat_threshold; - } - - if (_got_palette_size) { - pal->_pal_x_size = _pal_size[0]; - pal->_pal_y_size = _pal_size[1]; - } - - if (_force_power_2) { - pal->_force_power_2 = true; - } - if (_off_force_power_2) { - pal->_force_power_2 = false; - } - if (_aggressively_clean_mapdir) { - pal->_aggressively_clean_mapdir = true; - } - if (_off_aggressively_clean_mapdir) { - pal->_aggressively_clean_mapdir = false; - } - if (_got_default_groupname) { pal->_default_groupname = _default_groupname; } else { diff --git a/pandatool/src/egg-palettize/eggPalettize.h b/pandatool/src/egg-palettize/eggPalettize.h index 791a68f6c2..b7160d835f 100644 --- a/pandatool/src/egg-palettize/eggPalettize.h +++ b/pandatool/src/egg-palettize/eggPalettize.h @@ -8,17 +8,8 @@ #include -#include "txaFile.h" - #include -#include - -class PNMFileType; -class EggFile; -class PaletteGroup; -class TextureImage; - //////////////////////////////////////////////////////////////////// // Class : EggPalettize // Description : This is the program wrapper for egg-palettize, but it @@ -46,20 +37,6 @@ public: bool _got_default_groupname; string _default_groupdir; bool _got_default_groupdir; - int _pal_size[2]; - bool _got_palette_size; - int _margin; - bool _got_margin; - double _repeat_threshold; - bool _got_repeat_threshold; - bool _force_power_2; - bool _off_force_power_2; - bool _aggressively_clean_mapdir; - bool _off_aggressively_clean_mapdir; - string _image_type; - bool _got_image_type; - PNMFileType *_color_type; - PNMFileType *_alpha_type; private: // The following values control behavior specific to this session. diff --git a/pandatool/src/egg-palettize/palettizer.cxx b/pandatool/src/egg-palettize/palettizer.cxx index 89a61d4678..84cb83dd30 100644 --- a/pandatool/src/egg-palettize/palettizer.cxx +++ b/pandatool/src/egg-palettize/palettizer.cxx @@ -38,11 +38,11 @@ Palettizer() { _margin = 2; _repeat_threshold = 250.0; - _aggressively_clean_mapdir = false; - _force_power_2 = false; + _aggressively_clean_mapdir = true; + _force_power_2 = true; _color_type = PNMFileTypeRegistry::get_ptr()->get_type_from_extension("rgb"); _alpha_type = (PNMFileType *)NULL; - _pal_x_size = _pal_y_size = 256; + _pal_x_size = _pal_y_size = 512; _round_uvs = true; _round_unit = 0.1; @@ -123,7 +123,7 @@ report_pi() const { cout << "\n"; } - cerr << "textures\n"; + cout << "textures\n"; for (ti = _textures.begin(); ti != _textures.end(); ++ti) { TextureImage *texture = (*ti).second; texture->write_scale_info(cout, 2); @@ -139,6 +139,12 @@ report_pi() const { //////////////////////////////////////////////////////////////////// void Palettizer:: run(const TxaFile &txa_file) { + if (_color_type == (PNMFileType *)NULL) { + nout << "No valid output image file type available, cannot run.\n" + << "Use :imagetype command in .txa file.\n"; + exit(1); + } + set command_line_textures; // Start by scanning all the egg files we read up on the command diff --git a/pandatool/src/egg-palettize/palettizer.h b/pandatool/src/egg-palettize/palettizer.h index f57e30eddb..b22d1d54e1 100644 --- a/pandatool/src/egg-palettize/palettizer.h +++ b/pandatool/src/egg-palettize/palettizer.h @@ -121,6 +121,7 @@ private: static TypeHandle _type_handle; friend class EggPalettize; + friend class TxaLine; }; extern Palettizer *pal; diff --git a/pandatool/src/egg-palettize/string_utils.cxx b/pandatool/src/egg-palettize/string_utils.cxx index ad4fdb6971..4f2cddc8b7 100644 --- a/pandatool/src/egg-palettize/string_utils.cxx +++ b/pandatool/src/egg-palettize/string_utils.cxx @@ -30,6 +30,75 @@ trim_right(const string &str) { return str.substr(begin, end - begin); } +//////////////////////////////////////////////////////////////////// +// Function: string_to_int +// Description: A string-interface wrapper around the C library +// strtol(). This parses the ASCII representation of an +// integer, and then sets tail to everything that +// follows the first valid integer read. If, on exit, +// str == tail, there was no valid integer in the +// source string; if !tail.empty(), there was garbage +// after the integer. +// +// It is legal if str and tail refer to the same string. +//////////////////////////////////////////////////////////////////// +int +string_to_int(const string &str, string &tail) { + const char *nptr = str.c_str(); + char *endptr; + int result = strtol(nptr, &endptr, 10); + tail = endptr; + return result; +} + +//////////////////////////////////////////////////////////////////// +// Function: string_to_int +// Description: Another flavor of string_to_int(), this one returns +// true if the string is a perfectly valid integer (and +// sets result to that value), or false otherwise. +//////////////////////////////////////////////////////////////////// +bool +string_to_int(const string &str, int &result) { + string tail; + result = string_to_int(str, tail); + return tail.empty(); +} + +//////////////////////////////////////////////////////////////////// +// Function: string_to_double +// Description: A string-interface wrapper around the C library +// strtol(). This parses the ASCII representation of an +// floating-point number, and then sets tail to +// everything that follows the first valid integer read. +// If, on exit, str == tail, there was no valid integer +// in the source string; if !tail.empty(), there was +// garbage after the number. +// +// It is legal if str and tail refer to the same string. +//////////////////////////////////////////////////////////////////// +double +string_to_double(const string &str, string &tail) { + const char *nptr = str.c_str(); + char *endptr; + double result = strtod(nptr, &endptr); + tail = endptr; + return result; +} + +//////////////////////////////////////////////////////////////////// +// Function: string_to_double +// Description: Another flavor of string_to_double(), this one +// returns true if the string is a perfectly valid +// number (and sets result to that value), or false +// otherwise. +//////////////////////////////////////////////////////////////////// +bool +string_to_double(const string &str, double &result) { + string tail; + result = string_to_double(str, tail); + return tail.empty(); +} + void extract_words(const string &str, vector_string &words) { size_t pos = 0; diff --git a/pandatool/src/egg-palettize/string_utils.h b/pandatool/src/egg-palettize/string_utils.h index af6e24da45..240ae99455 100644 --- a/pandatool/src/egg-palettize/string_utils.h +++ b/pandatool/src/egg-palettize/string_utils.h @@ -14,6 +14,11 @@ class PNMFileType; string trim_left(const string &str); string trim_right(const string &str); +int string_to_int(const string &str, string &tail); +bool string_to_int(const string &str, int &result); +double string_to_double(const string &str, string &tail); +bool string_to_double(const string &str, double &result); + void extract_words(const string &str, vector_string &words); void extract_param_value(const string &str, string ¶m, string &value); diff --git a/pandatool/src/egg-palettize/txaFile.cxx b/pandatool/src/egg-palettize/txaFile.cxx index 0166d78896..b55d47e0b5 100644 --- a/pandatool/src/egg-palettize/txaFile.cxx +++ b/pandatool/src/egg-palettize/txaFile.cxx @@ -10,6 +10,7 @@ #include "textureImage.h" #include +#include //////////////////////////////////////////////////////////////////// // Function: TxaFile::Constructor @@ -58,8 +59,26 @@ read(Filename filename) { if (words[0] == ":group") { okflag = parse_group_line(words); + } else if (words[0] == ":palette") { + okflag = parse_palette_line(words); + + } else if (words[0] == ":margin") { + okflag = parse_margin_line(words); + + } else if (words[0] == ":repeat") { + okflag = parse_repeat_line(words); + + } else if (words[0] == ":imagetype") { + okflag = parse_imagetype_line(words); + + } else if (words[0] == ":round") { + okflag = parse_round_line(words); + + } else if (words[0] == ":remap") { + okflag = parse_remap_line(words); + } else { - nout << "Invalid keyword: " << line << "\n"; + nout << "Invalid keyword " << words[0] << "\n"; okflag = false; } @@ -200,3 +219,190 @@ parse_group_line(const vector_string &words) { return true; } + +//////////////////////////////////////////////////////////////////// +// Function: TxaFile::parse_palette_line +// Access: Private +// Description: Handles the line in a .txa file that begins with the +// keyword ":palette" and indicates the appropriate size +// for the palette images. +//////////////////////////////////////////////////////////////////// +bool TxaFile:: +parse_palette_line(const vector_string &words) { + if (words.size() != 3) { + nout << "Exactly two parameters required for :palette, the x and y " + << "size of the palette images to generate.\n"; + return false; + } + + if (!string_to_int(words[1], pal->_pal_x_size) || + !string_to_int(words[2], pal->_pal_y_size)) { + nout << "Invalid palette size: " << words[1] << " " << words[2] << "\n"; + return false; + } + + if (pal->_pal_x_size <= 0 || pal->_pal_y_size <= 0) { + nout << "Invalid palette size: " << pal->_pal_x_size + << " " << pal->_pal_y_size << "\n"; + return false; + } + + return true; +} + +//////////////////////////////////////////////////////////////////// +// Function: TxaFile::parse_margin_line +// Access: Private +// Description: Handles the line in a .txa file that begins with the +// keyword ":margin" and indicates the default margin +// size. +//////////////////////////////////////////////////////////////////// +bool TxaFile:: +parse_margin_line(const vector_string &words) { + if (words.size() != 2) { + nout << "Exactly one parameter required for :margin, the " + << "size of the default margin to apply.\n"; + return false; + } + + if (!string_to_int(words[1], pal->_margin)) { + nout << "Invalid margin: " << words[1] << "\n"; + return false; + } + + if (pal->_margin < 0) { + nout << "Invalid margin: " << pal->_margin << "\n"; + return false; + } + + return true; +} + +//////////////////////////////////////////////////////////////////// +// Function: TxaFile::parse_repeat_line +// Access: Private +// Description: Handles the line in a .txa file that begins with the +// keyword ":repeat" and indicates the default repeat +// threshold. +//////////////////////////////////////////////////////////////////// +bool TxaFile:: +parse_repeat_line(const vector_string &words) { + if (words.size() != 2) { + nout << "Exactly one parameter required for :repeat, the " + << "percentage for the default repeat threshold.\n"; + return false; + } + + string tail; + pal->_repeat_threshold = string_to_double(words[1], tail); + if (!(tail.empty() || tail == "%")) { + // This is an invalid number. + nout << "Invalid repeat threshold: " << words[1] << "\n"; + return false; + } + + if (pal->_repeat_threshold <= 0.0) { + nout << "Invalid repeat threshold: " << pal->_repeat_threshold << "\n"; + return false; + } + + return true; +} + +//////////////////////////////////////////////////////////////////// +// Function: TxaFile::parse_imagetype_line +// Access: Private +// Description: Handles the line in a .txa file that begins with the +// keyword ":imagetype" and indicates the default image +// file type to convert palettes and textures to. +//////////////////////////////////////////////////////////////////// +bool TxaFile:: +parse_imagetype_line(const vector_string &words) { + if (words.size() != 2) { + nout << "Exactly one parameter required for :imagetype.\n"; + return false; + } + const string &imagetype = words[1]; + if (!parse_image_type_request(imagetype, pal->_color_type, pal->_alpha_type)) { + nout << "\nKnown image types are:\n"; + PNMFileTypeRegistry::get_ptr()->write_types(nout, 2); + nout << "\n"; + return false; + } + + return true; +} + +//////////////////////////////////////////////////////////////////// +// Function: TxaFile::parse_round_line +// Access: Private +// Description: Handles the line in a .txa file that begins with the +// keyword ":round" and indicates how or whether to +// round up UV minmax boxes. +//////////////////////////////////////////////////////////////////// +bool TxaFile:: +parse_round_line(const vector_string &words) { + if (words.size() == 2) { + if (words[1] == "no") { + pal->_round_uvs = false; + return true; + } else { + nout << "Invalid round keyword: " << words[1] << ".\n" + << "Expected 'no', or a round fraction and fuzz factor.\n"; + return false; + } + } + + if (words.size() != 3) { + nout << "Exactly two parameters required for :round, the fraction " + << "to round to, and the fuzz factor.\n"; + return false; + } + + if (!string_to_double(words[1], pal->_round_unit) || + !string_to_double(words[2], pal->_round_fuzz)) { + nout << "Invalid rounding: " << words[1] << " " << words[2] << "\n"; + return false; + } + + if (pal->_round_unit <= 0.0 || pal->_round_fuzz < 0.0) { + nout << "Invalid rounding: " << pal->_round_unit + << " " << pal->_round_fuzz << "\n"; + return false; + } + + pal->_round_uvs = true; + return true; +} + +//////////////////////////////////////////////////////////////////// +// Function: TxaFile::parse_remap_line +// Access: Private +// Description: Handles the line in a .txa file that begins with the +// keyword ":remap" and indicates how or whether to +// remap UV coordinates in egg files to the unit box. +//////////////////////////////////////////////////////////////////// +bool TxaFile:: +parse_remap_line(const vector_string &words) { + if (words.size() != 2) { + nout << "Exactly one parameter required for :remap.\n"; + return false; + } + + const string &keyword = words[1]; + if (keyword == "never") { + pal->_remap_uv = Palettizer::RU_never; + + } else if (keyword == "group") { + pal->_remap_uv = Palettizer::RU_group; + + } else if (keyword == "poly") { + pal->_remap_uv = Palettizer::RU_poly; + + } else { + nout << "Invalid remap keyword: " << keyword << "\n"; + return false; + } + + return true; +} diff --git a/pandatool/src/egg-palettize/txaFile.h b/pandatool/src/egg-palettize/txaFile.h index 4abadea33f..e9da116fc9 100644 --- a/pandatool/src/egg-palettize/txaFile.h +++ b/pandatool/src/egg-palettize/txaFile.h @@ -34,6 +34,12 @@ public: private: bool parse_group_line(const vector_string &words); + bool parse_palette_line(const vector_string &words); + bool parse_margin_line(const vector_string &words); + bool parse_repeat_line(const vector_string &words); + bool parse_imagetype_line(const vector_string &words); + bool parse_round_line(const vector_string &words); + bool parse_remap_line(const vector_string &words); typedef vector Lines; Lines _lines;