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