Browse Source

add alpha_file_channel

David Rose 23 years ago
parent
commit
f776e83c6e

+ 51 - 12
pandatool/src/egg-palettize/imageFile.cxx

@@ -21,13 +21,13 @@
 #include "filenameUnifier.h"
 #include "paletteGroup.h"
 
-#include <pnmImage.h>
-#include <pnmFileType.h>
-#include <eggTexture.h>
-#include <datagram.h>
-#include <datagramIterator.h>
-#include <bamReader.h>
-#include <bamWriter.h>
+#include "pnmImage.h"
+#include "pnmFileType.h"
+#include "eggTexture.h"
+#include "datagram.h"
+#include "datagramIterator.h"
+#include "bamReader.h"
+#include "bamWriter.h"
 
 TypeHandle ImageFile::_type_handle;
 
@@ -38,6 +38,7 @@ TypeHandle ImageFile::_type_handle;
 ////////////////////////////////////////////////////////////////////
 ImageFile::
 ImageFile() {
+  _alpha_file_channel = 0;
   _size_known = false;
   _x_size = 0;
   _y_size = 0;
@@ -251,6 +252,22 @@ get_alpha_filename() const {
   return _alpha_filename;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: ImageFile::get_alpha_file_channel
+//       Access: Public
+//  Description: Returns the particular channel number of the alpha
+//               image file from which the alpha channel should be
+//               extracted.  This is normally 0 to represent the
+//               grayscale combination of r, g, and b; or it may be a
+//               1-based channel number (for instance, 4 for the alpha
+//               channel of a 4-component image).
+////////////////////////////////////////////////////////////////////
+int ImageFile::
+get_alpha_file_channel() const {
+  return _alpha_file_channel;
+}
+
+
 ////////////////////////////////////////////////////////////////////
 //     Function: ImageFile::exists
 //       Access: Public
@@ -306,15 +323,29 @@ read(PNMImage &image) const {
     }
 
     image.add_alpha();
-    if (alpha_image.has_alpha()) {
-      for (int y = 0; y < image.get_y_size(); y++) {
-        for (int x = 0; x < image.get_x_size(); x++) {
+
+    if (_alpha_file_channel == 4 || 
+        (_alpha_file_channel == 2 && alpha_image.get_num_channels() == 2)) {
+      // Use the alpha channel.
+      for (int x = 0; x < image.get_x_size(); x++) {
+        for (int y = 0; y < image.get_y_size(); y++) {
           image.set_alpha(x, y, alpha_image.get_alpha(x, y));
         }
       }
+      
+    } else if (_alpha_file_channel >= 1 && _alpha_file_channel <= 3 &&
+               alpha_image.get_num_channels() >= 3) {
+      // Use the appropriate red, green, or blue channel.
+      for (int x = 0; x < image.get_x_size(); x++) {
+        for (int y = 0; y < image.get_y_size(); y++) {
+          image.set_alpha(x, y, alpha_image.get_channel_val(x, y, _alpha_file_channel - 1));
+        }
+      }
+      
     } else {
-      for (int y = 0; y < image.get_y_size(); y++) {
-        for (int x = 0; x < image.get_x_size(); x++) {
+      // Use the grayscale channel.
+      for (int x = 0; x < image.get_x_size(); x++) {
+        for (int y = 0; y < image.get_y_size(); y++) {
           image.set_alpha(x, y, alpha_image.get_gray(x, y));
         }
       }
@@ -408,8 +439,10 @@ update_egg_tex(EggTexture *egg_tex) const {
   if (_properties.uses_alpha() &&
       !_alpha_filename.empty()) {
     egg_tex->set_alpha_filename(FilenameUnifier::make_egg_filename(_alpha_filename));
+    egg_tex->set_alpha_file_channel(_alpha_file_channel);
   } else {
     egg_tex->clear_alpha_filename();
+    egg_tex->clear_alpha_file_channel();
   }
 
   _properties.update_egg_tex(egg_tex);
@@ -442,6 +475,7 @@ write_datagram(BamWriter *writer, Datagram &datagram) {
   _properties.write_datagram(writer, datagram);
   datagram.add_string(FilenameUnifier::make_bam_filename(_filename));
   datagram.add_string(FilenameUnifier::make_bam_filename(_alpha_filename));
+  datagram.add_uint8(_alpha_file_channel);
   datagram.add_bool(_size_known);
   datagram.add_int32(_x_size);
   datagram.add_int32(_y_size);
@@ -478,6 +512,11 @@ fillin(DatagramIterator &scan, BamReader *manager) {
   _properties.fillin(scan, manager);
   _filename = FilenameUnifier::get_bam_filename(scan.get_string());
   _alpha_filename = FilenameUnifier::get_bam_filename(scan.get_string());
+  if (Palettizer::_read_pi_version >= 10) {
+    _alpha_file_channel = scan.get_uint8();
+  } else {
+    _alpha_file_channel = 0;
+  }
   _size_known = scan.get_bool();
   _x_size = scan.get_int32();
   _y_size = scan.get_int32();

+ 5 - 3
pandatool/src/egg-palettize/imageFile.h

@@ -19,12 +19,12 @@
 #ifndef IMAGEFILE_H
 #define IMAGEFILE_H
 
-#include <pandatoolbase.h>
+#include "pandatoolbase.h"
 
 #include "textureProperties.h"
 
-#include <filename.h>
-#include <typedWritable.h>
+#include "filename.h"
+#include "typedWritable.h"
 
 class PNMImage;
 class EggTexture;
@@ -57,6 +57,7 @@ public:
   void set_filename(const string &dirname, const string &basename);
   const Filename &get_filename() const;
   const Filename &get_alpha_filename() const;
+  int get_alpha_file_channel() const;
   bool exists() const;
 
   bool read(PNMImage &image) const;
@@ -71,6 +72,7 @@ protected:
   TextureProperties _properties;
   Filename _filename;
   Filename _alpha_filename;
+  int _alpha_file_channel;
 
   bool _size_known;
   int _x_size, _y_size;

+ 2 - 1
pandatool/src/egg-palettize/palettizer.cxx

@@ -41,9 +41,10 @@ Palettizer *pal = (Palettizer *)NULL;
 // allows us to easily update egg-palettize to write out additional
 // information to its pi file, without having it increment the bam
 // version number for all bam and boo files anywhere in the world.
-int Palettizer::_pi_version = 9;
+int Palettizer::_pi_version = 10;
 // Updated to version 8 on 3/20/03 to remove extensions from texture key names.
 // Updated to version 9 on 4/13/03 to add a few properties in various places.
+// Updated to version 10 on 4/15/03 to add _alpha_file_channel.
 
 int Palettizer::_min_pi_version = 8;
 // Dropped support for versions 7 and below on 7/14/03.

+ 2 - 1
pandatool/src/egg-palettize/sourceTextureImage.cxx

@@ -50,11 +50,12 @@ SourceTextureImage() {
 ////////////////////////////////////////////////////////////////////
 SourceTextureImage::
 SourceTextureImage(TextureImage *texture, const Filename &filename,
-                   const Filename &alpha_filename) :
+                   const Filename &alpha_filename, int alpha_file_channel) :
   _texture(texture)
 {
   _filename = filename;
   _alpha_filename = alpha_filename;
+  _alpha_file_channel = alpha_file_channel;
   _egg_count = 0;
   _read_header = false;
   _successfully_read_header = false;

+ 1 - 1
pandatool/src/egg-palettize/sourceTextureImage.h

@@ -36,7 +36,7 @@ private:
 
 public:
   SourceTextureImage(TextureImage *texture, const Filename &filename,
-                     const Filename &alpha_filename);
+                     const Filename &alpha_filename, int alpha_file_channel);
 
   TextureImage *get_texture() const;
 

+ 10 - 6
pandatool/src/egg-palettize/textureImage.cxx

@@ -506,8 +506,9 @@ get_alpha_mode() const {
 //               that.
 ////////////////////////////////////////////////////////////////////
 SourceTextureImage *TextureImage::
-get_source(const Filename &filename, const Filename &alpha_filename) {
-  string key = get_source_key(filename, alpha_filename);
+get_source(const Filename &filename, const Filename &alpha_filename,
+           int alpha_file_channel) {
+  string key = get_source_key(filename, alpha_filename, alpha_file_channel);
 
   Sources::iterator si;
   si = _sources.find(key);
@@ -516,7 +517,7 @@ get_source(const Filename &filename, const Filename &alpha_filename) {
   }
 
   SourceTextureImage *source =
-    new SourceTextureImage(this, filename, alpha_filename);
+    new SourceTextureImage(this, filename, alpha_filename, alpha_file_channel);
   _sources.insert(Sources::value_type(key, source));
 
   // Clear out the preferred source image to force us to rederive this
@@ -1217,11 +1218,13 @@ copy_new_dests(const TextureImage::Dests &a, const TextureImage::Dests &b) {
 //               stored in, given its one or two filenames.
 ////////////////////////////////////////////////////////////////////
 string TextureImage::
-get_source_key(const Filename &filename, const Filename &alpha_filename) {
+get_source_key(const Filename &filename, const Filename &alpha_filename,
+               int alpha_file_channel) {
   Filename f = FilenameUnifier::make_bam_filename(filename);
   Filename a = FilenameUnifier::make_bam_filename(alpha_filename);
 
-  return f.get_fullpath() + ":" + a.get_fullpath();
+  return f.get_fullpath() + ":" + a.get_fullpath() + ":" +
+    format_string(alpha_file_channel);
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -1317,7 +1320,8 @@ complete_pointers(TypedWritable **p_list, BamReader *manager) {
     SourceTextureImage *source;
     DCAST_INTO_R(source, p_list[pi++], pi);
     string key = get_source_key(source->get_filename(),
-                                source->get_alpha_filename());
+                                source->get_alpha_filename(),
+                                source->get_alpha_file_channel());
 
     bool inserted = _sources.insert(Sources::value_type(key, source)).second;
     if (!inserted) {

+ 4 - 2
pandatool/src/egg-palettize/textureImage.h

@@ -77,7 +77,8 @@ public:
   EggRenderMode::AlphaMode get_alpha_mode() const;
 
   SourceTextureImage *get_source(const Filename &filename,
-                                 const Filename &alpha_filename);
+                                 const Filename &alpha_filename,
+                                 int alpha_file_channel);
 
   SourceTextureImage *get_preferred_source();
   void clear_source_basic_properties();
@@ -108,7 +109,8 @@ private:
   void copy_new_dests(const Dests &a, const Dests &b);
 
   string get_source_key(const Filename &filename,
-                        const Filename &alpha_filename);
+                        const Filename &alpha_filename,
+                        int alpha_file_channel);
 
 private:
   TextureRequest _request;

+ 15 - 13
pandatool/src/egg-palettize/textureReference.cxx

@@ -25,18 +25,18 @@
 #include "palettizer.h"
 #include "eggFile.h"
 
-#include <indent.h>
-#include <eggTexture.h>
-#include <eggData.h>
-#include <eggGroupNode.h>
-#include <eggGroup.h>
-#include <eggNurbsSurface.h>
-#include <eggVertexPool.h>
-#include <datagram.h>
-#include <datagramIterator.h>
-#include <bamReader.h>
-#include <bamWriter.h>
-#include <string_utils.h>
+#include "indent.h"
+#include "eggTexture.h"
+#include "eggData.h"
+#include "eggGroupNode.h"
+#include "eggGroup.h"
+#include "eggNurbsSurface.h"
+#include "eggVertexPool.h"
+#include "datagram.h"
+#include "datagramIterator.h"
+#include "bamReader.h"
+#include "bamWriter.h"
+#include "string_utils.h"
 
 #include <math.h>
 
@@ -100,6 +100,7 @@ from_egg(EggFile *egg_file, EggData *data, EggTexture *egg_tex) {
   if (_egg_tex->has_alpha_filename()) {
     alpha_filename = _egg_tex->get_alpha_filename();
   }
+  int alpha_file_channel = _egg_tex->get_alpha_file_channel();
 
   _properties._format = _egg_tex->get_format();
   _properties._minfilter = _egg_tex->get_minfilter();
@@ -108,7 +109,8 @@ from_egg(EggFile *egg_file, EggData *data, EggTexture *egg_tex) {
 
   string name = filename.get_basename_wo_extension();
   TextureImage *texture = pal->get_texture(name);
-  _source_texture = texture->get_source(filename, alpha_filename);
+  _source_texture = texture->get_source(filename, alpha_filename, 
+                                        alpha_file_channel);
   _source_texture->update_properties(_properties);
 
   _uses_alpha = false;

+ 3 - 3
pandatool/src/egg-palettize/textureReference.h

@@ -19,13 +19,13 @@
 #ifndef TEXTUREREFERENCE_H
 #define TEXTUREREFERENCE_H
 
-#include <pandatoolbase.h>
+#include "pandatoolbase.h"
 
 #include "textureProperties.h"
 #include "palettizer.h"
 
-#include <luse.h>
-#include <typedWritable.h>
+#include "luse.h"
+#include "typedWritable.h"
 
 class TextureImage;
 class SourceTextureImage;