From 0a29169b5c462b0a84405259e4e6581f6b992148 Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 7 Dec 2000 04:21:56 +0000 Subject: [PATCH] *** empty log message *** --- pandatool/src/egg-palettize-old/Sources.pp | 38 +++++------ pandatool/src/egg-palettize/Sources.pp | 54 +++++++-------- pandatool/src/egg-palettize/eggFile.cxx | 21 +++++- pandatool/src/egg-palettize/eggFile.h | 1 + pandatool/src/egg-palettize/eggPalettize.cxx | 4 +- .../src/egg-palettize/filenameUnifier.cxx | 3 + pandatool/src/egg-palettize/palettizer.cxx | 20 +++++- pandatool/src/egg-palettize/palettizer.h | 4 +- pandatool/src/egg-palettize/textureImage.cxx | 67 +++++++++++++++++-- pandatool/src/egg-palettize/textureImage.h | 4 ++ .../src/egg-palettize/texturePlacement.cxx | 13 +++- 11 files changed, 171 insertions(+), 58 deletions(-) diff --git a/pandatool/src/egg-palettize-old/Sources.pp b/pandatool/src/egg-palettize-old/Sources.pp index b0de94a466..54a0035cf6 100644 --- a/pandatool/src/egg-palettize-old/Sources.pp +++ b/pandatool/src/egg-palettize-old/Sources.pp @@ -1,22 +1,22 @@ -#begin bin_target - #define TARGET egg-palettize-new - #define LOCAL_LIBS \ - eggbase progbase - #define OTHER_LIBS \ - egg:c linmath:c putil:c express:c pnmimage:c pnmimagetypes:c \ - pandaegg:m panda:m pandaexpress:m \ - dtoolutil:c dconfig:c dtool:m pystub +// #begin bin_target +// #define TARGET egg-palettize-new +// #define LOCAL_LIBS \ +// eggbase progbase +// #define OTHER_LIBS \ +// egg:c linmath:c putil:c express:c pnmimage:c pnmimagetypes:c \ +// pandaegg:m panda:m pandaexpress:m \ +// dtoolutil:c dconfig:c dtool:m pystub - #define SOURCES \ - attribFile.cxx attribFile.h config_egg_palettize.cxx \ - eggPalettize.cxx eggPalettize.h \ - palette.cxx palette.h paletteGroup.cxx \ - paletteGroup.h pTexture.cxx pTexture.h sourceEgg.cxx \ - sourceEgg.h string_utils.cxx string_utils.h \ - textureEggRef.cxx textureEggRef.h textureOmitReason.h \ - texturePacking.cxx \ - texturePacking.h userAttribLine.cxx userAttribLine.h +// #define SOURCES \ +// attribFile.cxx attribFile.h config_egg_palettize.cxx \ +// eggPalettize.cxx eggPalettize.h \ +// palette.cxx palette.h paletteGroup.cxx \ +// paletteGroup.h pTexture.cxx pTexture.h sourceEgg.cxx \ +// sourceEgg.h string_utils.cxx string_utils.h \ +// textureEggRef.cxx textureEggRef.h textureOmitReason.h \ +// texturePacking.cxx \ +// texturePacking.h userAttribLine.cxx userAttribLine.h - #define INSTALL_HEADERS +// #define INSTALL_HEADERS -#end bin_target +// #end bin_target diff --git a/pandatool/src/egg-palettize/Sources.pp b/pandatool/src/egg-palettize/Sources.pp index 1016dd4c46..4c59f103db 100644 --- a/pandatool/src/egg-palettize/Sources.pp +++ b/pandatool/src/egg-palettize/Sources.pp @@ -1,30 +1,30 @@ -// #begin bin_target -// #define TARGET egg-palettize -// #define LOCAL_LIBS \ -// eggbase progbase -// #define OTHER_LIBS \ -// egg:c loader:c linmath:c putil:c express:c pnmimage:c pnmimagetypes:c \ -// pandaegg:m panda:m pandaexpress:m \ -// dtoolutil:c dconfig:c dtool:m pystub +#begin bin_target + #define TARGET egg-palettize + #define LOCAL_LIBS \ + eggbase progbase + #define OTHER_LIBS \ + egg:c loader:c linmath:c putil:c express:c pnmimage:c pnmimagetypes:c \ + pandaegg:m panda:m pandaexpress:m \ + dtoolutil:c dconfig:c dtool:m pystub -// #define SOURCES \ -// config_egg_palettize.cxx config_egg_palettize.h \ -// destTextureImage.cxx destTextureImage.h \ -// eggFile.cxx eggFile.h eggPalettize.cxx eggPalettize.h \ -// filenameUnifier.cxx filenameUnifier.h \ -// imageFile.cxx imageFile.h omitReason.cxx omitReason.h \ -// paletteGroup.h paletteGroup.cxx \ -// paletteGroups.h paletteGroups.cxx paletteImage.h paletteImage.cxx \ -// palettePage.cxx palettePage.h \ -// palettizer.cxx palettizer.h \ -// sourceTextureImage.cxx sourceTextureImage.h string_utils.cxx \ -// string_utils.h textureImage.cxx textureImage.h \ -// texturePlacement.cxx texturePlacement.h \ -// texturePosition.cxx texturePosition.h \ -// textureProperties.cxx textureProperties.h textureReference.cxx \ -// textureReference.h textureRequest.h textureRequest.cxx \ -// txaFile.cxx txaFile.h \ -// txaLine.cxx txaLine.h + #define SOURCES \ + config_egg_palettize.cxx config_egg_palettize.h \ + destTextureImage.cxx destTextureImage.h \ + eggFile.cxx eggFile.h eggPalettize.cxx eggPalettize.h \ + filenameUnifier.cxx filenameUnifier.h \ + imageFile.cxx imageFile.h omitReason.cxx omitReason.h \ + paletteGroup.h paletteGroup.cxx \ + paletteGroups.h paletteGroups.cxx paletteImage.h paletteImage.cxx \ + palettePage.cxx palettePage.h \ + palettizer.cxx palettizer.h \ + sourceTextureImage.cxx sourceTextureImage.h string_utils.cxx \ + string_utils.h textureImage.cxx textureImage.h \ + texturePlacement.cxx texturePlacement.h \ + texturePosition.cxx texturePosition.h \ + textureProperties.cxx textureProperties.h textureReference.cxx \ + textureReference.h textureRequest.h textureRequest.cxx \ + txaFile.cxx txaFile.h \ + txaLine.cxx txaLine.h -// #end bin_target +#end bin_target diff --git a/pandatool/src/egg-palettize/eggFile.cxx b/pandatool/src/egg-palettize/eggFile.cxx index 1f988b4a38..28745c3e14 100644 --- a/pandatool/src/egg-palettize/eggFile.cxx +++ b/pandatool/src/egg-palettize/eggFile.cxx @@ -16,6 +16,8 @@ #include #include #include +#include +#include TypeHandle EggFile::_type_handle; @@ -44,6 +46,12 @@ from_command_line(EggData *data, const Filename &source_filename, const Filename &dest_filename) { _data = data; + + // We save the current directory at the time the egg file appeared + // on the command line, so that we'll later be able to properly + // resolve external references (like textures) that might be + // relative to this directory. + _current_directory = ExecutionEnvironment::get_cwd(); _source_filename = source_filename; _dest_filename = dest_filename; @@ -367,6 +375,14 @@ read_egg() { return false; } + // We also want to search for filenames based on our current + // directory from which we originally loaded the egg file. This is + // important because it's possible the egg file referenced some + // textures or something relative to that directory. + DSearchPath dir; + dir.append_directory(_current_directory); + data->resolve_filenames(dir); + if (!data->resolve_externals()) { // Failure reading an external. delete data; @@ -390,7 +406,8 @@ write_egg() { nassertr(!_dest_filename.empty(), false); _dest_filename.make_dir(); - nout << "Writing " << _dest_filename << "\n"; + nout << "Writing " << FilenameUnifier::make_user_filename(_dest_filename) + << "\n"; if (!_data->write_egg(_dest_filename)) { // Some error while writing. Most unusual. _is_stale = true; @@ -464,6 +481,7 @@ write_datagram(BamWriter *writer, Datagram &datagram) { // We don't write out _data; that needs to be reread each session. + datagram.add_string(FilenameUnifier::make_bam_filename(_current_directory)); datagram.add_string(FilenameUnifier::make_bam_filename(_source_filename)); datagram.add_string(FilenameUnifier::make_bam_filename(_dest_filename)); @@ -545,6 +563,7 @@ make_EggFile(const FactoryParams ¶ms) { void EggFile:: fillin(DatagramIterator &scan, BamReader *manager) { set_name(scan.get_string()); + _current_directory = FilenameUnifier::get_bam_filename(scan.get_string()); _source_filename = FilenameUnifier::get_bam_filename(scan.get_string()); _dest_filename = FilenameUnifier::get_bam_filename(scan.get_string()); diff --git a/pandatool/src/egg-palettize/eggFile.h b/pandatool/src/egg-palettize/eggFile.h index 3b516efca3..0349e61a78 100644 --- a/pandatool/src/egg-palettize/eggFile.h +++ b/pandatool/src/egg-palettize/eggFile.h @@ -67,6 +67,7 @@ public: private: EggData *_data; + Filename _current_directory; Filename _source_filename; Filename _dest_filename; diff --git a/pandatool/src/egg-palettize/eggPalettize.cxx b/pandatool/src/egg-palettize/eggPalettize.cxx index 938f43b79b..b5215430fd 100644 --- a/pandatool/src/egg-palettize/eggPalettize.cxx +++ b/pandatool/src/egg-palettize/eggPalettize.cxx @@ -335,9 +335,9 @@ run() { } if (_all_textures) { - pal->process_all(); + pal->process_all(_redo_all); } else { - pal->process_command_line_eggs(); + pal->process_command_line_eggs(_redo_all); } if (_force_optimal) { diff --git a/pandatool/src/egg-palettize/filenameUnifier.cxx b/pandatool/src/egg-palettize/filenameUnifier.cxx index 8b046a28d5..72dd7c8f25 100644 --- a/pandatool/src/egg-palettize/filenameUnifier.cxx +++ b/pandatool/src/egg-palettize/filenameUnifier.cxx @@ -25,6 +25,9 @@ void FilenameUnifier:: set_txa_filename(const Filename &txa_filename) { _txa_filename = txa_filename; _txa_dir = txa_filename.get_dirname(); + if (_txa_dir.empty()) { + _txa_dir = "."; + } _txa_dir.make_canonical(); } diff --git a/pandatool/src/egg-palettize/palettizer.cxx b/pandatool/src/egg-palettize/palettizer.cxx index 4232725d03..7f7d4e35e8 100644 --- a/pandatool/src/egg-palettize/palettizer.cxx +++ b/pandatool/src/egg-palettize/palettizer.cxx @@ -193,9 +193,13 @@ read_txa_file(const Filename &txa_filename) { // Description: Processes all the textures named in the // _command_line_eggs, placing them on the appropriate // palettes or whatever needs to be done with them. +// +// If force_texture_read is true, it forces each texture +// image file to be read (and thus legitimately checked +// for grayscaleness etc.) before placing. //////////////////////////////////////////////////////////////////// void Palettizer:: -process_command_line_eggs() { +process_command_line_eggs(bool force_texture_read) { _command_line_textures.clear(); // Start by scanning all the egg files we read up on the command @@ -222,6 +226,10 @@ process_command_line_eggs() { ++ti) { TextureImage *texture = *ti; + if (force_texture_read) { + texture->read_source_image(); + } + texture->pre_txa_file(); _txa_file.match_texture(texture); texture->post_txa_file(); @@ -273,9 +281,13 @@ process_command_line_eggs() { // Function: Palettizer::process_all // Access: Public // Description: Reprocesses all textures known. +// +// If force_texture_read is true, it forces each texture +// image file to be read (and thus legitimately checked +// for grayscaleness etc.) before placing. //////////////////////////////////////////////////////////////////// void Palettizer:: -process_all() { +process_all(bool force_texture_read) { // If there *were* any egg files on the command line, deal with // them. CommandLineEggs::const_iterator ei; @@ -304,6 +316,10 @@ process_all() { for (ti = _textures.begin(); ti != _textures.end(); ++ti) { TextureImage *texture = (*ti).second; + if (force_texture_read) { + texture->read_source_image(); + } + texture->pre_txa_file(); _txa_file.match_texture(texture); texture->post_txa_file(); diff --git a/pandatool/src/egg-palettize/palettizer.h b/pandatool/src/egg-palettize/palettizer.h index 59526b5e25..0f4c9cbcff 100644 --- a/pandatool/src/egg-palettize/palettizer.h +++ b/pandatool/src/egg-palettize/palettizer.h @@ -35,8 +35,8 @@ public: void report_pi() const; void read_txa_file(const Filename &txa_filename); - void process_command_line_eggs(); - void process_all(); + void process_command_line_eggs(bool force_texture_read); + void process_all(bool force_texture_read); void optimal_resize(); void reset_images(); void generate_images(bool redo_all); diff --git a/pandatool/src/egg-palettize/textureImage.cxx b/pandatool/src/egg-palettize/textureImage.cxx index b25f6c6ce4..2b34a51d8c 100644 --- a/pandatool/src/egg-palettize/textureImage.cxx +++ b/pandatool/src/egg-palettize/textureImage.cxx @@ -29,6 +29,9 @@ TextureImage() { _read_source_image = false; _got_dest_image = false; _is_surprise = true; + _ever_read_image = false; + _forced_grayscale = false; + _forced_unalpha = false; } //////////////////////////////////////////////////////////////////// @@ -55,7 +58,13 @@ note_egg_file(EggFile *egg_file) { //////////////////////////////////////////////////////////////////// void TextureImage:: assign_groups() { - nassertv(!_egg_files.empty()); + if (_egg_files.empty()) { + // If we're not referenced by any egg files any more, assign us to + // no groups. + PaletteGroups empty; + assign_to_groups(empty); + return; + } PaletteGroups definitely_in; @@ -265,12 +274,12 @@ post_txa_file() { } else { // If we didn't request a particular number of channels, examine // the image to determine if we can downgrade it, for instance - // from color to grayscale. + // from color to grayscale. if (_properties._got_num_channels && (_properties._num_channels == 3 || _properties._num_channels == 4)) { consider_grayscale(); } - + // Also consider downgrading from alpha to non-alpha. if (_properties._got_num_channels && (_properties._num_channels == 2 || _properties._num_channels == 4)) { @@ -504,6 +513,7 @@ read_source_image() { source->read(_source_image); } _read_source_image = true; + _ever_read_image = true; } return _source_image; @@ -570,7 +580,24 @@ write_source_pathnames(ostream &out, int indent_level) const { void TextureImage:: write_scale_info(ostream &out, int indent_level) { SourceTextureImage *source = get_preferred_source(); - indent(out, indent_level) << get_name() << " orig "; + indent(out, indent_level) << get_name(); + + // Write the list of groups we're placed in. + if (_placement.empty()) { + out << " (not used)"; + } else { + Placement::const_iterator pi; + pi = _placement.begin(); + out << " (" << (*pi).second->get_group()->get_name(); + ++pi; + while (pi != _placement.end()) { + out << " " << (*pi).second->get_group()->get_name(); + ++pi; + } + out << ")"; + } + + out << " orig "; if (source == (SourceTextureImage *)NULL || !source->is_size_known()) { @@ -694,6 +721,19 @@ assign_to_groups(const PaletteGroups &groups) { //////////////////////////////////////////////////////////////////// void TextureImage:: consider_grayscale() { + // Since this isn't likely to change for a particular texture after + // its creation, we save a bit of time by not performing this check + // unless this is the first time we've ever seen this texture. This + // will save us from having to load the texture images time we look + // at them. On the other hand, if we've already loaded up the + // image, then go ahead. + if (!_read_source_image && _ever_read_image) { + if (_forced_grayscale) { + _properties._num_channels -= 2; + } + return; + } + const PNMImage &source = read_source_image(); if (!source.is_valid()) { return; @@ -704,6 +744,7 @@ consider_grayscale() { const xel &v = source.get_xel_val(x, y); if (PPM_GETR(v) != PPM_GETG(v) || PPM_GETR(v) != PPM_GETB(v)) { // Here's a colored pixel. We can't go grayscale. + _forced_grayscale = false; return; } } @@ -711,6 +752,7 @@ consider_grayscale() { // All pixels in the image were grayscale! _properties._num_channels -= 2; + _forced_grayscale = true; } //////////////////////////////////////////////////////////////////// @@ -723,6 +765,15 @@ consider_grayscale() { //////////////////////////////////////////////////////////////////// void TextureImage:: consider_unalpha() { + // As above, we don't bother doing this if we've already done this + // in a previous session. + if (!_read_source_image && _ever_read_image) { + if (_forced_unalpha) { + _properties._num_channels--; + } + return; + } + const PNMImage &source = read_source_image(); if (!source.is_valid()) { return; @@ -736,6 +787,7 @@ consider_unalpha() { for (int x = 0; x < source.get_x_size(); x++) { if (source.get_alpha_val(x, y) != source.get_maxval()) { // Here's a non-white pixel; the alpha channel is meaningful. + _forced_unalpha = false; return; } } @@ -743,6 +795,7 @@ consider_unalpha() { // All alpha pixels in the image were white! _properties._num_channels--; + _forced_unalpha = true; } //////////////////////////////////////////////////////////////////// @@ -860,6 +913,9 @@ write_datagram(BamWriter *writer, Datagram &datagram) { // session. datagram.add_bool(_is_surprise); + datagram.add_bool(_ever_read_image); + datagram.add_bool(_forced_grayscale); + datagram.add_bool(_forced_unalpha); // We don't write out _explicitly_assigned_groups; this is re-read // from the .txa file each time. @@ -971,6 +1027,9 @@ fillin(DatagramIterator &scan, BamReader *manager) { set_name(scan.get_string()); _is_surprise = scan.get_bool(); + _ever_read_image = scan.get_bool(); + _forced_grayscale = scan.get_bool(); + _forced_unalpha = scan.get_bool(); _actual_assigned_groups.fillin(scan, manager); diff --git a/pandatool/src/egg-palettize/textureImage.h b/pandatool/src/egg-palettize/textureImage.h index 4f735739a1..d7a0509379 100644 --- a/pandatool/src/egg-palettize/textureImage.h +++ b/pandatool/src/egg-palettize/textureImage.h @@ -92,6 +92,10 @@ private: TextureProperties _pre_txa_properties; SourceTextureImage *_preferred_source; bool _is_surprise; + + bool _ever_read_image; + bool _forced_grayscale; + bool _forced_unalpha; PaletteGroups _explicitly_assigned_groups; PaletteGroups _actual_assigned_groups; diff --git a/pandatool/src/egg-palettize/texturePlacement.cxx b/pandatool/src/egg-palettize/texturePlacement.cxx index 11f87a8d07..ee5ac12461 100644 --- a/pandatool/src/egg-palettize/texturePlacement.cxx +++ b/pandatool/src/egg-palettize/texturePlacement.cxx @@ -123,6 +123,9 @@ get_group() const { void TexturePlacement:: add_egg(TextureReference *reference) { reference->mark_egg_stale(); + + // Turns out that turning these off is a bad idea, because it may + // make us forget the size information halfway through processing. /* _has_uvs = false; _size_known = false; @@ -207,11 +210,17 @@ determine_size() { return false; } + // This seems to be unnecessary (because of omit_solitary() and + // not_solitary()), and in fact bitches the logic in omit_solitary() + // and not_solitary() so that we call mark_egg_stale() + // unnecessarily. + /* if (_omit_reason == OR_solitary) { // If the texture was previously 'omitted' for being solitary, we // give it a second chance now. _omit_reason = OR_none; } + */ // Determine the actual minmax of the UV's in use, as well as // whether we should wrap or clamp. @@ -552,8 +561,10 @@ force_replace() { _image->unplace(this); _image = (PaletteImage *)NULL; } + if (_omit_reason == OR_none) { + mark_eggs_stale(); + } _omit_reason = OR_working; - mark_eggs_stale(); } ////////////////////////////////////////////////////////////////////