Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

PaletteImage Class Reference

This is a single palette image, one of several within a PalettePage, which is in turn one of several pages within a PaletteGroup. More...

#include <paletteImage.h>

Inheritance diagram for PaletteImage:

ImageFile TypedWritable TypedObject List of all members.

Public Member Functions

 PaletteImage (PalettePage *page, int index)
PalettePageget_page () const
 Returns the particular PalettePage this image is associated with.

bool is_empty () const
 Returns true if there are no textures, or only one "solitary" texture, placed on the image.

double count_utilization () const
 Returns the fraction of the PaletteImage that is actually used by any textures.

double count_coverage () const
 Returns the a weighted average of the fraction of coverage represented by all of the textures placed on the palette.

bool place (TexturePlacement *placement)
 Attempts to place the indicated texture on the image.

void unplace (TexturePlacement *placement)
 Removes the texture from the image.

void check_solitary ()
 To be called after all textures have been placed on the image, this checks to see if there is only one texture on the image.

void optimal_resize ()
 Attempts to resize the palette image to as small as it can go.

bool resize_image (int x_size, int y_size)
 Attempts to resize the palette image, and repack all of the textures within the new size.

void write_placements (ostream &out, int indent_level=0) const
 Writes a list of the textures that have been placed on this image to the indicated output stream, one per line.

void reset_image ()
 Unpacks each texture that has been placed on this image, resetting the image to empty.

void setup_shadow_image ()
 Ensures the _shadow_image has the correct filename and image types, based on what was supplied on the command line and in the .txa file.

void update_image (bool redo_all)
 If the palette has changed since it was last written out, updates the image and writes out a new one.

virtual void write_datagram (BamWriter *writer, Datagram &datagram)
 Fills the indicated datagram up with a binary representation of the current object, in preparation for writing to a Bam file.

virtual int complete_pointers (TypedWritable **p_list, BamReader *manager)
 Called after the object is otherwise completely read from a Bam file, this function's job is to store the pointers that were retrieved from the Bam file for each pointer object written.

virtual TypeHandle get_type () const
void make_shadow_image (const string &basename)
 Sets up the ImageFile as a "shadow image" of a particular PaletteImage.

bool is_size_known () const
 Returns true if the size of the image file is known, false otherwise.

int get_x_size () const
 Returns the size of the image file in pixels in the X direction.

int get_y_size () const
 Returns the size of the image file in pixels in the Y direction.

bool has_num_channels () const
 Returns true if the number of channels in the image is known, false otherwise.

int get_num_channels () const
 Returns the number of channels of the image.

const TexturePropertiesget_properties () const
 Returns the grouping properties of the image.

void clear_basic_properties ()
 Resets the properties to a neutral state, for instance in preparation for calling update_properties() with all the known contributing properties.

void update_properties (const TextureProperties &properties)
 If the indicate TextureProperties structure is more specific than this one, updates this one.

void set_filename (PaletteGroup *group, const string &basename)
 Sets the filename, and if applicable, the alpha_filename, from the indicated basename.

void set_filename (const string &dirname, const string &basename)
 Sets the filename, and if applicable, the alpha_filename, from the indicated basename.

const Filenameget_filename () const
 Returns the primary filename of the image file.

const Filenameget_alpha_filename () const
 Returns the alpha filename of the image file.

int get_alpha_file_channel () const
 Returns the particular channel number of the alpha image file from which the alpha channel should be extracted.

bool exists () const
 Returns true if the file or files named by the image file exist, false otherwise.

bool read (PNMImage &image) const
 Reads in the image (or images, if the alpha_filename is separate) and stores it in the indicated PNMImage.

bool write (const PNMImage &image) const
 Writes out the image in the indicated PNMImage to the _filename and/or _alpha_filename.

void unlink ()
 Deletes the image file or files.

void update_egg_tex (EggTexture *egg_tex) const
 Sets the indicated EggTexture to refer to this file.

void output_filename (ostream &out) const
 Writes the filename (or pair of filenames) to the indicated output stream.

virtual void finalize ()
virtual TypeHandle force_init_type (void)
int get_type_index () const
bool is_of_type (TypeHandle handle) const
bool is_exact_type (TypeHandle handle) const

