WrBitF.c revision 258a0ebe
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#include "reallocarray.h"
37
38#define ERR_RETURN NULL
39
40static 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
87int
88XWriteBitmapFile(
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#ifdef __UNIXOS2__
110  filename = (char*)__XOS2RedirRoot(filename);
111#endif
112  if (!(stream = fopen(filename, "w")))
113    return(BitmapOpenFailed);
114
115  /* Convert bitmap to an image */
116  image = XGetImage(display, bitmap, 0,0,width, height, 1L, XYPixmap);
117  if (!image) {
118    fclose(stream);
119    return(4); /* XXX spec does not say what to return */
120  }
121
122  /* Get standard format for data */
123  data = Format_Image(image, &size);
124  XDestroyImage(image);
125  if (!data) {
126    fclose(stream);
127    return(BitmapNoMemory);
128  }
129
130  /* Write out standard header */
131  fprintf(stream, "#define %s_width %d\n", name, width);
132  fprintf(stream, "#define %s_height %d\n", name, height);
133  if (x_hot != -1) {
134    fprintf(stream, "#define %s_x_hot %d\n", name, x_hot);
135    fprintf(stream, "#define %s_y_hot %d\n", name, y_hot);
136  }
137
138  /* Print out the data itself */
139  fprintf(stream, "static unsigned char %s_bits[] = {", name);
140  for (byte=0, ptr=data; byte<size; byte++, ptr++) {
141    if (!byte)
142      fprintf(stream, "\n   ");
143    else if (!(byte % BYTES_PER_OUTPUT_LINE))
144      fprintf(stream, ",\n   ");
145    else
146      fprintf(stream, ", ");
147    c = *ptr;
148    if (c<0)
149      c += 256;
150    fprintf(stream, "0x%02x", c);
151  }
152  fprintf(stream, "};\n");
153
154  Xfree(data);
155  fclose(stream);
156  return(BitmapSuccess);
157}
158