home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / video / security / title.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.7 KB  |  171 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include "title.h"
  18. #include <bstring.h>
  19. #include <malloc.h>
  20. #include "convertImageFormat.h"
  21. #ifdef DEBUG
  22. #define dprintf printf
  23. #else
  24. #define dprintf 0&& 
  25. #endif
  26.  
  27. extern "C" {
  28. // The following are external pswrap-generated routines
  29. // PSWforwardTransfer resets the color transfer   
  30.   void PSWforwardTransfer(DPSContext);
  31. // PSWreverseTransfer sets the color transfer to be negative
  32. // this is only used for erase so it erases to black and not white
  33.   void PSWreverseTransfer(DPSContext);
  34. // PSWDisplayText adds the string at the pointsize indicated
  35. // for a NTSC size window, ptsize = 32 will hold ~35 chars across
  36.   void PSWDisplayText(char *, int);
  37. }
  38.  
  39. void *
  40. flipImage(void *inVoid, int width, int height)
  41. {
  42.   unsigned char *inImage, *outImage;
  43.   inImage = (unsigned char *)inVoid;
  44.   outImage = new unsigned char [width * height * sizeof(long)];
  45.   for(int y = 0; y < height; y++){
  46.     bcopy(&inImage[width * (height - 1 - y) * sizeof(long)],
  47.       &outImage[width * y * sizeof(long)], width * sizeof(long));
  48.   }
  49.   return (void *)outImage;
  50. }
  51.  
  52. Title::Title(Widget d, int w, int h)
  53. {
  54.   XGCValues values;
  55.   _hasDPS = FALSE;
  56.   _dpy = XtDisplay(d);
  57.   _width = w;
  58.   _height = h;
  59.   _depth = DefaultDepth(_dpy, 0);
  60.   _pixmap = NULL;
  61.   _gc = NULL;
  62.   _img = NULL;
  63.   _ctxt = NULL;
  64.   if(!XDPSExtensionPresent(_dpy)){
  65. //    fprintf(stderr,"No DPS extension to the X server\n");
  66.     return;
  67.   }
  68.   _pixmap = XCreatePixmap(_dpy, XtWindow(d),_width, _height, _depth);
  69.   _gc = XCreateGC(_dpy, _pixmap, NULL, &values); 
  70.   _img = XGetImage(_dpy,_pixmap,0,0,_width, _height,0xffffffff, ZPixmap);
  71.   _ctxt = XDPSCreateSimpleContext(_dpy, _pixmap, _gc, 0, 0,
  72.                  (DPSTextProc)DPSDefaultTextBackstop,
  73.                  (DPSErrorProc)DPSDefaultErrorProc, NULL );
  74.   if(!_ctxt){
  75.     fprintf(stderr,"Cannot creat context\n");
  76.     return;
  77.   }
  78.   DPSSetContext(_ctxt);
  79.   PSinitmatrix();
  80.   PSinitclip();
  81.   PSinitviewclip();
  82.   erase();
  83.   _hasDPS = TRUE;
  84. }
  85.  
  86. Title::~Title()
  87. {
  88.   if(_gc)XFreeGC(_dpy, _gc);
  89.   if(_pixmap)XFreePixmap(_dpy, _pixmap);
  90.   if(_img)XDestroyImage(_img);
  91.   if(_ctxt)DPSDestroySpace(DPSSpaceFromContext(_ctxt));
  92. }
  93.  
  94. void Title::setOffset(int x, int y)
  95. {
  96. // offset to point indicated
  97.   dprintf("Calling setOffset\n");
  98.   if(!_hasDPS) return;
  99.   PSsetXoffset(x,y);
  100. }
  101.  
  102. void Title::setColor(float r, float g, float b)
  103. {
  104. // set the current working RGB color  
  105.   dprintf("Calling setColor\n");
  106.   if(!_hasDPS) return;
  107.   PSsetrgbcolor(r,g,b);
  108. }
  109.  
  110. void Title::erase()
  111. {
  112. // erase the screen. To do this, reverse the color transfer function, erase, and reset
  113.   dprintf("Calling erase\n");
  114.   if(!_hasDPS) return;
  115.   PSWreverseTransfer(_ctxt);
  116.   PSerasepage();
  117.   PSWforwardTransfer(_ctxt);
  118. }
  119.  
  120. void Title::text(char *str, int ptsize)
  121. {
  122. // draw string with pointsize   
  123.   dprintf("Calling text\n");
  124.   if(!_hasDPS) return;
  125.   PSWDisplayText(str, ptsize);
  126. }
  127.  
  128. void *
  129. Title::getImage()
  130. {
  131. // return a ABGR char array of the pixmap  
  132.   XImage *img;
  133.   void *flippedImage;
  134.   unsigned char *charImage;
  135.   dprintf("Calling getImage\n");
  136.   if(!_hasDPS) return NULL;
  137.   DPSWaitContext(_ctxt);
  138.   img = XGetImage(_dpy,_pixmap,0,0,_width, _height,0xffffffff, ZPixmap);
  139.   if(img->depth == 8){
  140.     charImage = new unsigned char [_width * _height * sizeof(long)];
  141.     flippedImage = (void *)charImage;
  142.     csFlipVertically(TRUE);
  143.     RGB332ToRGBX(_width, _height, 0, (void *)img->data, 0, flippedImage);
  144.   }else{
  145.     flippedImage = flipImage((void *)img->data, _width, _height);
  146.   }
  147.   XDestroyImage(img);
  148.   return flippedImage;
  149. }
  150.  
  151. void Title::putImage(void *image)
  152. {
  153. // Really backwards and slow way to put an image into the pixmap.
  154.   void *flippedImage;
  155.   unsigned char *charImage;
  156.   dprintf("Calling putImage\n");
  157.   if(!_hasDPS) return;
  158.   if(_depth == 8){
  159.     charImage = new unsigned char [_width*_height];
  160.     flippedImage = (void *)charImage;
  161.     csFlipVertically(TRUE);
  162.     RGBXToRGB332(_width, _height, 0, image, 0, flippedImage);
  163.   }else{
  164.     flippedImage = flipImage(image, _width, _height);
  165.   }
  166.   free(_img->data);
  167.   _img->data = (char *)flippedImage;
  168.   XPutImage(_dpy, _pixmap, _gc, _img, 0,0,0,0,_width, _height);
  169. }
  170.  
  171.