Static Public Member Functions

void register_with_read_factory ()
 Registers the current object as something that can be read from a Bam file.

TypeHandle get_class_type ()
void init_type ()

Static Public Attributes

TypedWritable *const Null

Protected Member Functions

void fillin (DatagramIterator &scan, BamReader *manager)
 Reads the binary data from the given datagram iterator, which was written by a previous call to write_datagram().


Static Protected Member Functions

TypedWritablemake_PaletteImage (const FactoryParams &params)
 This method is called by the BamReader when an object of this type is encountered in a Bam file; it should allocate and return a new object with all the data read.


Protected Attributes

TextureProperties _properties
Filename _filename
Filename _alpha_filename
int _alpha_file_channel
bool _size_known
int _x_size
int _y_size

Private Types

typedef pvector< ClearedRegionClearedRegions
typedef pvector< TexturePlacement * > Placements

Private Member Functions

 PaletteImage ()
 The default constructor is only for the convenience of the Bam reader.

bool find_hole (int &x, int &y, int x_size, int y_size) const
 Searches for a hole of at least x_size by y_size pixels somewhere within the PaletteImage.

TexturePlacementfind_overlap (int x, int y, int x_size, int y_size) const
 If the rectangle whose top left corner is x, y and whose size is x_size, y_size describes an empty hole that does not overlap any placed images, returns NULL; otherwise, returns the first placed texture that the image does overlap.

void get_image ()
 Reads or generates the PNMImage that corresponds to the palette as it is known so far.

void remove_image ()
 Deletes the image file.


Private Attributes

ClearedRegions _cleared_regions
Placements _placements
PalettePage_page
int _index
string _basename
bool _new_image
bool _got_image
PNMImage _image
ImageFile _shadow_image
int _num_placements

Static Private Attributes

TypeHandle _type_handle

Detailed Description

This is a single palette image, one of several within a PalettePage, which is in turn one of several pages within a PaletteGroup.

Each palette image is a collage of several different textures that were all assigned to the same PaletteGroup, and all share the same properties of the PalettePage.

Definition at line 51 of file paletteImage.h.


Member Typedef Documentation

typedef pvector<ClearedRegion> PaletteImage::ClearedRegions [private]
 

Definition at line 100 of file paletteImage.h.

typedef pvector<TexturePlacement *> PaletteImage::Placements [private]
 

Definition at line 103 of file paletteImage.h.


Constructor & Destructor Documentation

PaletteImage::PaletteImage  )  [private]
 

The default constructor is only for the convenience of the Bam reader.

Definition at line 181 of file paletteImage.cxx.

PaletteImage::PaletteImage PalettePage page,
int  index
 

Definition at line 196 of file paletteImage.cxx.

References _page.


Member Function Documentation

void PaletteImage::check_solitary  ) 
 

To be called after all textures have been placed on the image, this checks to see if there is only one texture on the image.

If there is, it is flagged as 'solitary' so that the egg files will not needlessly reference the palettized image.

However, if pal->_omit_solitary is false, we generally don't change textures to solitary state.

Definition at line 418 of file paletteImage.cxx.

References _cleared_regions, _placements, ImageFile::_x_size, ImageFile::_y_size, place(), and remove_image().

void ImageFile::clear_basic_properties  )  [inherited]
 

Resets the properties to a neutral state, for instance in preparation for calling update_properties() with all the known contributing properties.

Definition at line 187 of file imageFile.cxx.

int PaletteImage::complete_pointers TypedWritable **  p_list,
BamReader manager
[virtual]
 

Called after the object is otherwise completely read from a Bam file, this function's job is to store the pointers that were retrieved from the Bam file for each pointer object written.

The return value is the number of pointers processed from the list.

Reimplemented from ImageFile.

Definition at line 940 of file paletteImage.cxx.

double PaletteImage::count_coverage  )  const
 

Returns the a weighted average of the fraction of coverage represented by all of the textures placed on the palette.

This number represents the fraction of wasted pixels in the palette image consumed by copying the same pixels multiple times into the palette, or if the number is negative, it represents the fraction of pixels saved by not having to copy the entire texture into the palette.

