home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xloadimg.zip / xloadimage.4.1 / vicar.c < prev    next >
C/C++ Source or Header  |  1993-10-21  |  4KB  |  184 lines

  1. /*
  2. ** vic.c - load a VICAR Image file for use inside xloadimage
  3. **
  4. ** Khalid Soofi (August 1992)
  5. */
  6.  
  7. #define TRUE      1
  8. #define FALSE     0
  9. #define DEPTH     8
  10. #define LBLSIZE   label[0]
  11. #define LINES     label[8]
  12. #define SAMPLES   label[6]
  13.  
  14. #include <stdio.h>
  15. #include <malloc.h>
  16. #include "image.h"
  17. #include <sys/types.h>
  18.  
  19. FILE *fp,*fopen();
  20. int  label[10];
  21. char labstr[80];
  22. int  flag=0;
  23.  
  24.  
  25.  
  26. /*
  27. ** vicarIdent
  28. **
  29. ** Identify passed file a VICAR Image
  30. **   return 1 for true
  31. **   return 0 otherwise
  32. **
  33. */
  34.  
  35. unsigned int vicarIdent (fullname, name)
  36.      char *fullname, *name;
  37. {
  38.   int i=0;
  39.   char IDENT[7];
  40.   char c;
  41.  
  42.   if ( ! (fp = fopen(fullname,"r")) ) 
  43.     return(0);
  44.  
  45.   /* identify the VICAR IMAGE */
  46.  
  47.   fscanf(fp,"%6s",IDENT);
  48.   if ( strcmp(IDENT,"LBLSIZ") == 0 ) {
  49.      printf("%s is a VICAR Image: ",fullname);
  50.      fclose(fp);
  51.   }
  52.   else 
  53.      return(0);
  54.  
  55.   /* Fill up the label block of image */
  56.  
  57.   fp = fopen(fullname,"r");
  58.  
  59.   while ( i < 10) {
  60.     fscanf(fp,"%c",&c);
  61.     if ( c == '=' )
  62.       fscanf(fp,"%d",&label[i++]);
  63.   }
  64.  
  65.   printf("%d lines and %d samples\n", LINES , SAMPLES );
  66.   fclose(fp);
  67.   return(1);
  68. }
  69.          
  70.  
  71. /* vicHeader - print the history record */
  72.  
  73. static void vicarHeader(ptr)
  74.      char *ptr;
  75. {
  76.   int i;
  77.   int prflag;
  78.   int rtflag;
  79.  
  80.   printf("\n ---HISTORY RECORD------------------------------------\n");
  81.   for ( i=0, prflag=FALSE, rtflag=0 ; i < LBLSIZE ; i++ ) {
  82.     if ( *(ptr++) == '\'' ) {
  83.       prflag = !prflag;
  84.       rtflag++;
  85.     }
  86.     if ( prflag && (char)(*ptr) != '\'' )
  87.       printf("%c",(char)(*ptr));
  88.     if ( rtflag == 2 ) {
  89.       printf("\n");
  90.       rtflag = 0;
  91.     }
  92.     
  93.   }
  94.   printf("\n -----------------------------------------------------\n");
  95. }
  96.  
  97.  
  98. /*
  99. ** vicarLoad
  100. **
  101. ** Load Vicar image into an Image structure.
  102. **
  103. ** Return pointer to allocated struct if successful, NULL otherwise
  104. **
  105. */
  106.  
  107. Image *vicarLoad(fullname, name, verbose)
  108.      char *fullname, *name;
  109.      unsigned int verbose;
  110. {
  111.   char         c;
  112.   int          i,j;
  113.   unsigned int mapsize, size;
  114.   Image        *image;
  115.   byte         *lineptr;
  116.   byte         *map;
  117.   byte         *mapred,*mapgreen,*mapblue;
  118.   byte         *buf;
  119.   ZFILE        *zf;
  120.  
  121.   if ( vicarIdent(fullname,name,verbose) == 0 )
  122.     return(0);
  123.  
  124.   /* define the image structure */
  125.  
  126.   image = newRGBImage( SAMPLES , LINES , DEPTH );
  127.   image->width = SAMPLES ;
  128.   image->height = LINES ;
  129.   image->depth = DEPTH ;
  130.   image->title = dupString(name);
  131.  
  132.  /* 
  133.   * Set up the grey scale lookup table:-
  134.   *   From  Jim frost 09.27.89 (sunraster.c) 
  135.   *   Copyright 1989, 1991 Jim Frost.
  136.   *   See included file "copyright.h" for complete copyright information.
  137.   */
  138.  
  139.   mapsize = 256*3; 
  140.   map= lmalloc(mapsize); 
  141.   for (i = 0; i < 256; i += 1) { 
  142.    map[i] = map[256+i] = map[2*256+i] = i;
  143.   }
  144.   mapsize /= 3;
  145.   mapred= map;
  146.   mapgreen= mapred + mapsize;
  147.   mapblue= mapgreen + mapsize;
  148.   if (image->rgb.size == 0)
  149.       newRGBMapData(&image->rgb, mapsize);
  150.   for (i= 0; i < mapsize; i++) {
  151.     *(image->rgb.red + i)= (*(mapred++) << 8);
  152.     *(image->rgb.green + i)= (*(mapgreen++) << 8);
  153.     *(image->rgb.blue + i)= (*(mapblue++) << 8);
  154.   }
  155.   lfree(map);
  156.   image->rgb.used= mapsize;
  157.  
  158.  /* 
  159.   * Finally start the image data
  160.   */
  161.  
  162.   lineptr = image->data;
  163.  
  164.   if ( !(zf=zopen(fullname))) {
  165.      perror(fullname);
  166.      zclose(zf);
  167.      exit(1);
  168.   }
  169.  
  170.   if ( verbose ){
  171.     zread(zf,lineptr,LBLSIZE);                     /* dump label */
  172.     vicarHeader(lineptr);
  173.   }
  174.   else
  175.     zread(zf,lineptr,LBLSIZE);                     /* skip label */
  176.  
  177.   size = image->width * image->height;
  178.   if (zread(zf,lineptr,size) != size)
  179.     fprintf(stderr, "%s: Warning, image data was missing\n", fullname);
  180.   zclose(zf);
  181.   return(image);
  182.  
  183. }
  184.