Panda3D
paletteImage.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file paletteImage.h
10  * @author drose
11  * @date 2000-11-28
12  */
13 
14 #ifndef PALETTEIMAGE_H
15 #define PALETTEIMAGE_H
16 
17 #include "pandatoolbase.h"
18 
19 #include "imageFile.h"
20 
21 #include "pnmImage.h"
22 
23 class PalettePage;
24 class TexturePlacement;
25 
26 /**
27  * This is a single palette image, one of several within a PalettePage, which
28  * is in turn one of several pages within a PaletteGroup. Each palette image
29  * is a collage of several different textures that were all assigned to the
30  * same PaletteGroup, and all share the same properties of the PalettePage.
31  */
32 class PaletteImage : public ImageFile {
33 private:
34  PaletteImage();
35 
36 public:
37  PaletteImage(PalettePage *page, int index);
38  PaletteImage(PalettePage *page, int index, unsigned swapIndex);
39 
40  PalettePage *get_page() const;
41 
42  bool is_empty() const;
43  double count_utilization() const;
44  double count_coverage() const;
45 
46  bool place(TexturePlacement *placement);
47  void unplace(TexturePlacement *placement);
48  void check_solitary();
49 
50  void optimal_resize();
51  bool resize_image(int x_size, int y_size);
52  void resize_swapped_image(int x_size, int y_size);
53 
54  void write_placements(std::ostream &out, int indent_level = 0) const;
55  void reset_image();
56  void setup_shadow_image();
57  void update_image(bool redo_all);
58 
59  bool update_filename();
60 
61 private:
62  bool setup_filename();
63  bool find_hole(int &x, int &y, int x_size, int y_size) const;
64  TexturePlacement *find_overlap(int x, int y, int x_size, int y_size) const;
65  void get_image();
66  void release_image();
67  void remove_image();
68  void get_swapped_image(int index);
69  void get_swapped_images();
70 
71  // The ClearedRegion object keeps track of TexturePlacements that were
72  // recently removed and thus need to be set to black.
73  class ClearedRegion {
74  public:
75  ClearedRegion();
76  ClearedRegion(TexturePlacement *placement);
77  ClearedRegion(const ClearedRegion &copy);
78  void operator = (const ClearedRegion &copy);
79  void clear(PNMImage &image);
80 
81  void write_datagram(Datagram &datagram) const;
82  void fillin(DatagramIterator &scan);
83 
84  private:
85  int _x, _y;
86  int _x_size, _y_size;
87  };
88 
90  ClearedRegions _cleared_regions;
91 
93  Placements _placements;
94 
95  Placements *_masterPlacements;
96 
97  PalettePage *_page;
98  int _index;
99  std::string _basename;
100 
101  bool _new_image;
102  bool _got_image;
103  PNMImage _image;
104 
105  unsigned _swapped_image; // 0 for non swapped image
106 
107  ImageFile _shadow_image;
108 
110  SwappedImages _swappedImages;
111 
112  // The TypedWritable interface follows.
113 public:
114  static void register_with_read_factory();
115  virtual void write_datagram(BamWriter *writer, Datagram &datagram);
116  virtual int complete_pointers(TypedWritable **p_list,
117  BamReader *manager);
118 
119 protected:
120  static TypedWritable *make_PaletteImage(const FactoryParams &params);
121  void fillin(DatagramIterator &scan, BamReader *manager);
122 
123 private:
124  // This value is only filled in while reading from the bam file; don't use
125  // it otherwise.
126  int _num_placements;
127 
128 public:
129  static TypeHandle get_class_type() {
130  return _type_handle;
131  }
132  static void init_type() {
133  ImageFile::init_type();
134  register_type(_type_handle, "PaletteImage",
135  ImageFile::get_class_type());
136  }
137  virtual TypeHandle get_type() const {
138  return get_class_type();
139  }
140 
141 private:
142  static TypeHandle _type_handle;
143 };
144 
145 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
Definition: pnmImage.h:58
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool is_empty() const
Returns true if there are no textures, or only one "solitary" texture, placed on the image.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
double count_coverage() const
Returns the a weighted average of the fraction of coverage represented by all of the textures placed ...
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
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...
Definition: palettePage.h:33
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.
void optimal_resize()
Attempts to resize the palette image to as small as it can go.
void check_solitary()
To be called after all textures have been placed on the image, this checks to see if there is only on...
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.
This is the base class of both TextureImage and PaletteImage.
Definition: imageFile.h:33
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...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
This corresponds to a particular assignment of a TextureImage with a PaletteGroup,...
void setup_shadow_image()
Ensures the _shadow_image has the correct filename and image types, based on what was supplied on the...
This is a single palette image, one of several within a PalettePage, which is in turn one of several ...
Definition: paletteImage.h:32
void unplace(TexturePlacement *placement)
Removes the texture from the image.
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.
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.
Definition: typeHandle.h:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
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.
bool update_filename()
Changes the image filename to match the current naming scheme, assuming something has changed since t...