Definition at line 322 of file paletteImage.cxx.

References _placements.

double PaletteImage::count_utilization  )  const
 

Returns the fraction of the PaletteImage that is actually used by any textures.

This is 1.0 if every pixel in the PaletteImage is used, or 0.0 if none are. Normally it will be somewhere in between.

Definition at line 281 of file paletteImage.cxx.

bool ImageFile::exists  )  const [inherited]
 

Returns true if the file or files named by the image file exist, false otherwise.

Definition at line 361 of file imageFile.cxx.

References ImageFile::_alpha_filename, TextureProperties::_alpha_type, TextureProperties::_color_type, ImageFile::_filename, ImageFile::_properties, Filename::empty(), Filename::exists(), PNMImage::get_maxval(), PNMImage::get_x_size(), PNMImage::get_y_size(), PNMImage::has_alpha(), Filename::make_dir(), FilenameUnifier::make_user_filename(), nassertr, nout, NULL, Filename::unlink(), PNMImage::write(), x, and y.

Referenced by TextureImage::is_used().

void PaletteImage::fillin DatagramIterator scan,
BamReader manager
[protected]
 

Reads the binary data from the given datagram iterator, which was written by a previous call to write_datagram().

Reimplemented from ImageFile.

Definition at line 998 of file paletteImage.cxx.

bool PaletteImage::find_hole int x,
int y,
int  x_size,
int  y_size
const [private]
 

Searches for a hole of at least x_size by y_size pixels somewhere within the PaletteImage.

If a suitable hole is found, sets x and y to the top left corner and returns true; otherwise, returns false.

Definition at line 735 of file paletteImage.cxx.

TexturePlacement * PaletteImage::find_overlap int  x,
int  y,
int  x_size,
int  y_size
const [private]
 

If the rectangle whose top left corner is x, y and whose size is x_size, y_size describes an empty hole that does not overlap any placed images, returns NULL; otherwise, returns the first placed texture that the image does overlap.

It is assumed the rectangle lies completely within the boundaries of the image itself.

Definition at line 787 of file paletteImage.cxx.

References _num_placements, _placements, ImageFile::complete_pointers(), DCAST_INTO_R, and NULL.

Referenced by setup_shadow_image().

int ImageFile::get_alpha_file_channel  )  const [inherited]
 

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).

Definition at line 345 of file imageFile.cxx.

const Filename & ImageFile::get_alpha_filename  )  const [inherited]
 

Returns the alpha filename of the image file.

This is the name of the file that contains the alpha channel, if it is stored in a separate file, or the empty string if it is not.

Definition at line 322 of file imageFile.cxx.

TypeHandle PaletteImage::get_class_type void   )  [inline, static]
 

Reimplemented from ImageFile.

Definition at line 133 of file paletteImage.h.

References _type_handle.

const Filename & ImageFile::get_filename  )  const [inherited]
 

Returns the primary filename of the image file.

Definition at line 303 of file imageFile.cxx.

References ImageFile::_properties, and nout.

Referenced by TextureImage::clear_source_basic_properties(), TextureImage::get_preferred_source(), TextureImage::is_newer_than(), TextureImage::is_used(), and unplace().

void PaletteImage::get_image  )  [private]
 

Reads or generates the PNMImage that corresponds to the palette as it is known so far.

Definition at line 811 of file paletteImage.cxx.

int ImageFile::get_num_channels  )  const [inherited]
 

Returns the number of channels of the image.

It is an error to call this unless has_num_channels() returns true.

Definition at line 155 of file imageFile.cxx.

References ImageFile::_properties.

Referenced by TextureImage::mark_eggs_stale().

PalettePage * PaletteImage::get_page  )  const
 

Returns the particular PalettePage this image is associated with.

Definition at line 232 of file paletteImage.cxx.

References _placements, TexturePlacement::get_placed_x_size(), and TexturePlacement::get_placed_y_size().

const TextureProperties & ImageFile::get_properties  )  const [inherited]
 

Returns the grouping properties of the image.

Definition at line 168 of file imageFile.cxx.

