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   INLINE void clear_no_data_value();
00108   INLINE bool has_no_data_value() const;
00109   INLINE const LPoint4f &get_no_data_value() const;
00110 
00111   BLOCKING void resize(int new_x_size, int new_y_size);
00112   BLOCKING void box_filter_from(double radius, const PfmFile &copy);
00113   BLOCKING void gaussian_filter_from(double radius, const PfmFile &copy);
00114   BLOCKING void quick_filter_from(const PfmFile &copy);
00115 
00116   BLOCKING void reverse_rows();
00117   BLOCKING void flip(bool flip_x, bool flip_y, bool transpose);
00118   BLOCKING void xform(const LMatrix4f &transform);
00119   INLINE BLOCKING void xform(const LMatrix4d &transform);
00120   BLOCKING void forward_distort(const PfmFile &dist, PN_float32 scale_factor = 1.0);
00121   BLOCKING void reverse_distort(const PfmFile &dist, PN_float32 scale_factor = 1.0);
00122   BLOCKING void merge(const PfmFile &other);
00123   BLOCKING void copy_channel(int to_channel, const PfmFile &other, int from_channel);
00124   BLOCKING void apply_crop(int x_begin, int x_end, int y_begin, int y_end);
00125   BLOCKING void clear_to_texcoords(int x_size, int y_size);
00126 
00127   BLOCKING int pull_spot(const LPoint4f &delta, double xc, double yc, 
00128                          double xr, double yr, double exponent);
00129 
00130   bool calc_tight_bounds(LPoint3f &min_point, LPoint3f &max_point) const;
00131   BLOCKING PT(BoundingHexahedron) compute_planar_bounds(const LPoint2f &center, PN_float32 point_dist, PN_float32 sample_radius, bool points_only) const;
00132   INLINE BLOCKING PT(BoundingHexahedron) compute_planar_bounds(const LPoint2d &center, PN_float32 point_dist, PN_float32 sample_radius, bool points_only) const;
00133   void compute_sample_point(LPoint3f &result,
00134                             PN_float32 x, PN_float32 y, PN_float32 sample_radius) const;
00135 
00136   void copy_sub_image(const PfmFile &copy, int xto, int yto,
00137                       int xfrom = 0, int yfrom = 0,
00138                       int x_size = -1, int y_size = -1);
00139 
00140   void output(ostream &out) const;
00141 
00142 public:
00143   INLINE const vector_float &get_table() const;
00144   INLINE void swap_table(vector_float &table);
00145 
00146 private:
00147   INLINE void setup_sub_image(const PfmFile &copy, int &xto, int &yto,
00148                               int &xfrom, int &yfrom, int &x_size, int &y_size,
00149                               int &xmin, int &ymin, int &xmax, int &ymax);
00150 
00151   void box_filter_region(PN_float32 &result,
00152                          PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
00153   void box_filter_region(LPoint2f &result,
00154                          PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
00155   void box_filter_region(LPoint3f &result,
00156                          PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
00157   void box_filter_region(LPoint4f &result,
00158                          PN_float32 x0, PN_float32 y0, PN_float32 x1, PN_float32 y1) const;
00159   void box_filter_line(PN_float32 &result, PN_float32 &coverage,
00160                        PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
00161   void box_filter_line(LPoint2f &result, PN_float32 &coverage,
00162                        PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
00163   void box_filter_line(LPoint3f &result, PN_float32 &coverage,
00164                        PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
00165   void box_filter_line(LPoint4f &result, PN_float32 &coverage,
00166                        PN_float32 x0, int y, PN_float32 x1, PN_float32 y_contrib) const;
00167   void box_filter_point(PN_float32 &result, PN_float32 &coverage,
00168                         int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
00169   void box_filter_point(LPoint2f &result, PN_float32 &coverage,
00170                         int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
00171   void box_filter_point(LPoint3f &result, PN_float32 &coverage,
00172                         int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
00173   void box_filter_point(LPoint4f &result, PN_float32 &coverage,
00174                         int x, int y, PN_float32 x_contrib, PN_float32 y_contrib) const;
00175 
00176   class MiniGridCell {
00177   public:
00178     MiniGridCell() : _sxi(-1), _syi(-1), _dist(-1) { }
00179     int _sxi, _syi;
00180     int _dist;
00181   };
00182 
00183   void fill_mini_grid(MiniGridCell *mini_grid, int x_size, int y_size, 
00184                       int xi, int yi, int dist, int sxi, int syi) const;
00185 
00186   static bool has_point_noop(const PfmFile *file, int x, int y);
00187   static bool has_point_1(const PfmFile *file, int x, int y);
00188   static bool has_point_2(const PfmFile *file, int x, int y);
00189   static bool has_point_3(const PfmFile *file, int x, int y);
00190   static bool has_point_4(const PfmFile *file, int x, int y);
00191   static bool has_point_chan4(const PfmFile *file, int x, int y);
00192   static bool has_point_nan_1(const PfmFile *file, int x, int y);
00193   static bool has_point_nan_2(const PfmFile *file, int x, int y);
00194   static bool has_point_nan_3(const PfmFile *file, int x, int y);
00195   static bool has_point_nan_4(const PfmFile *file, int x, int y);
00196 
00197 private:
00198   typedef vector_float Table;
00199   Table _table;
00200 
00201   PN_float32 _scale;
00202 
00203   bool _has_no_data_value;
00204   LPoint4f _no_data_value;
00205 
00206   typedef bool HasPointFunc(const PfmFile *file, int x, int y);
00207   HasPointFunc *_has_point;
00208 
00209   friend class PfmVizzer;
00210 };
00211 
00212 #include "pfmFile.I"
00213 
00214 #endif