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