Panda3D
pfmFile.h
1 // Filename: pfmFile.h
2 // Created by: drose (23Dec10)
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 PFMFILE_H
16 #define PFMFILE_H
17 
18 #include "pandabase.h"
19 #include "pnmImageHeader.h"
20 #include "luse.h"
21 #include "boundingHexahedron.h"
22 #include "vector_float.h"
23 
24 class PNMImage;
25 class PNMReader;
26 class PNMWriter;
27 
28 ////////////////////////////////////////////////////////////////////
29 // Class : PfmFile
30 // Description : Defines a pfm file, a 2-d table of floating-point
31 // numbers, either 3-component or 1-component, or with a
32 // special extension, 2- or 4-component.
33 ////////////////////////////////////////////////////////////////////
34 class EXPCL_PANDA_PNMIMAGE PfmFile : public PNMImageHeader {
35 PUBLISHED:
36  PfmFile();
37  PfmFile(const PfmFile &copy);
38  void operator = (const PfmFile &copy);
39 
40  void clear();
41  void clear(int x_size, int y_size, int num_channels);
42 
43  BLOCKING bool read(const Filename &fullpath);
44  BLOCKING bool read(istream &in, const Filename &fullpath = Filename());
45  BLOCKING bool read(PNMReader *reader);
46  BLOCKING bool write(const Filename &fullpath);
47  BLOCKING bool write(ostream &out, const Filename &fullpath = Filename());
48  BLOCKING bool write(PNMWriter *writer);
49 
50  BLOCKING bool load(const PNMImage &pnmimage);
51  BLOCKING bool store(PNMImage &pnmimage) const;
52  BLOCKING bool store_mask(PNMImage &pnmimage) const;
53 
54  INLINE bool is_valid() const;
55 
56  INLINE PN_float32 get_scale() const;
57  INLINE void set_scale(PN_float32 scale);
58 
59  INLINE bool has_point(int x, int y) const;
60  INLINE PN_float32 get_channel(int x, int y, int c) const;
61  INLINE void set_channel(int x, int y, int c, PN_float32 value);
62  INLINE PN_float32 get_point1(int x, int y) const;
63  INLINE void set_point1(int x, int y, PN_float32 point);
64  INLINE const LPoint2f &get_point2(int x, int y) const;
65  INLINE void set_point2(int x, int y, const LVecBase2f &point);
66  INLINE void set_point2(int x, int y, const LVecBase2d &point);
67  INLINE LPoint2f &modify_point2(int x, int y);
68  INLINE const LPoint3f &get_point(int x, int y) const;
69  INLINE void set_point(int x, int y, const LVecBase3f &point);
70  INLINE void set_point(int x, int y, const LVecBase3d &point);
71  INLINE LPoint3f &modify_point(int x, int y);
72  INLINE const LPoint3f &get_point3(int x, int y) const;
73  INLINE void set_point3(int x, int y, const LVecBase3f &point);
74  INLINE void set_point3(int x, int y, const LVecBase3d &point);
75  INLINE LPoint3f &modify_point3(int x, int y);
76  INLINE const LPoint4f &get_point4(int x, int y) const;
77  INLINE void set_point4(int x, int y, const LVecBase4f &point);
78  INLINE void set_point4(int x, int y, const LVecBase4d &point);
79  INLINE LPoint4f &modify_point4(int x, int y);
80 
81  INLINE void fill(PN_float32 value);
82  INLINE void fill(const LPoint2f &value);
83  INLINE void fill(const LPoint3f &value);
84  void fill(const LPoint4f &value);
85  void fill_nan();
86  void fill_no_data_value();
87  void fill_channel(int channel, PN_float32 value);
88  void fill_channel_nan(int channel);
89  void fill_channel_masked(int channel, PN_float32 value);
90  void fill_channel_masked_nan(int channel);
91 
92  BLOCKING bool calc_average_point(LPoint3f &result, PN_float32 x, PN_float32 y, PN_float32 radius) const;
93  BLOCKING bool calc_bilinear_point(LPoint3f &result, PN_float32 x, PN_float32 y) const;
94  BLOCKING bool calc_min_max(LVecBase3f &min_points, LVecBase3f &max_points) const;
95  BLOCKING bool calc_autocrop(int &x_begin, int &x_end, int &y_begin, int &y_end) const;
96  BLOCKING INLINE bool calc_autocrop(LVecBase4f &range) const;
97  BLOCKING INLINE bool calc_autocrop(LVecBase4d &range) const;
98 
99  bool is_row_empty(int y, int x_begin, int x_end) const;
100  bool is_column_empty(int x, int y_begin, int y_end) const;
101 
102  INLINE void set_zero_special(bool zero_special);
103  INLINE void set_no_data_chan4(bool chan4);
104  void set_no_data_nan(int num_channels);
105  void set_no_data_value(const LPoint4f &no_data_value);
106  INLINE void set_no_data_value(const LPoint4d &no_data_value);
107  void set_no_data_threshold(const LPoint4f &no_data_value);
108  INLINE void set_no_data_threshold(const LPoint4d &no_data_value);
109  INLINE void clear_no_data_value();
110  INLINE bool has_no_data_value() const;
111  INLINE bool has_no_data_threshold() const;
112  INLINE const LPoint4f &get_no_data_value() const;
113 
114  BLOCKING void resize(int new_x_size, int new_y_size);
115  BLOCKING void box_filter_from(float radius, const PfmFile &copy);
116  BLOCKING void gaussian_filter_from(float radius, const PfmFile &copy);
117  BLOCKING void quick_filter_from(const PfmFile &copy);
118 
119  BLOCKING void reverse_rows();
120  BLOCKING void flip(bool flip_x, bool flip_y, bool transpose);
121  BLOCKING void xform(const LMatrix4f &transform);
122  INLINE BLOCKING void xform(const LMatrix4d &transform);
123  BLOCKING void forward_distort(const PfmFile &dist, PN_float32 scale_factor = 1.0);
124  BLOCKING void reverse_distort(const PfmFile &dist, PN_float32 scale_factor = 1.0);
125  BLOCKING void merge(const PfmFile &other);
126  BLOCKING void copy_channel(int to_channel, const PfmFile &other, int from_channel);
127  BLOCKING void copy_channel_masked(int to_channel, const PfmFile &other, int from_channel);
128  BLOCKING void apply_crop(int x_begin, int x_end, int y_begin, int y_end);
129  BLOCKING void clear_to_texcoords(int x_size, int y_size);
130 
131  BLOCKING int pull_spot(const LPoint4f &delta, float xc, float yc,
132  float xr, float yr, float exponent);
133 
134  bool calc_tight_bounds(LPoint3f &min_point, LPoint3f &max_point) const;
135  BLOCKING PT(BoundingHexahedron) compute_planar_bounds(const LPoint2f &center, PN_float32 point_dist, PN_float32 sample_radius, bool points_only) const;
136  INLINE BLOCKING PT(BoundingHexahedron) compute_planar_bounds(const LPoint2d &center, PN_float32 point_dist, PN_float32 sample_radius, bool points_only) const;
137  void compute_sample_point(LPoint3f &result,
138  PN_float32 x, PN_float32 y, PN_float32 sample_radius) const;
139 
140  void copy_sub_image(const PfmFile &copy, int xto, int yto,
141  int xfrom = 0, int yfrom = 0,
142  int x_size = -1, int y_size = -1);
143  void add_sub_image(const PfmFile &copy, int xto, int yto,
144  int xfrom = 0, int yfrom = 0,
145  int x_size = -1, int y_size = -1,
146  float pixel_scale = 1.0);
147  void mult_sub_image(const PfmFile &copy, int xto, int yto,
148  int xfrom = 0, int yfrom = 0,
149  int x_size = -1, int y_size = -1,
150  float pixel_scale = 1.0);
151  void divide_sub_image(const PfmFile &copy, int xto, int yto,
152  int xfrom = 0, int yfrom = 0,
153  int x_size = -1, int y_size = -1,
154  float pixel_scale = 1.0);
155 
156  void operator *= (float multiplier);
157 
158  void output(ostream &out) const;
159 
160  EXTENSION(PyObject *get_points() const);
161 
162 #if PY_VERSION_HEX >= 0x02060000
163  EXTENSION(int __getbuffer__(PyObject *self, Py_buffer *view, int flags) const);
164 #endif
165 
166 public:
167  INLINE const vector_float &get_table() const;
168  INLINE void swap_table(vector_float &table);
169 
170 private:
171  INLINE void setup_sub_image(const PfmFile &copy, int &xto, int &yto,
172  int &xfrom, int &yfrom, int &x_size, int &y_size,
173  int &xmin, int &ymin, int &xmax, int &ymax);
174 
175  void box_filter_region(PN_float32 &result,
176  PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
177  void box_filter_region(LPoint2f &result,
178  PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
179  void box_filter_region(LPoint3f &result,
180  PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
181  void box_filter_region(LPoint4f &result,
182  PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
183  void box_filter_line(PN_float32 &result, PN_float32 &coverage,
184  PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
185  void box_filter_line(LPoint2f &result, PN_float32 &coverage,
186  PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
187  void box_filter_line(LPoint3f &result, PN_float32 &coverage,
188  PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
189  void box_filter_line(LPoint4f &result, PN_float32 &coverage,
190  PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
191  void box_filter_point(PN_float32 &result, PN_float32 &coverage,
192  int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
193  void box_filter_point(LPoint2f &result, PN_float32 &coverage,
194  int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
195  void box_filter_point(LPoint3f &result, PN_float32 &coverage,
196  int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
197  void box_filter_point(LPoint4f &result, PN_float32 &coverage,
198  int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
199 
200  class MiniGridCell {
201  public:
202  MiniGridCell() : _sxi(-1), _syi(-1), _dist(-1) { }
203  int _sxi, _syi;
204  int _dist;
205  };
206 
207  void fill_mini_grid(MiniGridCell *mini_grid, int x_size, int y_size,
208  int xi, int yi, int dist, int sxi, int syi) const;
209 
210  static bool has_point_noop(const PfmFile *file, int x, int y);
211  static bool has_point_1(const PfmFile *file, int x, int y);
212  static bool has_point_2(const PfmFile *file, int x, int y);
213  static bool has_point_3(const PfmFile *file, int x, int y);
214  static bool has_point_4(const PfmFile *file, int x, int y);
215  static bool has_point_threshold_1(const PfmFile *file, int x, int y);
216  static bool has_point_threshold_2(const PfmFile *file, int x, int y);
217  static bool has_point_threshold_3(const PfmFile *file, int x, int y);
218  static bool has_point_threshold_4(const PfmFile *file, int x, int y);
219  static bool has_point_chan4(const PfmFile *file, int x, int y);
220  static bool has_point_nan_1(const PfmFile *file, int x, int y);
221  static bool has_point_nan_2(const PfmFile *file, int x, int y);
222  static bool has_point_nan_3(const PfmFile *file, int x, int y);
223  static bool has_point_nan_4(const PfmFile *file, int x, int y);
224 
225 private:
226  typedef vector_float Table;
227  Table _table;
228 
229  PN_float32 _scale;
230 
231  bool _has_no_data_value;
232  bool _has_no_data_threshold;
233  LPoint4f _no_data_value;
234 
235  typedef bool HasPointFunc(const PfmFile *file, int x, int y);
236  HasPointFunc *_has_point;
237 
238  friend class PfmVizzer;
239 };
240 
241 #include "pfmFile.I"
242 
243 #endif
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:4716
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 base class for all two-component vectors and points.
Definition: lvecBase2.h:1257
This is a four-component point in space.
Definition: lpoint4.h:457
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:1677
This is a two-component point in space.
Definition: lpoint2.h:424
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
Defines a pfm file, a 2-d table of floating-point numbers, either 3-component or 1-component, or with a special extension, 2- or 4-component.
Definition: pfmFile.h:34
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:105
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:1471
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
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
This is a four-component point in space.
Definition: lpoint4.h:91
This is the base class of PNMImage, PNMReader, and PNMWriter.
This is a two-component point in space.
Definition: lpoint2.h:92
This defines a bounding convex hexahedron.
This class aids in the visualization and manipulation of PfmFile objects.
Definition: pfmVizzer.h:33