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