home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libimage / open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.2 KB  |  255 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 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. /*
  18.  *    iopen -
  19.  *
  20.  *                Paul Haeberli - 1984
  21.  *
  22.  */
  23. #include    <stdio.h>
  24. #include    <stdlib.h>
  25. #include    "image.h"
  26.  
  27. IMAGE *imgopen();
  28.  
  29. IMAGE *iopen(file, mode, type, dim, xsize, ysize, zsize)
  30. char *file;
  31. register char *mode;
  32. unsigned int type, dim, xsize, ysize, zsize;
  33. {
  34.     return(imgopen(0, file, mode, type, dim, xsize, ysize, zsize));
  35. }
  36.  
  37. IMAGE *fiopen(f, mode, type, dim, xsize, ysize, zsize)
  38. int f;
  39. register char *mode;
  40. unsigned int type, dim, xsize, ysize, zsize;
  41. {
  42.     return(imgopen(f, 0, mode, type, dim, xsize, ysize, zsize));
  43. }
  44.  
  45. IMAGE *imgopen(f, file, mode, type, dim, xsize, ysize, zsize)
  46. char *file;
  47. int f;
  48. register char *mode;
  49. unsigned int type, dim, xsize, ysize, zsize;
  50. {
  51.     register IMAGE     *image;
  52.     register rw;
  53.     int tablesize;
  54.     register int i, max;
  55.  
  56.     image = (IMAGE*)calloc(1,sizeof(IMAGE));
  57.     rw = mode[1] == '+';
  58.     if(rw) {
  59.         i_errhdlr("iopen: read/write mode not supported\n");
  60.         return NULL;
  61.     }
  62.     if (*mode=='w') {
  63.         if (file) {
  64.             f = creat(file, 0666);
  65.             if (rw && f>=0) {
  66.                 close(f);
  67.                 f = open(file, 2);
  68.             }
  69.         }
  70.         if (f < 0) {
  71.             i_errhdlr("iopen: can't open output file %s\n",file);
  72.             return NULL;
  73.         }
  74.         image->imagic = IMAGIC;
  75.         image->type = type;
  76.         image->xsize = xsize;
  77.         image->ysize = 1;
  78.         image->zsize = 1;
  79.         if (dim>1)
  80.             image->ysize = ysize;
  81.         if (dim>2)
  82.             image->zsize = zsize;
  83.         if(image->zsize == 1) {
  84.             image->dim = 2;
  85.             if(image->ysize == 1)
  86.             image->dim = 1;
  87.         } else {
  88.             image->dim = 3;
  89.         }
  90.         image->min = 10000000;
  91.         image->max = 0;
  92.         isetname(image,"no name"); 
  93.         image->wastebytes = 0;
  94.         image->dorev = 0;
  95.         if (write(f,image,sizeof(IMAGE)) != sizeof(IMAGE)) {
  96.             i_errhdlr("iopen: error on write of image header\n");
  97.             return NULL;
  98.         }
  99.     } else {
  100.         if (file)
  101.             f = open(file, rw? 2: 0);
  102.         if (f < 0)
  103.             return(NULL);
  104.         if (read(f,image,sizeof(IMAGE)) != sizeof(IMAGE)) {
  105.             i_errhdlr("iopen: error on read of image header\n");
  106.             return NULL;
  107.         }
  108.         if( ((image->imagic>>8) | ((image->imagic&0xff)<<8)) 
  109.                                  == IMAGIC ) {
  110.             image->dorev = 1;
  111.             cvtimage(image);
  112.         } else
  113.             image->dorev = 0;
  114.         if (image->imagic != IMAGIC) {
  115.             i_errhdlr("iopen: bad magic in image file %x\n",image->imagic);
  116.             return NULL;
  117.         }
  118.     }
  119.     if (rw)
  120.         image->flags = _IORW;
  121.     else if (*mode != 'r')
  122.         image->flags = _IOWRT;
  123.     else
  124.         image->flags = _IOREAD;
  125.     if(ISRLE(image->type)) {
  126.         tablesize = image->ysize*image->zsize*sizeof(long);
  127.         image->rowstart = (unsigned long *)malloc(tablesize);
  128.         image->rowsize = (long *)malloc(tablesize);
  129.         if( image->rowstart == 0 || image->rowsize == 0 ) {
  130.         i_errhdlr("iopen: error on table alloc\n");
  131.         return NULL;
  132.         }
  133.         image->rleend = 512L+2*tablesize;
  134.         if (*mode=='w') {
  135.         max = image->ysize*image->zsize;
  136.         for(i=0; i<max; i++) {
  137.             image->rowstart[i] = 0;
  138.             image->rowsize[i] = -1;
  139.         }
  140.         } else {
  141.         tablesize = image->ysize*image->zsize*sizeof(long);
  142.         lseek(f, 512L, 0);
  143.         if (read(f,image->rowstart,tablesize) != tablesize) {
  144.             i_errhdlr("iopen: error on read of rowstart\n");
  145.             return NULL;
  146.         }
  147.         if(image->dorev)
  148.             cvtlongs(image->rowstart,tablesize);
  149.         if (read(f,image->rowsize,tablesize) != tablesize) {
  150.             i_errhdlr("iopen: error on read of rowsize\n");
  151.             return NULL;
  152.         }
  153.         if(image->dorev)
  154.             cvtlongs(image->rowsize,tablesize);
  155.         }
  156.     }
  157.     image->cnt = 0;
  158.     image->ptr = 0;
  159.     image->base = 0;
  160.     if( (image->tmpbuf = ibufalloc(image)) == 0 ) {    
  161.         i_errhdlr("iopen: error on tmpbuf alloc %d\n",image->xsize);
  162.         return NULL;
  163.     }
  164.     image->x = image->y = image->z = 0;
  165.     image->file = f;
  166.     image->offset = 512L;            /* set up for img_optseek */
  167.     lseek(image->file, 512L, 0);
  168.     return(image);
  169. }
  170.  
  171. unsigned short *ibufalloc(image)
  172. register IMAGE *image;
  173. {
  174.     return (unsigned short *)malloc(IBUFSIZE(image->xsize));
  175. }
  176.  
  177. reverse(lwrd) 
  178. register unsigned long lwrd;
  179. {
  180.     return ((lwrd>>24)         | 
  181.        (lwrd>>8 & 0xff00)     | 
  182.        (lwrd<<8 & 0xff0000) | 
  183.        (lwrd<<24)         );
  184. }
  185.  
  186. cvtshorts( buffer, n)
  187. register unsigned short buffer[];
  188. register long n;
  189. {
  190.     register short i;
  191.     register long nshorts = n>>1;
  192.     register unsigned short swrd;
  193.  
  194.     for(i=0; i<nshorts; i++) {
  195.     swrd = *buffer;
  196.     *buffer++ = (swrd>>8) | (swrd<<8);
  197.     }
  198. }
  199.  
  200. cvtlongs( buffer, n)
  201. register long buffer[];
  202. register long n;
  203. {
  204.     register short i;
  205.     register long nlongs = n>>2;
  206.     register unsigned long lwrd;
  207.  
  208.     for(i=0; i<nlongs; i++) {
  209.     lwrd = buffer[i];
  210.     buffer[i] =     ((lwrd>>24)         | 
  211.                (lwrd>>8 & 0xff00)     | 
  212.                (lwrd<<8 & 0xff0000)     | 
  213.                (lwrd<<24)         );
  214.     }
  215. }
  216.  
  217. cvtimage( buffer )
  218. register long buffer[];
  219. {
  220.     cvtshorts(buffer,12);
  221.     cvtlongs(buffer+3,12);
  222.     cvtlongs(buffer+26,4);
  223. }
  224.  
  225. static void (*i_errfunc)();
  226.  
  227. /*    error handler for the image library.  If the iseterror() routine
  228.     has been called, sprintf's the args into a string and calls the
  229.     error function.  Otherwise calls fprintf with the args and then
  230.     exit.  This allows 'old' programs to assume that no errors
  231.     ever need be worried about, while programs that know how and
  232.     want to can handle the errors themselves.  Olson, 11/88
  233. */
  234. i_errhdlr(fmt, a1, a2, a3, a4)    /* most args currently used is 2 */
  235. char *fmt;
  236. {
  237.     if(i_errfunc) {
  238.         char ebuf[2048];    /* be generous; if an error includes a
  239.             pathname, the maxlen is 1024, so we shouldn't ever 
  240.             overflow this! */
  241.         sprintf(ebuf, fmt, a1, a2, a3, a4);
  242.         (*i_errfunc)(ebuf);
  243.         return;
  244.     }
  245.     fprintf(stderr, fmt, a1, a2, a3, a4);
  246.     exit(1);
  247. }
  248.  
  249. /* this function sets the error handler for i_errhdlr */
  250. i_seterror(func)
  251. void (*func)();
  252. {
  253.     i_errfunc = func;
  254. }
  255.