contrib/mul/mbl/mbl_eps_writer.h
Go to the documentation of this file.
00001 #ifndef mbl_eps_writer_h_
00002 #define mbl_eps_writer_h_
00003 //:
00004 // \file
00005 // \brief Class to generate simple EPS files containing images and lines
00006 // \author Tim Cootes
00007 
00008 #include <vcl_string.h>
00009 #include <vcl_fstream.h>
00010 #include <vsl/vsl_fwd.h>
00011 #include <vil/vil_image_view.h>
00012 #include <vgl/vgl_point_2d.h>
00013 
00014 #include <vxl_config.h>
00015 
00016 //: Class to generate simple EPS files containing images and lines
00017 //  Note that Postscript uses units of points=1/72 inch. Scaling may
00018 //  be required.  Also, by default origin is in bottom left.  This
00019 //  class treats the origin at top right, to comply with usual VXL
00020 //  standards.  The drawing area is a box (nx,ny), based at the origin.
00021 //
00022 // The output eps file should be suitable for use in LaTeX documents.
00023 //
00024 // Example usage:
00025 // \code
00026 // mbl_eps_writer eps_writer("example.eps",100,100);
00027 // eps_writer.set_colour("blue");
00028 // eps_writer.draw_circle(centre,radius);
00029 // eps_writer.close();
00030 // \endcode
00031 
00032 class mbl_eps_writer
00033 {
00034  private:
00035   //: Stream to eps file
00036   vcl_ofstream ofs_;
00037 
00038   //: Bounding box (in points)
00039   double nx_,ny_;
00040 
00041   //: Current scaling
00042   double sx_,sy_;
00043 
00044   //: Creates a greyscale image  at (tx,ty) with given pixel widths
00045   //  Size in points given by sx(),sy() * given values.
00046   //  Image must be no bigger than 255x255.
00047   //  Some postscript can't cope with bigger blocks.
00048   void draw_grey_image_block(const vil_image_view<vxl_byte>& image,
00049                              double tx, double ty,
00050                              double pixel_width_x, double pixel_width_y);
00051 
00052   //: Creates a colour image with given pixel widths
00053   //  Image assumed to be a 3 plane image.
00054   //  Image must be no bigger than 255x255.
00055   //  Some postscript can't cope with bigger blocks.
00056   void draw_rgb_image_block(const vil_image_view<vxl_byte>& image,
00057                             double tx, double ty,
00058                             double pixel_width_x, double pixel_width_y);
00059 
00060   //: Creates a grey or colour image with given pixel widths
00061   //  Image assumed to have either 1 or 3 planes.
00062   //  Image must be no bigger than 255x255.
00063   //  Some postscript can't cope with bigger blocks.
00064   void draw_image_block(const vil_image_view<vxl_byte>& image,
00065                         double tx, double ty,
00066                         double pixel_width_x, double pixel_width_y);
00067 
00068  public:
00069 
00070   //: Dflt ctor
00071   mbl_eps_writer();
00072 
00073   //: Open file and write header, given bounding box
00074   // Bounding box specified in "points" (72 points=1 inch)
00075   mbl_eps_writer(const char* path, double nx, double ny);
00076 
00077 
00078   //: Destructor
00079   virtual ~mbl_eps_writer();
00080 
00081   //: Current stream used to write EPS file.
00082   vcl_ofstream& ofs() { return ofs_; }
00083 
00084   //: Define shade of subsequent graphics [0,1] = black-white
00085   void set_grey_shade(double shade);
00086 
00087   //: Define colour of subsequent graphics r,g,b in [0,1]
00088   void set_rgb(double r, double g, double b);
00089 
00090   //: Set colour of subsequent graphics using a named colour
00091   //  Valid options include black,white,grey,red,green,blue,
00092   //  cyan,yellow.  Note: Probably need a tidy map thing to
00093   //  do this properly.  Sets to grey if colour not known.
00094   void set_colour(const vcl_string& colour_name);
00095 
00096   //: Define scaling factor
00097   void set_scaling(double s);
00098 
00099   //: Define scaling factor
00100   void set_scaling(double sx, double sy);
00101 
00102   //: Open file and write header, given bounding box
00103   // Bounding box specified in "points" (72 points=1 inch)
00104   bool open(const char* path, double nx, double ny);
00105 
00106   //: Creates file and draws image, setting bounding box to that of image
00107   //  Sets scaling to pixel widths, so that subsequent points are
00108   //  interpreted in pixel units.
00109   bool open(const char* path, const vil_image_view<vxl_byte>& image,
00110             double pixel_width_x, double pixel_width_y);
00111 
00112   //: Write tail and close
00113   void close();
00114 
00115   //: Draws disk of radius r around p, with current colour
00116   void draw_disk(const vgl_point_2d<double>& p, double r);
00117 
00118   //: Draws circle of radius r around p, with current colour
00119   void draw_circle(const vgl_point_2d<double>& p, double r);
00120 
00121   //: Draws line segment from p1 to p2
00122   void draw_line(const vgl_point_2d<double>& p1,
00123                  const vgl_point_2d<double>& p2);
00124 
00125   //: Draws polygon connecting points.
00126   //  If closed, then adds line joining last to first point.
00127   //  If filled, then fills with current colour/greyshade.
00128   void draw_polygon(const vcl_vector<vgl_point_2d<double> >& pts,
00129                     bool closed=true, bool filled=false);
00130 
00131   //: Define line width.
00132   void set_line_width(double w);
00133 
00134   //: Writes first plane of image in hex format to os
00135   void write_image_data(vcl_ostream& os,
00136                         const vil_image_view<vxl_byte>& image);
00137 
00138   //: Creates an image  at (tx,ty) with given pixel widths
00139   //  Size in points given by sx(),sy() * given values.
00140   void draw_image(const vil_image_view<vxl_byte>& image,
00141                   double tx, double ty,
00142                   double pixel_width_x, double pixel_width_y);
00143 };
00144 
00145 #endif // mbl_eps_writer_h_