home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / imgnew.c < prev    next >
C/C++ Source or Header  |  1991-06-21  |  10KB  |  280 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    imgnew.c (Image New)
  6.  * Purpose:    Middle level routines to do all for a new image or buffer
  7.  * Subroutine:    new_display()            returns: void
  8.  * Subroutine:    new_panimage()            returns: void
  9.  * Subroutine:    show_filename()            returns: void
  10.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  11.  *        You may do anything you like with this file except remove
  12.  *        this copyright.  The Smithsonian Astrophysical Observatory
  13.  *        makes no representations about the suitability of this
  14.  *        software for any purpose.  It is provided "as is" without
  15.  *        express or implied warranty.
  16.  * Modified:    {0} Michael VanHilst    initial version        1 July 1989
  17.  *        {1} Jay Travisano (STScI)    VMS changes        10 Nov 1989
  18.  *              {2} MVH BSDonly strings.h compatability         19 Feb 1990
  19.  *        {3} MVH support for file buffer coord sys    19 Feb 1990
  20.  *        {4} MVH altered inconsistancy in load_mainbuf     1 Jan 1991
  21.  *        {5} MVH changed call to copy_buf_replicate    19 Jun 1991
  22.  *        {n} <who> -- <does what> -- <when>
  23.  */
  24.  
  25. #include <stdio.h>        /* stderr, NULL, etc. */
  26.  
  27. #ifndef VMS
  28. #ifdef SYSV
  29. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  30. #else
  31. #include <strings.h>        /* strlen, strcat, strcpy, rindex */
  32. #define strchr index
  33. #define strrchr rindex
  34. #endif
  35. #else
  36. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  37. #endif
  38.  
  39. #include <X11/Xlib.h>        /* X window stuff */
  40. #include <X11/Xutil.h>        /* X window manager stuff */
  41. #include "hfiles/define.h"    /* define SZ_FNAME */
  42. #include "hfiles/constant.h"    /* constants and codes */
  43. #include "hfiles/struct.h"    /* declare structure types */
  44. #include "hfiles/extern.h"    /* extern main parameter structures */
  45. extern struct windowRec desktop;
  46.  
  47.  
  48. /*  Subroutine:    new_display
  49.  *  Purpose:    Create all image coordinate parameters from the beginning
  50.  *         and take care of all that might be affected
  51.  */
  52. void new_display ( dispnow, clear, load, scale )
  53.      int dispnow;    /* i: put-display-in-window-now flag */
  54.      int clear;        /* i: clear img buf before reading flag */
  55.      int load;        /* i: load-the-buffer flag */
  56.      int scale;        /* i: make-new-scalemap flag (must have load)  */
  57. {
  58.   void set_disptran(), new_pancursor(), new_scalemap();
  59.   void combine_transform(), set_edges(), set_dispoff(), set_magnifier();
  60.   void adjust_cursor_coords(), map_dispbox(), disp_dispbox();
  61.   static void  load_mainbuf();
  62.  
  63.   /*  Set disp to img and disp to file transforms  */
  64.   set_disptran(&coord);
  65.   /*  If the buffer needs new contents, fill it  */
  66.   if( load || coord.buferror ) {
  67.     /*  Load main buffer and set related coordinate parameters  */
  68.     load_mainbuf(clear);
  69.     /*  Set up or redraw the panbox cursor  */
  70.     new_pancursor(0);
  71.     /*  If none exists, set up buffer-to-display scaling map(s)  */
  72.     if( scale )
  73.       new_scalemap();
  74.   }
  75.   /*  Set transform to get from disp to buf coords  */
  76.   combine_transform(&coord.disptobuf, &coord.disptoimg, &coord.imgtobuf);
  77.   set_edges(&coord.disptobuf, &coord.buf, &coord.disp, &coord.bd);
  78.   /*  Redefine the edges in the display window if at edge of buffer  */
  79.   if( (coord.bd.block < 0) || (coord.bd.clip != 0) )
  80.     set_dispoff(&coord.disptobuf, &coord.disp, &coord.bd);
  81.   /*  Update cursor and magnifier info to correspond to current display  */
  82.   adjust_cursor_coords(&cursor, &coord);
  83.   set_magnifier();
  84.   /*  Fill display buffer with new display image  */
  85.   map_dispbox();
  86.   if( dispnow )
  87.     /*  Display the image  */
  88.     disp_dispbox();
  89. }
  90.  
  91.  
  92. /*  Subroutine:    load_mainbuf
  93.  *  Purpose:    Set buf related coords and load image data into main buffer
  94.  */
  95. static void load_mainbuf ( clear )
  96.      int clear;
  97. {
  98.   int zoom;
  99.   void set_buftran(), set_fbuftran(), load_image(), show_filename();
  100.  
  101.   /*  Select appropriate buffer contents in image field and file coords  */
  102.   set_buftran(&coord);
  103.   /*  Set file data buffer transform  */
  104.   set_fbuftran(&img, &coord.buf, &coord.fbuf, &coord.buftofbuf);
  105.   /*  Announce the loading  */
  106.   if( control.verbose )
  107.     (void)printf("Loading new image data into main buffer, please wait.\n");
  108.   /*  Clear the buffer  */
  109.   if( clear ) {
  110.     bzero((char *)buffer.shortbuf, buffer.shortbuf_sz * sizeof(short));
  111. /* need to zero file buffer too? 
  112.     bzero((char *)buffer.filebuf, buffer.filebuf_sz);
  113. */
  114.   }
  115.   /*  Load the image  */
  116.   load_image(&coord.fb, &coord.buf, &coord.buftofile, buffer.shortbuf,
  117.          buffer.filebuf, buffer.shortbuf_square, buffer.shortbuf_double);
  118.   coord.buferror = 0;
  119.   /*  Set the summed value compensation flag  */
  120.   if( (img.block_type == SOP_ZoomSum) && ((zoom = coord.fb.block) > 1) )
  121.     buffer.shortbuf_summing = zoom * zoom;
  122.   else
  123.     buffer.shortbuf_summing = 1;
  124.   /*  Free data buffer if we don't need to keep it  */
  125.   if( (!img.buffix) && ((char *)buffer.shortbuf != buffer.filebuf) ) {
  126.     free( buffer.filebuf );
  127.     buffer.filebuf = NULL;
  128.     buffer.filebuf_sz = 0;
  129.   }
  130.   show_filename();
  131. }
  132.  
  133.  
  134. /*  Subroutine:    new_panimage
  135.  *  Purpose:    Read image data into the panbox short buffer
  136.  */
  137. void new_panimage ()
  138. {
  139.   int block;
  140.   void load_image(), copy_buf_replicate(), copy_buf_max(), map_panbox();
  141.  
  142.   buffer.panbuf_summing = 1;
  143.   block = coord.fp.block;
  144.   /*  If main buffer does not cover entire image, read from the file  */
  145.   if( coord.bufcheck ) {
  146.     if( control.verbose )
  147.       (void)printf("Loading image data for wide view panning window.\n");
  148.     /* Load in image data directly (%% warning, rework is needed aka filebuf */
  149.     load_image(&coord.fp, &coord.pan, &coord.pantofile,
  150.            buffer.panbuf, buffer.filebuf, 0, 0);
  151.     if( img.block_type == SOP_ZoomSum )
  152.       buffer.panbuf_summing = block * block;
  153.   } else {
  154.     if( coord.pantoimg.inx_outx < 1.0 ) {
  155.       copy_buf_replicate(buffer.shortbuf, buffer.panbuf, coord.pantoimg.ixzoom,
  156.              coord.buf.width, coord.pan.Xwdth, coord.pan.Yhght);
  157.     } else {
  158.       if( (block = (int)coord.pantoimg.inx_outx) > 1 ) {
  159.     /*  Subzoom, blockaverage or zoommax may be used  */
  160.     switch( img.panbox_zoomtype ) {
  161.     case SOP_ZoomSamp:
  162.       copy_buf_subsample(buffer.shortbuf, buffer.panbuf, block,
  163.                  coord.buf.width, coord.pan.Xwdth,
  164.                  coord.pan.Yhght);
  165.       break;
  166.     case SOP_ZoomSum:
  167.       copy_buf_sum(buffer.shortbuf, buffer.panbuf, block, 0,
  168.                coord.buf.width, coord.pan.Xwdth, coord.pan.Yhght);
  169.       break;
  170.     case SOP_ZoomAv:
  171.       copy_buf_sum(buffer.shortbuf, buffer.panbuf, block, 1,
  172.                coord.buf.width, coord.pan.Xwdth, coord.pan.Yhght);
  173.       break;
  174.     case SOP_ZoomMax:
  175.     default:
  176.       copy_buf_max(buffer.shortbuf, buffer.panbuf, block,
  177.                coord.buf.width, coord.pan.Xwdth, coord.pan.Yhght);
  178.       break;
  179.     }
  180.       } else {
  181.     bcopy((char *)buffer.shortbuf, (char *)buffer.panbuf,
  182.           coord.pan.Xwdth * coord.pan.Yhght * sizeof(short));
  183.       }
  184.     }
  185.     /*  Same zoom summing as buffer  */
  186.     buffer.panbuf_summing = buffer.shortbuf_summing;
  187.   }
  188.   /*  Calculate the max value in the buffer for scaling purposes  */
  189.   if( img.block_type == SOP_ZoomSum ) {
  190.     register int buf_max;
  191.     register short *buf, *bufend;
  192.  
  193.     buf = buffer.panbuf;
  194.     bufend = buf + (panbox.xwidth * panbox.yheight * sizeof(short));
  195.     buf_max = *buf;
  196.     while( buf < bufend ) {
  197.       if( *buf > buf_max )
  198.     buf_max = *buf;
  199.       buf++;
  200.     }
  201.     buffer.panbuf_max = buf_max;
  202.   }
  203.   /*  Map the short data into the display buffer  */
  204.   map_panbox();
  205. }
  206.  
  207.  
  208. /*  Subroutine:    show_filename
  209.  *  Purpose:    Print the image file name in the desktop
  210.  */
  211. void show_filename ( )
  212. {
  213.   int len, dir_len, file_len;
  214.   int dir_x, dir_y, file_y;
  215.   char *dir_end;
  216.   char dir_string[SZ_FNAME];
  217.   char file_string[SZ_FNAME];
  218.   static XFontStruct *fontstruct = NULL;
  219.   GC gc, set_edit_gc();
  220.   XFontStruct *get_fontstruct();
  221.  
  222.   if( fontstruct == NULL )
  223.     fontstruct = get_fontstruct(1);
  224.   file_y = 2 * fontstruct->ascent;
  225.   dir_y = file_y + fontstruct->ascent + fontstruct->descent;
  226.   dir_x = 5;
  227.   XClearArea(desktop.display, desktop.ID, 0, 0, panbox.x, btnbox.y, False);
  228.   if( (img.filename != NULL) && ((len = strlen(img.filename)) > 0) ) {
  229.    if( (img.file_type == SOP_Imtool) || (img.file_type == SOP_PROS) ) {
  230.       dir_len  = strlen(strcpy(dir_string, "(IRAF)"));
  231.       file_len = strlen(strcpy(file_string, img.filename));
  232.    } else {
  233. #ifndef VMS
  234.     (void)strcpy(file_string, "file: ");
  235.     if( (dir_end = strrchr(img.filename, '/')) == NULL ) {
  236.       (void)strcpy(dir_string, "dir: .");
  237.       dir_len = 6;
  238.       (void)strcat(file_string, img.filename);
  239.       file_len = len + 6;
  240.     } else {
  241.       (void)strcpy(dir_string, "dir: ");
  242.       dir_len = dir_end - img.filename;
  243.       (void)strncat(dir_string, img.filename, dir_len);
  244.       dir_len += 5;
  245.       (void)strcat(file_string, dir_end+1);
  246.       file_len = 10 + len - dir_len;
  247.     }
  248. #else
  249.     /*  Parse VMS-style directory/filename  */
  250.  
  251.     (void)strcpy(file_string, "file: ");
  252.     (void)strcpy(dir_string,  "dir:  ");
  253.  
  254.     if( (dir_end = strrchr(img.filename, '/')) == NULL )
  255.       if( (dir_end = strrchr(img.filename, ']')) == NULL )
  256.         dir_end = strrchr(img.filename, ':');
  257.  
  258.     if( dir_end == NULL ) {
  259.       (void)strcat(dir_string, "[]");
  260.       (void)strcat(file_string, img.filename);
  261.     } else {
  262.       dir_len = dir_end - img.filename + 1;
  263.       (void)strncat(dir_string, img.filename, dir_len);
  264.       (void)strcat(file_string, dir_end+1);
  265.     }
  266.  
  267.     dir_len  = strlen(dir_string);
  268.     file_len = strlen(file_string);
  269. #endif
  270.    }
  271.  
  272.     gc = set_edit_gc(fontstruct->fid, color.gcset.menu.foreground,
  273.              color.gcset.menu.background);
  274.     XDrawImageString(desktop.display, desktop.ID, gc,
  275.              dir_x, file_y, file_string, file_len);
  276.     XDrawImageString(desktop.display, desktop.ID, gc,
  277.              dir_x, dir_y, dir_string, dir_len);
  278.   }
  279. }
  280.