add alpha_file_channel

This commit is contained in:
David Rose 2003-04-15 22:58:49 +00:00
parent df2a60cfb0
commit f776e83c6e
9 changed files with 93 additions and 42 deletions

View File

@ -21,13 +21,13 @@
#include "filenameUnifier.h" #include "filenameUnifier.h"
#include "paletteGroup.h" #include "paletteGroup.h"
#include <pnmImage.h> #include "pnmImage.h"
#include <pnmFileType.h> #include "pnmFileType.h"
#include <eggTexture.h> #include "eggTexture.h"
#include <datagram.h> #include "datagram.h"
#include <datagramIterator.h> #include "datagramIterator.h"
#include <bamReader.h> #include "bamReader.h"
#include <bamWriter.h> #include "bamWriter.h"
TypeHandle ImageFile::_type_handle; TypeHandle ImageFile::_type_handle;
@ -38,6 +38,7 @@ TypeHandle ImageFile::_type_handle;
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
ImageFile:: ImageFile::
ImageFile() { ImageFile() {
_alpha_file_channel = 0;
_size_known = false; _size_known = false;
_x_size = 0; _x_size = 0;
_y_size = 0; _y_size = 0;
@ -251,6 +252,22 @@ get_alpha_filename() const {
return _alpha_filename; 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 // Function: ImageFile::exists
// Access: Public // Access: Public
@ -306,15 +323,29 @@ read(PNMImage &image) const {
} }
image.add_alpha(); image.add_alpha();
if (alpha_image.has_alpha()) {
for (int y = 0; y < image.get_y_size(); y++) { if (_alpha_file_channel == 4 ||
for (int x = 0; x < image.get_x_size(); x++) { (_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)); 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 { } else {
for (int y = 0; y < image.get_y_size(); y++) { // Use the grayscale channel.
for (int x = 0; x < image.get_x_size(); x++) { 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)); 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() && if (_properties.uses_alpha() &&
!_alpha_filename.empty()) { !_alpha_filename.empty()) {
egg_tex->set_alpha_filename(FilenameUnifier::make_egg_filename(_alpha_filename)); egg_tex->set_alpha_filename(FilenameUnifier::make_egg_filename(_alpha_filename));
egg_tex->set_alpha_file_channel(_alpha_file_channel);
} else { } else {
egg_tex->clear_alpha_filename(); egg_tex->clear_alpha_filename();
egg_tex->clear_alpha_file_channel();
} }
_properties.update_egg_tex(egg_tex); _properties.update_egg_tex(egg_tex);
@ -442,6 +475,7 @@ write_datagram(BamWriter *writer, Datagram &datagram) {
_properties.write_datagram(writer, datagram); _properties.write_datagram(writer, datagram);
datagram.add_string(FilenameUnifier::make_bam_filename(_filename)); datagram.add_string(FilenameUnifier::make_bam_filename(_filename));
datagram.add_string(FilenameUnifier::make_bam_filename(_alpha_filename)); datagram.add_string(FilenameUnifier::make_bam_filename(_alpha_filename));
datagram.add_uint8(_alpha_file_channel);
datagram.add_bool(_size_known); datagram.add_bool(_size_known);
datagram.add_int32(_x_size); datagram.add_int32(_x_size);
datagram.add_int32(_y_size); datagram.add_int32(_y_size);
@ -478,6 +512,11 @@ fillin(DatagramIterator &scan, BamReader *manager) {
_properties.fillin(scan, manager); _properties.fillin(scan, manager);
_filename = FilenameUnifier::get_bam_filename(scan.get_string()); _filename = FilenameUnifier::get_bam_filename(scan.get_string());
_alpha_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(); _size_known = scan.get_bool();
_x_size = scan.get_int32(); _x_size = scan.get_int32();
_y_size = scan.get_int32(); _y_size = scan.get_int32();

View File

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

View File

@ -41,9 +41,10 @@ Palettizer *pal = (Palettizer *)NULL;
// allows us to easily update egg-palettize to write out additional // allows us to easily update egg-palettize to write out additional
// information to its pi file, without having it increment the bam // information to its pi file, without having it increment the bam
// version number for all bam and boo files anywhere in the world. // 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 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 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; int Palettizer::_min_pi_version = 8;
// Dropped support for versions 7 and below on 7/14/03. // Dropped support for versions 7 and below on 7/14/03.

View File

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

View File

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

View File

@ -506,8 +506,9 @@ get_alpha_mode() const {
// that. // that.
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
SourceTextureImage *TextureImage:: SourceTextureImage *TextureImage::
get_source(const Filename &filename, const Filename &alpha_filename) { get_source(const Filename &filename, const Filename &alpha_filename,
string key = get_source_key(filename, alpha_filename); int alpha_file_channel) {
string key = get_source_key(filename, alpha_filename, alpha_file_channel);
Sources::iterator si; Sources::iterator si;
si = _sources.find(key); si = _sources.find(key);
@ -516,7 +517,7 @@ get_source(const Filename &filename, const Filename &alpha_filename) {
} }
SourceTextureImage *source = SourceTextureImage *source =
new SourceTextureImage(this, filename, alpha_filename); new SourceTextureImage(this, filename, alpha_filename, alpha_file_channel);
_sources.insert(Sources::value_type(key, source)); _sources.insert(Sources::value_type(key, source));
// Clear out the preferred source image to force us to rederive this // 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. // stored in, given its one or two filenames.
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
string TextureImage:: 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 f = FilenameUnifier::make_bam_filename(filename);
Filename a = FilenameUnifier::make_bam_filename(alpha_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; SourceTextureImage *source;
DCAST_INTO_R(source, p_list[pi++], pi); DCAST_INTO_R(source, p_list[pi++], pi);
string key = get_source_key(source->get_filename(), 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; bool inserted = _sources.insert(Sources::value_type(key, source)).second;
if (!inserted) { if (!inserted) {

View File

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

View File

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

View File

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