home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / mgfyinit.c < prev    next >
C/C++ Source or Header  |  1990-04-20  |  8KB  |  220 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    mgfyinit.c (Magnify Initialize)
  6.  * Purpose:    Initialize parameters for drawing the zoomed piece of image
  7.  *        in the magnifier box
  8.  * Subroutine:    init_magnifier()            returns: void
  9.  * Subroutine:    set_magnifier()                returns: void
  10.  * Subroutine:    set_magnifier_dither()
  11.  * Xlib calls:    XTextWidth()
  12.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  13.  *        You may do anything you like with this file except remove
  14.  *        this copyright.  The Smithsonian Astrophysical Observatory
  15.  *        makes no representations about the suitability of this
  16.  *        software for any purpose.  It is provided "as is" without
  17.  *        express or implied warranty.
  18.  * Modified:    {0} Michael VanHilst    initial version         6 June 1989
  19.  *        {n} <who> -- <does what> -- <when>
  20.  */
  21.  
  22. #include <stdio.h>        /* stderr, NULL, etc. */
  23. #include <X11/Xlib.h>        /* X window stuff */
  24. #include <X11/Xutil.h>        /* X window manager stuff */
  25. #include "hfiles/define.h"    /* define MAX, etc. */
  26. #include "hfiles/struct.h"    /* declare structure types */
  27. #include "hfiles/extern.h"    /* extern main SAOimage parameter structures */
  28. #include "hfiles/scale.h"    /* get SCALEOFF */
  29. #include "hfiles/magnify.h"    /* magnifier quick access structure */
  30.  
  31. extern struct magRec magset;
  32.  
  33. /*
  34.  * Subroutine:    init_magnifier
  35.  * Purpose:    Init that which gets set only once, and then the rest as well
  36.  * Xlib calls:    XTextWidth()
  37.  */
  38. void init_magnifier ( scope_track, text_track )
  39.      int scope_track, text_track;
  40. {
  41.   XFontStruct *get_fontstruct();
  42.   void set_magnifier();
  43.  
  44.   /* set initial tracking status */
  45.   magset.view = scope_track;
  46.   magset.label = text_track;
  47.   /* default magnification factor */
  48.   magset.magnify = 4;
  49.   /* set window known info */
  50.   magset.win.display = magnibox.display;
  51.   magset.win.ID = magnibox.ID;
  52.   magset.win.x = magnibox.xzero;
  53.   magset.win.y = magnibox.yzero;
  54.   magset.image = &magnibox.image;
  55.   magset.gcset_disp = &color.gcset.disp;
  56.   magset.gcset_aim = &color.gcset.menu;
  57.   /* set pointer to scalemap for signed indexing */
  58.   magset.lookup = buffer.scalemap + SCALEOFF;
  59.   /* set initial coords to default to display center */
  60.   magset.buf.X = -1.0;
  61.   /* note that buffer is not yet declared */
  62.   magset.data_size = 0;
  63.   /* set up initial test label params */
  64.   magset.text.fontstruct = get_fontstruct(1);
  65.   magset.text.font = magset.text.fontstruct->fid;
  66.   magset.text.space = XTextWidth(magset.text.fontstruct, " ", 1);
  67.   /* check for proportional fonts */
  68.   if( XTextWidth(magset.text.fontstruct, "0", 1) != magset.text.space ) {
  69.     char num[2];
  70.     int i, max = 0;
  71.     for( i=0; i<10; i++ ) {
  72.       num[0] = '0' + i;
  73.       if( (magset.text.numsz[i] =
  74.        XTextWidth(magset.text.fontstruct, num, 1)) > max )
  75.     max = magset.text.numsz[i];
  76.     }
  77.     magset.text.dash = XTextWidth(magset.text.fontstruct, "-", 1);
  78.     magset.text.dot = XTextWidth(magset.text.fontstruct, ".", 1);
  79.     magset.text.e = XTextWidth(magset.text.fontstruct, "e", 1);
  80.     magset.text.width = 6 * max;
  81.     /* this value may need to be tweeked */
  82.     magset.text.x_x = 3;
  83.     magset.text.y_x = magset.text.x_x + (7 * max);
  84.     magset.text.val_x = magset.text.y_x + (7 * max);
  85.     magset.text.proportional = 1;
  86.   } else {
  87.     /* uniform width font */
  88.     magset.text.width = 23;
  89.     magset.text.x_xoff = 5;
  90.     magset.text.x_x = 5;
  91.     magset.text.proportional = 0;
  92.   }
  93.   magset.text.yoff =
  94.     (2 * magset.text.fontstruct->descent) + magset.text.fontstruct->ascent;
  95.   magset.text.foreground = color.hard.std_white;
  96.   magset.text.background = color.hard.std_black;
  97.   magset.matrix = color.halftone.matrix;
  98.   magset.matrix_end = magset.matrix + 256;
  99.   magset.inverse = color.halftone.inverse;
  100.   set_magnifier();
  101. }
  102.  
  103. /*
  104.  * Subroutine:    set_magnifier()
  105.  * Purpose:    Set up initial parameters for magnifier box
  106.  * Use:        Call whenever the magnifier box is resized or the image buffer
  107.  *        is altered (reloaded or altered)
  108.  */
  109. void set_magnifier ( )
  110. {
  111.   int buf_sz;
  112.   float hot_x, hot_y;
  113.   char *calloc_errchk();
  114.   void init_magnifier_XYmark(), init_magnifier_Zmark();
  115.  
  116.   /* set source buffer */
  117.   magset.buf.shortbuf = buffer.shortbuf;
  118.   magset.buf.width = coord.buf.width;
  119.   /* set current window drawing area parameters */
  120.   magset.win.width = magnibox.xwidth;
  121.   magset.win.height = magnibox.yheight;
  122.   if( color.ncolors <= 1 ) {
  123.     magset.halftone = 1;
  124.     magset.bytes_per_line = (magset.win.width + 7) / 8;
  125.     magset.image->format = XYBitmap;
  126.     magset.image->depth = 1;
  127.     magset.image->bits_per_pixel = 1;
  128.   } else {
  129.     magset.halftone = 0;
  130.     magset.bytes_per_line = magset.win.width;
  131.     magset.image->format = ZPixmap;
  132.     magset.image->depth = color.screen_depth;
  133.     magset.image->bits_per_pixel = 8;
  134.   }
  135.   buf_sz = magset.bytes_per_line * (magset.win.height + 1);
  136.   if( buf_sz > magset.data_size ) {
  137.     if( magset.data != NULL )
  138.       free((char *)magset.data);
  139.     /* recoverable alloc (we could settle for less) */
  140.     if( (magset.data = calloc_errchk(buf_sz, sizeof(char), (char *)NULL))
  141.        == 0 ) {
  142.       magset.win.width = 128;
  143.       magset.win.height = 128;
  144.       if( magset.halftone )
  145.     magset.bytes_per_line = (magset.win.width + 7) / 8;
  146.       else
  147.     magset.bytes_per_line = magset.win.width;
  148.       buf_sz = magset.bytes_per_line * (magset.win.height + 1);
  149.       /* this is our minimum request */
  150.       magset.data = calloc_errchk(buf_sz, sizeof(char), "magnifier buffer");
  151.     }
  152.     magset.data_size = buf_sz;
  153.   }
  154.   magset.bitmap_size = magset.bytes_per_line * magset.win.height;
  155.   magset.image->data = magset.data;
  156.   magset.image->bytes_per_line = magset.bytes_per_line;
  157.   magset.image->width = magset.win.width;
  158.   magset.image->height = magset.win.height;
  159.   /* determine where to place aim sighting mark */
  160.   magset.data_x_hot = (magset.win.width - 1) / 2;
  161.   magset.data_y_hot = (magset.win.height - 1) / 2;
  162.   /* disp to magnibox zoom must not be less than 1 (needless extra work) */
  163.   if( magset.magnify < 1 )
  164.     magset.magnify = 1;
  165.   /* calculate buffer to magnifier zoom */
  166.   if( coord.disptobuf.inx_outx != 0.0 ) {
  167.     magset.zoom_rep =
  168.       MAX(1, (int)(0.5 + ((float)magset.magnify / coord.disptobuf.inx_outx)));
  169.   } else if( coord.disptobuf.iny_outx != 0.0 ) {
  170.     magset.zoom_rep =
  171.       MAX(1, (int)(0.5 + ((float)magset.magnify / coord.disptobuf.iny_outx)));
  172.   } else
  173.     magset.zoom_rep = magset.magnify;
  174.   /* define the margin regions (that clip against an edge) */
  175.   hot_x = 0.5 + (float)((magset.win.width - 1) / 2);
  176.   hot_y = 0.5 + (float)((magset.win.height - 1) / 2);
  177.   magset.buf.Xcen = hot_x / (float)magset.zoom_rep;
  178.   magset.buf.X2bdr = (float)coord.buf.width - magset.buf.Xcen;
  179.   magset.buf.X2lim = (float)coord.buf.width + magset.buf.Xcen;
  180.   magset.buf.X1lim =
  181.     (hot_x - (float)magset.win.width) / (float)magset.zoom_rep;
  182.   magset.buf.Ycen = hot_y / (float)magset.zoom_rep;
  183.   magset.buf.Y2bdr = (float)coord.buf.height - magset.buf.Ycen;
  184.   magset.buf.Y2lim = (float)coord.buf.height + magset.buf.Ycen;
  185.   magset.buf.Y1lim =
  186.     (hot_y - (float)magset.win.height) / (float)magset.zoom_rep;
  187.   init_magnifier_XYmark();
  188.   init_magnifier_Zmark();
  189.   /* set parameters for text label */
  190.   magset.text.y = btnbox.y - (magset.text.yoff + 2);
  191. #ifdef FOO /* this would center the label, else left justify */
  192.   if( magset.text.proportional == 0 )
  193.     magset.text.x_x = (panbox.x - (magset.text.width * magset.text.space)) / 2;
  194. #endif
  195. }
  196.  
  197. /*
  198.  * Subroutine:    set_magnifier_matrix
  199.  * Purpose:    Change the dither matrix used by scope
  200.  */
  201. void set_magnifier_matrix ( matrix, inverse )
  202.      short *matrix;
  203.      int inverse;
  204. {
  205.   void redraw_magnifier();
  206.   if( (magset.matrix != matrix) || (magset.inverse != inverse) ) {
  207.     magset.matrix = matrix;
  208.     magset.matrix_end = matrix + 256;
  209.     magset.inverse = inverse;
  210.     redraw_magnifier();
  211.   }
  212. }
  213.                                                   
  214.  
  215.                                                                
  216.                                                                
  217.                                                                
  218.                                                                
  219.                      
  220.