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