References Palettizer::_map_dirname, PaletteGroup::get_dirname(), NULL, and pal.

Referenced by TextureMemoryCounter::add_texture(), TextureImage::copy_unplaced(), DestTextureImage::DestTextureImage(), and TexturePlacement::get_name().

virtual TypeHandle PaletteImage::get_type void   )  const [inline, virtual]
 

Reimplemented from ImageFile.

Definition at line 141 of file paletteImage.h.

int ImageFile::get_x_size  )  const [inherited]
 

Returns the size of the image file in pixels in the X direction.

It is an error to call this unless is_size_known() returns true.

Definition at line 104 of file imageFile.cxx.

References ImageFile::_properties, and TextureProperties::has_num_channels().

Referenced by TextureImage::copy_unplaced(), DestTextureImage::DestTextureImage(), TextureImage::is_used(), and TextureImage::mark_eggs_stale().

int ImageFile::get_y_size  )  const [inherited]
 

Returns the size of the image file in pixels in the Y direction.

It is an error to call this unless is_size_known() returns true.

Definition at line 122 of file imageFile.cxx.

Referenced by TextureImage::copy_unplaced(), DestTextureImage::DestTextureImage(), TextureImage::is_used(), and TextureImage::mark_eggs_stale().

bool ImageFile::has_num_channels  )  const [inherited]
 

Returns true if the number of channels in the image is known, false otherwise.

Definition at line 138 of file imageFile.cxx.

References ImageFile::_properties, and TextureProperties::clear_basic().

void PaletteImage::init_type void   )  [inline, static]
 

Reimplemented from ImageFile.

Definition at line 136 of file paletteImage.h.

bool PaletteImage::is_empty  )  const
 

Returns true if there are no textures, or only one "solitary" texture, placed on the image.

In either case, the PaletteImage need not be generated.

Definition at line 249 of file paletteImage.cxx.

Referenced by unplace().

bool ImageFile::is_size_known  )  const [inherited]
 

Returns true if the size of the image file is known, false otherwise.

Definition at line 87 of file imageFile.cxx.

References ImageFile::_y_size, and nassertr.

Referenced by TextureImage::copy_unplaced(), DestTextureImage::DestTextureImage(), TexturePlacement::mark_eggs_stale(), and TextureImage::read_source_image().

TypedWritable * PaletteImage::make_PaletteImage const FactoryParams params  )  [static, protected]
 

This method is called by the BamReader when an object of this type is encountered in a Bam file; it should allocate and return a new object with all the data read.

Definition at line 975 of file paletteImage.cxx.

void ImageFile::make_shadow_image const string &  basename  )  [inherited]
 

Sets up the ImageFile as a "shadow image" of a particular PaletteImage.

This is a temporary ImageFile that's used to read and write the shadow palette image, which is used to keep a working copy of the palette.

Definition at line 69 of file imageFile.cxx.

References ImageFile::_size_known.

void PaletteImage::optimal_resize  ) 
 

Attempts to resize the palette image to as small as it can go.

Definition at line 463 of file paletteImage.cxx.

Referenced by PalettePage::unplace().

void ImageFile::output_filename ostream &  out  )  const [inherited]
 

Writes the filename (or pair of filenames) to the indicated output stream.

Definition at line 561 of file imageFile.cxx.

Referenced by TextureImage::copy_unplaced(), SourceTextureImage::get_type(), and DestTextureImage::get_type().

bool PaletteImage::place TexturePlacement placement  ) 
 

Attempts to place the indicated texture on the image.

Returns true if successful, or false if there was no available space.

Definition at line 359 of file paletteImage.cxx.

Referenced by check_solitary(), and PalettePage::place_all().

bool ImageFile::read PNMImage image  )  const [inherited]
 

Reads in the image (or images, if the alpha_filename is separate) and stores it in the indicated PNMImage.

Returns true on success, false on failure.

Definition at line 389 of file imageFile.cxx.

void PaletteImage::register_with_read_factory void   )  [static]
 

Registers the current object as something that can be read from a Bam file.

Definition at line 876 of file paletteImage.cxx.

void PaletteImage::remove_image  )  [private]
 

