home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume4 / moveicon / bitmap.c next >
C/C++ Source or Header  |  1986-11-30  |  3KB  |  123 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <pixrect/pixrect_hs.h>
  6. #include "bitmap.h"
  7.  
  8. #define HEADER_FORMAT    "/* Format_version=%d, Width=%d, Height=%d, Depth=%d, \
  9.                 Valid_bits_per_item=%d"
  10.  
  11. /*
  12.  * Load bitmap header.
  13.  */
  14. extern FILE *
  15. bm_open(filename, bmh_ptr)
  16. char        *filename;
  17. Bitmap_hdr    *bmh_ptr;
  18. {
  19.     register int    c;
  20.     FILE        *fp;
  21.     char        buf[BUFSIZ];
  22.     struct stat    statbuf;
  23.  
  24.     /* stat(2) the file to make sure it is a ``regular'' file */
  25.     if (stat(filename, &statbuf) == -1) {
  26.         perror("stat");
  27.         return(NULL);
  28.     }
  29.     if (statbuf.st_mode & S_IFMT != S_IFREG) {
  30.         return(NULL);
  31.     }
  32.     /* open the file */
  33.     if ((fp = fopen(filename, "r")) == NULL) {
  34.         return(NULL);
  35.     }
  36.     /* read header information */
  37.     for (;;) {
  38.         /* get a line */
  39.         if (fgets(buf, BUFSIZ, fp) == NULL) {
  40.             (void) fclose(fp);
  41.             return(NULL);
  42.         }
  43.         /* check to see if a '=' character appears in the line */
  44.         if (index(buf, '=') == 0) continue;
  45.         /* since the '=' was present, assume this line is the format */
  46.         if (sscanf(buf, HEADER_FORMAT, &bmh_ptr->format_version, 
  47.             &bmh_ptr->width, &bmh_ptr->height, &bmh_ptr->depth, 
  48.                 &bmh_ptr->valid_bits_per_item) != 5) {
  49.             (void) fclose(fp);
  50.             return(NULL);
  51.         }
  52.         break;
  53.     } /* end for */
  54.     /* read until we get past all the comments */
  55.     while ((c = getc(fp)) != EOF && c != '\t');
  56.     /* if <c> equals EOF the file is improperly formatted */
  57.     if (c == EOF) {
  58.         (void) fclose(fp);
  59.         return(NULL);
  60.     }
  61.     /* return the file pointer */
  62.     return(fp);
  63. } /* end bm_open() */
  64.  
  65. /*
  66.  * Load specific bitmap.
  67.  */
  68. extern Bitmap *
  69. bm_load(filename)
  70. char    *filename;
  71. {
  72.     register int        i, nitem;
  73.     register u_int        *data, *data_ptr;
  74.     Bitmap            *bm_ptr;
  75.     Bitmap_hdr        bmh_buf;
  76.     FILE            *fp;
  77.  
  78.     /* open icon file and read header information */
  79.     if ((fp = bm_open(filename, &bmh_buf)) == NULL) {
  80.         return(NULL_BM);
  81.     }
  82.     /* check to make sure we still are using version 1 */
  83.     if (bmh_buf.format_version != 1) {
  84.         (void) fclose(fp);
  85.         return(NULL_BM);
  86.     }
  87.     /* compute the number of items */
  88.     nitem = ((bmh_buf.width + WORDSIZE - 1) / WORDSIZE) * bmh_buf.height;
  89.     /* create data space for bitmap */
  90.     data_ptr = data = (u_int *) malloc(sizeof(u_int) * nitem);
  91.     /* read data from file */
  92.     for (i = 0; i < nitem; i++) {
  93.         if (fscanf(fp, " 0x%X,", data_ptr++) != 1) {
  94.             free(data);
  95.             (void) fclose(fp);
  96.             return(NULL_BM);
  97.         }
  98.     }
  99.     /* create bitmap */
  100.     bm_ptr = (Bitmap *) malloc(sizeof(Bitmap));
  101.     /* initialize values */
  102.     bm_ptr->width = bmh_buf.width;
  103.     bm_ptr->height = bmh_buf.height;
  104.     bm_ptr->depth = bmh_buf.depth;
  105.     /* create bitmap pixrect */
  106.     if ((bm_ptr->bitmap_pr = mem_create(bm_ptr->width, 
  107.         bm_ptr->height, bm_ptr->depth)) == NULL) {
  108.         free(data);
  109.         free(bm_ptr);
  110.         (void) fclose(fp);
  111.         return(NULL_BM);
  112.     }
  113.     /* put data into bitmap */
  114.     data_ptr = (u_int *) mpr_d(bm_ptr->bitmap_pr)->md_image;
  115.     for (i = ((nitem % 2 == 0) ? nitem : nitem + 1); i-- > 0; i--) {
  116.         data_ptr[i / 2] = data[i];
  117.         data_ptr[i / 2] |= (data[i - 1] << WORDSIZE);
  118.     }
  119.     free(data);
  120.     (void) fclose(fp);
  121.     return(bm_ptr);
  122. } /* end bm_load() */
  123.