Panda3D
pnmImageHeader.h
1 // Filename: pnmImageHeader.h
2 // Created by: drose (14Jun00)
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 PNMIMAGEHEADER_H
16 #define PNMIMAGEHEADER_H
17 
18 #include "pandabase.h"
19 
20 #include "pnmimage_base.h"
21 
22 #include "typedObject.h"
23 #include "filename.h"
24 #include "pnotify.h"
25 #include "pmap.h"
26 #include "pvector.h"
27 #include "colorSpace.h"
28 
29 class PNMFileType;
30 class PNMReader;
31 class PNMWriter;
32 
33 ////////////////////////////////////////////////////////////////////
34 // Class : PNMImageHeader
35 // Description : This is the base class of PNMImage, PNMReader, and
36 // PNMWriter. It encapsulates all the information
37 // associated with an image that describes its size,
38 // number of channels, etc; that is, all the information
39 // about the image except the image data itself. It's
40 // the sort of information you typically read from the
41 // image file's header.
42 ////////////////////////////////////////////////////////////////////
43 class EXPCL_PANDA_PNMIMAGE PNMImageHeader {
44 PUBLISHED:
45  INLINE PNMImageHeader();
46  INLINE PNMImageHeader(const PNMImageHeader &copy);
47  INLINE void operator = (const PNMImageHeader &copy);
48  INLINE ~PNMImageHeader();
49 
50  // This enumerated type indicates the number of channels in the
51  // image, and also implies an image type. You can treat it either
52  // as an integer number of channels or as an enumerated image type.
53  enum ColorType {
54  CT_invalid = 0,
55  CT_grayscale = 1,
56  CT_two_channel = 2,
57  CT_color = 3,
58  CT_four_channel = 4,
59  };
60 
61  INLINE ColorType get_color_type() const;
62  INLINE int get_num_channels() const;
63 
64  INLINE static bool is_grayscale(ColorType color_type);
65  INLINE bool is_grayscale() const;
66 
67  INLINE static bool has_alpha(ColorType color_type);
68  INLINE bool has_alpha() const;
69 
70  INLINE xelval get_maxval() const;
71  INLINE ColorSpace get_color_space() const;
72 
73  INLINE int get_x_size() const;
74  INLINE int get_y_size() const;
75 
76  INLINE string get_comment() const;
77  INLINE void set_comment(const string &comment);
78 
79  INLINE bool has_type() const;
80  INLINE PNMFileType *get_type() const;
81  INLINE void set_type(PNMFileType *type);
82 
83  BLOCKING bool read_header(const Filename &filename, PNMFileType *type = NULL,
84  bool report_unknown_type = true);
85  BLOCKING bool read_header(istream &data, const string &filename = string(),
86  PNMFileType *type = NULL, bool report_unknown_type = true);
87 
88  PNMReader *make_reader(const Filename &filename,
89  PNMFileType *type = NULL,
90  bool report_unknown_type = true) const;
91  PNMReader *make_reader(istream *file, bool owns_file = true,
92  const Filename &filename = Filename(),
93  string magic_number = string(),
94  PNMFileType *type = NULL,
95  bool report_unknown_type = true) const;
96 
97  PNMWriter *make_writer(const Filename &filename,
98  PNMFileType *type = NULL) const;
99  PNMWriter *make_writer(ostream *file, bool owns_file = true,
100  const Filename &filename = Filename(),
101  PNMFileType *type = NULL) const;
102 
103  static bool read_magic_number(istream *file, string &magic_number,
104  int num_bytes);
105 
106  void output(ostream &out) const;
107 
108  // Contains a single pixel specification used in compute_histogram()
109  // and make_histogram(). Note that pixels are stored by integer
110  // value, not by floating-point scaled value.
111  class EXPCL_PANDA_PNMIMAGE PixelSpec {
112  PUBLISHED:
113  INLINE PixelSpec(xelval gray_value);
114  INLINE PixelSpec(xelval gray_value, xelval alpha);
115  INLINE PixelSpec(xelval red, xelval green, xelval blue);
116  INLINE PixelSpec(xelval red, xelval green, xelval blue, xelval alpha);
117  INLINE PixelSpec(const xel &rgb);
118  INLINE PixelSpec(const xel &rgb, xelval alpha);
119  INLINE PixelSpec(const PixelSpec &copy);
120  INLINE void operator = (const PixelSpec &copy);
121 
122  INLINE bool operator < (const PixelSpec &other) const;
123  INLINE bool operator == (const PixelSpec &other) const;
124  INLINE bool operator != (const PixelSpec &other) const;
125  INLINE int compare_to(const PixelSpec &other) const;
126 
127  INLINE xelval get_red() const;
128  INLINE xelval get_green() const;
129  INLINE xelval get_blue() const;
130  INLINE xelval get_alpha() const;
131 
132  INLINE void set_red(xelval red);
133  INLINE void set_green(xelval green);
134  INLINE void set_blue(xelval blue);
135  INLINE void set_alpha(xelval alpha);
136 
137  INLINE xelval operator [](int n) const;
138  INLINE static int size();
139 
140  void output(ostream &out) const;
141 
142  public:
143  xelval _red, _green, _blue, _alpha;
144  };
145 
146  // Associates a pixel specification with an appearance count, for
147  // use in Histogram, below.
148  class EXPCL_PANDA_PNMIMAGE PixelSpecCount {
149  public:
150  INLINE PixelSpecCount(const PixelSpec &pixel, int count);
151  INLINE bool operator < (const PixelSpecCount &other) const;
152 
153  PixelSpec _pixel;
154  int _count;
155  };
156 
159  typedef pvector<PixelSpec> Palette;
160 
161  // Used to return a pixel histogram in PNMImage::get_histogram().
162  class EXPCL_PANDA_PNMIMAGE Histogram {
163  PUBLISHED:
164  INLINE Histogram();
165 
166  INLINE int get_num_pixels() const;
167  INLINE const PixelSpec &get_pixel(int n) const;
168  INLINE int get_count(int n) const;
169  INLINE int get_count(const PixelSpec &pixel) const;
170  MAKE_SEQ(get_pixels, get_num_pixels, get_pixel);
171 
172  void write(ostream &out) const;
173 
174  public:
175  INLINE void swap(PixelCount &pixels, HistMap &hist_map);
176 
177  private:
178  PixelCount _pixels;
179  HistMap _hist_map;
180  };
181 
182 protected:
183  bool compute_histogram(HistMap &hist, xel *array, xelval *alpha,
184  int max_colors = 0);
185  bool compute_palette(Palette &palette, xel *array, xelval *alpha,
186  int max_colors = 0);
187  INLINE void record_color(HistMap &hist, const PixelSpec &color);
188 
189  int _x_size, _y_size;
190  int _num_channels;
191  xelval _maxval;
192  ColorSpace _color_space;
193  string _comment;
194  PNMFileType *_type;
195 };
196 
197 INLINE ostream &operator << (ostream &out, const PNMImageHeader &header) {
198  header.output(out);
199  return out;
200 }
201 
202 INLINE ostream &operator << (ostream &out, const PNMImageHeader::PixelSpec &pixel) {
203  pixel.output(out);
204  return out;
205 }
206 
207 #include "pnmImageHeader.I"
208 
209 #endif
This is the base class of a family of classes that represent particular image file types that PNMImag...
Definition: pnmFileType.h:35
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
This is an abstract base class that defines the interface for reading image files of various types...
Definition: pnmReader.h:31
This is an abstract base class that defines the interface for writing image files of various types...
Definition: pnmWriter.h:31
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:79
This is the base class of PNMImage, PNMReader, and PNMWriter.