Deletes the image file.

Definition at line 857 of file paletteImage.cxx.

References _basename, _index, and _new_image.

Referenced by check_solitary().

void PaletteImage::reset_image  ) 
 

Unpacks each texture that has been placed on this image, resetting the image to empty.

Definition at line 601 of file paletteImage.cxx.

References _placements.

bool PaletteImage::resize_image int  x_size,
int  y_size
 

Attempts to resize the palette image, and repack all of the textures within the new size.

Returns true if successful, false otherwise. If this fails, it will still result in repacking all the textures in the original size.

Definition at line 509 of file paletteImage.cxx.

References _placements.

Referenced by unplace().

void ImageFile::set_filename const string &  dirname,
const string &  basename
[inherited]
 

Sets the filename, and if applicable, the alpha_filename, from the indicated basename.

The extension appropriate to the image file type specified in _color_type (and _alpha_type) is automatically applied.

Definition at line 269 of file imageFile.cxx.

References ImageFile::_alpha_filename, TextureProperties::_alpha_type, ImageFile::_filename, ImageFile::_properties, Filename::empty(), Filename::exists(), NULL, and TextureProperties::uses_alpha().

void ImageFile::set_filename PaletteGroup group,
const string &  basename
[inherited]
 

Sets the filename, and if applicable, the alpha_filename, from the indicated basename.

The extension appropriate to the image file type specified in _color_type (and _alpha_type) is automatically applied.

Definition at line 223 of file imageFile.cxx.

void PaletteImage::setup_shadow_image  ) 
 

Ensures the _shadow_image has the correct filename and image types, based on what was supplied on the command line and in the .txa file.

Definition at line 628 of file paletteImage.cxx.

References ImageFile::_x_size, ImageFile::_y_size, find_overlap(), TexturePlacement::get_placed_x(), TexturePlacement::get_placed_x_size(), TexturePlacement::get_placed_y(), TexturePlacement::get_placed_y_size(), nassertr, NULL, x, and y.

void ImageFile::unlink  )  [inherited]
 

Deletes the image file or files.

Definition at line 513 of file imageFile.cxx.

References ImageFile::_alpha_file_channel, ImageFile::_alpha_filename, ImageFile::_filename, Palettizer::_read_pi_version, and DatagramIterator::get_uint8().

void PaletteImage::unplace TexturePlacement placement  ) 
 

Removes the texture from the image.

Definition at line 382 of file paletteImage.cxx.

References ImageFile::_x_size, ImageFile::_y_size, ImageFile::get_filename(), is_empty(), FilenameUnifier::make_user_filename(), nassertv, nout, and resize_image().

Referenced by TexturePlacement::get_page(), and PalettePage::place().

void ImageFile::update_egg_tex EggTexture egg_tex  )  const [inherited]
 

Sets the indicated EggTexture to refer to this file.

Definition at line 533 of file imageFile.cxx.

Referenced by TextureReference::get_placement().

void PaletteImage::update_image bool  redo_all  ) 
 

If the palette has changed since it was last written out, updates the image and writes out a new one.

If redo_all is true, regenerates the image from scratch and writes it out again, whether it needed it or not.

Definition at line 647 of file paletteImage.cxx.

References y.

void ImageFile::update_properties const TextureProperties properties  )  [inherited]
 

If the indicate TextureProperties structure is more specific than this one, updates this one.

Definition at line 202 of file imageFile.cxx.

References TextureProperties::_color_type, ImageFile::_filename, ImageFile::_properties, Filename, PNMFileType::get_suggested_extension(), NULL, and Filename::set_extension().

bool ImageFile::write const PNMImage image  )  const [inherited]
 

Writes out the image in the indicated PNMImage to the _filename and/or _alpha_filename.

Returns true on success, false on failure.

Definition at line 459 of file imageFile.cxx.

References ImageFile::_alpha_filename, and ImageFile::_properties.

void PaletteImage::write_datagram BamWriter writer,
Datagram datagram
[virtual]
 

Fills the indicated datagram up with a binary representation of the current object, in preparation for writing to a Bam file.

Reimplemented from ImageFile.

