core/vul/vul_psfile.h
Go to the documentation of this file.
00001 // This is core/vul/vul_psfile.h
00002 #ifndef vul_psfile_h_
00003 #define vul_psfile_h_
00004 //:
00005 // \file
00006 // \brief write out images, points, lines, circles and/or ellipses to PostScript
00007 // \author Alan S. Liu
00008 //
00009 // \verbatim
00010 // Modifications
00011 //  7 Jan 2003 - Peter Vanroose - bug fix in image output: complete rewrite of
00012 //                                print_greyscale_image() & print_color_image()
00013 // \endverbatim
00014 
00015 #include <vcl_string.h>
00016 #include <vcl_fstream.h>
00017 
00018 //: Write a PostScript file
00019 class vul_psfile: public vcl_ofstream
00020 {
00021  public:
00022   enum paper_type {
00023     US_NORMAL,
00024     A4,
00025     B5,
00026     A3,
00027     US_LEGAL,
00028     ELEVEN_BY_SEVENTEEN,
00029     FOUR_BY_FIVE,
00030     THIRTY_FIVE_mm};
00031   enum paper_orientation{
00032     PORTRAIT,
00033     LANDSCAPE };
00034   enum paper_layout{
00035     CENTER,
00036     MAX };
00037 
00038   vul_psfile(char const* filename, bool debug_output=false);
00039   ~vul_psfile();
00040   operator bool() { return (void*)output_filestream!=(void*)0; }
00041 
00042   void set_paper_type(vul_psfile::paper_type type){printer_paper_type = type;}
00043   void set_paper_layout(vul_psfile::paper_layout layout) {printer_paper_layout = layout;}
00044   void set_paper_orientation(vul_psfile::paper_orientation o) {printer_paper_orientation = o;}
00045   void set_reduction_factor(int rf) {reduction_factor = rf;}
00046   //: set the horizontal scaling (in percent); no scaling is 100.
00047   void set_scale_x(float sx) {scale_x = sx * .01f;}
00048   //: set the vertical scaling (in percent); no scaling is 100.
00049   void set_scale_y(float sy) {scale_y = sy * .01f;}
00050   void set_fg_color(float r, float g, float b) {fg_r = r; fg_g = g; fg_b = b;}
00051   void set_bg_color(float r, float g, float b) {bg_r = r; bg_g = g; bg_b = b;}
00052   void set_line_width(float f_width) {line_width_ = f_width;}
00053   float line_width() const { return line_width_; }
00054 
00055   //: Write 8 bit grey scale image.
00056   void print_greyscale_image(unsigned char* data, int sizex, int sizey);
00057   //: Write 24 bit colour image.
00058   void print_color_image(unsigned char* data, int sizex, int sizey);
00059 
00060   //:  Add a line between the given points to the Postscript file.
00061   void line(float x1, float y1, float x2, float y2);
00062   //: Add a point at the given coordinates to the Postscript file.
00063   void point(float x, float y, float point_size = 0);
00064   //: Add an ellipse to the Postscript file.
00065   void ellipse(float x, float y, float a_axis, float b_axis, int angle = 0);
00066   //: Add a circle with the given centre point and radius to the Postscript file.
00067   void circle(float x, float y, float radius);
00068 
00069   void reset_bounding_box();
00070 
00071  protected:
00072   void set_min_max_xy(float x, float y);
00073   void set_min_max_xy(int x, int y);
00074   bool set_parameters(int sizex, int sizey);
00075 
00076   //: PostScript file header.  Automatically called by the constructor.
00077   void postscript_header();
00078 
00079   //: Set graphic coordinate (translate and rotate to local coordinate).
00080   void graphic_header();
00081   //: Utility program used in point(), line(), ellipse() and circle()
00082   void sobj_rgb_params(char const* str, bool filled);
00083   //: the defined procedure for PostScript script use.
00084   void print_graphics_prolog();
00085 
00086  private:
00087   void compute_bounding_box();
00088   
00089   void reset_postscript_header();
00090   void image_translate_and_scale();
00091   void object_translate_and_scale();
00092   void done();
00093 
00094   vcl_ofstream output_filestream;
00095 
00096   float fg_r, fg_g, fg_b;
00097   float bg_r, bg_g, bg_b;
00098   float line_width_;
00099   float scale_x, scale_y;
00100   int ox, oy, iw, ih;
00101   double iwf, ihf;
00102   double psizex, psizey;   /* current paper size, in inches */
00103   double pos_inx, pos_iny; /* top-left offset of image, in inches */
00104   int width, height;       /* image width and height */
00105   vcl_string filename;     /* postscript path/filename */
00106   paper_type printer_paper_type;
00107   paper_orientation printer_paper_orientation;
00108   paper_layout printer_paper_layout;
00109   int reduction_factor;
00110   bool doneps;
00111   int min_x, min_y;
00112   int max_x, max_y;
00113   int box_width, box_height;
00114 
00115  private: /*even more*/
00116 
00117   vcl_streampos translate_pos;
00118   vcl_streampos sobj_t_pos;
00119   vcl_streampos header_pos;
00120 
00121   bool graphics_prolog_exists;
00122   bool exist_image;
00123   bool exist_objs;
00124 };
00125 
00126 #endif // vul_psfile_h_