GhostScript.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
#include "GhostScript.h"
00046
00047
#ifdef WIN32
00048
#include <io.h>
00049
00050
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
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
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;
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
00186
togs <<
"<";
00187
writeBitmapHexString1Bit(bm,
togs,
true);
00188
togs <<
">\nimage\ngrestore" <<
endl;
00189
00190
00191
00192
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
00204
togs <<
"<";
00205
writeBitmapHexString8Bits(bm,
togs,
true);
00206
togs <<
">\nimage\ngrestore" <<
endl;
00207
00208
00209
00210
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
00222
00223
00224
00225
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
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 }
00356
Generated on Tue Aug 17 15:54:29 2004 for PLearn by
1.3.7