PLearn 0.1
GhostScript.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal
00006 //
00007 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 
00037  
00038 
00039 /* *******************************************************      
00040    * $Id: GhostScript.h 8451 2008-02-03 22:40:49Z plearner $
00041    * AUTHORS: Pascal Vincent & Yoshua Bengio
00042    * This file is part of the PLearn library.
00043    ******************************************************* */
00044 
00045 
00048 #ifndef GHOSTSCRIPT_INC
00049 #define GHOSTSCRIPT_INC
00050 
00051 #include <plearn/io/PStream.h>
00052 #include <plearn/math/Mat.h>
00053 
00054 namespace PLearn {
00055 using namespace std;
00056 
00061   real rgb2real(real r, real g, real b);
00062   void real2rgb(real colorval, real& r, real& g, real& b);
00063 
00064 class GhostScript
00065 {
00066  protected:
00067   FILE* gs_cstream;
00068   PStream togs; 
00069   char command_string[1000];
00070   bool isfile;
00071   static void writeBitmapHexString1Bit(const Mat& bm, PStream& out, bool lastrowfirst=false);
00072   static void writeBitmapHexString8Bits(const Mat& bm, PStream& out, bool lastrowfirst=false);
00073   static void writeBitmapHexString24Bits(const Mat& bm, PStream& out, bool lastrowfirst=false);
00074 
00075 // norman: win32 compiler knows ios::char_type! :)
00076 #if __GNUC__ > 2 && !defined(WIN32)
00077   typedef ios::char_type char_type;
00078 #else
00079   typedef ios::char_type char_type;
00080 #endif 
00081 // old:
00082 //#if __GNUC__ < 3
00083 //  typedef _IO_wchar_t char_type;
00084 //#else
00085 //  typedef ios::char_type char_type;
00086 //#endif
00087 
00088  public:
00089 
00091   GhostScript(int width=200, int height=200);
00092 
00094   GhostScript(const string& filename, real x1, real y1, real x2, real y2);
00095 
00096   ~GhostScript();
00097 
00098   void flush();
00099 
00100   PStream& operator<<(char* str)
00101     { return togs<<str; }
00102 
00103   void run(const char* filename)
00104     { togs << "\n(" << filename << ") run " << endl; }
00105 
00106   void copypage() 
00107     { togs << "\ncopypage" << endl; } 
00108 
00109   void showpage() 
00110     { togs << "\nshowpage" << endl; } 
00111 
00112   void gsave()
00113     { togs << "\ngsave\n"; }
00114 
00115   void grestore()
00116     { togs << "\ngrestore\n"; }
00117 
00118   void translate(real x, real y)
00119     { togs << "\n" << x << " " << y << " translate\n"; }
00120 
00121   void scale(real x, real y)
00122     { togs << "\n" << x << " " << y << " scale\n"; }
00123 
00124   void moveto(real x, real y)
00125     { togs << "\n" << x << " " << y << " moveto\n"; }
00126 
00127   void rotate(real angle)
00128     { togs << "\n" << angle << " rotate\n"; }
00129 
00134   void mapping(real sourcex, real sourcey, real sourcewidth, real sourceheight,
00135            real destx, real desty, real destwidth, real destheight)
00136     {
00137       scale(destwidth/sourcewidth, destheight/sourceheight);
00138       translate(-sourcex+destx*sourcewidth/destwidth,-sourcey+desty*sourceheight/destheight);
00139     }
00140 
00141   void show(const char* str)
00142   { togs << "(" << str << ") show" << endl; }
00143 
00144   void show(const string& str)
00145   { togs << "(" << str << ") show" << endl; }
00146 
00149   void show(real x, real y, const char* str, char halign='l', char valign='b');
00150 
00151   inline void show(real x, real y, const string& str, char halign='l', char valign='b')
00152   { show(x,y,str.c_str(), halign, valign); }
00153 
00154   void centerShow(real x, real y, const char* str)
00155   { show(x,y,str,'c','m'); }
00156 
00157   void centerShow(real x, real y, const string& str)
00158   { show(x,y,str,'c','m'); }
00159 
00161   void multilineShow(real x, real y, const string& text, real newlinesize, char halign='l', char valign='b');
00162 
00163   void show(real x, real y, Vec v) 
00164   { togs << "\n" << x << " " << y << " moveto (" << v << ") show" << endl; }
00165 
00166   void centerShow(real x, real y, Vec v) 
00167     { togs << "\n" << x << " " << y << " moveto (" << v << ") dup stringwidth exch -2 div exch -2 div rmoveto show" << endl; } 
00168 
00169   void erasepage()
00170     { togs << "\nerasepage" << endl; }
00171 
00172   void setcolor(real r, real g, real b)
00173     { togs << "\n/DeviceRGB setcolorspace " << r << " " << g << " " << b << " setcolor\n"; }
00174 
00176   void setcolor(real rgb)
00177     { 
00178       real r,g,b;
00179       real2rgb(rgb,r,g,b);
00180       setcolor(r,g,b);
00181     }
00182 
00183   void setcolor(char* colorname);
00184 
00185   void setgray(real g)
00186     { togs << "\n" << g << " setgray\n"; }
00187   
00188   void setlinewidth(real w)
00189     { togs << "\n" << w << " setlinewidth\n"; }
00190 
00192   void setdash(Vec ary, real x=0)
00193   { 
00194     if(ary.isEmpty())
00195       togs << "[ 3 3 ] " << x << " setdash\n"; 
00196     else
00197       togs << "[ " << ary << " ] " << x << " setdash\n"; 
00198   }
00199 
00200   void drawLine(real x1, real y1, real x2, real y2)
00201     { togs << "\n" << x1 << " " << y1 << " moveto " << x2 << " " << y2 << " lineto stroke" << endl; }
00202 
00203   void drawArrow(real x1, real y1, real x2, real y2)
00204     { 
00205       drawLine(x1,y1,x2,y2);
00206       drawCircle(x2,y2,2);
00207     }
00208 
00209   void drawBox(real x, real y, real width, real height);
00210   void fillBox(real x, real y, real width, real height);
00211 
00212   void drawCross(real x, real y, real r, bool vertical=false, real ry=-1); // default ry=r
00213 
00214   void drawCircle(real x, real y, real r);
00215   void fillCircle(real x, real y, real r);
00216 
00221   void displayBlack(const Mat& bm, real x, real y, real w, real h, bool painton1=true);
00222 
00224   void displayGray(const Mat& bm, real x, real y, real w, real h);
00225 
00227   void displayRGB(const Mat& bm, real x, real y, real w, real h);
00228 
00229   void usefont(const char* fontname="Times-Roman", real pointsize=12.0) 
00230     { togs << "\n/" << fontname << " findfont " << pointsize << " scalefont setfont" << endl; }
00231 };
00232 
00233 } // end of namespace PLearn
00234 
00235 #endif
00236 
00237 
00238 
00239 
00240 
00241 
00242 
00243 
00244 
00245 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines