pfmFile.h
00001 // Filename: pfmFile.h
00002 // Created by:  drose (23Dec10)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef PFMFILE_H
00016 #define PFMFILE_H
00017 
00018 #include "pandabase.h"
00019 #include "pnmImageHeader.h"
00020 #include "luse.h"
00021 #include "boundingHexahedron.h"
00022 #include "vector_float.h"
00023 
00024 class PNMImage;
00025 class PNMReader;
00026 class PNMWriter;
00027 
00028 ////////////////////////////////////////////////////////////////////
00029 //       Class : PfmFile
00030 // Description : Defines a pfm file, a 2-d table of floating-point
00031 //               numbers, either 3-component or 1-component, or with a
00032 //               special extension, 2- or 4-component.
00033 ////////////////////////////////////////////////////////////////////
00034 class EXPCL_PANDA_PNMIMAGE PfmFile : public PNMImageHeader {
00035 PUBLISHED:
00036   PfmFile();
00037   PfmFile(const PfmFile &copy);
00038   void operator = (const PfmFile &copy);
00039 
00040   void clear();
00041   void clear(int x_size, int y_size, int num_channels);
00042 
00043   BLOCKING bool read(const Filename &fullpath);
00044   BLOCKING bool read(istream &in, const Filename &fullpath = Filename());
00045   BLOCKING bool read(PNMReader *reader);
00046   BLOCKING bool write(const Filename &fullpath);
00047   BLOCKING bool write(ostream &out, const Filename &fullpath = Filename());
00048   BLOCKING bool write(PNMWriter *writer);
00049 
00050   BLOCKING bool load(const PNMImage &pnmimage);
00051   BLOCKING bool store(PNMImage &pnmimage) const;
00052   BLOCKING bool store_mask(PNMImage &pnmimage) const;
00053 
00054   INLINE bool is_valid() const;
00055 
00056   INLINE PN_float32 get_scale() const;
00057   INLINE void set_scale(PN_float32 scale);
00058 
00059   INLINE bool has_point(int x, int y) const;
00060   INLINE PN_float32 get_channel(int x, int y, int c) const;
00061   INLINE void set_channel(int x, int y, int c, PN_float32 value);
00062   INLINE PN_float32 get_point1(int x, int y) const;
00063   INLINE void set_point1(int x, int y, PN_float32 point);
00064   INLINE const LPoint2f &get_point2(int x, int y) const;
00065   INLINE void set_point2(int x, int y, const LVecBase2f &point);
00066   INLINE void set_point2(int x, int y, const LVecBase2d &point);
00067   INLINE LPoint2f &modify_point2(int x, int y);
00068   INLINE const LPoint3f &get_point(int x, int y) const;
00069   INLINE void set_point(int x, int y, const LVecBase3f &point);
00070   INLINE void set_point(int x, int y, const LVecBase3d &point);
00071   INLINE LPoint3f &modify_point(int x, int y);
00072   INLINE const LPoint3f &get_point3(int x, int y) const;
00073   INLINE void set_point3(int x, int y, const LVecBase3f &point);
00074   INLINE void set_point3(int x, int y, const LVecBase3d &point);
00075   INLINE LPoint3f &modify_point3(int x, int y);
00076   INLINE const LPoint4f &get_point4(int x, int y) const;
00077   INLINE void set_point4(int x, int y, const LVecBase4f &point);
00078   INLINE void set_point4(int x, int y, const LVecBase4d &point);
00079   INLINE LPoint4f &modify_point4(int x, int y);
00080 
00081   INLINE void fill(PN_float32 value);
00082   INLINE void fill(const LPoint2f &value);
00083   INLINE void fill(const LPoint3f &value);
00084   void fill(const LPoint4f &value);
00085   void fill_nan();
00086   void fill_no_data_value();
00087   void fill_channel(int channel, PN_float32 value);
00088   void fill_channel_nan(int channel);
00089   void fill_channel_masked(int channel, PN_float32 value);
00090   void fill_channel_masked_nan(int channel);
00091 
00092   BLOCKING bool calc_average_point(LPoint3f &result, PN_float32 x, PN_float32 y, PN_float32 radius) const;
00093   BLOCKING bool calc_bilinear_point(LPoint3f &result, PN_float32 x, PN_float32 y) const;
00094   BLOCKING bool calc_min_max(LVecBase3f &min_points, LVecBase3f &max_points) const;
00095   BLOCKING bool calc_autocrop(int &x_begin, int &x_end, int &y_begin, int &y_end) const;
00096   BLOCKING INLINE bool calc_autocrop(LVecBase4f &range) const;
00097   BLOCKING INLINE bool calc_autocrop(LVecBase4d &range) const;
00098 
00099   bool is_row_empty(int y, int x_begin, int x_end) const;
00100   bool is_column_empty(int x, int y_begin, int y_end) const;
00101 
00102   INLINE void set_zero_special(bool zero_special);
00103   INLINE void set_no_data_chan4(bool chan4);
00104   void set_no_data_nan(int num_channels);
00105   void set_no_data_value(const LPoint4f &no_data_value);
00106   INLINE void set_no_data_value(const LPoint4d &no_data_value);
00107   void set_no_data_threshold(const LPoint4f &no_data_value);
00108   INLINE void set_no_data_threshold(const LPoint4d &no_data_value);
00109   INLINE void clear_no_data_value();
00110   INLINE bool has_no_data_value() const;
00111   INLINE bool has_no_data_threshold() const;
00112   INLINE const LPoint4f &get_no_data_value() const;
00113 
00114   BLOCKING void resize(int new_x_size, int new_y_size);
00115   BLOCKING void box_filter_from(double radius, const PfmFile &copy);
00116   BLOCKING void gaussian_filter_from(double radius, const PfmFile &copy);
00117   BLOCKING void quick_filter_from(const PfmFile &copy);
00118 
00119   BLOCKING void reverse_rows();
00120   BLOCKING void flip(bool flip_x, bool flip_y, bool transpose);
00121   BLOCKING void xform(const LMatrix4f &transform);
00122   INLINE BLOCKING void xform(const LMatrix4d &transform);
00123   BLOCKING void forward_distort(const PfmFile &dist, PN_float32 scale_factor = 1.0);
00124   BLOCKING void reverse_distort(const PfmFile &dist, PN_float32 scale_factor = 1.0);
00125   BLOCKING void merge(const PfmFile &other);
00126   BLOCKING void copy_channel(int to_channel, const PfmFile &other, int from_channel);
00127   BLOCKING void copy_channel_masked(int to_channel, const PfmFile &other, int from_channel);
00128   BLOCKING void apply_crop(int x_begin, int x_end, int y_begin, int y_end);
00129   BLOCKING void clear_to_texcoords(int x_size, int y_size);
00130 
00131   BLOCKING int pull_spot(const LPoint4f &delta, double xc, double yc, 
00132                          double xr, double yr, double exponent);
00133 
00134   bool calc_tight_bounds(LPoint3f &min_point, LPoint3f &max_point) const;
00135   BLOCKING PT(BoundingHexahedron) compute_planar_bounds(const LPoint2f &center, PN_float32 point_dist, PN_float32 sample_radius, bool points_only) const;
00136   INLINE BLOCKING PT(BoundingHexahedron) compute_planar_bounds(const LPoint2d &center, PN_float32 point_dist, PN_float32 sample_radius, bool points_only) const;
00137   void compute_sample_point(LPoint3f &result,
00138                             PN_float32 x, PN_float32 y, PN_float32 sample_radius) const;
00139 
00140   void copy_sub_image(const PfmFile &copy, int xto, int yto,
00141                       int xfrom = 0, int yfrom = 0,
00142                       int x_size = -1, int y_size = -1);
00143 
00144   void output(ostream &out) const;
00145 
00146   EXTENSION(PyObject *get_points() const);
00147 
00148 #if PY_VERSION_HEX >= 0x02060000
00149   EXTENSION(int __getbuffer__(PyObject *self, Py_buffer *view, int flags) const);
00150 #endif
00151 
00152 public:
00153   INLINE const vector_float &get_table() const;
00154   INLINE void swap_table(vector_float &table);
00155 
00156 private:
00157   INLINE void setup_sub_image(const PfmFile &copy, int &xto, int &yto,
00158                               int &xfrom, int &yfrom, int &x_size, int &y_size,
00159                               int &xmin, int &ymin, int &xmax, int &ymax);
00160 
00161   void box_filter_region(PN_float32 &result,
00162                          PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
00163   void box_filter_region(LPoint2f &result,
00164                          PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
00165   void box_filter_region(LPoint3f &result,
00166                          PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
00167   void box_filter_region(LPoint4f &result,
00168                          PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
00169   void box_filter_line(PN_float32 &result, PN_float32 &coverage,
00170                        PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
00171   void box_filter_line(LPoint2f &result, PN_float32 &coverage,
00172                        PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
00173   void box_filter_line(LPoint3f &result, PN_float32 &coverage,
00174                        PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
00175   void box_filter_line(LPoint4f &result, PN_float32 &coverage,
00176                        PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
00177   void box_filter_point(PN_float32 &result, PN_float32 &coverage,
00178                         int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
00179   void box_filter_point(LPoint2f &result, PN_float32 &coverage,
00180                         int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
00181   void box_filter_point(LPoint3f &result, PN_float32 &coverage,
00182                         int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
00183   void box_filter_point(LPoint4f &result, PN_float32 &coverage,
00184                         int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
00185 
00186   class MiniGridCell {
00187   public:
00188     MiniGridCell() : _sxi(-1), _syi(-1), _dist(-1) { }
00189     int _sxi, _syi;
00190     int _dist;
00191   };
00192 
00193   void fill_mini_grid(MiniGridCell *mini_grid, int x_size, int y_size, 
00194                       int xi, int yi, int dist, int sxi, int syi) const;
00195 
00196   static bool has_point_noop(const PfmFile *file, int x, int y);
00197   static bool has_point_1(const PfmFile *file, int x, int y);
00198   static bool has_point_2(const PfmFile *file, int x, int y);
00199   static bool has_point_3(const PfmFile *file, int x, int y);
00200   static bool has_point_4(const PfmFile *file, int x, int y);
00201   static bool has_point_threshold_1(const PfmFile *file, int x, int y);
00202   static bool has_point_threshold_2(const PfmFile *file, int x, int y);
00203   static bool has_point_threshold_3(const PfmFile *file, int x, int y);
00204   static bool has_point_threshold_4(const PfmFile *file, int x, int y);
00205   static bool has_point_chan4(const PfmFile *file, int x, int y);
00206   static bool has_point_nan_1(const PfmFile *file, int x, int y);
00207   static bool has_point_nan_2(const PfmFile *file, int x, int y);
00208   static bool has_point_nan_3(const PfmFile *file, int x, int y);
00209   static bool has_point_nan_4(const PfmFile *file, int x, int y);
00210 
00211 private:
00212   typedef vector_float Table;
00213   Table _table;
00214 
00215   PN_float32 _scale;
00216 
00217   bool _has_no_data_value;
00218   bool _has_no_data_threshold;
00219   LPoint4f _no_data_value;
00220 
00221   typedef bool HasPointFunc(const PfmFile *file, int x, int y);
00222   HasPointFunc *_has_point;
00223 
00224   friend class PfmVizzer;
00225 };
00226 
00227 #include "pfmFile.I"
00228 
00229 #endif