SbPainterPS.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 #ifdef G4VIS_BUILD_OI_DRIVER
00027 
00028 /*----------------------------HEPVis----------------------------------------*/
00029 /*                                                                          */
00030 /* Node:             SbPainterPS                                            */
00031 /* Author:           Guy Barrand                                            */
00032 /*                                                                          */
00033 /*--------------------------------------------------------------------------*/
00034 // this :
00035 #include <HEPVis/SbPainterPS.h>
00036 
00037 //#include <HEPVis/SbString.h>
00038 #define STRDUP(str)  ((str) != NULL ? (::strcpy((char*)::malloc((unsigned)::strlen(str) + 1), str)) : (char*)NULL)
00039 #define STRDEL(str) {if((str)!=NULL) {::free(str);str=NULL;}}
00040 
00041 //#define DEBUG
00042 #include <stdlib.h>
00043 #include <string.h>
00044 #include <stdio.h>
00045 #include <stdarg.h>
00046 #include <time.h>
00047 #include <locale.h>
00048 
00049 #define METAFILE_DEFAULT "out.ps"
00050 #define METAFILE_SCALE 1.
00051 
00052 static char* GetDate();
00053 static double ConvertRGB_ToGrey(double,double,double);
00055 SbPainterPS::SbPainterPS(
00056 )
00057 :fDeviceWidth((8.5-1.) * 72. * METAFILE_SCALE) /* 540. * METAFILE_SCALE */
00058 ,fDeviceHeight(11.     * 72. * METAFILE_SCALE) /* 792. * METAFILE_SCALE */
00059 ,fPageNumber(0)
00060 ,fPagePos(0)
00061 ,fMarkerSize(2.)
00062 ,fFile(NULL)
00063 ,fFileName(NULL)
00064 ,fGSave(0)
00065 ,fBufferCount(0)
00066 ,fBufferString(NULL)
00069 {
00070   fParams.shade = Color;
00071   fParams.portrait = 1;
00072   fParams.nbit = 2;
00073   fParams.doBack = 1;
00074   fParams.lineWidth = -1.;
00075 #ifdef WIN32
00076   ::setlocale(LC_NUMERIC,"USA");
00077 #endif
00078 }
00080 SbPainterPS::~SbPainterPS(
00081 )
00084 {
00085   if(fFile!=NULL) closeStream ();
00086   if(fBufferString!=NULL) ::free(fBufferString);
00087   fBufferString = NULL;
00088   if(fGSave!=0) {
00089     ::printf("SbPainterPS : bad gsave/grestore balance : %d.\n",fGSave);
00090   }
00091 }
00095 void SbPainterPS::beginTraversal (
00096 )
00099 {
00100   if(fFile==NULL) openFileForWriting(NULL);
00101   if(fFile==NULL) return;
00102   putBeginPageInStream();
00103   putPageScaleInStream((float)fWindowWidth,(float)fWindowHeight);
00104   putSaveStateInStream();
00105 }
00107 void SbPainterPS::endTraversal(
00108 )
00111 {
00112   if(fFile==NULL) return;
00113   putFrameInStream(0.0,0.0,0.0,(float)fWindowWidth,(float)fWindowHeight);
00114   putRestoreStateInStream();
00115   putEndPageInStream();
00116 }
00118 void SbPainterPS::clearColorBuffer(
00119  float aRed
00120 ,float aGreen
00121 ,float aBlue
00122 )
00125 {
00126   if(fFile==NULL) return;
00127   putBackgroundInStream(aRed,aGreen,aBlue,
00128                            (float)fWindowWidth,(float)fWindowHeight);
00129 }
00130 /*
00132 void SbPainterPS::drawPrimitive (
00133  SbPrimitiveType aType
00134 ,int aPointn
00135 ,float* aXs
00136 ,float* aYs
00137 ,float* //aZs
00138 ,const SbPainterContext& aAtb
00139 )
00142 {
00143   if(fFile==NULL) return;
00144   switch(aType) {
00145   case SbPrimitivePoints:
00146     drawMarkers(aPointn,
00147                 aXs,aYs,
00148                 aAtb.fRed,aAtb.fGreen,aAtb.fBlue,
00149                 aAtb.fMarkerStyle,aAtb.fMarkerSize);
00150     break;
00151   case SbPrimitiveLineStrip:
00152   case SbPrimitiveLineLoop:
00153     drawLines(aPointn,
00154               aXs,aYs,
00155               aAtb.fRed,aAtb.fGreen,aAtb.fBlue,
00156               aAtb.fLineStyle,aAtb.fLineWidth);
00157     break;
00158   case SbPrimitivePolygon:
00159     drawPolygon(aPointn,
00160                 aXs,aYs,
00161                 aAtb.fRed,aAtb.fGreen,aAtb.fBlue,
00162                 aAtb.fAreaStyle);
00163     break;
00164   default:
00165     break;
00166   }
00167 }
00169 void SbPainterPS::drawPolygon(
00170  int aPointn
00171 ,float* aXs
00172 ,float* aYs
00173 ,float aRed
00174 ,float aGreen
00175 ,float aBlue
00176 ,const SbAreaStyle& //aStyle
00177 )
00180 {
00181   if(fFile==NULL) return;
00182   if(aPointn<=0) return;
00183   putNewPathInStream(); 
00184   putMoveInStream(aXs[0],aYs[0]);
00185   for(int count=1;count<aPointn;count++) {
00186     putLineToInStream(aXs[count] - aXs[count-1],
00187                       aYs[count] - aYs[count-1]);
00188   }
00189   if ( (aXs[0]==aXs[aPointn-1]) &&
00190        (aYs[0]==aYs[aPointn-1]) ) 
00191     putClosePathInStream();
00192   putRGB_InStream(aRed,aGreen,aBlue);
00193   putFillInStream();
00194 }
00196 void SbPainterPS::drawLines(
00197  int aPointn
00198 ,float* aXs
00199 ,float* aYs
00200 ,float aRed
00201 ,float aGreen
00202 ,float aBlue
00203 ,const SbLineStyle& aStyle
00204 ,int aWidth
00205 )
00208 {
00209   if(fFile==NULL) return;
00210   if(aPointn<=0) return;
00211   putMoveInStream(aXs[0],aYs[0]);
00212   for(int count=1;count<aPointn;count++) {
00213     putLineToInStream(aXs[count] - aXs[count-1],
00214                       aYs[count] - aYs[count-1]);
00215   }
00216   if ( (aXs[0]==aXs[aPointn-1]) &&
00217        (aYs[0]==aYs[aPointn-1]) ) 
00218     putClosePathInStream();
00219   putRGB_InStream(aRed,aGreen,aBlue);
00220   putLineWidthInStream(aWidth);
00221   putCapInStream(1);
00222   putLineStyleInStream(aStyle);
00223   putStrokeInStream();
00224 }
00226 void SbPainterPS::drawMarkers (
00227  int aPointn
00228 ,float* aXs
00229 ,float* aYs
00230 ,float aRed
00231 ,float aGreen
00232 ,float aBlue
00233 ,const SbMarkerStyle& aStyle 
00234 ,int aSize
00235 )
00238 {
00239   if(fFile==NULL) return;
00240   float mark_size  = (float)(aSize <=0 ? 1. : aSize);   
00241   mark_size *= 0.6F;
00242   if(aStyle==SbMarkerCircleLine) {
00243     putNewPathInStream();
00244     int icount = 1;
00245     for(int count=0;count<aPointn;count++) {
00246       putCircleInStream(aXs[count],aYs[count],mark_size);
00247 #define MAX_PATH_POINT 100
00248       if(icount==MAX_PATH_POINT) {
00249         putRGB_InStream(aRed,aGreen,aBlue);
00250         putLineWidthInStream(1);
00251         putCapInStream(1);
00252         putStrokeInStream();
00253         icount = 1;
00254         if(count!=aPointn-1) putNewPathInStream();
00255       } else {
00256         icount++;
00257       }
00258     }
00259     putRGB_InStream(aRed,aGreen,aBlue);
00260     putLineWidthInStream(1);
00261     putCapInStream(1);
00262     putStrokeInStream();
00263   } else {
00264     putNewPathInStream();
00265     int icount = 1;
00266     for(int count=0;count<aPointn;count++) {
00267       putMoveInStream(aXs[count],aYs[count]);
00268       putMarkerSizeInStream(mark_size);
00269       putMarkerStyleInStream(aStyle);
00270       if(icount==MAX_PATH_POINT) {
00271         putRGB_InStream(aRed,aGreen,aBlue);
00272         putLineWidthInStream(1);
00273         putCapInStream(1);
00274         putStrokeInStream();
00275         icount = 1;
00276         if(count!=aPointn-1) putNewPathInStream();
00277       } else {
00278         icount++;
00279       }
00280     }
00281     putRGB_InStream(aRed,aGreen,aBlue);
00282     putLineWidthInStream(1);
00283     putCapInStream(1);
00284     putStrokeInStream();
00285   }
00286 }
00287 */
00292 void SbPainterPS::setColorScheme(
00293  int aShade
00294 )
00297 {
00298   fParams.shade = aShade;
00299 }
00301 void SbPainterPS::setOrientation(
00302  int aPortrait
00303 )
00306 {
00307   fParams.portrait = aPortrait;
00308 }
00310 void SbPainterPS::setBackgroundDrawn(
00311  int aDoback
00312 )
00315 {
00316   fParams.doBack = aDoback;
00317 }
00319 void SbPainterPS::setBitsPerPixel(
00320  int aNbit
00321 )
00324 {
00325   if( (aNbit==2) || (aNbit==4) || (aNbit==8) )
00326     fParams.nbit = aNbit;
00327   else 
00328     fParams.nbit = 2;
00329 }
00331 void SbPainterPS::setLineWidth(
00332  int aWidth
00333 )
00336 {
00337   fParams.lineWidth = (float)aWidth;
00338 }
00342 void SbPainterPS::setFileName(
00343  const char* aString
00344 )
00347 {
00348   STRDEL(fFileName);
00349   fFileName = STRDUP(aString);
00350 }
00352 const char* SbPainterPS::getFileName(
00353 ) const
00356 {
00357   return fFileName;
00358 }
00360 void* SbPainterPS::getStream(
00361 )
00364 {
00365   return fFile;
00366 }
00368 void SbPainterPS::openFileForWriting(
00369  const char* aString
00370 )
00373 {
00374   if(fFile!=NULL) closeStream ();
00375   if( (aString==NULL) || (*aString=='\0') ) {
00376     if( (fFileName==NULL) || (*fFileName=='\0') ) { // Take default name :
00377       fFile = ::fopen(METAFILE_DEFAULT,"wb");
00378       STRDEL(fFileName);
00379       fFileName = STRDUP(METAFILE_DEFAULT);
00380     } else {
00381       fFile = ::fopen(fFileName,"wb");
00382     }
00383   } else {
00384     fFile = ::fopen(aString,"wb");
00385     STRDEL(fFileName);
00386     fFileName = STRDUP(aString);
00387   }
00388   if(fFile==NULL) return;
00389 
00390   fBufferCount = 0;
00391   fBufferPointer[METAFILE_RECORD_LENGTH] = '\0';
00392   fPageNumber = 0;
00393   // Header :
00394   printFLN   ("%%!PS-Adobe-2.0");
00395   printFLN   ("%%%%Creator: HEPVis::SbPainterPS.");
00396   printFLN("%%%%CreationDate: %s",GetDate());
00397   printFLN("%%%%Title: %s",fFileName);
00398   printFLN("%%%%Pages: (atend)");
00399   printFLN("%%%%BoundingBox: 0 0 %d %d",
00400            (int)fDeviceWidth,(int)fDeviceHeight);
00401   printFLN("%%%%DocumentFonts: Courier-Bold");
00402   printFLN("%%%%DocumentPaperSizes: a4");
00403   printFLN("%%%%EndComments");
00404   // PostScript :
00405   putSaveStateInStream      ();
00406   // General :
00407   putInStreamF("/n {newpath} def ");
00408   putInStreamF("/cl {closepath} def ");
00409   putInStreamF("/s {stroke} def ");
00410   putInStreamF("/f {fill} def ");
00411   // Move :
00412   putInStreamF("/m  {moveto} def ");
00413   putInStreamF("/rm {rmoveto} def ");
00414   putInStreamF("/rl {rlineto} def ");
00415   // Line :
00416   putInStreamF("/lc {setlinecap} def ");
00417   putInStreamF("/lw {setlinewidth} def ");
00418   putInStreamF("/rgb {setrgbcolor} def ");
00419   putInStreamF("/ss {[] 0 setdash} def ") ;            /* style solid       */
00420   putInStreamF("/sd {[12 6] 0 setdash} def ");         /* style dashed      */
00421   putInStreamF("/so {[6 12] 0 setdash} def ");         /* style dotted      */
00422   putInStreamF("/sdo {[18 12 6 12] 0 setdash} def ");  /* style dash dotted */
00423   // Mark :
00424   fMarkerSize = 2.;
00425   putInStreamF("/ms 2. def /msi .5 def ");        /* mark size */
00426   putInStreamF("/cross {ms ms scale -1. -1. rm  ");
00427   putInStreamF("2. 2. rl 0. -2. rm -2. 2. rl msi msi scale} def ");
00428   putInStreamF("/plus  {ms ms scale -1. 0. rm 2. 0. rl ");
00429   putInStreamF("-1. 1. rm 0. -2. rl msi msi scale} def ");
00430   putInStreamF("/asterisk {ms ms scale -1. 0. rm 2. 0. rl -1. 1. rm ");
00431   putInStreamF("0. -2. rl 0. 1. rm -0.707 -0.707 rm 1.414 1.414 rl ");
00432   putInStreamF("0. -1.414 rm -1.414 1.414 rl msi msi scale} def ");
00433   putInStreamF("/triangle {ms ms scale 0. 1. rm -0.6 -1.5 rl ");
00434   putInStreamF("1.2 0. rl -0.6 1.5 rl msi msi scale} def ");
00435   // Text :
00436   putInStreamF("/sh {show} def ");
00437   putInStreamF("/df {/Courier-Bold findfont} def ");
00438   putInStreamF("/mf {makefont setfont} def ");
00439   printFLN("%%%%EndProlog");
00440 }
00442 void SbPainterPS::closeStream(
00443 )
00446 {
00447   if(fFile==NULL) return;
00448   putRestoreStateInStream    ();
00449   printFLN("%%%%Trailer");
00450   printFLN("%%%%Pages: %d",fPageNumber);
00451   printFLN("%%%%EOF");
00452   if(fFile!=NULL) ::fclose(fFile);
00453   fFile = NULL;
00454   STRDEL(fFileName);
00455   fFileName = NULL;
00456 }
00458 void SbPainterPS::putInStreamF(
00459  const char* aFormat 
00460 ,...
00461 )
00464 {
00465   if(fFile==NULL) return;
00466   va_list  args;
00467   va_start(args,aFormat);
00468   printV(aFormat,args);
00469   va_end(args);
00470   int length = ::strlen(fBufferString);
00471   if(length>METAFILE_RECORD_LENGTH) {
00472     ::printf("SoPostScript::putInStreamF overflow\n");
00473     return;
00474   }
00475   int nlength = fBufferCount + length;
00476   if(nlength>METAFILE_RECORD_LENGTH) {
00477       fBufferPointer[fBufferCount] = '\0';
00478       if(::fprintf(fFile,"%s\n",(char*)fBufferPointer)<0) {
00479         ::printf("SoPostScript::putInStreamF fprintf error\n");
00480       }
00481       fBufferCount = 0;
00482       nlength = length;
00483     }
00484   unsigned char* pointer = fBufferPointer + fBufferCount;
00485   ::strcpy((char*)pointer,fBufferString);
00486   fBufferCount = nlength;
00487 }
00489 void SbPainterPS::printFLN(
00490  const char* aFormat 
00491 ,...
00492 )
00495 {
00496   if(fFile==NULL) return;
00497   va_list args;
00498   va_start(args,aFormat);
00499   printV(aFormat,args);
00500   va_end(args);
00501 /* put buffer in file */
00502   if(fBufferCount>0) {
00503     fBufferPointer[fBufferCount] = '\0';
00504     if(::fprintf (fFile,"%s\n",(char*)fBufferPointer)<0) {
00505       ::printf("SbPainterPS::printFLN fprintf error\n");
00506     }
00507     fBufferCount = 0;
00508   }
00509 /* put comment in file */
00510   if(::fprintf (fFile,"%s\n",fBufferString)<0) {
00511     ::printf("SbPainterPS::printFLN fprintf error\n");
00512   }
00513 }
00515 void SbPainterPS::printV(
00516  const char* This 
00517 ,va_list aArgs  
00518 )
00521 {
00522 #define MAX_STR    2048
00523   if(fBufferString==NULL) {
00524     fBufferString = (char*)::malloc(MAX_STR * sizeof(char));
00525     if(fBufferString==NULL) return;
00526   }
00527   fBufferString[MAX_STR-1]  = '\0';
00528   ::vsprintf(fBufferString,This,aArgs);
00529   if(fBufferString[MAX_STR-1]!='\0') {
00530     ::printf("SbPainterPS::printV overflow\n");
00531     fBufferString[0] = '\0';
00532   }
00533 }
00538 void SbPainterPS::putPageScaleInStream(
00539  float aWidth 
00540 ,float aHeight 
00541 )
00544 {
00545   if(aWidth <=0.) aWidth  = 100.;
00546   if(aHeight<=0.) aHeight = 100.;
00547 
00548   putScaleInStream (1./METAFILE_SCALE,1./METAFILE_SCALE);
00549   putTranslationInStream ((float)(fDeviceWidth/20.),
00550                           (float)(fDeviceHeight/30.));
00551 
00552   float scale;
00553   if(fDeviceWidth<=fDeviceHeight)
00554     scale = (aHeight<=aWidth ? 
00555              fDeviceWidth /aWidth  : fDeviceWidth /aHeight );
00556   else 
00557     scale = (aHeight<=aWidth ? 
00558              fDeviceHeight /aWidth : fDeviceHeight /aHeight );
00559 
00560   float xtra,ytra;
00561   if(fParams.portrait==1) {
00562     xtra = (fDeviceWidth  - scale * aWidth)/2;
00563     ytra = (fDeviceHeight - scale * aHeight)/2;
00564   } else {
00565     putTranslationInStream(fDeviceWidth,0.);
00566     putRotateInStream(90);
00567     xtra = (fDeviceHeight  - scale * aWidth)/2;
00568     ytra = (fDeviceWidth   - scale * aHeight)/2;
00569   }
00570   putTranslationInStream (xtra,ytra);
00571 
00572   putScaleInStream (scale,scale);
00573 }
00578 void SbPainterPS::putSaveStateInStream(
00579 )
00582 {
00583   putInStreamF("gsave ");
00584   fGSave++;
00585 }
00587 void SbPainterPS::putRestoreStateInStream(
00588 )
00591 {
00592   putInStreamF("grestore ");
00593   fGSave--;
00594 }
00596 void SbPainterPS::putTranslationInStream(
00597  float aX
00598 ,float aY
00599 )
00602 {
00603   putInStreamF("%.2f %.2f translate ",aX,aY);
00604 }
00606 void SbPainterPS::putScaleInStream(
00607  float aX
00608 ,float aY
00609 )
00612 {
00613   putInStreamF("%.2f %.2f scale ",aX,aY);
00614 }
00616 void SbPainterPS::putBeginPageInStream(
00617 )
00620 {
00621   fPageNumber++;
00622   printFLN("%%%%Page: %d %d",fPageNumber,fPageNumber);
00623   putSaveStateInStream();
00624 }
00626 void SbPainterPS::putEndPageInStream (
00627 )
00630 {
00631   putInStreamF("showpage ");
00632   putRestoreStateInStream();
00633 }
00635 void SbPainterPS::putRGB_InStream (
00636  float aR 
00637 ,float aG
00638 ,float aB 
00639 )
00642 {
00643   if(fParams.shade==Color)       
00644     putInStreamF("%.2f %.2f %.2f rgb ",aR,aG,aB);
00645   else if(fParams.shade==Grey)
00646     putInStreamF("%.2f setgray ",convertRGB_ToGrey(aR,aG,aB));
00647   else if(fParams.shade==BlackWhite)  
00648     putInStreamF("0. setgray ",convertRGB_ToGrey(aR,aG,aB));
00649 }
00651 void SbPainterPS::putLineWidthInStream(
00652  int aWidth
00653 )
00656 {
00657   if(fParams.lineWidth<0.) {
00658     if(aWidth==1) {
00659       putInStreamF("%.1f lw ",0.5); // For a better rendering.
00660     } else {
00661       putInStreamF("%.1f lw ",(float)(aWidth));
00662     }
00663   } else {
00664     putInStreamF("%.1f lw ",fParams.lineWidth); 
00665   }
00666 }
00668 void SbPainterPS::putMarkerSizeInStream (
00669  float aSize
00670 )
00673 {
00674   if(aSize==fMarkerSize) return;
00675   fMarkerSize = aSize;
00676   putInStreamF("/ms %g def /msi %g def ",aSize,1./aSize);
00677 }
00678 /*
00680 void SbPainterPS::putMarkerStyleInStream (
00681  SbMarkerStyle aStyle 
00682 )
00685 {
00686   switch (aStyle) {
00687   case SbMarkerPlus:
00688     putInStreamF("plus ");
00689     break;
00690   case SbMarkerAsterisk:
00691   case SbMarkerStar:
00692     putInStreamF("asterisk ");
00693     break;
00694   case SbMarkerCross:
00695     putInStreamF("cross ");
00696     break;
00697   case SbMarkerTriangleUpLine:
00698     putInStreamF("triangle ");
00699     break;
00700   default:
00701     putLineToInStream(0.,0.);
00702     break;
00703   }
00704 }
00705 */
00707 void SbPainterPS::putBackgroundInStream (
00708  float aR
00709 ,float aG
00710 ,float aB
00711 ,float aWidth 
00712 ,float aHeight 
00713 )
00716 {
00717   putNewPathInStream(); 
00718   putMoveInStream(0.,0.);
00719   putLineToInStream(aWidth,0.);
00720   putLineToInStream(0.,aHeight);
00721   putLineToInStream(-aWidth,0.);
00722   putLineToInStream(0.,-aHeight);
00723   putClosePathInStream();
00724   if(fParams.doBack==1) {
00725     // Back :
00726     putSaveStateInStream();
00727     putRGB_InStream(aR,aG,aB);
00728     putFillInStream();       
00729     putRestoreStateInStream();
00730   }
00731   // Clip :
00732   putInStreamF("clip ");
00733 }
00735 void SbPainterPS::putFrameInStream (
00736  float aR
00737 ,float aG
00738 ,float aB
00739 ,float aWidth 
00740 ,float aHeight 
00741 )
00744 {
00745   putNewPathInStream(); 
00746   putMoveInStream(0.,0.);
00747   putLineToInStream(aWidth,0.);
00748   putLineToInStream(0.,aHeight);
00749   putLineToInStream(-aWidth,0.);
00750   putLineToInStream(0.,-aHeight);
00751   putClosePathInStream();
00752   putRGB_InStream(aR,aG,aB);
00753   putLineWidthInStream(1);
00754   putCapInStream(1);
00755   putInStreamF("ss ");
00756   putStrokeInStream();
00757 }
00759 float SbPainterPS::convertRGB_ToGrey (
00760  float aRed
00761 ,float aGreen
00762 ,float aBlue
00763 )
00766 {
00767   return (0.3F * aRed + 0.59F * aGreen + 0.11F * aBlue);
00768 }
00770 void SbPainterPS::putRotateInStream(
00771  float aX                  
00772 )
00775 {
00776   putInStreamF("%.2f  rotate ",aX);
00777 }
00779 void SbPainterPS::putNewPathInStream(
00780 )
00783 {
00784   putInStreamF("n ");
00785 }
00787 void SbPainterPS::putStrokeInStream(
00788 )
00791 {
00792   putInStreamF("s ");
00793 }
00795 void SbPainterPS::putFillInStream(
00796 )
00799 {
00800   putInStreamF("f ");
00801 }
00803 void SbPainterPS::putClosePathInStream(
00804 )
00807 {
00808   putInStreamF("cl ");
00809 }
00811 void SbPainterPS::putCapInStream(
00812  int aX
00813 )
00816 {
00817   putInStreamF("%1d lc ",aX);
00818 }
00820 void SbPainterPS::putLineToInStream(
00821  float aX
00822 ,float aY
00823 )
00826 {
00827   putInStreamF ("%.2f %.2f rl ",aX,aY);
00828 }
00830 void SbPainterPS::putMoveInStream(
00831  float aX
00832 ,float aY
00833 )
00836 {
00837   putInStreamF ("%.2f %.2f m ",aX,aY);
00838 }
00840 void SbPainterPS::putCircleInStream(
00841  float aX                            
00842 ,float aY                            
00843 ,float aR                            
00844 )
00847 {
00848   putInStreamF("%.2f %.2f %.2f 0 360 arc s ",aX,aY,aR);
00849 }
00850 /*
00852 void SbPainterPS::putLineStyleInStream(
00853  SbLineStyle aStyle
00854 )
00857 {
00858   switch(aStyle) {
00859   case SbLineSolid:putInStreamF("ss ") ;break;
00860   case SbLineDashed:putInStreamF("sd ") ;break;
00861   case SbLineDotted:putInStreamF("so ") ;break;
00862   case SbLineDashDotted:putInStreamF("sdo ");break;
00863   }
00864 }
00865 */
00871 void SbPainterPS::putImageInStream (
00872  unsigned int aWidth
00873 ,unsigned int aHeight
00874 ,GetRGB_Function aProc
00875 )
00878 {
00879   if((aWidth<=0)||(aHeight<=0)) return;
00880   if(!aProc) return;
00881 
00882   putSaveStateInStream      ();
00883   putInStreamF     ("%d %d scale ", aWidth, aHeight ); 
00884   int status = 1;
00885   int      nbhex;
00886   unsigned int row,col,col_max;
00887   double   dr,dg,db;
00888   typedef unsigned char Uchar;
00889   Uchar    red,green,blue,b;
00890   if(fParams.shade!=0) { /*grey*/
00891     putInStreamF   ("/picstr %d string def ",aWidth); 
00892     putInStreamF   ("%d %d %d ",aWidth,aHeight,8); 
00893     putInStreamF   ("[ %d 0 0 -%d 0 %d ] ",aWidth,aHeight,aHeight); 
00894     putInStreamF   ("{ currentfile picstr readhexstring pop } " );
00895     printFLN ("image " );
00896     for ( row = 0; row < aHeight; row++ ){
00897       for ( col = 0; col < aWidth; col++){ 
00898         double    fgrey;
00899         Uchar     grey;
00900         status    = aProc(col,row,dr,dg,db)==0 ? 0 : status;
00901         fgrey     = ConvertRGB_ToGrey(dr,dg,db);
00902         grey      = (Uchar) ( 255. * fgrey);
00903         writeByte (grey);
00904       }
00905     }
00906     nbhex     = aWidth * aHeight * 2; 
00907     printFLN ("%%%% nbhex digit          :%d ",nbhex); 
00908     printFLN ("%%%% nbhex/record_length  :%d ",nbhex/METAFILE_RECORD_LENGTH); 
00909     printFLN ("%%%% nbhex%%record_length :%d ",nbhex%METAFILE_RECORD_LENGTH); 
00910   }else if(fParams.nbit==2){ 
00911     int       nbyte2;
00912     nbyte2    = (aWidth   *  3)/4;
00913     nbyte2   /=3;
00914     nbyte2   *=3;
00915     col_max   = (nbyte2  *  4)/3;
00916     /* 2 bit for r and g and b   */
00917     /* rgbs following each other */
00918     putInStreamF   ("/rgbstr %d string def ",nbyte2); 
00919     putInStreamF   ("%d %d %d ",col_max,aHeight,2); 
00920     putInStreamF   ("[ %d 0 0 -%d 0 %d ] ",col_max,aHeight,aHeight); 
00921     putInStreamF   ("{ currentfile rgbstr readhexstring pop } " );
00922     putInStreamF   ("false 3 " );
00923     printFLN ("colorimage " );
00924     for ( row = 0; row < aHeight; row++ ){
00925       for ( col = 0; col < col_max; col+=4){
00926         status  = aProc(col,row,dr,dg,db)==0 ? 0 : status;
00927         red     = (Uchar) ( 3. * dr);
00928         green   = (Uchar) ( 3. * dg);
00929         blue    = (Uchar) ( 3. * db);
00930         b       = red;
00931         b       = (b<<2)+green;
00932         b       = (b<<2)+blue;
00933         status  = aProc(col+1,row,dr,dg,db)==0 ? 0 : status;
00934         red     = (Uchar) ( 3. * dr);
00935         green   = (Uchar) ( 3. * dg);
00936         blue    = (Uchar) ( 3. * db);
00937         b     = (b<<2)+red;
00938         writeByte (b);
00939         
00940         b       = green;
00941         b       = (b<<2)+blue;
00942         status  = aProc(col+2,row,dr,dg,db)==0 ? 0 : status;
00943         red     = (Uchar) ( 3. * dr);
00944         green   = (Uchar) ( 3. * dg);
00945         blue    = (Uchar) ( 3. * db);
00946         b     = (b<<2)+red;
00947         b     = (b<<2)+green;
00948         writeByte (b);
00949         
00950         b       = blue;
00951         status  = aProc(col+3,row,dr,dg,db)==0 ? 0 : status;
00952         red     = (Uchar) ( 3. * dr);
00953         green   = (Uchar) ( 3. * dg);
00954         blue    = (Uchar) ( 3. * db);
00955         b     = (b<<2)+red;
00956         b     = (b<<2)+green;
00957         b     = (b<<2)+blue;
00958         writeByte (b);
00959       }
00960     }
00961   }else if(fParams.nbit==4){ 
00962     int       nbyte4;
00963     nbyte4    = (aWidth  * 3)/2;
00964     nbyte4   /=3;
00965     nbyte4   *=3;
00966     col_max   = (nbyte4 * 2)/3;
00967     /* 4 bit for r and g and b   */
00968     /* rgbs following each other */
00969     putInStreamF   ("/rgbstr %d string def ",nbyte4); 
00970     putInStreamF   ("%d %d %d ",col_max,aHeight,4); 
00971     putInStreamF   ("[ %d 0 0 -%d 0 %d ] ",col_max,aHeight,aHeight); 
00972     putInStreamF   ("{ currentfile rgbstr readhexstring pop } " );
00973     putInStreamF   ("false 3 " );
00974     printFLN ("colorimage " );
00975     for ( row = 0; row < aHeight; row++ ){
00976       for ( col = 0; col < col_max; col+=2){
00977         status  = aProc(col,row,dr,dg,db)==0 ? 0 : status;
00978         red     = (Uchar) ( 15. * dr);
00979         green   = (Uchar) ( 15. * dg);
00980         putInStreamF ("%x%x",red,green);
00981         blue    = (Uchar) ( 15. * db);
00982         
00983         status  = aProc(col+1,row,dr,dg,db)==0 ? 0 : status;
00984         red     = (Uchar) ( 15. * dr);
00985         putInStreamF ("%x%x",blue,red);
00986         green   = (Uchar) ( 15. * dg);
00987         blue    = (Uchar) ( 15. * db);
00988         putInStreamF ("%x%x",green,blue);
00989       }
00990     }
00991   }else{ 
00992     int       nbyte8;
00993     nbyte8    = aWidth   * 3;
00994     /* 8 bit for r and g and b   */
00995     putInStreamF   ("/rgbstr %d string def ",nbyte8); 
00996     putInStreamF   ("%d %d %d ",aWidth,aHeight,8); 
00997     putInStreamF   ("[ %d 0 0 -%d 0 %d ] ",aWidth,aHeight,aHeight); 
00998     putInStreamF   ("{ currentfile rgbstr readhexstring pop } " );
00999     putInStreamF   ("false 3 " );
01000     printFLN   ("colorimage " );
01001     for ( row = 0; row < aHeight; row++ ){
01002       for ( col = 0; col < aWidth; col++){
01003         status     = aProc(col,row,dr,dg,db)==0 ? 0 : status;
01004         red        = (Uchar) ( 255. * dr);
01005         writeByte (red);
01006         green      = (Uchar) ( 255. * dg);
01007         writeByte (green);
01008         blue       = (Uchar) ( 255. * db);
01009         writeByte (blue);
01010       }
01011     }
01012   }
01013   if(status==0) 
01014     ::printf("SbPainterPS::putImageInStream: problem to retreive some pixel rgb.\n");
01015   putRestoreStateInStream();
01016 }
01018 void SbPainterPS::writeByte (
01019  unsigned char a_byte
01020 )
01023 {
01024   unsigned char h = a_byte / 16;
01025   unsigned char l = a_byte % 16;
01026   putInStreamF ("%x%x",h,l);
01027 }
01031 char* GetDate (
01032 )
01034 // Return local date.
01036 {
01037   time_t d;
01038   time(&d);
01039   char* string = ctime(&d);
01040   string[24] = '\0';
01041   return string;
01042 }
01044 double ConvertRGB_ToGrey(
01045  double a_red
01046 ,double a_green
01047 ,double a_blue
01048 )
01051 {
01052   return (0.30 * a_red + 0.59 * a_green + 0.11 * a_blue);
01053 }
01054 
01055 #endif

Generated on Mon May 27 17:50:34 2013 for Geant4 by  doxygen 1.4.7