pfmFile.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 pfmFile.h
10  * @author drose
11  * @date 2010-12-23
12  */
13 
14 #ifndef PFMFILE_H
15 #define PFMFILE_H
16 
17 #include "pandabase.h"
18 #include "pnmImageHeader.h"
19 #include "luse.h"
20 #include "boundingHexahedron.h"
21 #include "vector_float.h"
22 
23 class PNMImage;
24 class PNMReader;
25 class PNMWriter;
26 
27 /**
28  * Defines a pfm file, a 2-d table of floating-point numbers, either
29  * 3-component or 1-component, or with a special extension, 2- or 4-component.
30  */
31 class EXPCL_PANDA_PNMIMAGE PfmFile : public PNMImageHeader {
32 PUBLISHED:
33  PfmFile();
34  PfmFile(const PfmFile &copy);
35  void operator = (const PfmFile &copy);
36 
37  void clear();
38  void clear(int x_size, int y_size, int num_channels);
39 
40  BLOCKING bool read(const Filename &fullpath);
41  BLOCKING bool read(istream &in, const Filename &fullpath = Filename());
42  BLOCKING bool read(PNMReader *reader);
43  BLOCKING bool write(const Filename &fullpath);
44  BLOCKING bool write(ostream &out, const Filename &fullpath = Filename());
45  BLOCKING bool write(PNMWriter *writer);
46 
47  BLOCKING bool load(const PNMImage &pnmimage);
48  BLOCKING bool store(PNMImage &pnmimage) const;
49  BLOCKING bool store_mask(PNMImage &pnmimage) const;
50  BLOCKING bool store_mask(PNMImage &pnmimage, const LVecBase4f &min_point, const LVecBase4f &max_point) const;
51 
52  INLINE bool is_valid() const;
53  MAKE_PROPERTY(valid, is_valid);
54 
55  INLINE PN_float32 get_scale() const;
56  INLINE void set_scale(PN_float32 scale);
57  MAKE_PROPERTY(scale, get_scale, set_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 apply_1d_lut(int channel, const PfmFile &lut, PN_float32 x_scale = 1.0);
126 
127  BLOCKING void merge(const PfmFile &other);
128  BLOCKING void apply_mask(const PfmFile &other);
129  BLOCKING void copy_channel(int to_channel, const PfmFile &other, int from_channel);
130  BLOCKING void copy_channel_masked(int to_channel, const PfmFile &other, int from_channel);
131  BLOCKING void apply_crop(int x_begin, int x_end, int y_begin, int y_end);
132  BLOCKING void clear_to_texcoords(int x_size, int y_size);
133 
134  BLOCKING int pull_spot(const LPoint4f &delta, float xc, float yc,
135  float xr, float yr, float exponent);
136 
137  bool calc_tight_bounds(LPoint3f &min_point, LPoint3f &max_point) const;
138  BLOCKING PT(BoundingHexahedron) compute_planar_bounds(const LPoint2f &center, PN_float32 point_dist, PN_float32 sample_radius, bool points_only) const;
139  INLINE BLOCKING PT(BoundingHexahedron) compute_planar_bounds(const LPoint2d &center, PN_float32 point_dist, PN_float32 sample_radius, bool points_only) const;
140  void compute_sample_point(LPoint3f &result,
141  PN_float32 x, PN_float32 y, PN_float32 sample_radius) const;
142 
143  void copy_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  void add_sub_image(const PfmFile &copy, int xto, int yto,
147  int xfrom = 0, int yfrom = 0,
148  int x_size = -1, int y_size = -1,
149  float pixel_scale = 1.0);
150  void mult_sub_image(const PfmFile &copy, int xto, int yto,
151  int xfrom = 0, int yfrom = 0,
152  int x_size = -1, int y_size = -1,
153  float pixel_scale = 1.0);
154  void divide_sub_image(const PfmFile &copy, int xto, int yto,
155  int xfrom = 0, int yfrom = 0,
156  int x_size = -1, int y_size = -1,
157  float pixel_scale = 1.0);
158 
159  void operator *= (float multiplier);
160 
161  void indirect_1d_lookup(const PfmFile &index_image, int channel,
162  const PfmFile &pixel_values);
163 
164  INLINE void gamma_correct(float from_gamma, float to_gamma);
165  INLINE void gamma_correct_alpha(float from_gamma, float to_gamma);
166  INLINE void apply_exponent(float gray_exponent);
167  INLINE void apply_exponent(float gray_exponent, float alpha_exponent);
168  INLINE void apply_exponent(float c0_exponent, float c1_exponent, float c2_exponent);
169  void apply_exponent(float c0_exponent, float c1_exponent, float c2_exponent, float c3_exponent);
170 
171  void output(ostream &out) const;
172 
173 #ifdef HAVE_PYTHON
174  EXTENSION(PyObject *get_points() const);
175 
176  EXTENSION(int __getbuffer__(PyObject *self, Py_buffer *view, int flags) const);
177 #endif
178 
179 public:
180  INLINE const vector_float &get_table() const;
181  INLINE void swap_table(vector_float &table);
182 
183 private:
184  INLINE void setup_sub_image(const PfmFile &copy, int &xto, int &yto,
185  int &xfrom, int &yfrom, int &x_size, int &y_size,
186  int &xmin, int &ymin, int &xmax, int &ymax);
187 
188  void box_filter_region(PN_float32 &result,
189  PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
190  void box_filter_region(LPoint2f &result,
191  PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
192  void box_filter_region(LPoint3f &result,
193  PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
194  void box_filter_region(LPoint4f &result,
195  PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
196  void box_filter_line(PN_float32 &result, PN_float32 &coverage,
197  PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
198  void box_filter_line(LPoint2f &result, PN_float32 &coverage,
199  PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
200  void box_filter_line(LPoint3f &result, PN_float32 &coverage,
201  PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
202  void box_filter_line(LPoint4f &result, PN_float32 &coverage,
203  PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
204  void box_filter_point(PN_float32 &result, PN_float32 &coverage,
205  int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
206  void box_filter_point(LPoint2f &result, PN_float32 &coverage,
207  int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
208  void box_filter_point(LPoint3f &result, PN_float32 &coverage,
209  int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
210  void box_filter_point(LPoint4f &result, PN_float32 &coverage,
211  int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
212 
213  class MiniGridCell {
214  public:
215  MiniGridCell() : _sxi(-1), _syi(-1), _dist(-1) { }
216  int _sxi, _syi;
217  int _dist;
218  };
219 
220  void fill_mini_grid(MiniGridCell *mini_grid, int x_size, int y_size,
221  int xi, int yi, int dist, int sxi, int syi) const;
222 
223  static bool has_point_noop(const PfmFile *file, int x, int y);
224  static bool has_point_1(const PfmFile *file, int x, int y);
225  static bool has_point_2(const PfmFile *file, int x, int y);
226  static bool has_point_3(const PfmFile *file, int x, int y);
227  static bool has_point_4(const PfmFile *file, int x, int y);
228  static bool has_point_threshold_1(const PfmFile *file, int x, int y);
229  static bool has_point_threshold_2(const PfmFile *file, int x, int y);
230  static bool has_point_threshold_3(const PfmFile *file, int x, int y);
231  static bool has_point_threshold_4(const PfmFile *file, int x, int y);
232  static bool has_point_chan4(const PfmFile *file, int x, int y);
233  static bool has_point_nan_1(const PfmFile *file, int x, int y);
234  static bool has_point_nan_2(const PfmFile *file, int x, int y);
235  static bool has_point_nan_3(const PfmFile *file, int x, int y);
236  static bool has_point_nan_4(const PfmFile *file, int x, int y);
237 
238 private:
239  typedef vector_float Table;
240  Table _table;
241 
242  PN_float32 _scale;
243 
244  bool _has_no_data_value;
245  bool _has_no_data_threshold;
246  LPoint4f _no_data_value;
247 
248  typedef bool HasPointFunc(const PfmFile *file, int x, int y);
249  HasPointFunc *_has_point;
250 
251  friend class PfmVizzer;
252 };
253 
254 #include "pfmFile.I"
255 
256 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
CPT(RenderAttrib) ShaderAttrib CPT(RenderAttrib) ShaderAttrib result _inputs clear()
Clears all the shader inputs on the attrib.
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
This is the base class of PNMImage, PNMReader, and PNMWriter.
This is an abstract base class that defines the interface for writing image files of various types...
Definition: pnmWriter.h:27
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
return result
Returns a new RenderEffects object that represents the same as the source state, with the new RenderE...
This defines a bounding convex hexahedron.
PT(EggVertex) QtessSurface LVertex point
Evaluates the surface at the given u, v position and sets the vertex to the appropriate values...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class aids in the visualization and manipulation of PfmFile objects.
Definition: pfmVizzer.h:30
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:31
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
PT(BoundingHexahedron) PfmFile compute_sample_point(p0, center[0]+point_dist, center[1]-point_dist, sample_radius)
Computes the minmax bounding volume of the points in 3-D space, assuming the points represent a mostl...
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
CPT(RenderAttrib) ColorScaleAttrib attrib _scale
Returns a new ColorScaleAttrib, just like this one, but with the scale changed to the indicated value...