Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

GhostScript.cc

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.cc,v 1.9 2004/02/29 16:44:05 nova77 Exp $ 00041 * AUTHORS: Pascal Vincent & Yoshua Bengio 00042 * This file is part of the PLearn library. 00043 ******************************************************* */ 00044 00045 #include "GhostScript.h" 00046 00047 #ifdef WIN32 00048 #include <io.h> 00049 // norman: potentially dangerous if there is a function called with the same name in this 00050 // file. Beware! 00051 #define popen _popen 00052 #define pclose _pclose 00053 #define fileno _fileno 00054 #endif 00055 00056 namespace PLearn { 00057 using namespace std; 00058 00059 real rgb2real(real r, real g, real b) 00060 { 00061 int r100 = int(r*99.99); 00062 int g100 = int(g*99.99); 00063 int b100 = int(b*99.99); 00064 return real(r100*10000+g100*100+b100); 00065 } 00066 00067 void real2rgb(real colorval, real& r, real& g, real& b) 00068 { 00069 int col = int(colorval); 00070 int r100 = col/10000; 00071 col = col%10000; 00072 int g100 = col/100; 00073 col = col%100; 00074 int b100 = col; 00075 r = real(r100)/100.0; 00076 g = real(g100)/100.0; 00077 b = real(b100)/100.0; 00078 } 00079 00080 GhostScript::GhostScript(int width, int height) 00081 :isfile(false) 00082 { 00083 static char command_string[1000]; 00084 // sprintf(command_string,"gs -sDEVICE=x11 -g%d%c%d - > /dev/null",width,'x',height); 00085 sprintf(command_string,"gs -sDEVICE=x11 -g%d%c%d > /dev/null",width,'x',height); 00086 00087 gs_cstream = popen(command_string,"w"); 00088 togs.attach(fileno(gs_cstream)); 00089 togs.outmode=PStream::raw_ascii; 00090 } 00091 00092 GhostScript::GhostScript(const string& filename, real x1, real y1, real x2, real y2) 00093 :isfile(true) 00094 // :gs_cstream(0), togs(filename.c_str()) 00095 { 00096 gs_cstream= fopen(filename.c_str(), "w"); 00097 togs.attach(fileno(gs_cstream)); 00098 togs.outmode=PStream::raw_ascii; 00099 togs << "%!PS-Adobe-2.0 EPSF-2.0" << endl; 00100 togs << "%%BoundingBox: " << x1 << " " << y1 << " " << x2 << " " << y2 << endl; 00101 } 00102 00103 GhostScript::~GhostScript() 00104 { 00105 if(gs_cstream) 00106 { 00107 if(!isfile) 00108 togs << "\nquit" << endl; 00109 fclose(gs_cstream); 00110 } 00111 } 00112 00113 void GhostScript::flush() 00114 { 00115 togs.flush(); 00116 if(gs_cstream) 00117 copypage(); 00118 } 00119 00120 void GhostScript::writeBitmapHexString1Bit(const Mat& bm, PStream& out, bool lastrowfirst) 00121 { 00122 unsigned char bitmask = 128; 00123 unsigned char value = 0; 00124 for(int i=0; i<bm.length(); i++) 00125 { 00126 const real* bm_i = lastrowfirst ?bm.rowdata(bm.length()-1-i) :bm.rowdata(i); 00127 for(int j=0; j<bm.width(); j++) 00128 { 00129 if(bm_i[j]>0) 00130 value |= bitmask; 00131 bitmask = bitmask>>1; 00132 if(bitmask==0) 00133 { 00134 out.writeAsciiHexNum((unsigned char)value); 00135 out.put(' '); 00136 bitmask = 128; 00137 value = 0; 00138 } 00139 } 00140 if(bitmask!=128) 00141 { 00142 out.writeAsciiHexNum((unsigned char)value); 00143 bitmask = 128; 00144 value = 0; 00145 } 00146 } 00147 } 00148 00149 void GhostScript::writeBitmapHexString8Bits(const Mat& bm, PStream& out, bool lastrowfirst) 00150 { 00151 for(int i=0; i<bm.length(); i++) 00152 { 00153 const real* bm_i = lastrowfirst ?bm.rowdata(bm.length()-1-i) :bm.rowdata(i); 00154 for( int j=0; j<bm.width(); j++) 00155 out.writeAsciiHexNum((unsigned char)(bm_i[j]*255.99)); 00156 out << "\n"; 00157 } 00158 } 00159 00160 void GhostScript::writeBitmapHexString24Bits(const Mat& bm, PStream& out, bool lastrowfirst) 00161 { 00162 for(int i=0; i<bm.length(); i++) 00163 { 00164 const real* bm_i = lastrowfirst ?bm.rowdata(bm.length()-1-i) :bm.rowdata(i); 00165 for( int j=0; j<bm.width(); j++) 00166 { 00167 real r,g,b; // values between 0 and 255 00168 real2rgb(bm_i[j],r,g,b); 00169 out.writeAsciiHexNum((unsigned char)(r*255.99)); 00170 out.writeAsciiHexNum((unsigned char)(g*255.99)); 00171 out.writeAsciiHexNum((unsigned char)(b*255.99)); 00172 } 00173 out << "\n"; 00174 } 00175 } 00176 00177 void GhostScript::displayBlack(const Mat& bm, real x, real y, real w, real h, bool painton1) 00178 { 00179 togs << "\ngsave\n" 00180 << "/pstr " << bm.width() << " string def\n" 00181 << x << " " << y << " translate\n" 00182 << w << " " << h << " scale\n" 00183 << bm.width() << " " << bm.length() << " " << (painton1 ?"true" :"false") << " " 00184 << "[" << bm.width() << " 0 0 " << bm.length() << " 0 0 ]\n"; 00185 // * Version using huge string 00186 togs << "<"; 00187 writeBitmapHexString1Bit(bm,togs,true); 00188 togs << ">\nimage\ngrestore" << endl; 00189 // * Version using procedure 00190 // togs << "{currentfile pstr readhexstring pop} imagemask\n"; 00191 // writeBitmapHexString1Bit(bm,togs,true); 00192 // togs << "\ngrestore\n"; 00193 } 00194 00195 void GhostScript::displayGray(const Mat& bm, real x, real y, real w, real h) 00196 { 00197 togs << "\ngsave\n" 00198 << "/pstr " << bm.width() << " string def\n" 00199 << x << " " << y << " translate\n" 00200 << w << " " << h << " scale\n" 00201 << bm.width() << " " << bm.length() << " 8 " 00202 << "[" << bm.width() << " 0 0 " << bm.length() << " 0 0 ]\n"; 00203 // * Version using huge string 00204 togs << "<"; 00205 writeBitmapHexString8Bits(bm,togs,true); 00206 togs << ">\nimage\ngrestore" << endl; 00207 // * Version using procedure 00208 // togs << "{currentfile pstr readhexstring pop} image\n"; 00209 // writeBitmapHexString8Bits(bm,togs,true); 00210 // togs << "\ngrestore\n"; 00211 } 00212 00213 void GhostScript::displayRGB(const Mat& bm, real x, real y, real w, real h) 00214 { 00215 togs << "\ngsave\n" 00216 << "/pstr " << 3*bm.width() << " string def\n" 00217 << x << " " << y << " translate\n" 00218 << w << " " << h << " scale\n" 00219 << bm.width() << " " << bm.length() << " 8 " 00220 << "[" << bm.width() << " 0 0 " << bm.length() << " 0 0 ]\n"; 00221 // * Version using huge string 00222 // togs << "<"; 00223 // writeBitmapHexString24Bits(bm,togs); 00224 // togs << ">\nfalse 3 image\ngrestore" << endl; 00225 // * Version using procedure 00226 togs << "{currentfile pstr readhexstring pop} false 3 colorimage\n"; 00227 writeBitmapHexString24Bits(bm,togs,true); 00228 togs << "\ngrestore\n"; 00229 } 00230 00231 void GhostScript::show(real x, real y, const char* str, char halign, char valign) 00232 { 00233 togs << "\n" << x << " " << y << " moveto "; 00234 togs << "(" << str << ") dup stringwidth "; 00235 00236 switch(valign) 00237 { 00238 case 't': 00239 togs << " neg "; 00240 break; 00241 case 'm': 00242 togs << " -2 div "; 00243 break; 00244 case 'b': 00245 togs << " pop 0 "; 00246 break; 00247 default: 00248 PLERROR("In GhostScript::show wrong valign parameter '%c'",valign); 00249 } 00250 00251 togs << " exch "; 00252 switch(halign) 00253 { 00254 case 'l': 00255 togs << " pop 0 "; 00256 break; 00257 case 'c': 00258 togs << " -2 div "; 00259 break; 00260 case 'r': 00261 togs << " neg "; 00262 break; 00263 default: 00264 PLERROR("In GhostScript::show wrong halign parameter '%c'",halign); 00265 } 00266 00267 togs << " exch rmoveto show" << endl; 00268 } 00269 00270 void GhostScript::setcolor(char* colorname) 00271 { 00272 if(!strcmp(colorname,"white")) setcolor(1,1,1); 00273 else if(!strcmp(colorname,"black")) setcolor(0,0,0); 00274 else if(!strcmp(colorname,"gray")) setcolor(.5,.5,.5); 00275 else if(!strcmp(colorname,"darkgray")) setcolor(.25,.25,.25); 00276 else if(!strcmp(colorname,"lightgray")) setcolor(.75,.75,.75); 00277 else if(!strcmp(colorname,"red")) setcolor(1,0,0); 00278 else if(!strcmp(colorname,"green")) setcolor(0,1,0); 00279 else if(!strcmp(colorname,"blue")) setcolor(0,0,1); 00280 else if(!strcmp(colorname,"magenta")) setcolor(1,0,1); 00281 else if(!strcmp(colorname,"yellow")) setcolor(1,1,0); 00282 else if(!strcmp(colorname,"cyan")) setcolor(0,1,1); 00283 else setcolor(0,0,0); 00284 } 00285 00286 void GhostScript::multilineShow(real x, real y, const string& text, real newlinesize, char halign, char valign) 00287 { 00288 vector<string> splits = split(text, '\n'); 00289 int nsplits = (int)splits.size(); 00290 for(int i=0; i<nsplits; i++) 00291 { 00292 show(x,y,splits[i].c_str(),halign,valign); 00293 y -= newlinesize; 00294 } 00295 } 00296 00297 void GhostScript::drawBox(real x, real y, real width, real height) 00298 { 00299 togs << "\n" << x << " " << y << " moveto " 00300 << x+width << " " << y << " lineto " 00301 << x+width << " " << y+height << " lineto " 00302 << x << " " << y+height << " lineto " 00303 << x << " " << y << " lineto " 00304 << "stroke" << endl; 00305 } 00306 00307 void GhostScript::fillBox(real x, real y, real width, real height) 00308 { 00309 togs << "\n" << x << " " << y << " moveto " 00310 << x+width << " " << y << " lineto " 00311 << x+width << " " << y+height << " lineto " 00312 << x << " " << y+height << " lineto " 00313 << x << " " << y << " lineto " 00314 << "fill" << endl; 00315 } 00316 00317 void GhostScript::drawCircle(real x, real y, real r) 00318 { 00319 moveto(x+r, y); 00320 togs << x << ' ' << y << ' ' << r << " 0 360 arc stroke" << endl; 00321 } 00322 00323 void GhostScript::drawCross(real x, real y, real r, bool vertical, real ry) 00324 { 00325 if (ry<0) ry=r; 00326 if(vertical) 00327 { 00328 drawLine(x-r,y,x+r,y); 00329 drawLine(x,y-ry,x,y+ry); 00330 } 00331 else // diagonal 00332 { 00333 #if defined(_MINGW_) 00334 r /= sqrt(2.0); 00335 #else 00336 r /= M_SQRT2; 00337 #endif 00338 drawLine(x-r,y-ry,x+r,y+ry); 00339 drawLine(x-r,y+ry,x+r,y-ry); 00340 } 00341 } 00342 00343 void GhostScript::fillCircle(real x, real y, real r) 00344 { 00345 moveto(x+r, y); 00346 togs << x << ' ' << y << ' ' << r << " 0 360 arc fill" << endl; 00347 } 00348 00349 #ifdef WIN32 00350 #undef _fileno 00351 #undef _popen 00352 #undef _pclose 00353 #endif 00354 00355 } // end of namespace PLearn 00356

Generated on Tue Aug 17 15:54:29 2004 for PLearn by doxygen 1.3.7