26 #ifdef G4VIS_BUILD_OI_DRIVER
38 #define STRDUP(str) ((str) != NULL ? (::strcpy((char*)::malloc((unsigned)::strlen(str) + 1), str)) : (char*)NULL)
39 #define STRDEL(str) {if((str)!=NULL) {::free(str);str=NULL;}}
49 #define METAFILE_DEFAULT "out.ps"
50 #define METAFILE_SCALE 1.
52 static char* GetDate();
53 static double ConvertRGB_ToGrey(
double,
double,
double);
57 :fDeviceWidth((8.5-1.) * 72. * METAFILE_SCALE)
58 ,fDeviceHeight(11. * 72. * METAFILE_SCALE)
70 fParams.shade =
Color;
74 fParams.lineWidth = -1.;
76 ::setlocale(LC_NUMERIC,
"USA");
86 if(fBufferString!=NULL) ::free(fBufferString);
89 ::printf(
"SbPainterPS : bad gsave/grestore balance : %d.\n",fGSave);
101 if(fFile==NULL)
return;
112 if(fFile==NULL)
return;
126 if(fFile==NULL)
return;
298 fParams.shade = aShade;
307 fParams.portrait = aPortrait;
316 fParams.doBack = aDoback;
325 if( (aNbit==2) || (aNbit==4) || (aNbit==8) )
326 fParams.nbit = aNbit;
337 fParams.lineWidth = (float)aWidth;
349 fFileName =
STRDUP(aString);
375 if( (aString==NULL) || (*aString==
'\0') ) {
376 if( (fFileName==NULL) || (*fFileName==
'\0') ) {
377 fFile = ::fopen(METAFILE_DEFAULT,
"wb");
379 fFileName =
STRDUP(METAFILE_DEFAULT);
381 fFile = ::fopen(fFileName,
"wb");
384 fFile = ::fopen(aString,
"wb");
386 fFileName =
STRDUP(aString);
388 if(fFile==NULL)
return;
394 printFLN (
"%%!PS-Adobe-2.0");
395 printFLN (
"%%%%Creator: HEPVis::SbPainterPS.");
396 printFLN(
"%%%%CreationDate: %s",GetDate());
397 printFLN(
"%%%%Title: %s",fFileName);
398 printFLN(
"%%%%Pages: (atend)");
399 printFLN(
"%%%%BoundingBox: 0 0 %d %d",
400 (
int)fDeviceWidth,(
int)fDeviceHeight);
401 printFLN(
"%%%%DocumentFonts: Courier-Bold");
402 printFLN(
"%%%%DocumentPaperSizes: a4");
403 printFLN(
"%%%%EndComments");
407 putInStreamF(
"/n {newpath} def ");
408 putInStreamF(
"/cl {closepath} def ");
409 putInStreamF(
"/s {stroke} def ");
410 putInStreamF(
"/f {fill} def ");
412 putInStreamF(
"/m {moveto} def ");
413 putInStreamF(
"/rm {rmoveto} def ");
414 putInStreamF(
"/rl {rlineto} def ");
416 putInStreamF(
"/lc {setlinecap} def ");
417 putInStreamF(
"/lw {setlinewidth} def ");
418 putInStreamF(
"/rgb {setrgbcolor} def ");
419 putInStreamF(
"/ss {[] 0 setdash} def ") ;
420 putInStreamF(
"/sd {[12 6] 0 setdash} def ");
421 putInStreamF(
"/so {[6 12] 0 setdash} def ");
422 putInStreamF(
"/sdo {[18 12 6 12] 0 setdash} def ");
425 putInStreamF(
"/ms 2. def /msi .5 def ");
426 putInStreamF(
"/cross {ms ms scale -1. -1. rm ");
427 putInStreamF(
"2. 2. rl 0. -2. rm -2. 2. rl msi msi scale} def ");
428 putInStreamF(
"/plus {ms ms scale -1. 0. rm 2. 0. rl ");
429 putInStreamF(
"-1. 1. rm 0. -2. rl msi msi scale} def ");
430 putInStreamF(
"/asterisk {ms ms scale -1. 0. rm 2. 0. rl -1. 1. rm ");
431 putInStreamF(
"0. -2. rl 0. 1. rm -0.707 -0.707 rm 1.414 1.414 rl ");
432 putInStreamF(
"0. -1.414 rm -1.414 1.414 rl msi msi scale} def ");
433 putInStreamF(
"/triangle {ms ms scale 0. 1. rm -0.6 -1.5 rl ");
434 putInStreamF(
"1.2 0. rl -0.6 1.5 rl msi msi scale} def ");
436 putInStreamF(
"/sh {show} def ");
437 putInStreamF(
"/df {/Courier-Bold findfont} def ");
438 putInStreamF(
"/mf {makefont setfont} def ");
439 printFLN(
"%%%%EndProlog");
447 if(fFile==NULL)
return;
449 printFLN(
"%%%%Trailer");
450 printFLN(
"%%%%Pages: %d",fPageNumber);
458 void SbPainterPS::putInStreamF(
465 if(fFile==NULL)
return;
467 va_start(args,aFormat);
468 printV(aFormat,args);
470 int length = ::strlen(fBufferString);
472 ::printf(
"SoPostScript::putInStreamF overflow\n");
475 int nlength = fBufferCount + length;
477 fBufferPointer[fBufferCount] =
'\0';
478 if(::fprintf(fFile,
"%s\n",(
char*)fBufferPointer)<0) {
479 ::printf(
"SoPostScript::putInStreamF fprintf error\n");
484 unsigned char* pointer = fBufferPointer + fBufferCount;
485 ::strcpy((
char*)pointer,fBufferString);
486 fBufferCount = nlength;
489 void SbPainterPS::printFLN(
496 if(fFile==NULL)
return;
498 va_start(args,aFormat);
499 printV(aFormat,args);
503 fBufferPointer[fBufferCount] =
'\0';
504 if(::fprintf (fFile,
"%s\n",(
char*)fBufferPointer)<0) {
505 ::printf(
"SbPainterPS::printFLN fprintf error\n");
510 if(::fprintf (fFile,
"%s\n",fBufferString)<0) {
511 ::printf(
"SbPainterPS::printFLN fprintf error\n");
515 void SbPainterPS::printV(
523 if(fBufferString==NULL) {
524 fBufferString = (
char*)::malloc(MAX_STR *
sizeof(
char));
525 if(fBufferString==NULL)
return;
527 fBufferString[MAX_STR-1] =
'\0';
528 ::vsprintf(fBufferString,This,aArgs);
529 if(fBufferString[MAX_STR-1]!=
'\0') {
530 ::printf(
"SbPainterPS::printV overflow\n");
531 fBufferString[0] =
'\0';
545 if(aWidth <=0.) aWidth = 100.;
546 if(aHeight<=0.) aHeight = 100.;
550 (
float)(fDeviceHeight/30.));
553 if(fDeviceWidth<=fDeviceHeight)
554 scale = (aHeight<=aWidth ?
555 fDeviceWidth /aWidth : fDeviceWidth /aHeight );
557 scale = (aHeight<=aWidth ?
558 fDeviceHeight /aWidth : fDeviceHeight /aHeight );
561 if(fParams.portrait==1) {
562 xtra = (fDeviceWidth - scale * aWidth)/2;
563 ytra = (fDeviceHeight - scale * aHeight)/2;
567 xtra = (fDeviceHeight - scale * aWidth)/2;
568 ytra = (fDeviceWidth - scale * aHeight)/2;
583 putInStreamF(
"gsave ");
592 putInStreamF(
"grestore ");
603 putInStreamF(
"%.2f %.2f translate ",aX,aY);
613 putInStreamF(
"%.2f %.2f scale ",aX,aY);
622 printFLN(
"%%%%Page: %d %d",fPageNumber,fPageNumber);
631 putInStreamF(
"showpage ");
643 if(fParams.shade==
Color)
644 putInStreamF(
"%.2f %.2f %.2f rgb ",aR,aG,aB);
645 else if(fParams.shade==Grey)
646 putInStreamF(
"%.2f setgray ",convertRGB_ToGrey(aR,aG,aB));
647 else if(fParams.shade==BlackWhite)
648 putInStreamF(
"0. setgray ",convertRGB_ToGrey(aR,aG,aB));
657 if(fParams.lineWidth<0.) {
659 putInStreamF(
"%.1f lw ",0.5);
661 putInStreamF(
"%.1f lw ",(
float)(aWidth));
664 putInStreamF(
"%.1f lw ",fParams.lineWidth);
674 if(aSize==fMarkerSize)
return;
676 putInStreamF(
"/ms %g def /msi %g def ",aSize,1./aSize);
724 if(fParams.doBack==1) {
732 putInStreamF(
"clip ");
759 float SbPainterPS::convertRGB_ToGrey (
767 return (0.3F * aRed + 0.59F * aGreen + 0.11F * aBlue);
776 putInStreamF(
"%.2f rotate ",aX);
817 putInStreamF(
"%1d lc ",aX);
827 putInStreamF (
"%.2f %.2f rl ",aX,aY);
837 putInStreamF (
"%.2f %.2f m ",aX,aY);
848 putInStreamF(
"%.2f %.2f %.2f 0 360 arc s ",aX,aY,aR);
873 ,
unsigned int aHeight
874 ,GetRGB_Function aProc
879 if((aWidth<=0)||(aHeight<=0))
return;
883 putInStreamF (
"%d %d scale ", aWidth, aHeight );
886 unsigned int row,
col,col_max;
888 typedef unsigned char Uchar;
890 if(fParams.shade!=0) {
891 putInStreamF (
"/picstr %d string def ",aWidth);
892 putInStreamF (
"%d %d %d ",aWidth,aHeight,8);
893 putInStreamF (
"[ %d 0 0 -%d 0 %d ] ",aWidth,aHeight,aHeight);
894 putInStreamF (
"{ currentfile picstr readhexstring pop } " );
895 printFLN (
"image " );
896 for ( row = 0; row < aHeight; row++ ){
897 for ( col = 0; col < aWidth; col++){
900 status = aProc(col,row,dr,dg,db)==0 ? 0 : status;
901 fgrey = ConvertRGB_ToGrey(dr,dg,db);
902 grey = (Uchar) ( 255. * fgrey);
906 nbhex = aWidth * aHeight * 2;
907 printFLN (
"%%%% nbhex digit :%d ",nbhex);
910 }
else if(fParams.nbit==2){
912 nbyte2 = (aWidth * 3)/4;
915 col_max = (nbyte2 * 4)/3;
918 putInStreamF (
"/rgbstr %d string def ",nbyte2);
919 putInStreamF (
"%d %d %d ",col_max,aHeight,2);
920 putInStreamF (
"[ %d 0 0 -%d 0 %d ] ",col_max,aHeight,aHeight);
921 putInStreamF (
"{ currentfile rgbstr readhexstring pop } " );
922 putInStreamF (
"false 3 " );
923 printFLN (
"colorimage " );
924 for ( row = 0; row < aHeight; row++ ){
925 for ( col = 0; col < col_max; col+=4){
926 status = aProc(col,row,dr,dg,db)==0 ? 0 : status;
927 red = (Uchar) ( 3. * dr);
928 green = (Uchar) ( 3. * dg);
929 blue = (Uchar) ( 3. * db);
933 status = aProc(col+1,row,dr,dg,db)==0 ? 0 : status;
934 red = (Uchar) ( 3. * dr);
935 green = (Uchar) ( 3. * dg);
936 blue = (Uchar) ( 3. * db);
942 status = aProc(col+2,row,dr,dg,db)==0 ? 0 : status;
943 red = (Uchar) ( 3. * dr);
944 green = (Uchar) ( 3. * dg);
945 blue = (Uchar) ( 3. * db);
951 status = aProc(col+3,row,dr,dg,db)==0 ? 0 : status;
952 red = (Uchar) ( 3. * dr);
953 green = (Uchar) ( 3. * dg);
954 blue = (Uchar) ( 3. * db);
961 }
else if(fParams.nbit==4){
963 nbyte4 = (aWidth * 3)/2;
966 col_max = (nbyte4 * 2)/3;
969 putInStreamF (
"/rgbstr %d string def ",nbyte4);
970 putInStreamF (
"%d %d %d ",col_max,aHeight,4);
971 putInStreamF (
"[ %d 0 0 -%d 0 %d ] ",col_max,aHeight,aHeight);
972 putInStreamF (
"{ currentfile rgbstr readhexstring pop } " );
973 putInStreamF (
"false 3 " );
974 printFLN (
"colorimage " );
975 for ( row = 0; row < aHeight; row++ ){
976 for ( col = 0; col < col_max; col+=2){
977 status = aProc(col,row,dr,dg,db)==0 ? 0 : status;
978 red = (Uchar) ( 15. * dr);
979 green = (Uchar) ( 15. * dg);
980 putInStreamF (
"%x%x",red,green);
981 blue = (Uchar) ( 15. * db);
983 status = aProc(col+1,row,dr,dg,db)==0 ? 0 : status;
984 red = (Uchar) ( 15. * dr);
985 putInStreamF (
"%x%x",blue,red);
986 green = (Uchar) ( 15. * dg);
987 blue = (Uchar) ( 15. * db);
988 putInStreamF (
"%x%x",green,blue);
995 putInStreamF (
"/rgbstr %d string def ",nbyte8);
996 putInStreamF (
"%d %d %d ",aWidth,aHeight,8);
997 putInStreamF (
"[ %d 0 0 -%d 0 %d ] ",aWidth,aHeight,aHeight);
998 putInStreamF (
"{ currentfile rgbstr readhexstring pop } " );
999 putInStreamF (
"false 3 " );
1000 printFLN (
"colorimage " );
1001 for ( row = 0; row < aHeight; row++ ){
1002 for ( col = 0; col < aWidth; col++){
1003 status = aProc(col,row,dr,dg,db)==0 ? 0 : status;
1004 red = (Uchar) ( 255. * dr);
1006 green = (Uchar) ( 255. * dg);
1008 blue = (Uchar) ( 255. * db);
1014 ::printf(
"SbPainterPS::putImageInStream: problem to retreive some pixel rgb.\n");
1018 void SbPainterPS::writeByte (
1019 unsigned char a_byte
1024 unsigned char h = a_byte / 16;
1025 unsigned char l = a_byte % 16;
1026 putInStreamF (
"%x%x",h,l);
1039 char*
string = ctime(&d);
1044 double ConvertRGB_ToGrey(
1052 return (0.30 * a_red + 0.59 * a_green + 0.11 * a_blue);