Definition at line 894 of file paletteImage.cxx.

void PaletteImage::write_placements ostream &  out,
int  indent_level = 0
const
 

Writes a list of the textures that have been placed on this image to the indicated output stream, one per line.

Definition at line 582 of file paletteImage.cxx.


Member Data Documentation

int ImageFile::_alpha_file_channel [protected, inherited]
 

Definition at line 84 of file imageFile.h.

Referenced by SourceTextureImage::SourceTextureImage(), and ImageFile::unlink().

Filename ImageFile::_alpha_filename [protected, inherited]
 

Definition at line 83 of file imageFile.h.

Referenced by ImageFile::exists(), ImageFile::set_filename(), SourceTextureImage::SourceTextureImage(), ImageFile::unlink(), and ImageFile::write().

string PaletteImage::_basename [private]
 

Definition at line 108 of file paletteImage.h.

Referenced by remove_image().

ClearedRegions PaletteImage::_cleared_regions [private]
 

Definition at line 101 of file paletteImage.h.

Referenced by check_solitary().

Filename ImageFile::_filename [protected, inherited]
 

Definition at line 82 of file imageFile.h.

Referenced by ImageFile::exists(), ImageFile::set_filename(), SourceTextureImage::SourceTextureImage(), ImageFile::unlink(), and ImageFile::update_properties().

bool PaletteImage::_got_image [private]
 

Definition at line 111 of file paletteImage.h.

PNMImage PaletteImage::_image [private]
 

Definition at line 112 of file paletteImage.h.

int PaletteImage::_index [private]
 

Definition at line 107 of file paletteImage.h.

Referenced by remove_image().

bool PaletteImage::_new_image [private]
 

Definition at line 110 of file paletteImage.h.

Referenced by remove_image().

int PaletteImage::_num_placements [private]
 

Definition at line 130 of file paletteImage.h.

Referenced by find_overlap().

PalettePage* PaletteImage::_page [private]
 

Definition at line 106 of file paletteImage.h.

Referenced by PaletteImage().

Placements PaletteImage::_placements [private]
 

Definition at line 104 of file paletteImage.h.

Referenced by check_solitary(), count_coverage(), find_overlap(), get_page(), reset_image(), and resize_image().

TextureProperties ImageFile::_properties [protected, inherited]
 

Definition at line 81 of file imageFile.h.

Referenced by DestTextureImage::DestTextureImage(), ImageFile::exists(), ImageFile::get_filename(), ImageFile::get_num_channels(), ImageFile::get_x_size(), ImageFile::has_num_channels(), TextureImage::mark_eggs_stale(), ImageFile::set_filename(), ImageFile::update_properties(), and ImageFile::write().

ImageFile PaletteImage::_shadow_image [private]
 

Definition at line 114 of file paletteImage.h.

bool ImageFile::_size_known [protected, inherited]
 

Definition at line 86 of file imageFile.h.

Referenced by DestTextureImage::DestTextureImage(), SourceTextureImage::increment_egg_count(), ImageFile::make_shadow_image(), and TextureImage::mark_eggs_stale().

TypeHandle PaletteImage::_type_handle [static, private]
 

Reimplemented from ImageFile.

Definition at line 39 of file paletteImage.cxx.

Referenced by get_class_type().

int ImageFile::_x_size [protected, inherited]
 

Definition at line 87 of file imageFile.h.

Referenced by check_solitary(), PaletteImage::ClearedRegion::ClearedRegion(), DestTextureImage::DestTextureImage(), TextureImage::mark_eggs_stale(), TextureImage::pre_txa_file(), setup_shadow_image(), and unplace().

int ImageFile::_y_size [protected, inherited]
 

Definition at line 87 of file imageFile.h.

Referenced by check_solitary(), PaletteImage::ClearedRegion::ClearedRegion(), DestTextureImage::DestTextureImage(), ImageFile::is_size_known(), TextureImage::mark_eggs_stale(), TextureImage::pre_txa_file(), setup_shadow_image(), and unplace().


The documentation for this class was generated from the following files:
Generated on Fri May 2 03:27:45 2003 for Panda-Tool by doxygen1.3