source: trunk/source/visualization/OpenInventor/src/SbPainterPS.cc @ 1348

Last change on this file since 1348 was 1348, checked in by garnier, 13 years ago

update

  • Property svn:mime-type set to text/cpp
File size: 36.1 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26#ifdef G4VIS_BUILD_OI_DRIVER
27
28/*----------------------------HEPVis----------------------------------------*/
29/*                                                                          */
30/* Node:             SbPainterPS                                            */
31/* Author:           Guy Barrand                                            */
32/*                                                                          */
33/*--------------------------------------------------------------------------*/
34// this :
35#include <HEPVis/SbPainterPS.h>
36
37//#include <HEPVis/SbString.h>
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;}}
40
41//#define DEBUG
42#include <stdlib.h>
43#include <string.h>
44#include <stdio.h>
45#include <stdarg.h>
46#include <time.h>
47#include <locale.h>
48
49#define METAFILE_DEFAULT "out.ps"
50#define METAFILE_SCALE 1.
51
52static char* GetDate();
53static double ConvertRGB_ToGrey(double,double,double);
54//////////////////////////////////////////////////////////////////////////////
55SbPainterPS::SbPainterPS(
56)
57:fDeviceWidth((8.5-1.) * 72. * METAFILE_SCALE) /* 540. * METAFILE_SCALE */
58,fDeviceHeight(11.     * 72. * METAFILE_SCALE) /* 792. * METAFILE_SCALE */
59,fPageNumber(0)
60,fPagePos(0)
61,fMarkerSize(2.)
62,fFile(NULL)
63,fFileName(NULL)
64,fGSave(0)
65,fBufferCount(0)
66,fBufferString(NULL)
67,fBufferPointer(0)
68//////////////////////////////////////////////////////////////////////////////
69//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
70{
71  fParams.shade = Color;
72  fParams.portrait = 1;
73  fParams.nbit = 2;
74  fParams.doBack = 1;
75  fParams.lineWidth = -1.;
76#ifdef WIN32
77  ::setlocale(LC_NUMERIC,"USA");
78#endif
79}
80//////////////////////////////////////////////////////////////////////////////
81SbPainterPS::~SbPainterPS(
82)
83//////////////////////////////////////////////////////////////////////////////
84//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
85{
86  if(fFile!=NULL) closeStream ();
87  if(fBufferString!=NULL) ::free(fBufferString);
88  fBufferString = NULL;
89  if(fGSave!=0) {
90    ::printf("SbPainterPS : bad gsave/grestore balance : %d.\n",fGSave);
91  }
92}
93//////////////////////////////////////////////////////////////////////////////
94//////////////////////////////////////////////////////////////////////////////
95//////////////////////////////////////////////////////////////////////////////
96void SbPainterPS::beginTraversal (
97)
98//////////////////////////////////////////////////////////////////////////////
99//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
100{
101  if(fFile==NULL) openFileForWriting(NULL);
102  if(fFile==NULL) return;
103  putBeginPageInStream();
104  putPageScaleInStream((float)fWindowWidth,(float)fWindowHeight);
105  putSaveStateInStream();
106}
107//////////////////////////////////////////////////////////////////////////////
108void SbPainterPS::endTraversal(
109)
110//////////////////////////////////////////////////////////////////////////////
111//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
112{
113  if(fFile==NULL) return;
114  putFrameInStream(0.0,0.0,0.0,(float)fWindowWidth,(float)fWindowHeight);
115  putRestoreStateInStream();
116  putEndPageInStream();
117}
118//////////////////////////////////////////////////////////////////////////////
119void SbPainterPS::clearColorBuffer(
120 float aRed
121,float aGreen
122,float aBlue
123)
124//////////////////////////////////////////////////////////////////////////////
125//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
126{
127  if(fFile==NULL) return;
128  putBackgroundInStream(aRed,aGreen,aBlue,
129                           (float)fWindowWidth,(float)fWindowHeight);
130}
131/*
132//////////////////////////////////////////////////////////////////////////////
133void SbPainterPS::drawPrimitive (
134 SbPrimitiveType aType
135,int aPointn
136,float* aXs
137,float* aYs
138,float* //aZs
139,const SbPainterContext& aAtb
140)
141//////////////////////////////////////////////////////////////////////////////
142//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
143{
144  if(fFile==NULL) return;
145  switch(aType) {
146  case SbPrimitivePoints:
147    drawMarkers(aPointn,
148                aXs,aYs,
149                aAtb.fRed,aAtb.fGreen,aAtb.fBlue,
150                aAtb.fMarkerStyle,aAtb.fMarkerSize);
151    break;
152  case SbPrimitiveLineStrip:
153  case SbPrimitiveLineLoop:
154    drawLines(aPointn,
155              aXs,aYs,
156              aAtb.fRed,aAtb.fGreen,aAtb.fBlue,
157              aAtb.fLineStyle,aAtb.fLineWidth);
158    break;
159  case SbPrimitivePolygon:
160    drawPolygon(aPointn,
161                aXs,aYs,
162                aAtb.fRed,aAtb.fGreen,aAtb.fBlue,
163                aAtb.fAreaStyle);
164    break;
165  default:
166    break;
167  }
168}
169//////////////////////////////////////////////////////////////////////////////
170void SbPainterPS::drawPolygon(
171 int aPointn
172,float* aXs
173,float* aYs
174,float aRed
175,float aGreen
176,float aBlue
177,const SbAreaStyle& //aStyle
178)
179//////////////////////////////////////////////////////////////////////////////
180//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
181{
182  if(fFile==NULL) return;
183  if(aPointn<=0) return;
184  putNewPathInStream();
185  putMoveInStream(aXs[0],aYs[0]);
186  for(int count=1;count<aPointn;count++) {
187    putLineToInStream(aXs[count] - aXs[count-1],
188                      aYs[count] - aYs[count-1]);
189  }
190  if ( (aXs[0]==aXs[aPointn-1]) &&
191       (aYs[0]==aYs[aPointn-1]) )
192    putClosePathInStream();
193  putRGB_InStream(aRed,aGreen,aBlue);
194  putFillInStream();
195}
196//////////////////////////////////////////////////////////////////////////////
197void SbPainterPS::drawLines(
198 int aPointn
199,float* aXs
200,float* aYs
201,float aRed
202,float aGreen
203,float aBlue
204,const SbLineStyle& aStyle
205,int aWidth
206)
207//////////////////////////////////////////////////////////////////////////////
208//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
209{
210  if(fFile==NULL) return;
211  if(aPointn<=0) return;
212  putMoveInStream(aXs[0],aYs[0]);
213  for(int count=1;count<aPointn;count++) {
214    putLineToInStream(aXs[count] - aXs[count-1],
215                      aYs[count] - aYs[count-1]);
216  }
217  if ( (aXs[0]==aXs[aPointn-1]) &&
218       (aYs[0]==aYs[aPointn-1]) )
219    putClosePathInStream();
220  putRGB_InStream(aRed,aGreen,aBlue);
221  putLineWidthInStream(aWidth);
222  putCapInStream(1);
223  putLineStyleInStream(aStyle);
224  putStrokeInStream();
225}
226//////////////////////////////////////////////////////////////////////////////
227void SbPainterPS::drawMarkers (
228 int aPointn
229,float* aXs
230,float* aYs
231,float aRed
232,float aGreen
233,float aBlue
234,const SbMarkerStyle& aStyle
235,int aSize
236)
237//////////////////////////////////////////////////////////////////////////////
238//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
239{
240  if(fFile==NULL) return;
241  float mark_size  = (float)(aSize <=0 ? 1. : aSize);   
242  mark_size *= 0.6F;
243  if(aStyle==SbMarkerCircleLine) {
244    putNewPathInStream();
245    int icount = 1;
246    for(int count=0;count<aPointn;count++) {
247      putCircleInStream(aXs[count],aYs[count],mark_size);
248#define MAX_PATH_POINT 100
249      if(icount==MAX_PATH_POINT) {
250        putRGB_InStream(aRed,aGreen,aBlue);
251        putLineWidthInStream(1);
252        putCapInStream(1);
253        putStrokeInStream();
254        icount = 1;
255        if(count!=aPointn-1) putNewPathInStream();
256      } else {
257        icount++;
258      }
259    }
260    putRGB_InStream(aRed,aGreen,aBlue);
261    putLineWidthInStream(1);
262    putCapInStream(1);
263    putStrokeInStream();
264  } else {
265    putNewPathInStream();
266    int icount = 1;
267    for(int count=0;count<aPointn;count++) {
268      putMoveInStream(aXs[count],aYs[count]);
269      putMarkerSizeInStream(mark_size);
270      putMarkerStyleInStream(aStyle);
271      if(icount==MAX_PATH_POINT) {
272        putRGB_InStream(aRed,aGreen,aBlue);
273        putLineWidthInStream(1);
274        putCapInStream(1);
275        putStrokeInStream();
276        icount = 1;
277        if(count!=aPointn-1) putNewPathInStream();
278      } else {
279        icount++;
280      }
281    }
282    putRGB_InStream(aRed,aGreen,aBlue);
283    putLineWidthInStream(1);
284    putCapInStream(1);
285    putStrokeInStream();
286  }
287}
288*/
289//////////////////////////////////////////////////////////////////////////////
290//////////////////////////////////////////////////////////////////////////////
291//////////////////////////////////////////////////////////////////////////////
292//////////////////////////////////////////////////////////////////////////////
293void SbPainterPS::setColorScheme(
294 int aShade
295)
296//////////////////////////////////////////////////////////////////////////////
297//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
298{
299  fParams.shade = aShade;
300}
301//////////////////////////////////////////////////////////////////////////////
302void SbPainterPS::setOrientation(
303 int aPortrait
304)
305//////////////////////////////////////////////////////////////////////////////
306//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
307{
308  fParams.portrait = aPortrait;
309}
310//////////////////////////////////////////////////////////////////////////////
311void SbPainterPS::setBackgroundDrawn(
312 int aDoback
313)
314//////////////////////////////////////////////////////////////////////////////
315//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
316{
317  fParams.doBack = aDoback;
318}
319//////////////////////////////////////////////////////////////////////////////
320void SbPainterPS::setBitsPerPixel(
321 int aNbit
322)
323//////////////////////////////////////////////////////////////////////////////
324//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
325{
326  if( (aNbit==2) || (aNbit==4) || (aNbit==8) )
327    fParams.nbit = aNbit;
328  else
329    fParams.nbit = 2;
330}
331//////////////////////////////////////////////////////////////////////////////
332void SbPainterPS::setLineWidth(
333 int aWidth
334)
335//////////////////////////////////////////////////////////////////////////////
336//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
337{
338  fParams.lineWidth = (float)aWidth;
339}
340//////////////////////////////////////////////////////////////////////////////
341//////////////////////////////////////////////////////////////////////////////
342//////////////////////////////////////////////////////////////////////////////
343void SbPainterPS::setFileName(
344 const char* aString
345)
346//////////////////////////////////////////////////////////////////////////////
347//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
348{
349  STRDEL(fFileName);
350  fFileName = STRDUP(aString);
351}
352//////////////////////////////////////////////////////////////////////////////
353const char* SbPainterPS::getFileName(
354) const
355//////////////////////////////////////////////////////////////////////////////
356//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
357{
358  return fFileName;
359}
360//////////////////////////////////////////////////////////////////////////////
361void* SbPainterPS::getStream(
362)
363//////////////////////////////////////////////////////////////////////////////
364//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
365{
366  return fFile;
367}
368//////////////////////////////////////////////////////////////////////////////
369void SbPainterPS::openFileForWriting(
370 const char* aString
371)
372//////////////////////////////////////////////////////////////////////////////
373//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
374{
375  if(fFile!=NULL) closeStream ();
376  if( (aString==NULL) || (*aString=='\0') ) {
377    if( (fFileName==NULL) || (*fFileName=='\0') ) { // Take default name :
378      fFile = ::fopen(METAFILE_DEFAULT,"wb");
379      STRDEL(fFileName);
380      fFileName = STRDUP(METAFILE_DEFAULT);
381    } else {
382      fFile = ::fopen(fFileName,"wb");
383    }
384  } else {
385    fFile = ::fopen(aString,"wb");
386    STRDEL(fFileName);
387    fFileName = STRDUP(aString);
388  }
389  if(fFile==NULL) return;
390
391  fBufferCount = 0;
392  fBufferPointer[METAFILE_RECORD_LENGTH] = '\0';
393  fPageNumber = 0;
394  // Header :
395  printFLN   ("%%!PS-Adobe-2.0");
396  printFLN   ("%%%%Creator: HEPVis::SbPainterPS.");
397  printFLN("%%%%CreationDate: %s",GetDate());
398  printFLN("%%%%Title: %s",fFileName);
399  printFLN("%%%%Pages: (atend)");
400  printFLN("%%%%BoundingBox: 0 0 %d %d",
401           (int)fDeviceWidth,(int)fDeviceHeight);
402  printFLN("%%%%DocumentFonts: Courier-Bold");
403  printFLN("%%%%DocumentPaperSizes: a4");
404  printFLN("%%%%EndComments");
405  // PostScript :
406  putSaveStateInStream      ();
407  // General :
408  putInStreamF("/n {newpath} def ");
409  putInStreamF("/cl {closepath} def ");
410  putInStreamF("/s {stroke} def ");
411  putInStreamF("/f {fill} def ");
412  // Move :
413  putInStreamF("/m  {moveto} def ");
414  putInStreamF("/rm {rmoveto} def ");
415  putInStreamF("/rl {rlineto} def ");
416  // Line :
417  putInStreamF("/lc {setlinecap} def ");
418  putInStreamF("/lw {setlinewidth} def ");
419  putInStreamF("/rgb {setrgbcolor} def ");
420  putInStreamF("/ss {[] 0 setdash} def ") ;            /* style solid       */
421  putInStreamF("/sd {[12 6] 0 setdash} def ");         /* style dashed      */
422  putInStreamF("/so {[6 12] 0 setdash} def ");         /* style dotted      */
423  putInStreamF("/sdo {[18 12 6 12] 0 setdash} def ");  /* style dash dotted */
424  // Mark :
425  fMarkerSize = 2.;
426  putInStreamF("/ms 2. def /msi .5 def ");        /* mark size */
427  putInStreamF("/cross {ms ms scale -1. -1. rm  ");
428  putInStreamF("2. 2. rl 0. -2. rm -2. 2. rl msi msi scale} def ");
429  putInStreamF("/plus  {ms ms scale -1. 0. rm 2. 0. rl ");
430  putInStreamF("-1. 1. rm 0. -2. rl msi msi scale} def ");
431  putInStreamF("/asterisk {ms ms scale -1. 0. rm 2. 0. rl -1. 1. rm ");
432  putInStreamF("0. -2. rl 0. 1. rm -0.707 -0.707 rm 1.414 1.414 rl ");
433  putInStreamF("0. -1.414 rm -1.414 1.414 rl msi msi scale} def ");
434  putInStreamF("/triangle {ms ms scale 0. 1. rm -0.6 -1.5 rl ");
435  putInStreamF("1.2 0. rl -0.6 1.5 rl msi msi scale} def ");
436  // Text :
437  putInStreamF("/sh {show} def ");
438  putInStreamF("/df {/Courier-Bold findfont} def ");
439  putInStreamF("/mf {makefont setfont} def ");
440  printFLN("%%%%EndProlog");
441}
442//////////////////////////////////////////////////////////////////////////////
443void SbPainterPS::closeStream(
444)
445//////////////////////////////////////////////////////////////////////////////
446//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
447{
448  if(fFile==NULL) return;
449  putRestoreStateInStream    ();
450  printFLN("%%%%Trailer");
451  printFLN("%%%%Pages: %d",fPageNumber);
452  printFLN("%%%%EOF");
453  if(fFile!=NULL) ::fclose(fFile);
454  fFile = NULL;
455  STRDEL(fFileName);
456  fFileName = NULL;
457}
458//////////////////////////////////////////////////////////////////////////////
459void SbPainterPS::putInStreamF(
460 const char* aFormat
461,...
462)
463//////////////////////////////////////////////////////////////////////////////
464//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
465{
466  if(fFile==NULL) return;
467  va_list  args;
468  va_start(args,aFormat);
469  printV(aFormat,args);
470  va_end(args);
471  int length = ::strlen(fBufferString);
472  if(length>METAFILE_RECORD_LENGTH) {
473    ::printf("SoPostScript::putInStreamF overflow\n");
474    return;
475  }
476  int nlength = fBufferCount + length;
477  if(nlength>METAFILE_RECORD_LENGTH) {
478      fBufferPointer[fBufferCount] = '\0';
479      if(::fprintf(fFile,"%s\n",(char*)fBufferPointer)<0) {
480        ::printf("SoPostScript::putInStreamF fprintf error\n");
481      }
482      fBufferCount = 0;
483      nlength = length;
484    }
485  unsigned char* pointer = fBufferPointer + fBufferCount;
486  ::strcpy((char*)pointer,fBufferString);
487  fBufferCount = nlength;
488}
489//////////////////////////////////////////////////////////////////////////////
490void SbPainterPS::printFLN(
491 const char* aFormat
492,...
493)
494//////////////////////////////////////////////////////////////////////////////
495//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
496{
497  if(fFile==NULL) return;
498  va_list args;
499  va_start(args,aFormat);
500  printV(aFormat,args);
501  va_end(args);
502/* put buffer in file */
503  if(fBufferCount>0) {
504    fBufferPointer[fBufferCount] = '\0';
505    if(::fprintf (fFile,"%s\n",(char*)fBufferPointer)<0) {
506      ::printf("SbPainterPS::printFLN fprintf error\n");
507    }
508    fBufferCount = 0;
509  }
510/* put comment in file */
511  if(::fprintf (fFile,"%s\n",fBufferString)<0) {
512    ::printf("SbPainterPS::printFLN fprintf error\n");
513  }
514}
515//////////////////////////////////////////////////////////////////////////////
516void SbPainterPS::printV(
517 const char* This
518,va_list aArgs 
519)
520//////////////////////////////////////////////////////////////////////////////
521//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
522{
523#define MAX_STR    2048
524  if(fBufferString==NULL) {
525    fBufferString = (char*)::malloc(MAX_STR * sizeof(char));
526    if(fBufferString==NULL) return;
527  }
528  fBufferString[MAX_STR-1]  = '\0';
529  ::vsprintf(fBufferString,This,aArgs);
530  if(fBufferString[MAX_STR-1]!='\0') {
531    ::printf("SbPainterPS::printV overflow\n");
532    fBufferString[0] = '\0';
533  }
534}
535//////////////////////////////////////////////////////////////////////////////
536//////////////////////////////////////////////////////////////////////////////
537//////////////////////////////////////////////////////////////////////////////
538//////////////////////////////////////////////////////////////////////////////
539void SbPainterPS::putPageScaleInStream(
540 float aWidth
541,float aHeight
542)
543//////////////////////////////////////////////////////////////////////////////
544//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
545{
546  if(aWidth <=0.) aWidth  = 100.;
547  if(aHeight<=0.) aHeight = 100.;
548
549  putScaleInStream (1./METAFILE_SCALE,1./METAFILE_SCALE);
550  putTranslationInStream ((float)(fDeviceWidth/20.),
551                          (float)(fDeviceHeight/30.));
552
553  float scale;
554  if(fDeviceWidth<=fDeviceHeight)
555    scale = (aHeight<=aWidth ?
556             fDeviceWidth /aWidth  : fDeviceWidth /aHeight );
557  else
558    scale = (aHeight<=aWidth ?
559             fDeviceHeight /aWidth : fDeviceHeight /aHeight );
560
561  float xtra,ytra;
562  if(fParams.portrait==1) {
563    xtra = (fDeviceWidth  - scale * aWidth)/2;
564    ytra = (fDeviceHeight - scale * aHeight)/2;
565  } else {
566    putTranslationInStream(fDeviceWidth,0.);
567    putRotateInStream(90);
568    xtra = (fDeviceHeight  - scale * aWidth)/2;
569    ytra = (fDeviceWidth   - scale * aHeight)/2;
570  }
571  putTranslationInStream (xtra,ytra);
572
573  putScaleInStream (scale,scale);
574}
575//////////////////////////////////////////////////////////////////////////////
576//////////////////////////////////////////////////////////////////////////////
577//////////////////////////////////////////////////////////////////////////////
578//////////////////////////////////////////////////////////////////////////////
579void SbPainterPS::putSaveStateInStream(
580)
581//////////////////////////////////////////////////////////////////////////////
582//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
583{
584  putInStreamF("gsave ");
585  fGSave++;
586}
587//////////////////////////////////////////////////////////////////////////////
588void SbPainterPS::putRestoreStateInStream(
589)
590//////////////////////////////////////////////////////////////////////////////
591//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
592{
593  putInStreamF("grestore ");
594  fGSave--;
595}
596//////////////////////////////////////////////////////////////////////////////
597void SbPainterPS::putTranslationInStream(
598 float aX
599,float aY
600)
601//////////////////////////////////////////////////////////////////////////////
602//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
603{
604  putInStreamF("%.2f %.2f translate ",aX,aY);
605}
606//////////////////////////////////////////////////////////////////////////////
607void SbPainterPS::putScaleInStream(
608 float aX
609,float aY
610)
611//////////////////////////////////////////////////////////////////////////////
612//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
613{
614  putInStreamF("%.2f %.2f scale ",aX,aY);
615}
616//////////////////////////////////////////////////////////////////////////////
617void SbPainterPS::putBeginPageInStream(
618)
619//////////////////////////////////////////////////////////////////////////////
620//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
621{
622  fPageNumber++;
623  printFLN("%%%%Page: %d %d",fPageNumber,fPageNumber);
624  putSaveStateInStream();
625}
626//////////////////////////////////////////////////////////////////////////////
627void SbPainterPS::putEndPageInStream (
628)
629//////////////////////////////////////////////////////////////////////////////
630//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
631{
632  putInStreamF("showpage ");
633  putRestoreStateInStream();
634}
635//////////////////////////////////////////////////////////////////////////////
636void SbPainterPS::putRGB_InStream (
637 float aR
638,float aG
639,float aB
640)
641//////////////////////////////////////////////////////////////////////////////
642//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
643{
644  if(fParams.shade==Color)       
645    putInStreamF("%.2f %.2f %.2f rgb ",aR,aG,aB);
646  else if(fParams.shade==Grey)
647    putInStreamF("%.2f setgray ",convertRGB_ToGrey(aR,aG,aB));
648  else if(fParams.shade==BlackWhite) 
649    putInStreamF("0. setgray ",convertRGB_ToGrey(aR,aG,aB));
650}
651//////////////////////////////////////////////////////////////////////////////
652void SbPainterPS::putLineWidthInStream(
653 int aWidth
654)
655//////////////////////////////////////////////////////////////////////////////
656//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
657{
658  if(fParams.lineWidth<0.) {
659    if(aWidth==1) {
660      putInStreamF("%.1f lw ",0.5); // For a better rendering.
661    } else {
662      putInStreamF("%.1f lw ",(float)(aWidth));
663    }
664  } else {
665    putInStreamF("%.1f lw ",fParams.lineWidth);
666  }
667}
668//////////////////////////////////////////////////////////////////////////////
669void SbPainterPS::putMarkerSizeInStream (
670 float aSize
671)
672//////////////////////////////////////////////////////////////////////////////
673//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
674{
675  if(aSize==fMarkerSize) return;
676  fMarkerSize = aSize;
677  putInStreamF("/ms %g def /msi %g def ",aSize,1./aSize);
678}
679/*
680//////////////////////////////////////////////////////////////////////////////
681void SbPainterPS::putMarkerStyleInStream (
682 SbMarkerStyle aStyle
683)
684//////////////////////////////////////////////////////////////////////////////
685//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
686{
687  switch (aStyle) {
688  case SbMarkerPlus:
689    putInStreamF("plus ");
690    break;
691  case SbMarkerAsterisk:
692  case SbMarkerStar:
693    putInStreamF("asterisk ");
694    break;
695  case SbMarkerCross:
696    putInStreamF("cross ");
697    break;
698  case SbMarkerTriangleUpLine:
699    putInStreamF("triangle ");
700    break;
701  default:
702    putLineToInStream(0.,0.);
703    break;
704  }
705}
706*/
707//////////////////////////////////////////////////////////////////////////////
708void SbPainterPS::putBackgroundInStream (
709 float aR
710,float aG
711,float aB
712,float aWidth
713,float aHeight
714)
715//////////////////////////////////////////////////////////////////////////////
716//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
717{
718  putNewPathInStream();
719  putMoveInStream(0.,0.);
720  putLineToInStream(aWidth,0.);
721  putLineToInStream(0.,aHeight);
722  putLineToInStream(-aWidth,0.);
723  putLineToInStream(0.,-aHeight);
724  putClosePathInStream();
725  if(fParams.doBack==1) {
726    // Back :
727    putSaveStateInStream();
728    putRGB_InStream(aR,aG,aB);
729    putFillInStream();       
730    putRestoreStateInStream();
731  }
732  // Clip :
733  putInStreamF("clip ");
734}
735//////////////////////////////////////////////////////////////////////////////
736void SbPainterPS::putFrameInStream (
737 float aR
738,float aG
739,float aB
740,float aWidth
741,float aHeight
742)
743//////////////////////////////////////////////////////////////////////////////
744//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
745{
746  putNewPathInStream();
747  putMoveInStream(0.,0.);
748  putLineToInStream(aWidth,0.);
749  putLineToInStream(0.,aHeight);
750  putLineToInStream(-aWidth,0.);
751  putLineToInStream(0.,-aHeight);
752  putClosePathInStream();
753  putRGB_InStream(aR,aG,aB);
754  putLineWidthInStream(1);
755  putCapInStream(1);
756  putInStreamF("ss ");
757  putStrokeInStream();
758}
759//////////////////////////////////////////////////////////////////////////////
760float SbPainterPS::convertRGB_ToGrey (
761 float aRed
762,float aGreen
763,float aBlue
764)
765//////////////////////////////////////////////////////////////////////////////
766//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
767{
768  return (0.3F * aRed + 0.59F * aGreen + 0.11F * aBlue);
769}
770//////////////////////////////////////////////////////////////////////////////
771void SbPainterPS::putRotateInStream(
772 float aX                 
773)
774//////////////////////////////////////////////////////////////////////////////
775//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
776{
777  putInStreamF("%.2f  rotate ",aX);
778}
779//////////////////////////////////////////////////////////////////////////////
780void SbPainterPS::putNewPathInStream(
781)
782//////////////////////////////////////////////////////////////////////////////
783//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
784{
785  putInStreamF("n ");
786}
787//////////////////////////////////////////////////////////////////////////////
788void SbPainterPS::putStrokeInStream(
789)
790//////////////////////////////////////////////////////////////////////////////
791//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
792{
793  putInStreamF("s ");
794}
795//////////////////////////////////////////////////////////////////////////////
796void SbPainterPS::putFillInStream(
797)
798//////////////////////////////////////////////////////////////////////////////
799//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
800{
801  putInStreamF("f ");
802}
803//////////////////////////////////////////////////////////////////////////////
804void SbPainterPS::putClosePathInStream(
805)
806//////////////////////////////////////////////////////////////////////////////
807//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
808{
809  putInStreamF("cl ");
810}
811//////////////////////////////////////////////////////////////////////////////
812void SbPainterPS::putCapInStream(
813 int aX
814)
815//////////////////////////////////////////////////////////////////////////////
816//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
817{
818  putInStreamF("%1d lc ",aX);
819}
820//////////////////////////////////////////////////////////////////////////////
821void SbPainterPS::putLineToInStream(
822 float aX
823,float aY
824)
825//////////////////////////////////////////////////////////////////////////////
826//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
827{
828  putInStreamF ("%.2f %.2f rl ",aX,aY);
829}
830//////////////////////////////////////////////////////////////////////////////
831void SbPainterPS::putMoveInStream(
832 float aX
833,float aY
834)
835//////////////////////////////////////////////////////////////////////////////
836//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
837{
838  putInStreamF ("%.2f %.2f m ",aX,aY);
839}
840//////////////////////////////////////////////////////////////////////////////
841void SbPainterPS::putCircleInStream(
842 float aX                           
843,float aY                           
844,float aR                           
845)
846//////////////////////////////////////////////////////////////////////////////
847//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
848{
849  putInStreamF("%.2f %.2f %.2f 0 360 arc s ",aX,aY,aR);
850}
851/*
852//////////////////////////////////////////////////////////////////////////////
853void SbPainterPS::putLineStyleInStream(
854 SbLineStyle aStyle
855)
856//////////////////////////////////////////////////////////////////////////////
857//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
858{
859  switch(aStyle) {
860  case SbLineSolid:putInStreamF("ss ") ;break;
861  case SbLineDashed:putInStreamF("sd ") ;break;
862  case SbLineDotted:putInStreamF("so ") ;break;
863  case SbLineDashDotted:putInStreamF("sdo ");break;
864  }
865}
866*/
867//////////////////////////////////////////////////////////////////////////////
868//////////////////////////////////////////////////////////////////////////////
869/////// Image ////////////////////////////////////////////////////////////////
870//////////////////////////////////////////////////////////////////////////////
871//////////////////////////////////////////////////////////////////////////////
872void SbPainterPS::putImageInStream (
873 unsigned int aWidth
874,unsigned int aHeight
875,GetRGB_Function aProc
876)
877//////////////////////////////////////////////////////////////////////////////
878//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
879{
880  if((aWidth<=0)||(aHeight<=0)) return;
881  if(!aProc) return;
882
883  putSaveStateInStream      ();
884  putInStreamF     ("%d %d scale ", aWidth, aHeight );
885  int status = 1;
886  int      nbhex;
887  unsigned int row,col,col_max;
888  double   dr,dg,db;
889  typedef unsigned char Uchar;
890  Uchar    red,green,blue,b;
891  if(fParams.shade!=0) { /*grey*/
892    putInStreamF   ("/picstr %d string def ",aWidth);
893    putInStreamF   ("%d %d %d ",aWidth,aHeight,8);
894    putInStreamF   ("[ %d 0 0 -%d 0 %d ] ",aWidth,aHeight,aHeight);
895    putInStreamF   ("{ currentfile picstr readhexstring pop } " );
896    printFLN ("image " );
897    for ( row = 0; row < aHeight; row++ ){
898      for ( col = 0; col < aWidth; col++){
899        double    fgrey;
900        Uchar     grey;
901        status    = aProc(col,row,dr,dg,db)==0 ? 0 : status;
902        fgrey     = ConvertRGB_ToGrey(dr,dg,db);
903        grey      = (Uchar) ( 255. * fgrey);
904        writeByte (grey);
905      }
906    }
907    nbhex     = aWidth * aHeight * 2;
908    printFLN ("%%%% nbhex digit          :%d ",nbhex);
909    printFLN ("%%%% nbhex/record_length  :%d ",nbhex/METAFILE_RECORD_LENGTH);
910    printFLN ("%%%% nbhex%%record_length :%d ",nbhex%METAFILE_RECORD_LENGTH);
911  }else if(fParams.nbit==2){
912    int       nbyte2;
913    nbyte2    = (aWidth   *  3)/4;
914    nbyte2   /=3;
915    nbyte2   *=3;
916    col_max   = (nbyte2  *  4)/3;
917    /* 2 bit for r and g and b   */
918    /* rgbs following each other */
919    putInStreamF   ("/rgbstr %d string def ",nbyte2);
920    putInStreamF   ("%d %d %d ",col_max,aHeight,2);
921    putInStreamF   ("[ %d 0 0 -%d 0 %d ] ",col_max,aHeight,aHeight);
922    putInStreamF   ("{ currentfile rgbstr readhexstring pop } " );
923    putInStreamF   ("false 3 " );
924    printFLN ("colorimage " );
925    for ( row = 0; row < aHeight; row++ ){
926      for ( col = 0; col < col_max; col+=4){
927        status  = aProc(col,row,dr,dg,db)==0 ? 0 : status;
928        red     = (Uchar) ( 3. * dr);
929        green   = (Uchar) ( 3. * dg);
930        blue    = (Uchar) ( 3. * db);
931        b       = red;
932        b       = (b<<2)+green;
933        b       = (b<<2)+blue;
934        status  = aProc(col+1,row,dr,dg,db)==0 ? 0 : status;
935        red     = (Uchar) ( 3. * dr);
936        green   = (Uchar) ( 3. * dg);
937        blue    = (Uchar) ( 3. * db);
938        b     = (b<<2)+red;
939        writeByte (b);
940       
941        b       = green;
942        b       = (b<<2)+blue;
943        status  = aProc(col+2,row,dr,dg,db)==0 ? 0 : status;
944        red     = (Uchar) ( 3. * dr);
945        green   = (Uchar) ( 3. * dg);
946        blue    = (Uchar) ( 3. * db);
947        b     = (b<<2)+red;
948        b     = (b<<2)+green;
949        writeByte (b);
950       
951        b       = blue;
952        status  = aProc(col+3,row,dr,dg,db)==0 ? 0 : status;
953        red     = (Uchar) ( 3. * dr);
954        green   = (Uchar) ( 3. * dg);
955        blue    = (Uchar) ( 3. * db);
956        b     = (b<<2)+red;
957        b     = (b<<2)+green;
958        b     = (b<<2)+blue;
959        writeByte (b);
960      }
961    }
962  }else if(fParams.nbit==4){
963    int       nbyte4;
964    nbyte4    = (aWidth  * 3)/2;
965    nbyte4   /=3;
966    nbyte4   *=3;
967    col_max   = (nbyte4 * 2)/3;
968    /* 4 bit for r and g and b   */
969    /* rgbs following each other */
970    putInStreamF   ("/rgbstr %d string def ",nbyte4);
971    putInStreamF   ("%d %d %d ",col_max,aHeight,4);
972    putInStreamF   ("[ %d 0 0 -%d 0 %d ] ",col_max,aHeight,aHeight);
973    putInStreamF   ("{ currentfile rgbstr readhexstring pop } " );
974    putInStreamF   ("false 3 " );
975    printFLN ("colorimage " );
976    for ( row = 0; row < aHeight; row++ ){
977      for ( col = 0; col < col_max; col+=2){
978        status  = aProc(col,row,dr,dg,db)==0 ? 0 : status;
979        red     = (Uchar) ( 15. * dr);
980        green   = (Uchar) ( 15. * dg);
981        putInStreamF ("%x%x",red,green);
982        blue    = (Uchar) ( 15. * db);
983       
984        status  = aProc(col+1,row,dr,dg,db)==0 ? 0 : status;
985        red     = (Uchar) ( 15. * dr);
986        putInStreamF ("%x%x",blue,red);
987        green   = (Uchar) ( 15. * dg);
988        blue    = (Uchar) ( 15. * db);
989        putInStreamF ("%x%x",green,blue);
990      }
991    }
992  }else{
993    int       nbyte8;
994    nbyte8    = aWidth   * 3;
995    /* 8 bit for r and g and b   */
996    putInStreamF   ("/rgbstr %d string def ",nbyte8);
997    putInStreamF   ("%d %d %d ",aWidth,aHeight,8);
998    putInStreamF   ("[ %d 0 0 -%d 0 %d ] ",aWidth,aHeight,aHeight);
999    putInStreamF   ("{ currentfile rgbstr readhexstring pop } " );
1000    putInStreamF   ("false 3 " );
1001    printFLN   ("colorimage " );
1002    for ( row = 0; row < aHeight; row++ ){
1003      for ( col = 0; col < aWidth; col++){
1004        status     = aProc(col,row,dr,dg,db)==0 ? 0 : status;
1005        red        = (Uchar) ( 255. * dr);
1006        writeByte (red);
1007        green      = (Uchar) ( 255. * dg);
1008        writeByte (green);
1009        blue       = (Uchar) ( 255. * db);
1010        writeByte (blue);
1011      }
1012    }
1013  }
1014  if(status==0)
1015    ::printf("SbPainterPS::putImageInStream: problem to retreive some pixel rgb.\n");
1016  putRestoreStateInStream();
1017}
1018//////////////////////////////////////////////////////////////////////////////
1019void SbPainterPS::writeByte (
1020 unsigned char a_byte
1021)
1022//////////////////////////////////////////////////////////////////////////////
1023//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
1024{
1025  unsigned char h = a_byte / 16;
1026  unsigned char l = a_byte % 16;
1027  putInStreamF ("%x%x",h,l);
1028}
1029//////////////////////////////////////////////////////////////////////////////
1030//////////////////////////////////////////////////////////////////////////////
1031//////////////////////////////////////////////////////////////////////////////
1032char* GetDate (
1033)
1034//////////////////////////////////////////////////////////////////////////////
1035// Return local date.
1036//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
1037{
1038  time_t d;
1039  time(&d);
1040  char* string = ctime(&d);
1041  string[24] = '\0';
1042  return string;
1043}
1044//////////////////////////////////////////////////////////////////////////////
1045double ConvertRGB_ToGrey(
1046 double a_red
1047,double a_green
1048,double a_blue
1049)
1050//////////////////////////////////////////////////////////////////////////////
1051//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
1052{
1053  return (0.30 * a_red + 0.59 * a_green + 0.11 * a_blue);
1054}
1055
1056#endif
Note: See TracBrowser for help on using the repository browser.