37 PaletteImage::ClearedRegion::
48 PaletteImage::ClearedRegion::
59 PaletteImage::ClearedRegion::
60 ClearedRegion(
const PaletteImage::ClearedRegion ©) :
63 _x_size(copy._x_size),
71 void PaletteImage::ClearedRegion::
72 operator = (
const PaletteImage::ClearedRegion ©) {
75 _x_size = copy._x_size;
76 _y_size = copy._y_size;
82 void PaletteImage::ClearedRegion::
84 LRGBColorf rgb(pal->_background[0], pal->_background[1], pal->_background[2]);
85 float alpha = pal->_background[3];
87 for (
int y = _y; y < _y + _y_size; y++) {
88 for (
int x = _x; x < _x + _x_size; x++) {
93 for (
int y = _y; y < _y + _y_size; y++) {
94 for (
int x = _x; x < _x + _x_size; x++) {
104 void PaletteImage::ClearedRegion::
105 write_datagram(
Datagram &datagram)
const {
115 void PaletteImage::ClearedRegion::
151 _x_size = pal->_pal_x_size;
152 _y_size = pal->_pal_y_size;
164 PaletteImage(
PalettePage *page,
int index,
unsigned swapIndex) :
167 _swapped_image(swapIndex)
171 _x_size = pal->_pal_x_size;
172 _y_size = pal->_pal_y_size;
195 if (_placements.empty()) {
199 }
else if (_placements.size() == 1) {
202 return (_placements[0]->get_omit_reason() == OR_solitary);
219 Placements::const_iterator pi;
220 for (pi = _placements.begin(); pi != _placements.end(); ++pi) {
226 used_pixels += texture_pixels;
231 return (
double)used_pixels / (double)total_pixels;
244 int coverage_pixels = 0;
246 Placements::const_iterator pi;
247 for (pi = _placements.begin(); pi != _placements.end(); ++pi) {
250 nassertr(texture !=
nullptr, 0.0);
259 coverage_pixels += placed_pixels - orig_pixels;
264 return (
double)coverage_pixels / (double)total_pixels;
279 _placements.push_back(placement);
282 TexturePlacement::TextureSwaps::iterator tsi;
283 for (tsi = placement->_textureSwaps.begin(); tsi != placement->_textureSwaps.end(); ++tsi) {
284 if ((tsi - placement->_textureSwaps.begin()) >= (
int)_swappedImages.size()) {
285 PaletteImage *swappedImage =
new PaletteImage(_page, _swappedImages.size(), tsi - placement->_textureSwaps.begin() + 1);
286 swappedImage->_masterPlacements = &_placements;
287 _swappedImages.push_back(swappedImage);
304 Placements::iterator pi;
305 pi = find(_placements.begin(), _placements.end(), placement);
306 while (pi != _placements.end()) {
307 _placements.erase(pi);
308 pi = find(_placements.begin(), _placements.end(), placement);
310 _cleared_regions.push_back(ClearedRegion(placement));
324 if (_placements.size() == 1) {
330 if (pal->_omit_solitary || placement->
get_omit_reason() == OR_solitary) {
340 Placements::const_iterator pi;
341 for (pi = _placements.begin(); pi != _placements.end(); ++pi) {
366 bool resized_any =
false;
370 nassertv(_x_size > 0 && _y_size > 0);
387 << _x_size <<
" " << _y_size <<
"\n";
390 SwappedImages::iterator si;
391 for (si = _swappedImages.begin(); si != _swappedImages.end(); ++si) {
407 _cleared_regions.clear();
412 saved.swap(_placements);
415 int saved_x_size = _x_size;
416 int saved_y_size = _y_size;
423 Placements::iterator pi;
424 for (pi = saved.begin(); pi != saved.end(); ++pi) {
425 (*pi)->force_replace();
433 for (pi = saved.begin(); pi != saved.end() && packed; ++pi) {
441 _x_size = saved_x_size;
442 _y_size = saved_y_size;
445 remove.swap(_placements);
446 for (pi = remove.begin(); pi != remove.end(); ++pi) {
447 (*pi)->force_replace();
450 bool all_packed =
true;
451 for (pi = saved.begin(); pi != saved.end(); ++pi) {
456 nassertr(all_packed,
false);
480 Placements::const_iterator pi;
481 for (pi = _placements.begin(); pi != _placements.end(); ++pi) {
495 Placements::const_iterator pi;
496 for (pi = copy_placements.begin(); pi != copy_placements.end(); ++pi) {
502 _cleared_regions.clear();
515 SwappedImages::iterator si;
516 for (si = _swappedImages.begin(); si != _swappedImages.end(); ++si) {
529 if (
is_empty() && pal->_aggressively_clean_mapdir) {
546 _new_image || !
exists() ||
547 !_cleared_regions.empty();
549 Placements::iterator pi;
553 for (pi = _placements.begin(); pi != _placements.end(); ++pi) {
567 if (source !=
nullptr &&
577 TexturePlacement::TextureSwaps::iterator tsi;
578 for (tsi = placement->_textureSwaps.begin(); tsi != placement->_textureSwaps.end(); ++tsi) {
584 if (sourceSwapTexture !=
nullptr &&
604 get_swapped_images();
607 ClearedRegions::iterator ci;
608 for (ci = _cleared_regions.begin(); ci != _cleared_regions.end(); ++ci) {
609 ClearedRegion ®ion = (*ci);
610 region.clear(_image);
613 SwappedImages::iterator si;
614 for (si = _swappedImages.begin(); si != _swappedImages.end(); ++si) {
616 region.clear(swappedImage->_image);
619 _cleared_regions.clear();
622 for (pi = _placements.begin(); pi != _placements.end(); ++pi) {
628 SwappedImages::iterator si;
629 for (si = _swappedImages.begin(); si != _swappedImages.end(); ++si) {
639 if (pal->_shadow_color_type !=
nullptr) {
640 _shadow_image.
write(_image);
646 SwappedImages::iterator si;
647 for (si = _swappedImages.begin(); si != _swappedImages.end(); ++si) {
649 swappedImage->
write(swappedImage->_image);
650 if (pal->_shadow_color_type !=
nullptr) {
651 swappedImage->_shadow_image.
write(swappedImage->_image);
653 swappedImage->release_image();
665 Filename orig_alpha_filename = _alpha_filename;
668 if (setup_filename()) {
672 if (!orig_filename.empty() && orig_filename.
exists()) {
676 if (!orig_alpha_filename.empty() && orig_alpha_filename.
exists()) {
678 orig_alpha_filename.
unlink();
680 if (!orig_shadow_filename.empty() && orig_shadow_filename.
exists()) {
682 orig_shadow_filename.
unlink();
692 Placements::iterator pi;
693 for (pi = _placements.begin(); pi != _placements.end(); ++pi) {
715 _basename = std::string();
717 std::string::iterator si = pal->_generated_image_pattern.begin();
718 while (si != pal->_generated_image_pattern.end()) {
722 if (si != pal->_generated_image_pattern.end()) {
729 _basename += _page->
get_group()->get_name();
733 _basename += _page->get_name();
737 _basename += format_string(_index + 1);
753 if (_swapped_image > 0) {
754 _basename +=
"_swp_";
755 _basename += format_string(_swapped_image);
762 if (_basename.empty() || _basename[_basename.length() - 1] !=
'.') {
766 bool any_changed =
false;
785 find_hole(
int &x,
int &y,
int x_size,
int y_size)
const {
787 while (y + y_size <= _y_size) {
788 int next_y = _y_size;
791 while (x + x_size <= _x_size) {
797 if (overlap ==
nullptr) {
804 nassertr(next_x > x,
false);
808 nassertr(next_y > y,
false);
824 find_overlap(
int x,
int y,
int x_size,
int y_size)
const {
825 Placements::const_iterator pi;
826 for (pi = _placements.begin(); pi != _placements.end(); ++pi) {
829 placement->
intersects(x, y, x_size, y_size)) {
848 if (pal->_shadow_color_type !=
nullptr) {
861 nout <<
"Generating new " 865 _cleared_regions.clear();
868 _image.
fill(pal->_background[0], pal->_background[1], pal->_background[2]);
877 Placements::iterator pi;
878 for (pi = _placements.begin(); pi != _placements.end(); ++pi) {
888 get_swapped_image(
int index) {
894 if (pal->_shadow_color_type !=
nullptr) {
907 nout <<
"Generating new " 911 _cleared_regions.clear();
914 _image.
fill(pal->_background[0], pal->_background[1], pal->_background[2]);
923 Placements::iterator pi;
924 for (pi = _masterPlacements->begin(); pi != _masterPlacements->end(); ++pi) {
926 if ((
int)placement->_textureSwaps.size() > index) {
939 get_swapped_images() {
940 SwappedImages::iterator si;
941 for (si = _swappedImages.begin(); si != _swappedImages.end(); ++si) {
943 swappedImage->get_swapped_image(si - _swappedImages.begin());
962 if (pal->_shadow_color_type !=
nullptr) {
974 register_factory(get_class_type(), make_PaletteImage);
986 ClearedRegions::const_iterator ci;
987 for (ci = _cleared_regions.begin(); ci != _cleared_regions.end(); ++ci) {
988 (*ci).write_datagram(datagram);
992 Placements::const_iterator pi;
993 for (pi = _placements.begin(); pi != _placements.end(); ++pi) {
1020 _placements.reserve(_num_placements);
1021 for (i = 0; i < _num_placements; i++) {
1023 DCAST_INTO_R(placement, p_list[index], index);
1024 _placements.push_back(placement);
1028 if (p_list[index] !=
nullptr) {
1029 DCAST_INTO_R(_page, p_list[index], index);
1048 me->fillin(scan, manager);
1058 ImageFile::fillin(scan, manager);
1061 _cleared_regions.reserve(num_cleared_regions);
1062 for (
int i = 0; i < num_cleared_regions; i++) {
1063 _cleared_regions.push_back(ClearedRegion());
1064 _cleared_regions.back().fillin(scan);
bool write(const PNMImage &image) const
Writes out the image in the indicated PNMImage to the _filename and/or _alpha_filename.
bool is_filled() const
Returns true if the texture has been filled (i.e.
bool read(PNMImage &image) const
Reads in the image (or images, if the alpha_filename is separate) and stores it in the indicated PNMI...
int get_y_size() const
Returns the size in the Y dimension, in pixels, of the texture image as it must appear in the palette...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void not_solitary()
Indicates that the texture, formerly indicated as solitary, is now no longer.
double count_utilization() const
Returns the fraction of the PaletteImage that is actually used by any textures.
static void register_with_read_factory()
Registers the current object as something that can be read from a Bam file.
bool get_bool()
Extracts a boolean value.
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
bool exists() const
Returns true if the file or files named by the image file exist, false otherwise.
bool is_empty() const
Returns true if there are no textures, or only one "solitary" texture, placed on the image.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
int get_placed_x_size() const
Returns the size in the X dimension, in pixels, of the texture image as it has been placed within the...
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...
void alpha_fill(float alpha=0.0)
Sets the entire alpha channel to the given level.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
double count_coverage() const
Returns the a weighted average of the fraction of coverage represented by all of the textures placed ...
int get_y_size() const
Returns the size of the image file in pixels in the Y direction.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PaletteImage * get_image() const
Returns the particular PaletteImage on which the texture has been placed.
Base class for objects that can be written to and read from Bam files.
int get_placed_x() const
Returns the X pixel at which the texture has been placed within its PaletteImage.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
virtual void write_datagram(BamWriter *writer, Datagram &datagram)
Fills the indicated datagram up with a binary representation of the current object,...
This is a particular collection of textures, within a PaletteGroup, that all share the same TexturePr...
int32_t get_int32()
Extracts a signed 32-bit integer.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void place_at(PaletteImage *image, int x, int y)
Assigns the texture to a particular position within the indicated PaletteImage.
void omit_solitary()
Sets the omit reason (returned by get_omit()) to OR_solitary, indicating that the palettized version ...
std::string get_string()
Extracts a variable-length string.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_placed_y() const
Returns the Y pixel at which the texture has been placed within its PaletteImage.
void add_uint32(uint32_t value)
Adds an unsigned 32-bit integer to the datagram.
SourceTextureImage * get_preferred_source()
Determines the preferred source image for examining size and reading pixels, etc.
PalettePage * get_page() const
Returns the particular PalettePage this image is associated with.
bool place(TexturePlacement *placement)
Attempts to place the indicated texture on the image.
virtual void write_datagram(BamWriter *writer, Datagram &datagram)
Fills the indicated datagram up with a binary representation of the current object,...
bool set_filename(PaletteGroup *group, const std::string &basename)
Sets the filename, and if applicable, the alpha_filename, from the indicated basename.
void optimal_resize()
Attempts to resize the palette image to as small as it can go.
void parse_params(const FactoryParams ¶ms, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
void force_replace()
Removes the texture from its particular PaletteImage, but does not remove it from the PaletteGroup.
void mark_unfilled()
Marks the texture as unfilled, so that it will need to be copied into the palette image again.
void add_bool(bool value)
Adds a boolean value to the datagram.
bool is_size_known() const
Returns true if the texture's size is known, false otherwise.
void check_solitary()
To be called after all textures have been placed on the image, this checks to see if there is only on...
static Filename make_user_filename(Filename filename)
Returns a new filename that's made relative to the current directory, suitable for reporting to the u...
The name of a file, such as a texture file or an Egg file.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_x_size() const
Returns the size of the image file in pixels in the X direction.
void write_placements(std::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,...
void reset_image()
Unpacks each texture that has been placed on this image, resetting the image to empty.
PaletteGroup * get_group() const
Returns the group this particular PalettePage belongs to.
void read_pointers(DatagramIterator &scan, int count)
A convenience function to read a contiguous list of pointers.
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...
void add_string(const std::string &str)
Adds a variable-length string to the datagram.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
uint32_t get_uint32()
Extracts an unsigned 32-bit integer.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This corresponds to a particular assignment of a TextureImage with a PaletteGroup,...
bool unlink() const
Permanently deletes the file associated with the filename, if possible.
This is a texture image reference as it appears in an egg file: the source image of the texture.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
OmitReason get_omit_reason() const
Returns the reason the texture has been omitted from a palette image, or OR_none if it has not.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int compare_timestamps(const Filename &other, bool this_missing_is_old=true, bool other_missing_is_old=true) const
Returns a number less than zero if the file named by this object is older than the given file,...
void unlink()
Deletes the image file or files.
void clear()
Frees all memory allocated for the image, and clears all its parameters (size, color,...
TextureImage * get_texture() const
Returns the texture that this placement represents.
void add_int32(int32_t value)
Adds a signed 32-bit integer to the datagram.
void set_alpha(int x, int y, float a)
Sets the alpha component color only at the indicated pixel.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
int get_x_size() const
Returns the size in the X dimension, in pixels, of the texture image as it must appear in the palette...
void write_placed(std::ostream &out, int indent_level=0)
Writes the placement position information on a line by itself.
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
void setup_shadow_image()
Ensures the _shadow_image has the correct filename and image types, based on what was supplied on the...
bool is_texture_named() const
Returns true if this particular texture has been named by the user for procession this session,...
void fill_swapped_image(PNMImage &image, int index)
Fills in the rectangle of the swapped palette image represented by the texture placement with the ima...
bool make_shadow_image(const std::string &basename)
Sets up the ImageFile as a "shadow image" of a particular PaletteImage.
This is a single palette image, one of several within a PalettePage, which is in turn one of several ...
void unplace(TexturePlacement *placement)
Removes the texture from the image.
bool intersects(int x, int y, int x_size, int y_size)
Returns true if the particular position this texture has been assigned to overlaps the rectangle whos...
A class to retrieve the individual data elements previously stored in a Datagram.
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.
This represents a single source texture that is referenced by one or more egg files.
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.
TypeHandle is the identifier used to differentiate C++ class types.
void fill(float red, float green, float blue)
Sets the entire image (except the alpha channel) to the given color.
const Filename & get_filename() const
Returns the primary filename of the image file.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
void resize_swapped_image(int x_size, int y_size)
Attempts to resize the palette image, and repack all of the textures within the new size.
int get_placed_y_size() const
Returns the size in the Y dimension, in pixels, of the texture image as it has been placed within the...
void set_xel(int x, int y, const LRGBColorf &value)
Changes the RGB color at the indicated pixel.
void mark_eggs_stale()
Marks all the egg files that reference this placement stale.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool exists() const
Returns true if the filename exists on the disk, false otherwise.
bool update_filename()
Changes the image filename to match the current naming scheme, assuming something has changed since t...
void fill_image(PNMImage &image)
Fills in the rectangle of the palette image represented by the texture placement with the image pixel...
bool is_placed() const
Returns true if the texture has been placed on a palette image, false otherwise.
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
const TextureProperties & get_properties() const
Returns the texture grouping properties that all textures in this page share.