home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / xbm / code / xwrbitf.c < prev   
Encoding:
C/C++ Source or Header  |  1994-06-20  |  3.5 KB  |  142 lines

  1. /* $XConsortium: XWrBitF.c,v 1.9 91/02/01 16:34:58 gildea Exp $ */
  2. /* Copyright, 1987, Massachusetts Institute of Technology */
  3.  
  4. /*
  5. Permission to use, copy, modify, distribute, and sell this software and its
  6. documentation for any purpose is hereby granted without fee, provided that
  7. the above copyright notice appear in all copies and that both that
  8. copyright notice and this permission notice appear in supporting
  9. documentation, and that the name of M.I.T. not be used in advertising or
  10. publicity pertaining to distribution of the software without specific,
  11. written prior permission.  M.I.T. makes no representations about the
  12. suitability of this software for any purpose.  It is provided "as is"
  13. without express or implied warranty.
  14. */
  15.  
  16. #include "Xlibint.h"
  17. #include <X11/Xos.h>
  18. #include "Xutil.h"
  19. #include <stdio.h>
  20.  
  21. #define ERR_RETURN 0
  22.  
  23. static char *Format_Image(image, resultsize)
  24. XImage *image;
  25. int *resultsize;
  26. {
  27.   register int x, c, b;
  28.   register char *ptr;
  29.   int y;
  30.   char *data;
  31.   int width, height;
  32.   int bytes_per_line;
  33.  
  34.   width = image->width;
  35.   height = image->height;
  36.  
  37.   bytes_per_line = (width+7)/8;
  38.   *resultsize = bytes_per_line * height;           /* Calculate size of data */
  39.  
  40.   data = (char *) Xmalloc( *resultsize );           /* Get space for data */
  41.   if (!data)
  42.     return(ERR_RETURN);
  43.  
  44.   /*
  45.    * The slow but robust brute force method of converting the image:
  46.    */
  47.   ptr = data;
  48.   c = 0; b=1;
  49.   for (y=0; y<height; y++) {
  50.     for (x=0; x<width;) {
  51.       if (XGetPixel(image, x, y))
  52.     c |= b;
  53.       b <<= 1;
  54.       if (!(++x & 7)) {
  55.     *(ptr++)=c;
  56.     c=0; b=1;
  57.       }
  58.     }
  59.     if (x & 7) {
  60.       *(ptr++)=c;
  61.       c=0; b=1;
  62.     }
  63.   }
  64.  
  65.   return(data);
  66. }
  67.    
  68. #define BYTES_PER_OUTPUT_LINE 12
  69.  
  70. #if NeedFunctionPrototypes
  71. int XWriteBitmapFile(
  72.      Display *display,
  73.      _Xconst char *filename,
  74.      Pixmap bitmap,
  75.      unsigned int width,
  76.      unsigned int height,
  77.      int x_hot,
  78.      int y_hot)
  79. #else
  80. int XWriteBitmapFile(display, filename, bitmap, width, height, x_hot, y_hot)
  81.      Display *display;
  82.      char *filename;
  83.      Pixmap bitmap;
  84.      unsigned int width, height;
  85.      int x_hot, y_hot;
  86. #endif
  87. {
  88.   char *data, *ptr;
  89.   int size, byte;
  90.   int c;
  91.   XImage *image;
  92.   FILE *stream;
  93.   char *name;
  94.  
  95.   if (!(name = rindex(filename, '/')))
  96.     name = (char *)filename;
  97.   else
  98.     name++;
  99.  
  100.   if (!(stream = fopen(filename, "w")))
  101.     return(BitmapOpenFailed);
  102.  
  103.   /* Convert bitmap to an image */
  104.   image = XGetImage(display, bitmap, 0,0,width, height, 1L, XYPixmap);
  105.  
  106.   /* Get standard format for data */
  107.   data = Format_Image(image, &size);
  108.   XDestroyImage(image);
  109.   if (!data) {
  110.     fclose(stream);
  111.     return(BitmapNoMemory);
  112.   }
  113.  
  114.   /* Write out standard header */
  115.   fprintf(stream, "#define %s_width %d\n", name, width);
  116.   fprintf(stream, "#define %s_height %d\n", name, height);
  117.   if (x_hot != -1) {
  118.     fprintf(stream, "#define %s_x_hot %d\n", name, x_hot);
  119.     fprintf(stream, "#define %s_y_hot %d\n", name, y_hot);
  120.   }
  121.  
  122.   /* Print out the data itself */
  123.   fprintf(stream, "static char %s_bits[] = {", name);
  124.   for (byte=0, ptr=data; byte<size; byte++, ptr++) {
  125.     if (!byte)
  126.       fprintf(stream, "\n   ");
  127.     else if (!(byte % BYTES_PER_OUTPUT_LINE))
  128.       fprintf(stream, ",\n   ");
  129.     else
  130.       fprintf(stream, ", ");
  131.     c = *ptr;
  132.     if (c<0)
  133.       c += 256;
  134.     fprintf(stream, "0x%02x", c);
  135.   }
  136.   fprintf(stream, "};\n");
  137.  
  138.   Xfree(data);
  139.   fclose(stream);
  140.   return(BitmapSuccess);
  141. }
  142.