home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / readarr.c < prev    next >
C/C++ Source or Header  |  1990-05-02  |  4KB  |  134 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    readarr.c (Read Array)
  6.  * Purpose:    Read in raster line array images
  7.  * Subroutine:    read_array()            returns: void
  8.  * Copyright:    1988 Smithsonian Astrophysical Observatory
  9.  *        You may do anything you like with this file except remove
  10.  *        this copyright.  The Smithsonian Astrophysical Observatory
  11.  *        makes no representations about the suitability of this
  12.  *        software for any purpose.  It is provided "as is" without
  13.  *        express or implied warranty.
  14.  * Modified:    {0} Michael VanHilst    initial version         31 October 1988
  15.  *        {n} <who> -- <does what> -- <when>
  16.  */
  17.  
  18. #include <stdio.h>        /* define stderr */
  19. #include <X11/Xlib.h>        /* needed for control.h */
  20. #include "hfiles/constant.h"    /* define data type codes */
  21. #include "hfiles/control.h"    /* define IOP codes */
  22. #include "hfiles/image.h"
  23.  
  24. /*
  25.  * Subroutine:    read_array
  26.  * Purpose:    Read array data from a file
  27.  * Note:    Assumes file was tested benignly, exits here if trouble
  28.  */
  29. void read_array ( fd, img, imgbuf, filebuf, width, height, X, Y, block,
  30.           verbose )
  31.      int fd;            /* if >=0 handle to open & ready image file */
  32.      struct imageRec *img;    /* record describing image file and its use */
  33.      short *imgbuf;        /* buffer to receive i*2 data */
  34.      char *filebuf;        /* buffer to receive raw data */
  35.      int width, height;        /* width and height of buffer */
  36.      int X, Y;            /* starting point in image file (no support) */
  37.      int block;            /* blocking factor (not yet supported */
  38.      int verbose;        /* whether to print explanatory messages */
  39. {
  40.   int vals;
  41.   static int read_data();
  42.   int open_disk(), lseek_disk();
  43.   void close_disk();
  44.   void say_goodbye(), scale_data_u1(), scale_data_i2(), scale_data_u2();
  45.   void scale_data_i4(), scale_data_r4(), scale_data_r8();
  46.  
  47.   if( (X!=0) || (Y!=0) || (block!=1) ) {
  48.     (void)fprintf(stderr, "Error: no subsection support yet\n");
  49.     return;
  50.   }
  51.   /* if not passed an open file, open it and move past the header */
  52.   if( fd == -1 ) {
  53.     /* open the image file */
  54.     if( (fd = open_disk(img->filename, IOP_Read, 0)) < 0 )
  55.       return;
  56.     /* skip header if necessary */
  57.     if( img->headersize > 0 ){
  58.       if( lseek_disk(fd, img->headersize, img->filename) < 0 ) {
  59.     close_disk(fd, img->filename);
  60.     return;
  61.       }
  62.     }
  63.   }
  64.   /* FOR NOW, READ ARRAY CANNOT HANDLE OVERSIZED ARRAYS */
  65.   if( (width != img->filecols) ||
  66.       (height != img->filerows) ) {
  67.     (void)fprintf(stderr, "Error: cannot handle %d x %d array\n",
  68.           img->filecols, img->filerows);
  69.     return;
  70.   }
  71.   /* read in the data */
  72.   vals = read_data(fd, img, filebuf);
  73.   /* read the image into the picture buffer */
  74.   switch( img->storage_type ) {
  75.   case ARR_U1:
  76.     scale_data_u1(img, imgbuf, (unsigned char *)filebuf, vals);
  77.     break;
  78.   case ARR_I2:
  79.     scale_data_i2(img, imgbuf, (short *)filebuf, vals);
  80.     break;
  81.   case ARR_U2:
  82.     scale_data_u2(img, imgbuf, (unsigned short *)filebuf, vals);
  83.     break;
  84.   case ARR_I4:
  85.     scale_data_i4(img, imgbuf, (long *)filebuf, vals, verbose);
  86.     break;
  87.   case ARR_R4:
  88.     scale_data_r4(img, imgbuf, (float *)filebuf, vals, verbose);
  89.     break;
  90.   case ARR_R8:
  91.     scale_data_r8(img, imgbuf, (double *)filebuf, vals, verbose);
  92.     break;
  93.   default:
  94.     (void)fprintf(stderr, "illegal array type: %d\n", img->storage_type);
  95.     exit(1);
  96.   }
  97.   /* adjust buffer scale and bias to include that of original file */
  98.   if( img->fscaled ) {
  99.     if( img->fiscaled ) {
  100.       img->fiscale *= img->fscale;
  101.       img->fibias = (img->fscale * img->fibias) + img->fbias;
  102.     } else {
  103.       img->fiscaled = img->fscaled;
  104.       img->fiscale = img->fscale;
  105.       img->fibias = img->fbias;
  106.     }
  107.   }
  108.   /* close the file */
  109.   close_disk(fd, img->filename);
  110. }
  111.  
  112. /*
  113.  * Subroutine:    read_data
  114.  * Purpose:    Read in the array data
  115.  * UNIX calls:    read
  116.  */
  117. static int read_data ( fd, img, databuf )
  118.      int fd;
  119.      struct imageRec *img;
  120.      char *databuf;
  121. {
  122.   int vals, nbytes;
  123.   int read_disk();
  124.  
  125.   /* find size of image */
  126.   vals = img->filecols * img->filerows;
  127.   nbytes = vals * img->bytepix;
  128.   /* read the file */
  129.   if( read_disk(fd, databuf, nbytes, 1, img->filename, "data") != nbytes )
  130.     return( 0 );
  131.   else
  132.     return( vals );
  133. }
  134.