home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlib_WrBitF.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  5KB  |  168 lines

  1. /* $XConsortium: WrBitF.c,v 1.13 94/04/17 20:21:32 rws Exp $ */
  2. /* $XFree86: xc/lib/X11/WrBitF.c,v 3.1 1996/04/15 11:15:50 dawes Exp $ */
  3. /*
  4.  
  5. Copyright (c) 1987  X Consortium
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining
  8. a copy of this software and associated documentation files (the
  9. "Software"), to deal in the Software without restriction, including
  10. without limitation the rights to use, copy, modify, merge, publish,
  11. distribute, sublicense, and/or sell copies of the Software, and to
  12. permit persons to whom the Software is furnished to do so, subject to
  13. the following conditions:
  14.  
  15. The above copyright notice and this permission notice shall be included
  16. in all copies or substantial portions of the Software.
  17.  
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. OTHER DEALINGS IN THE SOFTWARE.
  25.  
  26. Except as contained in this notice, the name of the X Consortium shall
  27. not be used in advertising or otherwise to promote the sale, use or
  28. other dealings in this Software without prior written authorization
  29. from the X Consortium.
  30.  
  31. */
  32.  
  33. #include "Xlib_private.h"
  34. #include <X11/Xos.h>
  35. #include "X11/Xutil.h"
  36. #include <stdio.h>
  37.  
  38. #define ERR_RETURN 0
  39.  
  40. static char *Format_Image(image, resultsize)
  41. XImage *image;
  42. int *resultsize;
  43. {
  44.   DBUG_ENTER("Format_Image")
  45.   register int x, c, b;
  46.   register char *ptr;
  47.   int y;
  48.   char *data;
  49.   int width, height;
  50.   int bytes_per_line;
  51.  
  52.   width = image->width;
  53.   height = image->height;
  54.  
  55.   bytes_per_line = (width+7)/8;
  56.   *resultsize = bytes_per_line * height;           /* Calculate size of data */
  57.  
  58.   data = (char *) Xmalloc( *resultsize );           /* Get space for data */
  59.   if (!data)
  60.     DBUG_RETURN(ERR_RETURN);
  61.  
  62.   /*
  63.    * The slow but robust brute force method of converting the image:
  64.    */
  65.   ptr = data;
  66.   c = 0; b=1;
  67.   for (y=0; y<height; y++) {
  68.     for (x=0; x<width;) {
  69.       if (XGetPixel(image, x, y))
  70.     c |= b;
  71.       b <<= 1;
  72.       if (!(++x & 7)) {
  73.     *(ptr++)=c;
  74.     c=0; b=1;
  75.       }
  76.     }
  77.     if (x & 7) {
  78.       *(ptr++)=c;
  79.       c=0; b=1;
  80.     }
  81.   }
  82.  
  83.   DBUG_RETURN(data);
  84. }
  85.    
  86. #define BYTES_PER_OUTPUT_LINE 12
  87.  
  88. #if NeedFunctionPrototypes
  89. int XWriteBitmapFile(
  90.      Display *display,
  91.      _Xconst char *filename,
  92.      Pixmap bitmap,
  93.      unsigned int width,
  94.      unsigned int height,
  95.      int x_hot,
  96.      int y_hot)
  97. #else
  98. int XWriteBitmapFile(display, filename, bitmap, width, height, x_hot, y_hot)
  99.      Display *display;
  100.      char *filename;
  101.      Pixmap bitmap;
  102.      unsigned int width, height;
  103.      int x_hot, y_hot;
  104. #endif
  105. {
  106.   DBUG_ENTER("XWriteBitmapFile")
  107.   char *data, *ptr;
  108.   int size, byte;
  109.   int c;
  110.   XImage *image;
  111.   FILE *stream;
  112.   char *name;
  113.  
  114.   if (!(name = strrchr(filename, '/')))
  115.     name = (char *)filename;
  116.   else
  117.     name++;
  118.  
  119. #ifdef __EMX__
  120.   filename = (char*)__XOS2RedirRoot((char *)filename);
  121. #endif
  122.   if (!(stream = fopen(filename, "w")))
  123.     DBUG_RETURN(BitmapOpenFailed);
  124.  
  125.   /* Convert bitmap to an image */
  126.   image = XGetImage(display, bitmap, 0,0,width, height, 1L, XYPixmap);
  127.   if (!image) {
  128.     fclose(stream);
  129.     DBUG_RETURN(4); /* XXX spec does not say what to return */
  130.   }
  131.  
  132.   /* Get standard format for data */
  133.   data = Format_Image(image, &size);
  134.   XDestroyImage(image);
  135.   if (!data) {
  136.     fclose(stream);
  137.     DBUG_RETURN(BitmapNoMemory);
  138.   }
  139.  
  140.   /* Write out standard header */
  141.   fprintf(stream, "#define %s_width %d\n", name, width);
  142.   fprintf(stream, "#define %s_height %d\n", name, height);
  143.   if (x_hot != -1) {
  144.     fprintf(stream, "#define %s_x_hot %d\n", name, x_hot);
  145.     fprintf(stream, "#define %s_y_hot %d\n", name, y_hot);
  146.   }
  147.  
  148.   /* Print out the data itself */
  149.   fprintf(stream, "static unsigned char %s_bits[] = {", name);
  150.   for (byte=0, ptr=data; byte<size; byte++, ptr++) {
  151.     if (!byte)
  152.       fprintf(stream, "\n   ");
  153.     else if (!(byte % BYTES_PER_OUTPUT_LINE))
  154.       fprintf(stream, ",\n   ");
  155.     else
  156.       fprintf(stream, ", ");
  157.     c = *ptr;
  158.     if (c<0)
  159.       c += 256;
  160.     fprintf(stream, "0x%02x", c);
  161.   }
  162.   fprintf(stream, "};\n");
  163.  
  164.   Xfree(data);
  165.   fclose(stream);
  166.   DBUG_RETURN(BitmapSuccess);
  167. }
  168.